generated from sourcecred/template-instance
-
Notifications
You must be signed in to change notification settings - Fork 5
/
generate-csv.js
107 lines (89 loc) · 3.38 KB
/
generate-csv.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// node generate-csv.js
const fs = require('fs')
const path = require('path')
const csvParser = require('csv-parser')
const csvWriter = require('csv-writer')
const currencyDetails = require('./config/currencyDetails.json')
// 0.50% of the weekly $NATION budget set in config/grain.json
const FLOOR = 0.01;
generateCSV()
/**
* Generates CSV files compatible with Gnosis Safe.
*/
function generateCSV() {
console.log('generateCSV')
// Iterate the CSV files generated by SourceCred in output/grainIntegration/
fs.readdir('output/grainIntegration/', function(err, files) {
if (err) {
console.error(err)
return
}
files.forEach(function(file, index) {
const filePath = path.join('output/grainIntegration/', file)
if (filePath.endsWith('.csv')) {
// Read the rows of data from the CSV file
let csvRows = []
fs.createReadStream(filePath)
.pipe(csvParser(['receiver', 'amount']))
.on('data', (row) => insertRow(csvRows, row))
.on('end', () => {
console.log('\nfilePath', filePath)
console.table(csvRows.map(row => ({ receiver: row.receiver, amount: row.amount })))
// Convert amount format
convertAmountFormat(csvRows)
csvRows = pruneRows(csvRows);
// Generate CSV for Gnosis Safe
const fileNameGnosis = `${file.substring(0, 10)}_gnosis.csv`
console.log('fileNameGnosis:', fileNameGnosis)
const filePathGnosis = path.join('grain-distributions/', fileNameGnosis)
console.log('filePathGnosis', filePathGnosis)
writeToGnosisCSV(filePathGnosis, csvRows)
})
}
})
})
}
function insertRow(rows, row) {
let newRow = {};
newRow.receiver = row.receiver
newRow.amount = row.amount
newRow.name = ''
newRow.token_type = 'erc20'
newRow.token_address = currencyDetails.integrationCurrency.tokenAddress
rows.push(newRow)
}
function pruneRows(rows) {
let pruned = rows.filter(row => row.amount >= FLOOR);
return pruned;
}
/**
* Convert the amount column to 18 decimal format:
* 0x3e465ABFa9b2A7E18a610F489fb3510765461d13,"7718330904890492"
* -->
* 0x3e465ABFa9b2A7E18a610F489fb3510765461d13,0.007718330904890492
*/
function convertAmountFormat(csvRows) {
console.log('convertAmountFormat')
csvRows.forEach(function(row, index) {
// Prepend zeros
while (row.amount.length <= 18) {
row.amount = "0" + row.amount
}
// Add decimal
row.amount = row.amount.substring(0, row.amount.length - 18) + "." + row.amount.substring(row.amount.length - 18, row.amount.length)
})
}
function writeToGnosisCSV(filePathGnosis, csvRows) {
console.log('writeToGnosisCSV')
const writer = csvWriter.createObjectCsvWriter({
path: filePathGnosis,
header: [
{id: 'token_type', title: 'token_type'},
{id: 'token_address', title: 'token_address'},
{id: 'receiver', title: 'receiver'},
{id: 'amount', title: 'amount'},
{id: 'id', title: 'id'}
]
})
writer.writeRecords(csvRows)
}