Created
April 12, 2019 18:49
-
-
Save JesseAbram/6992d798ce637fb3abc3936306c0c65a to your computer and use it in GitHub Desktop.
This file contains 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 Iota = require('@iota/core'); | |
exports.createTransaction = async (to, wallet, token) => { | |
const iota = Iota.composeAPI({ | |
provider: token.networkUrl | |
}); | |
const value = parseInt(token.maxSupplyAllowed); | |
// generates current account to send funds from | |
const inputAddress = Iota.generateAddress(wallet.privateKey, wallet.nonce, 2); | |
// generates new empty slot in in account array to send leftover funds to | |
const newAddress = Iota.generateAddress( | |
wallet.privateKey, | |
wallet.nonce + 5, | |
2 | |
); | |
// get balance of current account | |
const balances = await iota.getBalances([inputAddress], 100); | |
const [ balance ] = balances.balances; | |
// transfer funds to user, leftover funds to the newAddress created above | |
const transfers = [ | |
{ | |
value, | |
address: to | |
}, | |
{ | |
value: balance - value, | |
address: newAddress | |
} | |
]; | |
const option = { | |
inputs: [ | |
{ | |
address: inputAddress, | |
keyIndex: wallet.nonce, | |
balance: balance, | |
security: 2 | |
} | |
] | |
}; | |
const raw = await iota.prepareTransfers(wallet.privateKey, transfers, option); | |
const hash = await iota.sendTrytes(raw, 3, 9); | |
return hash[0].hash; | |
}; | |
exports.sendTransaction = async (txn, options) => { | |
return Object.assign(options, { | |
hash: txn | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment