Guest User

Untitled

a guest
Jan 16th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. Eos = require('eosjs')
  2.  
  3. // Default configuration
  4. config = {
  5. chainId: null, // 32 byte (64 char) hex string
  6. keyProvider: ['PrivateKeys...'], // WIF string or array of keys..
  7. httpEndpoint: 'http://127.0.0.1:8888',
  8. expireInSeconds: 60,
  9. broadcast: true,
  10. verbose: false, // API activity
  11. sign: true
  12. }
  13.  
  14. eos = Eos(config)
  15.  
  16. eos.transaction(
  17. {
  18. // ...headers,
  19. // context_free_actions: [],
  20. actions: [
  21. {
  22. account: 'eosio.token',
  23. name: 'transfer',
  24. authorization: [{
  25. actor: 'inita',
  26. permission: 'active'
  27. }],
  28. data: {
  29. from: 'inita',
  30. to: 'initb',
  31. quantity: '7.0000 SYS',
  32. memo: ''
  33. }
  34. }
  35. ]
  36. }
  37. // config -- example: {broadcast: false, sign: true}
  38. )
  39.  
  40. const { Api, JsonRpc, RpcError } = require('eosjs');
  41. const JsSignatureProvider = require('eosjs/dist/eosjs-jssig'); // development only
  42. const fetch = require('node-fetch'); // node only; not needed in browsers
  43. const { TextEncoder, TextDecoder } = require('util'); // node only; native TextEncoder/Decoder
  44. const { TextEncoder, TextDecoder } = require('text-encoding');
  45. const defaultPrivateKey = "5JtUScZK2XEp3g9gh7F8bwtPTRAkASmNrrftmx4AxDKD5K4zDnr"; // useraaaaaaaa
  46. const signatureProvider = new JsSignatureProvider([defaultPrivateKey]);
  47. const rpc = new JsonRpc('http://127.0.0.1:8888', { fetch });
  48. const api = new Api({ rpc, signatureProvider, textDecoder: new TextDecoder(), textEncoder: new TextEncoder() });
  49.  
  50. (async () => {
  51. const result = await api.transact({
  52. actions: [{
  53. account: 'eosio.token',
  54. name: 'transfer',
  55. authorization: [{
  56. actor: 'useraaaaaaaa',
  57. permission: 'active',
  58. }],
  59. data: {
  60. from: 'useraaaaaaaa',
  61. to: 'useraaaaaaab',
  62. quantity: '0.0001 SYS',
  63. memo: '',
  64. },
  65. }]
  66. }, {
  67. blocksBehind: 3,
  68. expireSeconds: 30,
  69. });
  70. console.dir(result);
  71. })();
  72.  
  73. /** in Api constructor */
  74. public chainId: string;
  75.  
  76. /** in transact() function */
  77. if (!this.chainId) {
  78. info = await this.rpc.get_info();
  79. this.chainId = info.chain_id;
  80. }
  81.  
  82. /** `fetch()` prepends the path with the host provided to JsonRpc and returns JSON containing `chain_id` */
  83. public async get_info(): Promise<GetInfoResult> {
  84. return await this.fetch("/v1/chain/get_info", {});
  85. }
Add Comment
Please, Sign In to add comment