Created
February 6, 2018 21:20
-
-
Save raypulver/eedb6c761e3e3d9900327cdfc950253d to your computer and use it in GitHub Desktop.
same script using promise-compose
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const db = require('../lib/db'); | |
let { | |
argv: { | |
_: [ symbol ], | |
o: outputFilename | |
} | |
} = require('yargs'); | |
const { | |
method, | |
bindKey, | |
partialRight, | |
constant, | |
property | |
} = require('lodash'); | |
const { | |
flow, | |
keyBy, | |
mapValues, | |
curryN, | |
map | |
} = require('lodash/fp'); | |
const compose = require('promise-compose'); | |
const writeFile = curryN(2, require('fs-extra').writeFile); | |
const toPlain = method('get', { plain: true }); | |
const mapToPlain = map(toPlain); | |
const jsonStringify = bindKey(JSON, 'stringify'); | |
const prettyStringify = partialRight(jsonStringify, null, 1); | |
if (!symbol) symbol = 'AURA'; | |
if (!outputFilename) outputFilename = ['balance-dump-', symbol, '.json'].join(''); | |
const findAllQuery = { | |
where: {} | |
}; | |
const findAllBalances = flow((token) => { | |
findAllQuery.where.token = token; | |
return findAllQuery; | |
}, (query) => db.getModel('Balance').findAll(query)); | |
const findOneQuery = { | |
where: {} | |
}; | |
const findTokenBySymbol = flow((symbol) => { | |
findOneQuery.where.symbol = symbol; | |
return findOneQuery; | |
}, (query) => db.getModel('Token').findOne(query)); | |
compose( | |
db.initializeDatabase, | |
constant(symbol), | |
findTokenBySymbol, | |
flow( | |
toPlain, | |
property('address'), | |
method('toLowerCase') | |
), | |
findAllBalances, | |
flow( | |
mapToPlain, | |
keyBy('address'), | |
mapValues(property('balance')), | |
prettyStringify | |
), | |
writeFile(outputFilename), | |
db.close | |
)().catch(flow( | |
property('stack'), | |
console.error | |
)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment