Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement