forked from circlefin/stablecoin-evm
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathABIUtils.js
76 lines (64 loc) · 2.25 KB
/
ABIUtils.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
var Tx = require('ethereumjs-tx');
function makeRawTransaction(msgData, msgSender, hexPrivateKey, contractAddress) {
var tx = new Tx({
nonce: web3.toHex(web3.eth.getTransactionCount(msgSender)),
gasPrice: web3.toHex(web3.toWei('20', 'gwei')),
gasLimit: 1000000,
to: contractAddress,
value: 0,
data: msgData,
});
var privateKey = Buffer.from(hexPrivateKey, 'hex');
tx.sign(privateKey);
var raw = '0x' + tx.serialize().toString('hex');
return raw
}
function sendRawTransaction(raw) {
return new Promise(function (resolve, reject) {
web3.eth.sendRawTransaction(raw, function (err, transactionHash) {
if (err !== null) return reject(err);
resolve(transactionHash);
});
});
}
function functionSignature(methodName) {
return web3.sha3(methodName).substr(0, 2 + 8);
}
function encodeAddress(address) {
address = address.substr(2, address.length - 2);
while (address.length < 64) address = "0" + address;
return address;
}
function encodeUint(value) {
value = value.toString(16);
while (value.length < 64) value = "0" + value;
return value;
}
// Create ABI calls for functions
function msgData0(methodName, value) {
return functionSignature(methodName) + encodeUint(value);
}
function msgData(methodName, addressValue) {
return functionSignature(methodName) + encodeAddress(addressValue);
}
function msgData1(methodName, address, value) {
return functionSignature(methodName) + encodeAddress(address) + encodeUint(value);
}
function msgData2(methodName, address1, address2, value) {
return functionSignature(methodName) + encodeAddress(address1) + encodeAddress(address2) + encodeUint(value);
}
function msgData3(methodName, address1, value1, address2, value2) {
return functionSignature(methodName) + encodeAddress(address1) + encodeUint(value1) + encodeAddress(address2) + encodeUint(value2);
}
module.exports = {
makeRawTransaction: makeRawTransaction,
sendRawTransaction: sendRawTransaction,
functionSignature: functionSignature,
encodeAddress: encodeAddress,
encodeUint: encodeUint,
msgData0: msgData0,
msgData: msgData,
msgData1: msgData1,
msgData2: msgData2,
msgData3: msgData3
};