Last active
July 20, 2018 15:14
-
-
Save tdreid/20ccbe9267d10cb07898e6a191f77932 to your computer and use it in GitHub Desktop.
Sample code to accompany tutorial on the steem blockchain
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
#!/usr/bin/env node | |
'use strict'; | |
const version = require('./package.json').version; | |
const tp = require('commander'); | |
tp | |
.arguments('<payee> <amount> <limit> <memo> <interval>') | |
.option('-p, --payor <payor>', 'Payor account on the steem blockchain') | |
.option('-w, --wif <wif>', 'Active key for payor account') | |
.option( | |
'-t, --test', | |
'Test mode. Direct transaction(s) to wss://testnet.steem.vc' | |
) | |
.version(version, '-v, --version'); | |
if (tp.args.length === 0) tp.help(); |
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
tp | |
.action((payee, amount, limit, memo, interval) => { | |
console.log('Doing something with these arguments...'); | |
console.log(`payee:${payee}, amount:${amount}, limit:${limit}, memo:${memo}, | |
interval:${interval}.`); | |
console.log('And these flags...'); | |
console.log(`test: ${tp.test}, payor:${tp.payor}, | |
wif:Shhh! It's bad practice to write that in clear text`); | |
}) | |
.parse(process.argv); |
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
const steem = require('steem'); | |
let currencyTransferred = 0; | |
let currencyLimit = 0.001; | |
let currencyIncrement = 0.001; | |
const incrementCurrencyTransferred = () => { | |
currencyTransferred += Number(currencyIncrement); | |
console.log( | |
`${(Number(), | |
parseFloat(currencyTransferred).toFixed(3))} SBD transferred so far.` | |
); | |
if (Number(currencyTransferred) >= Number(currencyLimit)) { | |
console.log(`Transfer limit (${currencyLimit}) reached. Exiting.`); | |
process.exit(); | |
} | |
}; |
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
const transfer = (wif, from, to, amount, memo) => { | |
if (tp.test) { | |
steem.api.setOptions({ | |
url: 'wss://testnet.steem.vc', | |
address_prefix: 'STX', | |
chain_id: | |
'79276aea5d4877d9a25892eaa01b0adf019d3e5cb12a97478df3298ccdd01673' | |
}); | |
} else { | |
steem.api.setOptions({ url: 'https://api.steemit.com' }); | |
} | |
steem.broadcast.transfer(wif, from, to, amount, memo, function(err, result) { | |
if (err) { | |
console.error(err); | |
} else { | |
incrementCurrencyTransferred(amount); | |
} | |
}); | |
}; |
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
tp.action((payee, amount, limit, memo, interval) => { | |
currencyIncrement = amount; | |
currencyLimit = limit; | |
setInterval(() => { | |
transfer(tp.wif, tp.payor, payee, `${amount} SBD`, memo); | |
}, interval * 1000); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment