Guest User

Untitled

a guest
Aug 13th, 2020
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const functions = require('firebase-functions');
  2. exports.distributeStakingRewards = functions.pubsub.schedule('every 1 minutes').onRun((context) => {
  3.     const request = require('request')
  4.     //Fetch list of users who has a positive balance for your asset..
  5.     const assetID = 'xxx'
  6.  
  7.     request('https://testnode1.wavesnodes.com/assets/' + assetID + '/distribution', function(err, res, body) {
  8.         if (res.statusCode === 200) {
  9.             const bodyJSON = JSON.parse(body)
  10.             var transfers = []
  11.             for (uid in bodyJSON) {
  12.                 var bal = bodyJSON[uid]
  13.                 //Set minimum balance to get rewarded
  14.                 if (bal >= 1) {
  15.                     var reward = '0.005'
  16.                     var transfer = { recipient: uid, amount: reward }
  17.                     transfers.push(transfer)
  18.                 }
  19.             }
  20.             const waves = require('waves-transactions')
  21.             const nodeUrl = 'https://testnode1.wavesnodes.com/'
  22.             const params = { transfers: transfers,assetId: assetID, attachment: 'Weekly staking rewards payout', timestamp: Date.now() }
  23.             const signedTx = waves.massTransfer(params,
  24.               {
  25.                 'privateKey': 'xxxx',
  26.               }
  27.             )
  28.             const id = signedTx.id
  29.             waves.nodeInteraction.broadcast(signedTx, nodeUrl).then(tx => {
  30.                 //If tx returns null or undefined tx.id will be undefined === false
  31.                 if (tx.id === id) {
  32.                     console.log('Successfully distributed staking rewards for ' + new Date().toDateString() + 'was complete')
  33.                 } else {
  34.                     console.log('Unable to distribute staking rewards for ' + new Date().toDateString())
  35.                 }
  36.             })
  37.         } else {
  38.             console.log('unable to fetch asset distribution ' + err)
  39.         }
  40.     })
  41. })
  42.  
Add Comment
Please, Sign In to add comment