-
Notifications
You must be signed in to change notification settings - Fork 30
/
IcxTransactionExample.js
138 lines (117 loc) · 5.65 KB
/
IcxTransactionExample.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* eslint-disable */
import IconService, { IconAmount, IconConverter, HttpProvider, IconWallet, IconBuilder, SignedTransaction } from 'icon-sdk-js';
import MockData from '../../mockData/index.js';
let icxTransactionExample;
class IcxTransactionExample {
constructor() {
// HttpProvider is used to communicate with http.
this.provider = new HttpProvider(MockData.NODE_URL);
// Create IconService instance
this.iconService = new IconService(this.provider);
// Load wallet
this.wallet = IconWallet.loadPrivateKey(MockData.PRIVATE_KEY_1);
this.txHash = '';
this.addListener();
(async () => {
try {
await this.getWalletBalance();
} catch(e) {
console.log(e);
}
})();
}
addListener() {
// 1. Send ICX Transaction
document.getElementById('I01').addEventListener('click', async () => {
document.getElementById('I03-2').innerHTML = '';
await this.sendTransaction();
});
// 2. Check Wallet Balance
document.getElementById('I02').addEventListener('click', async () => {
await this.getWalletBalance();
});
// 3. Check TX Status
document.getElementById('I03').addEventListener('click', async () => {
await this.checkTxStatus();
});
}
async sendTransaction() {
// Build raw transaction object
const transaction = await this.buildICXTransaction();
// Create signature of the transaction
const signedTransaction = new SignedTransaction(transaction, this.wallet);
// Read params to transfer to nodes
const signedTransactionProperties = JSON.stringify(signedTransaction.getProperties()).split(",").join(", \n")
document.getElementById('I01-1').innerHTML = `<b>Signed Transaction</b>: ${signedTransactionProperties}`;
// Send transaction
this.txHash = await this.iconService.sendTransaction(signedTransaction).execute();
console.log(this.txHash)
document.getElementById('I03-1').innerHTML = this.txHash;
// Print transaction hash
document.getElementById('I01-2').innerHTML = `<b>Transfer Request Complete.</b> Tx hash is ${this.txHash}`;
}
async buildICXTransaction() {
const { IcxTransactionBuilder } = IconBuilder;
const walletAddress = this.wallet.getAddress();
// 1 ICX -> 1000000000000000000 conversion
const value = IconAmount.of(1, IconAmount.Unit.ICX).toLoop();
// You can use "governance score apis" to get step costs.
const stepLimit = await this.getDefaultStepCost();
// networkId of node 1:mainnet, 2~:etc
const networkId = IconConverter.toBigNumber(3);
const version = IconConverter.toBigNumber(3);
// Timestamp is used to prevent the identical transactions. Only current time is required (Standard unit : us)
// If the timestamp is considerably different from the current time, the transaction will be rejected.
const timestamp = (new Date()).getTime() * 1000;
//Enter transaction information
const icxTransactionBuilder = new IcxTransactionBuilder();
const transaction = icxTransactionBuilder
.nid(networkId)
.from(walletAddress)
.to(MockData.WALLET_ADDRESS_2)
.value(value)
.stepLimit(stepLimit)
.timestamp(timestamp)
.version(version)
.build();
return transaction;
}
async getDefaultStepCost() {
const { CallBuilder } = IconBuilder;
// Get governance score api list
const governanceApi = await this.iconService.getScoreApi(MockData.GOVERNANCE_ADDRESS).execute();
console.log(governanceApi)
const methodName = 'getStepCosts';
// Check input and output parameters of api if you need
const getStepCostsApi = governanceApi.getMethod(methodName);
const getStepCostsApiInputs = getStepCostsApi.inputs.length > 0 ? JSON.stringify(getStepCostsApi.inputs) : 'none';
const getStepCostsApiOutputs = getStepCostsApi.outputs.length > 0 ? JSON.stringify(getStepCostsApi.outputs) : 'none';
console.log(`[getStepCosts]\n inputs: ${getStepCostsApiInputs} \n outputs: ${getStepCostsApiOutputs}`);
// Get step costs by iconService.call
const callBuilder = new CallBuilder();
const call = callBuilder
.to(MockData.GOVERNANCE_ADDRESS)
.method(methodName)
.build();
const stepCosts = await this.iconService.call(call).execute();
return stepCosts.default
}
async getWalletBalance() {
const balanceA = await this.iconService.getBalance(MockData.WALLET_ADDRESS_1).execute();
const balanceB = await this.iconService.getBalance(MockData.WALLET_ADDRESS_2).execute();
document.getElementById('I02-1').innerHTML = `<b>${IconAmount.of(balanceA, IconAmount.Unit.LOOP).convertUnit(IconAmount.Unit.ICX)}`;
document.getElementById('I02-2').innerHTML = `<b>${IconAmount.of(balanceB, IconAmount.Unit.LOOP).convertUnit(IconAmount.Unit.ICX)}`;
}
async checkTxStatus() {
if (!this.txHash) {
document.getElementById('I03-1').innerHTML = 'Make transaction first.';
}
const transactionResult = await this.iconService.getTransactionResult(this.txHash).execute();
const status = transactionResult.status === 1 ? 'success' : 'failure';
document.getElementById('I03-2').innerHTML = `<b>tx status</b>: ${status}`;
}
}
if (document.getElementById('I01')) {
icxTransactionExample = new IcxTransactionExample();
}
export default icxTransactionExample;