Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. const Iota = require('@iota/core');
  2.  
  3.  
  4. exports.createTransaction = async (to, wallet, token) => {
  5. const iota = Iota.composeAPI({
  6. provider: token.networkUrl
  7. });
  8. const value = parseInt(token.maxSupplyAllowed);
  9.  
  10. // generates current account to send funds from
  11. const inputAddress = Iota.generateAddress(wallet.privateKey, wallet.nonce, 2);
  12. // generates new empty slot in in account array to send leftover funds to
  13. const newAddress = Iota.generateAddress(
  14. wallet.privateKey,
  15. wallet.nonce + 5,
  16. 2
  17. );
  18. // get balance of current account
  19. const balances = await iota.getBalances([inputAddress], 100);
  20. const [ balance ] = balances.balances;
  21.  
  22. // transfer funds to user, leftover funds to the newAddress created above
  23. const transfers = [
  24. {
  25. value,
  26. address: to
  27. },
  28. {
  29. value: balance - value,
  30. address: newAddress
  31. }
  32. ];
  33.  
  34. const option = {
  35. inputs: [
  36. {
  37. address: inputAddress,
  38. keyIndex: wallet.nonce,
  39. balance: balance,
  40. security: 2
  41. }
  42. ]
  43. };
  44. const raw = await iota.prepareTransfers(wallet.privateKey, transfers, option);
  45. const hash = await iota.sendTrytes(raw, 3, 9);
  46. return hash[0].hash;
  47. };
  48.  
  49. exports.sendTransaction = async (txn, options) => {
  50. return Object.assign(options, {
  51. hash: txn
  52. });
  53. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement