Transaction Functionality

Next we illustrate the API calls for querying the following information:

  • transaction information given its hash
  • transaction information given its index and block hash
  • transaction information given its index and block number
  • transaction receipt given its hash

and performing the following actions:

  • send transaction

The final subsection contains code illustrating all of the above interactions.

Retrieve Transaction By Hash

The examples below show how to query the APIs for the detailed information regarding a transaction given its hash. The functionality is compatible with eth_getTransactionByHash. In each code snippet, the transaction details are retrieved from the API and printed to the standard output.

// specify hash Hash256 hash = Hash256.wrap("0x5f2e74ade04ab9f6e8d4acd394f7f51832d4706d7268eea0ecc6391f94185b80"); // get tx with given hash Transaction tx = api.getChain().getTransactionByHash(hash).getObject(); // print tx information System.out.format("transaction details:%n%s%n", tx);
// specify hash let hash = '0x5f2e74ade04ab9f6e8d4acd394f7f51832d4706d7268eea0ecc6391f94185b80'; // get tx with given hash let tx = web3.eth.getTransaction(hash); // print tx information console.log("transaction details:"); console.log(tx);

Sample output:

transaction details: nrgPrice: 10000000000, nrg: 320922, nonce: 447, transactionIndex: 0, input: 0x1fec4cc69ce22f4a90d3d5ee88b4750e1c4ad0fbf9b2981ca82132742a48002f7e85e2fa00000000000000000000000000000040000000000000000000000000000000f000000000000000000000000000000005a082abceefa73078541d577ad576719c3f475b4ad0bd136918da43f0bce30429a0b612e3b6be803768451b7a331b1837face5be52b2b1fd253a31516bbdc1b50a044ba079d30fa1cdea965e93ab7cbaa9d18f8c02e569707bb320129a1840dada03cefb0ad4b6effd63825a1d6e345fc5b708f197b85c0a4f5583b125f52201aa0ee6827c6f05bd9192d444cb4fa0dec304829edf5c3c99d3df30d9618ebad1d00000000000000000000000000000005000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400, blockNumber: 247726, from: 0xa0dd16394f16ea21c8b45c00b2e43850ae7e8f00fe54789ddd1881d33b21df0c, to: 0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a, value: 0, hash: 0x5f2e74ade04ab9f6e8d4acd394f7f51832d4706d7268eea0ecc6391f94185b80, timestamp: 1529590672338000
transaction details: { nrgPrice: BigNumber { s: 1, e: 10, c: [ 10000000000 ] }, blockHash: '0x50a906f4ccaf05a3ebca69cc4f84a116e6aec881e3c4d080c4df505fea65afab', nrg: 2000000, transactionIndex: 3, nonce: 447, input: '0x1fec4cc69ce22f4a90d3d5ee88b4750e1c4ad0fbf9b2981ca82132742a48002f7e85e2fa00000000000000000000000000000040000000000000000000000000000000f000000000000000000000000000000005a082abceefa73078541d577ad576719c3f475b4ad0bd136918da43f0bce30429a0b612e3b6be803768451b7a331b1837face5be52b2b1fd253a31516bbdc1b50a044ba079d30fa1cdea965e93ab7cbaa9d18f8c02e569707bb320129a1840dada03cefb0ad4b6effd63825a1d6e345fc5b708f197b85c0a4f5583b125f52201aa0ee6827c6f05bd9192d444cb4fa0dec304829edf5c3c99d3df30d9618ebad1d00000000000000000000000000000005000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400', blockNumber: 247726, gas: 2000000, from: '0xa0dd16394f16ea21c8b45c00b2e43850ae7e8f00fe54789ddd1881d33b21df0c', to: '0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a', value: BigNumber { s: 1, e: 0, c: [ 0 ] }, hash: '0x5f2e74ade04ab9f6e8d4acd394f7f51832d4706d7268eea0ecc6391f94185b80', gasPrice: '0x2540be400', timestamp: 1529590674 }

Retrieve Transaction By Block Hash and Index

The examples below show how to query the APIs for the detailed information regarding a transaction given its index and the block hash where it occurred. The functionality is compatible with eth_getTransactionByBlockHashAndIndex. In each code snippet, the transaction details are retrieved from the API and printed to the standard output.

// specify block hash Hash256 hash = Hash256.wrap("0x50a906f4ccaf05a3ebca69cc4f84a116e6aec881e3c4d080c4df505fea65afab"); // specify tx index = 0 -> first tx int index = 0; // get tx with given hash & index Transaction tx = api.getChain().getTransactionByBlockHashAndIndex(hash, index).getObject(); // print tx information System.out.format("transaction details:%n%s%n", tx);
// specify block hash let hash = '0x50a906f4ccaf05a3ebca69cc4f84a116e6aec881e3c4d080c4df505fea65afab'; // specify tx index = 0 -> first tx let index = 0; // get tx with given hash & index let tx = web3.eth.getTransactionFromBlock(hash, index); // print tx information console.log("transaction details:"); console.log(tx);

Sample output:

transaction details: nrgPrice: 10000000000, nrg: 84107, nonce: 367, transactionIndex: 0, input: 0x1fec4cc691ae034f8976efbe9c89fa3a78c19d663bf61c76c6d3dd528259fb9cb565e8db00000000000000000000000000000040000000000000000000000000000000f000000000000000000000000000000005a064ff11a1563ef09fd5d4671272275dd962d46f7498f8d297d60bf3e4bee1eda07e5b667332c1af7a14bb51b86fe92e1cc0d2a5f0de70613c26ff3584c312c7a0a3afd832901ba4e1bc947721babb13039194ac1b780f2b13b9b86b2ed08639a09bb6e70f50e6f512e0aa3033f2477ad04f75704bcf14ce1c9aca37492cf25ba0ce3ce58c28307850315f934cda934e65252b5115aaeca58ead64e88d9569ad00000000000000000000000000000005000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400, blockNumber: 247726, from: 0xa05a3889b106e75baa621b8cc719679a3dbdd799afac1ca6b42d03dc93a23687, to: 0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a, value: 0, hash: 0xdb50c83faad497dc281df5a7ae5e2aa3294431d64a7868134895d33838882045, timestamp: 1529590657509000
transaction details: { nrgPrice: BigNumber { s: 1, e: 10, c: [ 10000000000 ] }, blockHash: '0x50a906f4ccaf05a3ebca69cc4f84a116e6aec881e3c4d080c4df505fea65afab', nrg: 2000000, transactionIndex: 0, nonce: 367, input: '0x1fec4cc691ae034f8976efbe9c89fa3a78c19d663bf61c76c6d3dd528259fb9cb565e8db00000000000000000000000000000040000000000000000000000000000000f000000000000000000000000000000005a064ff11a1563ef09fd5d4671272275dd962d46f7498f8d297d60bf3e4bee1eda07e5b667332c1af7a14bb51b86fe92e1cc0d2a5f0de70613c26ff3584c312c7a0a3afd832901ba4e1bc947721babb13039194ac1b780f2b13b9b86b2ed08639a09bb6e70f50e6f512e0aa3033f2477ad04f75704bcf14ce1c9aca37492cf25ba0ce3ce58c28307850315f934cda934e65252b5115aaeca58ead64e88d9569ad00000000000000000000000000000005000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400', blockNumber: 247726, gas: 2000000, from: '0xa05a3889b106e75baa621b8cc719679a3dbdd799afac1ca6b42d03dc93a23687', to: '0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a', value: BigNumber { s: 1, e: 0, c: [ 0 ] }, hash: '0xdb50c83faad497dc281df5a7ae5e2aa3294431d64a7868134895d33838882045', gasPrice: '0x2540be400', timestamp: 1529590674 }

Retrieve Transaction By Block Number and Index

The examples below show how to query the APIs for the detailed information regarding a transaction given its index and the block number where it occurred. The functionality is compatible with eth_getTransactionByBlockNumberAndIndex. In each code snippet, the transaction details are retrieved from the API and printed to the standard output.

// specify block number long number = 247726L; // specify tx index = 1 -> second tx int index = 1; // get tx with given number & index Transaction tx = api.getChain().getTransactionByBlockNumberAndIndex(number, index).getObject(); // print tx information System.out.format("transaction details:%n%s%n", tx);
// specify block number let number = 247726; // specify tx index = 1 -> second tx let index = 1; // get tx with given number & index let tx = web3.eth.getTransactionFromBlock(number, index); // print tx information console.log("transaction details:"); console.log(tx);

Sample output:

transaction details: nrgPrice: 10000000000, nrg: 84047, nonce: 368, transactionIndex: 0, input: 0x1fec4cc69ce22f4a90d3d5ee88b4750e1c4ad0fbf9b2981ca82132742a48002f7e85e2fa00000000000000000000000000000040000000000000000000000000000000f000000000000000000000000000000005a082abceefa73078541d577ad576719c3f475b4ad0bd136918da43f0bce30429a0b612e3b6be803768451b7a331b1837face5be52b2b1fd253a31516bbdc1b50a044ba079d30fa1cdea965e93ab7cbaa9d18f8c02e569707bb320129a1840dada03cefb0ad4b6effd63825a1d6e345fc5b708f197b85c0a4f5583b125f52201aa0ee6827c6f05bd9192d444cb4fa0dec304829edf5c3c99d3df30d9618ebad1d00000000000000000000000000000005000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400, blockNumber: 247726, from: 0xa05a3889b106e75baa621b8cc719679a3dbdd799afac1ca6b42d03dc93a23687, to: 0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a, value: 0, hash: 0x92aef634e0194a3c70a39163c45c2f2747ec86a8e40a9c6ddb93009741be8cb0, timestamp: 1529590672530000
transaction details: { nrgPrice: BigNumber { s: 1, e: 10, c: [ 10000000000 ] }, blockHash: '0x50a906f4ccaf05a3ebca69cc4f84a116e6aec881e3c4d080c4df505fea65afab', nrg: 2000000, transactionIndex: 1, nonce: 368, input: '0x1fec4cc69ce22f4a90d3d5ee88b4750e1c4ad0fbf9b2981ca82132742a48002f7e85e2fa00000000000000000000000000000040000000000000000000000000000000f000000000000000000000000000000005a082abceefa73078541d577ad576719c3f475b4ad0bd136918da43f0bce30429a0b612e3b6be803768451b7a331b1837face5be52b2b1fd253a31516bbdc1b50a044ba079d30fa1cdea965e93ab7cbaa9d18f8c02e569707bb320129a1840dada03cefb0ad4b6effd63825a1d6e345fc5b708f197b85c0a4f5583b125f52201aa0ee6827c6f05bd9192d444cb4fa0dec304829edf5c3c99d3df30d9618ebad1d00000000000000000000000000000005000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400', blockNumber: 247726, gas: 2000000, from: '0xa05a3889b106e75baa621b8cc719679a3dbdd799afac1ca6b42d03dc93a23687', to: '0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a', value: BigNumber { s: 1, e: 0, c: [ 0 ] }, hash: '0x92aef634e0194a3c70a39163c45c2f2747ec86a8e40a9c6ddb93009741be8cb0', gasPrice: '0x2540be400', timestamp: 1529590674 }

Retrieve Transaction Receipt

The examples below show how to query the APIs for the receipt for a transaction given its hash. The functionality is compatible with eth_getTransactionReceipt. In each code snippet, the transaction receipt is retrieved from the API and printed to the standard output.

// specify tx hash Hash256 hash = Hash256.wrap("0x2a1d8dc09f2c6670690bbda74377f59c8737df9812cf7c794fa35409d200fe3f"); // get receipt for given tx hash TxReceipt txReceipt = api.getTx().getTxReceipt(hash).getObject(); // print tx receipt System.out.format("transaction receipt:%n%s%n", txReceipt);

Sample output:

transaction receipt: txIndex: 2, blockNumber: 247726, nrg: 320982, nrgCumulativeUsed: 489136, blockHash: 0x50a906f4ccaf05a3ebca69cc4f84a116e6aec881e3c4d080c4df505fea65afab, txHash: 0x2a1d8dc09f2c6670690bbda74377f59c8737df9812cf7c794fa35409d200fe3f, from: 0xa0dd16394f16ea21c8b45c00b2e43850ae7e8f00fe54789ddd1881d33b21df0c, to: 0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a, contractAddress: , log: [ address: 0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a, data: 0x, topics: [ 0x0xfd24c246254d45bcfec939758f58d63bd1565596fdc53994ae974bb54f83bb69, 0x0x7f21f0710cfd7a24883f3d41c47ad324b49e484b56d3010250110a9cd6876c76, 0x0x0000000000000000000000000000000000000000000000000000000000000006, 0x0x0000000000000000000000000000000000000000000000000000000000000001 ] ], [ address: 0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a, data: 0x, topics: [ 0x0xdc3b8ebc415c945740a70187f1d472ad2d64a9e7a87047f38023aec56516976b, 0x0xa064ff11a1563ef09fd5d4671272275dd962d46f7498f8d297d60bf3e4bee1ed, 0x0x00000000000000000000000000000000000000000000000000000002540be400 ] ], [ address: 0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a, data: 0x, topics: [ 0x0xdc3b8ebc415c945740a70187f1d472ad2d64a9e7a87047f38023aec56516976b, 0x0xa07e5b667332c1af7a14bb51b86fe92e1cc0d2a5f0de70613c26ff3584c312c7, 0x0x00000000000000000000000000000000000000000000000000000002540be400 ] ], [ address: 0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a, data: 0x, topics: [ 0x0xdc3b8ebc415c945740a70187f1d472ad2d64a9e7a87047f38023aec56516976b, 0x0xa0a3afd832901ba4e1bc947721babb13039194ac1b780f2b13b9b86b2ed08639, 0x0x00000000000000000000000000000000000000000000000000000002540be400 ] ], [ address: 0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a, data: 0x, topics: [ 0x0xdc3b8ebc415c945740a70187f1d472ad2d64a9e7a87047f38023aec56516976b, 0x0xa09bb6e70f50e6f512e0aa3033f2477ad04f75704bcf14ce1c9aca37492cf25b, 0x0x00000000000000000000000000000000000000000000000000000002540be400 ] ], [ address: 0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a, data: 0x, topics: [ 0x0xdc3b8ebc415c945740a70187f1d472ad2d64a9e7a87047f38023aec56516976b, 0x0xa0ce3ce58c28307850315f934cda934e65252b5115aaeca58ead64e88d9569ad, 0x0x00000000000000000000000000000000000000000000000000000002540be400 ] ], [ address: 0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a, data: 0x, topics: [ 0x0x1fa305c7f8521af161de570532762ed7a60199cde79e18e1d259af3459562521, 0x0x91ae034f8976efbe9c89fa3a78c19d663bf61c76c6d3dd528259fb9cb565e8db, 0x0x7f21f0710cfd7a24883f3d41c47ad324b49e484b56d3010250110a9cd6876c76 ] ]
transaction receipt: { blockHash: '0x50a906f4ccaf05a3ebca69cc4f84a116e6aec881e3c4d080c4df505fea65afab', nrgPrice: '0x02540be400', logsBloom: '00000000002000000000004000000000000000000000000000000000010000000000000000000000000000000000002000000400040000000000000000000000000000000000000400400000000000000000000000000000000800000040000800000000001040000000000008000000000000000000000000084000010008000000005000000008000000200102000000000000000008000000000000000000020000040080000000000000000000000800000006000008000000000008000000000020000000000000000400000000002000200000000000000000000000080001000000200040000000000000000000000000000000000000000000000400', nrgUsed: 320982, contractAddress: null, transactionIndex: 2, transactionHash: '0x2a1d8dc09f2c6670690bbda74377f59c8737df9812cf7c794fa35409d200fe3f', gasLimit: '0x1e8480', cumulativeNrgUsed: 489136, gasUsed: '0x04e5d6', blockNumber: 247726, root: '17e8253caf42481001da608479fa8250557b2a76226b6f3cbc9924c6768c4bd0', cumulativeGasUsed: '0x776b0', from: '0xa0dd16394f16ea21c8b45c00b2e43850ae7e8f00fe54789ddd1881d33b21df0c', to: '0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a', logs: [ { address: '0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a', logIndex: 0, data: '0x', topics: [Array], blockNumber: 247726, transactionIndex: 2 }, { address: '0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a', logIndex: 1, data: '0x', topics: [Array], blockNumber: 247726, transactionIndex: 2 }, { address: '0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a', logIndex: 2, data: '0x', topics: [Array], blockNumber: 247726, transactionIndex: 2 }, { address: '0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a', logIndex: 3, data: '0x', topics: [Array], blockNumber: 247726, transactionIndex: 2 }, { address: '0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a', logIndex: 4, data: '0x', topics: [Array], blockNumber: 247726, transactionIndex: 2 }, { address: '0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a', logIndex: 5, data: '0x', topics: [Array], blockNumber: 247726, transactionIndex: 2 }, { address: '0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a', logIndex: 6, data: '0x', topics: [Array], blockNumber: 247726, transactionIndex: 2 } ], gasPrice: '0x02540be400', status: '0x1' }

Perform Transactions

The examples below show how to use the APIs to send a transaction. The functionality is compatible with eth_sendTransaction. In each code snippet, the transaction receipt is retrieved from the API and printed to the standard output. Other possible transaction parameters are energy price, energy limit, and data.

Note that these examples only consider best case scenarios where the account can be unlocked, the transaction execution does not produce any errors and the transaction is eventually included in a block. A separate tutorial will be provided with the recommended sanity checks to ensure the sent transaction was included in the main chain.

// specify accounts and amount Address sender = Address.wrap("a06f02e986965ddd3398c4de87e3708072ad58d96e9c53e87c31c8c970b211e5"); Address receiver = Address.wrap("a0bd0ef93902d9e123521a67bef7391e9487e963b2346ef3b3ff78208835545e"); BigInteger amount = BigInteger.valueOf(1_000_000_000_000_000_000L); // = 1 AION // unlock sender boolean isUnlocked = api.getWallet().unlockAccount(sender, "password", 100).getObject(); System.out.format("sender account %s%n", isUnlocked ? "unlocked" : "locked"); // create transaction TxArgs.TxArgsBuilder builder = new TxArgs.TxArgsBuilder().from(sender).to(receiver).value(amount); // perform transaction Hash256 txHash = ((MsgRsp) api.getTx().sendTransaction(builder.createTxArgs()).getObject()).getTxHash(); System.out.format("%ntransaction hash: %s%n", txHash); // print receipt TxReceipt txReceipt = api.getTx().getTxReceipt(txHash).getObject(); // repeat till tx processed while (txReceipt == null) { // wait 10 sec sleep(10000); txReceipt = api.getTx().getTxReceipt(txHash).getObject(); } System.out.format("%ntransaction receipt:%n%s%n", txReceipt);
// specify accounts and amount let sender = 'a06f02e986965ddd3398c4de87e3708072ad58d96e9c53e87c31c8c970b211e5'; let receiver = 'a0bd0ef93902d9e123521a67bef7391e9487e963b2346ef3b3ff78208835545e'; let amount = 1000000000000000000; // = 1 AION // unlock sender let isUnlocked = web3.personal.unlockAccount(sender, "password", 100) let status = isUnlocked ? "unlocked" : "locked"; console.log("sender account " + status); // perform transaction let txHash = web3.eth.sendTransaction({from: sender, to: receiver, value: amount}); console.log("\ntransaction hash: " + txHash); // print receipt let txReceipt = web3.eth.getTransactionReceipt(txHash); // repeat till tx processed while (txReceipt == null) { // wait 10 sec sleep(10000); txReceipt = web3.eth.getTransactionReceipt(txHash); } console.log("\ntransaction receipt:"); console.log(txReceipt); function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); }

Sample output:

sender account unlocked transaction hash: 16b43e02eec679db7adccb1587183c6012879bb3ab753ecd324e6eab0c2b802a transaction receipt: txIndex: 0, blockNumber: 306854, nrg: 21000, nrgCumulativeUsed: 21000, blockHash: 0xf00500c28fdf2027b0151a0a75ca667d6f9b91a2a60a8c472ce6a94a256fc688, txHash: 0x16b43e02eec679db7adccb1587183c6012879bb3ab753ecd324e6eab0c2b802a, from: 0xa06f02e986965ddd3398c4de87e3708072ad58d96e9c53e87c31c8c970b211e5, to: 0xa0bd0ef93902d9e123521a67bef7391e9487e963b2346ef3b3ff78208835545e, contractAddress: , log:
sender account unlocked transaction hash: 0x652109fcfdce09008df6a805c0dfb6294d0a1bb004268675c1402cc2819f01ea transaction receipt: { blockHash: '0x863811cc0169e477375bafb2c223fb5e14f3b2965fe328143678dcb764b16298', nrgPrice: '0x02540be400', logsBloom: '00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', nrgUsed: 21000, contractAddress: null, transactionIndex: 0, transactionHash: '0x652109fcfdce09008df6a805c0dfb6294d0a1bb004268675c1402cc2819f01ea', gasLimit: '0x07a120', cumulativeNrgUsed: 21000, gasUsed: '0x5208', blockNumber: 257956, root: '3267660f820bef9d7848a9324316be6e1db003165c8512c138ffa38a4d8fd374', cumulativeGasUsed: '0x5208', from: '0xa06f02e986965ddd3398c4de87e3708072ad58d96e9c53e87c31c8c970b211e5', to: '0xa0bd0ef93902d9e123521a67bef7391e9487e963b2346ef3b3ff78208835545e', logs: [], gasPrice: '0x02540be400', status: '0x1' }

Complete Examples

Each code example below retrieves and prints to the standard output the following:

  1. the transaction details given its hash,
  2. the transaction details for a given block hash and transaction index,
  3. the transaction details for a given block number and transaction index,
  4. the receipt for a given transaction hash, and
  5. the transaction hash and receipt for a newly performed transaction.
package org.aion.tutorials; import org.aion.api.IAionAPI; import org.aion.api.type.*; import org.aion.base.type.Address; import org.aion.base.type.Hash256; import java.math.BigInteger; import static java.lang.Thread.sleep; public class TransactionExample { public static void main(String[] args) throws InterruptedException { // connect to Java API IAionAPI api = IAionAPI.init(); ApiMsg apiMsg = api.connect(IAionAPI.LOCALHOST_URL); // failed connection if (apiMsg.isError()) { System.out.format("Could not connect due to <%s>%n", apiMsg.getErrString()); System.exit(-1); } // 1. eth_getTransactionByHash // specify hash Hash256 hash = Hash256.wrap("0x5f2e74ade04ab9f6e8d4acd394f7f51832d4706d7268eea0ecc6391f94185b80"); // get tx with given hash Transaction tx = api.getChain().getTransactionByHash(hash).getObject(); // print tx information System.out.format("transaction details:%n%s%n", tx); // 2. eth_getTransactionByBlockHashAndIndex // specify block hash hash = Hash256.wrap("0x50a906f4ccaf05a3ebca69cc4f84a116e6aec881e3c4d080c4df505fea65afab"); // specify tx index = 0 -> first tx int index = 0; // get tx with given hash & index tx = api.getChain().getTransactionByBlockHashAndIndex(hash, index).getObject(); // print tx information System.out.format("transaction details:%n%s%n", tx); // 3. eth_getTransactionByBlockNumberAndIndex // specify block number long number = 247726L; // specify tx index = 1 -> second tx index = 1; // get tx with given number & index tx = api.getChain().getTransactionByBlockNumberAndIndex(number, index).getObject(); // print tx information System.out.format("transaction details:%n%s%n", tx); // 4. eth_getTransactionReceipt // specify tx hash hash = Hash256.wrap("0x2a1d8dc09f2c6670690bbda74377f59c8737df9812cf7c794fa35409d200fe3f"); // get receipt for given tx hash TxReceipt txReceipt = api.getTx().getTxReceipt(hash).getObject(); // print tx receipt System.out.format("transaction receipt:%n%s%n", txReceipt); // 5. eth_sendTransaction // specify accounts and amount Address sender = Address.wrap("a06f02e986965ddd3398c4de87e3708072ad58d96e9c53e87c31c8c970b211e5"); Address receiver = Address.wrap("a0bd0ef93902d9e123521a67bef7391e9487e963b2346ef3b3ff78208835545e"); BigInteger amount = BigInteger.valueOf(1_000_000_000_000_000_000L); // = 1 AION // unlock sender boolean isUnlocked = api.getWallet().unlockAccount(sender, "password", 100).getObject(); System.out.format("sender account %s%n", isUnlocked ? "unlocked" : "locked"); // create transaction TxArgs.TxArgsBuilder builder = new TxArgs.TxArgsBuilder().from(sender).to(receiver).value(amount); // perform transaction Hash256 txHash = ((MsgRsp) api.getTx().sendTransaction(builder.createTxArgs()).getObject()).getTxHash(); System.out.format("%ntransaction hash: %s%n", txHash); // print receipt txReceipt = api.getTx().getTxReceipt(txHash).getObject(); // repeat till tx processed while (txReceipt == null) { // wait 10 sec sleep(10000); txReceipt = api.getTx().getTxReceipt(txHash).getObject(); } System.out.format("%ntransaction receipt:%n%s%n", txReceipt); // disconnect from api api.destroyApi(); System.exit(0); } }
const Web3 = require('/path/to/aion/web3'); const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); // 1. eth_getTransactionByHash // specify hash let hash = '0x5f2e74ade04ab9f6e8d4acd394f7f51832d4706d7268eea0ecc6391f94185b80'; // get tx with given hash let tx = web3.eth.getTransaction(hash); // print tx information console.log("\ntransaction details:"); console.log(tx); // 2. eth_getTransactionByBlockHashAndIndex // specify block hash hash = '0x50a906f4ccaf05a3ebca69cc4f84a116e6aec881e3c4d080c4df505fea65afab'; // specify tx index = 0 -> first tx let index = 0; // get tx with given hash & index tx = web3.eth.getTransactionFromBlock(hash, index); // print tx information console.log("\ntransaction details:"); console.log(tx); // 3. eth_getTransactionByBlockNumberAndIndex // specify block number let number = 247726; // specify tx index = 1 -> second tx index = 1; // get tx with given number & index tx = web3.eth.getTransactionFromBlock(number, index); // print tx information console.log("\ntransaction details:"); console.log(tx); // 4. eth_getTransactionReceipt // specify tx hash hash = '0x2a1d8dc09f2c6670690bbda74377f59c8737df9812cf7c794fa35409d200fe3f'; // get receipt for given tx hash let txReceipt = web3.eth.getTransactionReceipt(hash); // print tx receipt console.log("\ntransaction receipt:"); console.log(txReceipt); // 5. eth_sendTransaction // specify accounts and amount let sender = 'a06f02e986965ddd3398c4de87e3708072ad58d96e9c53e87c31c8c970b211e5'; let receiver = 'a0bd0ef93902d9e123521a67bef7391e9487e963b2346ef3b3ff78208835545e'; let amount = 1000000000000000000; // = 1 AION // unlock sender let isUnlocked = web3.personal.unlockAccount(sender, "password", 100) let status = isUnlocked ? "unlocked" : "locked"; console.log("\nsender account " + status); // perform transaction let txHash = web3.eth.sendTransaction({from: sender, to: receiver, value: amount}); console.log("\ntransaction hash: " + txHash); // print receipt txReceipt = web3.eth.getTransactionReceipt(txHash); // repeat till tx processed while (txReceipt == null) { // wait 10 sec sleep(10000); txReceipt = web3.eth.getTransactionReceipt(txHash); } console.log("\ntransaction receipt:"); console.log(txReceipt); function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); }

Sample output:

transaction details: nrgPrice: 10000000000, nrg: 320922, nonce: 447, transactionIndex: 0, input: 0x1fec4cc69ce22f4a90d3d5ee88b4750e1c4ad0fbf9b2981ca82132742a48002f7e85e2fa00000000000000000000000000000040000000000000000000000000000000f000000000000000000000000000000005a082abceefa73078541d577ad576719c3f475b4ad0bd136918da43f0bce30429a0b612e3b6be803768451b7a331b1837face5be52b2b1fd253a31516bbdc1b50a044ba079d30fa1cdea965e93ab7cbaa9d18f8c02e569707bb320129a1840dada03cefb0ad4b6effd63825a1d6e345fc5b708f197b85c0a4f5583b125f52201aa0ee6827c6f05bd9192d444cb4fa0dec304829edf5c3c99d3df30d9618ebad1d00000000000000000000000000000005000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400, blockNumber: 247726, from: 0xa0dd16394f16ea21c8b45c00b2e43850ae7e8f00fe54789ddd1881d33b21df0c, to: 0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a, value: 0, hash: 0x5f2e74ade04ab9f6e8d4acd394f7f51832d4706d7268eea0ecc6391f94185b80, timestamp: 1529590672338000 transaction details: nrgPrice: 10000000000, nrg: 84107, nonce: 367, transactionIndex: 0, input: 0x1fec4cc691ae034f8976efbe9c89fa3a78c19d663bf61c76c6d3dd528259fb9cb565e8db00000000000000000000000000000040000000000000000000000000000000f000000000000000000000000000000005a064ff11a1563ef09fd5d4671272275dd962d46f7498f8d297d60bf3e4bee1eda07e5b667332c1af7a14bb51b86fe92e1cc0d2a5f0de70613c26ff3584c312c7a0a3afd832901ba4e1bc947721babb13039194ac1b780f2b13b9b86b2ed08639a09bb6e70f50e6f512e0aa3033f2477ad04f75704bcf14ce1c9aca37492cf25ba0ce3ce58c28307850315f934cda934e65252b5115aaeca58ead64e88d9569ad00000000000000000000000000000005000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400, blockNumber: 247726, from: 0xa05a3889b106e75baa621b8cc719679a3dbdd799afac1ca6b42d03dc93a23687, to: 0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a, value: 0, hash: 0xdb50c83faad497dc281df5a7ae5e2aa3294431d64a7868134895d33838882045, timestamp: 1529590657509000 transaction details: nrgPrice: 10000000000, nrg: 84047, nonce: 368, transactionIndex: 0, input: 0x1fec4cc69ce22f4a90d3d5ee88b4750e1c4ad0fbf9b2981ca82132742a48002f7e85e2fa00000000000000000000000000000040000000000000000000000000000000f000000000000000000000000000000005a082abceefa73078541d577ad576719c3f475b4ad0bd136918da43f0bce30429a0b612e3b6be803768451b7a331b1837face5be52b2b1fd253a31516bbdc1b50a044ba079d30fa1cdea965e93ab7cbaa9d18f8c02e569707bb320129a1840dada03cefb0ad4b6effd63825a1d6e345fc5b708f197b85c0a4f5583b125f52201aa0ee6827c6f05bd9192d444cb4fa0dec304829edf5c3c99d3df30d9618ebad1d00000000000000000000000000000005000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400, blockNumber: 247726, from: 0xa05a3889b106e75baa621b8cc719679a3dbdd799afac1ca6b42d03dc93a23687, to: 0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a, value: 0, hash: 0x92aef634e0194a3c70a39163c45c2f2747ec86a8e40a9c6ddb93009741be8cb0, timestamp: 1529590672530000 transaction receipt: txIndex: 2, blockNumber: 247726, nrg: 320982, nrgCumulativeUsed: 489136, blockHash: 0x50a906f4ccaf05a3ebca69cc4f84a116e6aec881e3c4d080c4df505fea65afab, txHash: 0x2a1d8dc09f2c6670690bbda74377f59c8737df9812cf7c794fa35409d200fe3f, from: 0xa0dd16394f16ea21c8b45c00b2e43850ae7e8f00fe54789ddd1881d33b21df0c, to: 0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a, contractAddress: , log: [ address: 0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a, data: 0x, topics: [ 0x0xfd24c246254d45bcfec939758f58d63bd1565596fdc53994ae974bb54f83bb69, 0x0x7f21f0710cfd7a24883f3d41c47ad324b49e484b56d3010250110a9cd6876c76, 0x0x0000000000000000000000000000000000000000000000000000000000000006, 0x0x0000000000000000000000000000000000000000000000000000000000000001 ] ], [ address: 0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a, data: 0x, topics: [ 0x0xdc3b8ebc415c945740a70187f1d472ad2d64a9e7a87047f38023aec56516976b, 0x0xa064ff11a1563ef09fd5d4671272275dd962d46f7498f8d297d60bf3e4bee1ed, 0x0x00000000000000000000000000000000000000000000000000000002540be400 ] ], [ address: 0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a, data: 0x, topics: [ 0x0xdc3b8ebc415c945740a70187f1d472ad2d64a9e7a87047f38023aec56516976b, 0x0xa07e5b667332c1af7a14bb51b86fe92e1cc0d2a5f0de70613c26ff3584c312c7, 0x0x00000000000000000000000000000000000000000000000000000002540be400 ] ], [ address: 0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a, data: 0x, topics: [ 0x0xdc3b8ebc415c945740a70187f1d472ad2d64a9e7a87047f38023aec56516976b, 0x0xa0a3afd832901ba4e1bc947721babb13039194ac1b780f2b13b9b86b2ed08639, 0x0x00000000000000000000000000000000000000000000000000000002540be400 ] ], [ address: 0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a, data: 0x, topics: [ 0x0xdc3b8ebc415c945740a70187f1d472ad2d64a9e7a87047f38023aec56516976b, 0x0xa09bb6e70f50e6f512e0aa3033f2477ad04f75704bcf14ce1c9aca37492cf25b, 0x0x00000000000000000000000000000000000000000000000000000002540be400 ] ], [ address: 0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a, data: 0x, topics: [ 0x0xdc3b8ebc415c945740a70187f1d472ad2d64a9e7a87047f38023aec56516976b, 0x0xa0ce3ce58c28307850315f934cda934e65252b5115aaeca58ead64e88d9569ad, 0x0x00000000000000000000000000000000000000000000000000000002540be400 ] ], [ address: 0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a, data: 0x, topics: [ 0x0x1fa305c7f8521af161de570532762ed7a60199cde79e18e1d259af3459562521, 0x0x91ae034f8976efbe9c89fa3a78c19d663bf61c76c6d3dd528259fb9cb565e8db, 0x0x7f21f0710cfd7a24883f3d41c47ad324b49e484b56d3010250110a9cd6876c76 ] ] sender account unlocked transaction hash: 027412b1717f161efdec7199a4d7b697705dac72c4af6217f2bcb9ed807c30aa transaction receipt: txIndex: 0, blockNumber: 306878, nrg: 21000, nrgCumulativeUsed: 21000, blockHash: 0xa7d7f03626384670e7c2a161ddeceed4949f408f428d54e4e9172c9a1545e0fa, txHash: 0x027412b1717f161efdec7199a4d7b697705dac72c4af6217f2bcb9ed807c30aa, from: 0xa06f02e986965ddd3398c4de87e3708072ad58d96e9c53e87c31c8c970b211e5, to: 0xa0bd0ef93902d9e123521a67bef7391e9487e963b2346ef3b3ff78208835545e, contractAddress: , log:
transaction details: { nrgPrice: BigNumber { s: 1, e: 10, c: [ 10000000000 ] }, blockHash: '0x50a906f4ccaf05a3ebca69cc4f84a116e6aec881e3c4d080c4df505fea65afab', nrg: 2000000, transactionIndex: 3, nonce: 447, input: '0x1fec4cc69ce22f4a90d3d5ee88b4750e1c4ad0fbf9b2981ca82132742a48002f7e85e2fa00000000000000000000000000000040000000000000000000000000000000f000000000000000000000000000000005a082abceefa73078541d577ad576719c3f475b4ad0bd136918da43f0bce30429a0b612e3b6be803768451b7a331b1837face5be52b2b1fd253a31516bbdc1b50a044ba079d30fa1cdea965e93ab7cbaa9d18f8c02e569707bb320129a1840dada03cefb0ad4b6effd63825a1d6e345fc5b708f197b85c0a4f5583b125f52201aa0ee6827c6f05bd9192d444cb4fa0dec304829edf5c3c99d3df30d9618ebad1d00000000000000000000000000000005000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400', blockNumber: 247726, gas: 2000000, from: '0xa0dd16394f16ea21c8b45c00b2e43850ae7e8f00fe54789ddd1881d33b21df0c', to: '0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a', value: BigNumber { s: 1, e: 0, c: [ 0 ] }, hash: '0x5f2e74ade04ab9f6e8d4acd394f7f51832d4706d7268eea0ecc6391f94185b80', gasPrice: '0x2540be400', timestamp: 1529590674 } transaction details: { nrgPrice: BigNumber { s: 1, e: 10, c: [ 10000000000 ] }, blockHash: '0x50a906f4ccaf05a3ebca69cc4f84a116e6aec881e3c4d080c4df505fea65afab', nrg: 2000000, transactionIndex: 0, nonce: 367, input: '0x1fec4cc691ae034f8976efbe9c89fa3a78c19d663bf61c76c6d3dd528259fb9cb565e8db00000000000000000000000000000040000000000000000000000000000000f000000000000000000000000000000005a064ff11a1563ef09fd5d4671272275dd962d46f7498f8d297d60bf3e4bee1eda07e5b667332c1af7a14bb51b86fe92e1cc0d2a5f0de70613c26ff3584c312c7a0a3afd832901ba4e1bc947721babb13039194ac1b780f2b13b9b86b2ed08639a09bb6e70f50e6f512e0aa3033f2477ad04f75704bcf14ce1c9aca37492cf25ba0ce3ce58c28307850315f934cda934e65252b5115aaeca58ead64e88d9569ad00000000000000000000000000000005000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400', blockNumber: 247726, gas: 2000000, from: '0xa05a3889b106e75baa621b8cc719679a3dbdd799afac1ca6b42d03dc93a23687', to: '0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a', value: BigNumber { s: 1, e: 0, c: [ 0 ] }, hash: '0xdb50c83faad497dc281df5a7ae5e2aa3294431d64a7868134895d33838882045', gasPrice: '0x2540be400', timestamp: 1529590674 } transaction details: { nrgPrice: BigNumber { s: 1, e: 10, c: [ 10000000000 ] }, blockHash: '0x50a906f4ccaf05a3ebca69cc4f84a116e6aec881e3c4d080c4df505fea65afab', nrg: 2000000, transactionIndex: 1, nonce: 368, input: '0x1fec4cc69ce22f4a90d3d5ee88b4750e1c4ad0fbf9b2981ca82132742a48002f7e85e2fa00000000000000000000000000000040000000000000000000000000000000f000000000000000000000000000000005a082abceefa73078541d577ad576719c3f475b4ad0bd136918da43f0bce30429a0b612e3b6be803768451b7a331b1837face5be52b2b1fd253a31516bbdc1b50a044ba079d30fa1cdea965e93ab7cbaa9d18f8c02e569707bb320129a1840dada03cefb0ad4b6effd63825a1d6e345fc5b708f197b85c0a4f5583b125f52201aa0ee6827c6f05bd9192d444cb4fa0dec304829edf5c3c99d3df30d9618ebad1d00000000000000000000000000000005000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400000000000000000000000002540be400', blockNumber: 247726, gas: 2000000, from: '0xa05a3889b106e75baa621b8cc719679a3dbdd799afac1ca6b42d03dc93a23687', to: '0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a', value: BigNumber { s: 1, e: 0, c: [ 0 ] }, hash: '0x92aef634e0194a3c70a39163c45c2f2747ec86a8e40a9c6ddb93009741be8cb0', gasPrice: '0x2540be400', timestamp: 1529590674 } transaction receipt: { blockHash: '0x50a906f4ccaf05a3ebca69cc4f84a116e6aec881e3c4d080c4df505fea65afab', nrgPrice: '0x02540be400', logsBloom: '00000000002000000000004000000000000000000000000000000000010000000000000000000000000000000000002000000400040000000000000000000000000000000000000400400000000000000000000000000000000800000040000800000000001040000000000008000000000000000000000000084000010008000000005000000008000000200102000000000000000008000000000000000000020000040080000000000000000000000800000006000008000000000008000000000020000000000000000400000000002000200000000000000000000000080001000000200040000000000000000000000000000000000000000000000400', nrgUsed: 320982, contractAddress: null, transactionIndex: 2, transactionHash: '0x2a1d8dc09f2c6670690bbda74377f59c8737df9812cf7c794fa35409d200fe3f', gasLimit: '0x1e8480', cumulativeNrgUsed: 489136, gasUsed: '0x04e5d6', blockNumber: 247726, root: '17e8253caf42481001da608479fa8250557b2a76226b6f3cbc9924c6768c4bd0', cumulativeGasUsed: '0x776b0', from: '0xa0dd16394f16ea21c8b45c00b2e43850ae7e8f00fe54789ddd1881d33b21df0c', to: '0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a', logs: [ { address: '0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a', logIndex: 0, data: '0x', topics: [Array], blockNumber: 247726, transactionIndex: 2 }, { address: '0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a', logIndex: 1, data: '0x', topics: [Array], blockNumber: 247726, transactionIndex: 2 }, { address: '0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a', logIndex: 2, data: '0x', topics: [Array], blockNumber: 247726, transactionIndex: 2 }, { address: '0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a', logIndex: 3, data: '0x', topics: [Array], blockNumber: 247726, transactionIndex: 2 }, { address: '0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a', logIndex: 4, data: '0x', topics: [Array], blockNumber: 247726, transactionIndex: 2 }, { address: '0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a', logIndex: 5, data: '0x', topics: [Array], blockNumber: 247726, transactionIndex: 2 }, { address: '0xa0e1cca4fe786118c0abb1fdf45c04e44354f971b25c04ed77ac46f13cae179a', logIndex: 6, data: '0x', topics: [Array], blockNumber: 247726, transactionIndex: 2 } ], gasPrice: '0x02540be400', status: '0x1' } sender account unlocked transaction hash: 0x3f66966ad038c8fbc5d31722d077e79675811f6c8d0d86fd9bc9445d18c7b07c transaction receipt: { blockHash: '0x76944e5c82875b34e06c88634fcff023967f1c28be7a3809b3e191c6df1ef8ce', nrgPrice: '0x02540be400', logsBloom: '00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', nrgUsed: 21000, contractAddress: null, transactionIndex: 0, transactionHash: '0x3f66966ad038c8fbc5d31722d077e79675811f6c8d0d86fd9bc9445d18c7b07c', gasLimit: '0x1e8480', cumulativeNrgUsed: 21000, gasUsed: '0x5208', blockNumber: 306911, root: 'a3710bbf0c04c6f66bf4b268365ffe38ad78429a778f63ffdbb3fd4aa7c66d53', cumulativeGasUsed: '0x5208', from: '0xa06f02e986965ddd3398c4de87e3708072ad58d96e9c53e87c31c8c970b211e5', to: '0xa0bd0ef93902d9e123521a67bef7391e9487e963b2346ef3b3ff78208835545e', logs: [], gasPrice: '0x02540be400', status: '0x1' }