-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.js
137 lines (119 loc) · 4.26 KB
/
demo.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
// https://github.com/AElfProject/aelf-sdk.js
const AElf = require("aelf-sdk");
// aelf-sdk.js dependencie @aelfqueen/protobufjs
const protoBuf = require("@aelfqueen/protobufjs");
const descriptor = require("@aelfqueen/protobufjs/ext/descriptor");
const tokenProtoBase64 = require("./token_proto_base64.json");
const aelf = new AElf(
new AElf.providers.HttpProvider("http://18.163.40.216:8000")
);
const wallet = AElf.wallet.getWalletByPrivateKey(
"4e83df2aa7c8552a75961f9ab9f2f06e01e0dca0203e383da5468bbbe2915c97"
);
const contractAddress = "JRmBduh4nXWi1aXgdUsj5gJrzeZb2LxmrAbf7W99faZSvoAaE",
methodName = "Transfer",
toAddress = "cDPLA9axUVeujnTTk4Cyr3aqRby3cHHAB6Rh28o7BRTTxi8US";
const getServicesFromFileDescriptors = (descriptors) => {
const root = AElf.pbjs.Root.fromDescriptor(
descriptors,
"proto3"
).resolveAll();
return descriptors.file
.filter((f) => f.service.length > 0)
.map((f) => {
const sn = f.service[0].name;
const fullName = f.package ? `${f.package}.${sn}` : sn;
return root.lookupService(fullName);
});
};
const getRawTx = (blockHeightInput, blockHashInput, packedInput) => {
const rawTx = AElf.pbUtils.getTransaction(
wallet.address,
contractAddress,
methodName,
packedInput
);
rawTx.refBlockNumber = blockHeightInput;
const blockHash = blockHashInput.match(/^0x/)
? blockHashInput.substring(2)
: blockHashInput;
rawTx.refBlockPrefix = Buffer.from(blockHash, "hex").slice(0, 4);
return rawTx;
};
const handleTransaction = (height, hash, encoded) => {
// Create transaction
const rawTx = getRawTx(height, hash, encoded);
// signature
let tx = AElf.wallet.signTransaction(rawTx, wallet.keyPair);
tx = AElf.pbUtils.Transaction.encode(tx).finish();
if (tx instanceof Buffer) {
return tx.toString("hex");
}
return AElf.utils.uint8ArrayToHex(tx);
};
const getFileDescriptorsSet = async (contractAddress) => {
const fds = await aelf.chain.getContractFileDescriptorSet(contractAddress);
return getServicesFromFileDescriptors(fds);
};
const fileDescriptorSetFormatter = (result) => {
const buffer = Buffer.from(result, "base64");
return descriptor.FileDescriptorSet.decode(buffer);
};
const getLocalFileDescriptorsSet = (base64) => {
const fds = fileDescriptorSetFormatter(base64);
return getServicesFromFileDescriptors(fds);
};
const getProtoFileDescriptorsSet = async () => {
const root = (
await protoBuf.load("./protobuf/token_contract.proto")
).resolveAll();
return root.nested.token.nested;
};
const encodedTransfer = async (params) => {
// Get the protobuf definitions related to a contract
// 1.protobuf file descriptors from aelf-sdk.js
// const fileDescriptors = await getFileDescriptorsSet(contractAddress);
// const inputType =
// fileDescriptors[2].methods[methodName].resolve().resolvedRequestType;
// 2.protobuf file descriptors from local protobuf base64
// const fileDescriptors = getLocalFileDescriptorsSet(tokenProtoBase64);
// const inputType =
// fileDescriptors[2].methods[methodName].resolve().resolvedRequestType;
// 3.protobuf file descriptors from protobufjs
const fileDescriptors = await getProtoFileDescriptorsSet();
const inputType =
fileDescriptors.TokenContract.methods[methodName].resolve()
.resolvedRequestType;
let input = AElf.utils.transform.transformMapToArray(inputType, params);
input = AElf.utils.transform.transform(
inputType,
input,
AElf.utils.transform.INPUT_TRANSFORMERS
);
const message = inputType.fromObject(input);
return inputType.encode(message).finish();
};
(async () => {
const encoded = await encodedTransfer({
symbol: "ELF",
to: toAddress,
amount: "10000000",
memo: "test",
});
console.log(encoded, "=====encoded");
console.log(encoded.toString("hex"));
const { BestChainHeight, BestChainHash } = await aelf.chain.getChainStatus();
const transaction = handleTransaction(
BestChainHeight,
BestChainHash,
encoded
);
// Broadcast a transaction
console.log(transaction, "transaction")
const send = await aelf.chain.sendTransaction(transaction);
setTimeout(async () => {
// Get the result of a transaction
const result = await aelf.chain.getTxResult(send.TransactionId);
console.log(result, "=====tx-result");
}, 2000);
})();