· 6 years ago · Dec 24, 2019, 09:34 PM
1
2const rp = require('request-promise');
3const { Network, TransactionType, Transaction, Key } = require('semux-js')
4const Long = require('long')
5
6const hexData = "0xafd8d79600000000000000000000000000000000000000000000000000000000000000320000000000000000000000000000000000000000000000000000000000000000"
7const API = "http://localhost:5171/v2.4.0"
8const privateKey = "0x302e020100300506032b657004220420677433b0ae0103146516e2a780147e0f48d2239bb5e68e84e3b0058c0cc2ca84"
9
10async function getNonce() {
11 let result = await rp(`${API}/account?address=0xb262232509fdd5b0d29c95f818f6e648bca97ee4`)
12 result = JSON.parse(result)
13 return result.result.nonce
14}
15
16async function main() {
17 const nonce = await getNonce()
18 var tx = new Transaction( Network.TESTNET,
19 TransactionType.CALL,
20 hexBytes("0x2a9667154f117cc2962559b47d5084e607fa635a"), // to
21 Long.fromNumber(parseInt(10000000)), // value
22 Long.fromNumber("0"), // fee
23 Long.fromString("55000"), // gas
24 Long.fromString("1"), // gasPrice
25 Long.fromNumber(nonce), // nonce
26 Long.fromNumber(new Date().getTime()), // timestamp
27 hexBytes(hexData) // data
28 )
29 const encodedPK = Key.importEncodedPrivateKey(hexBytes(privateKey))
30 const signedTranasction = tx.sign(encodedPK)
31 const serialize = Buffer.from(signedTranasction.toBytes().buffer).toString('hex')
32 const {result} = await rp(`${API}/broadcast-raw-transaction?raw=${serialize}`)
33 console.log(result)
34}
35main()
36
37function hexBytes (s) {
38 return Buffer.from(s.replace('0x', ''), 'hex')
39}