-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
244 lines (215 loc) · 7.08 KB
/
index.js
File metadata and controls
244 lines (215 loc) · 7.08 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*******************************************************************************
web3js-raw
Reusable set of functions to send transactions using sendRawTransaction of web3js
* Copyright(c) 2018-2018 Chim Himidumage
* MIT Licensed
2018/06/13 - Introduced promises to all Async calls
- Updated all dependencies to latest versions
- Used <new web3.eth.Contract> for contract instance creation
2018/06/22 - Made web3.eth.getTransaction function call Async with async & await
2018/06/24 - getWeb3 now accepts ABI, Contract address and provider. Then initialise Web3 instance for the given contract
2018/07/03 - Introduced gasEstimate function to find the exact gas requirment
- Included gasLimit parameter to prepareSignSend, so the gasLimit can be set from the caller
2018/08/08 - Ability to get web3 instance wihtout binding to a contract (getWeb3Base)
2018/08/09 - Introduce error handling, updated try-catch on getWeb3, prepareSignSend, invokeGetTxnReceipt, getDefaultTxnAttributes
- Removed invokeSendRawTransaction, and included the web3.eth.sendSignedTransaction inside prepareSignSend itself
2018/08/10 - Removed the Promise around sendSignedTransaction as __prepareSignSend__ is already an async function
********************************************************************************/
"use strict";
const { Web3 } = require("web3"); // https://www.npmjs.com/package/web3
var Web3Utils = require("web3-utils");
var { LegacyTransaction } = require("@ethereumjs/tx");
var Web3EthAbi = require("web3-eth-abi");
var CryptoJS = require("crypto-js");
//Support Functions
module.exports = function (provider) {
this.ContractInstance;
this.web3 = new Web3(provider);
this.setProvider = function (provider) {
this.web3.setProvider(new this.web3.providers.HttpProvider(provider));
};
this.getWeb3 = async function (contractABI, contractAddress) {
try {
this.ContractInstance = await new this.web3.eth.Contract(
contractABI,
contractAddress
);
return this.web3;
} catch (err) {
console.log("Error in getWeb3", err);
return null;
}
};
this.getWeb3Base = function (provider) {
if (!provider) {
return this.web3;
} else {
this.web3.setProvider(new this.web3.providers.HttpProvider(provider));
return this.web3;
}
};
this.createContractInstance = function (contractABI, contractAddress) {
this.ContractInstance = new this.web3.eth.Contract(
contractABI,
contractAddress
);
};
this.encodeFunctionParams = function (abi, methodName, params) {
var types = this.getFunctionParams(abi, methodName);
var fullName = methodName + "(" + types.join() + ")";
var signature = CryptoJS.SHA3(fullName, { outputLength: 256 })
.toString(CryptoJS.enc.Hex)
.slice(0, 8);
var dataHex =
signature + Web3EthAbi.encodeParameters(...types, params).substring(2);
var payload = "0x" + dataHex;
return payload;
};
this.getFunctionParams = function (abi, funcName) {
return abi
.filter(function (json) {
return json.type === "function" && json.name === funcName;
})
.map(function (json) {
return json.inputs.map(function (input) {
return input.type;
});
});
};
this.encodeConstructorParams = function (abi, params) {
return (
abi
.filter(function (json) {
return (
json.type === "constructor" && json.inputs.length === params.length
);
})
.map(function (json) {
return json.inputs.map(function (input) {
return input.type;
});
})
.map(function (types) {
return Web3EthAbi.encodeParameters(types, params);
})[0] || ""
);
};
this.getSignedTransaction = function (txnRawData, pvtKey) {
var tx = LegacyTransaction.fromTxData(txnRawData);
var signedTx = tx.sign(pvtKey);
var serializedTx = signedTx.serialize();
var txToSend = "0x" + serializedTx.toString("hex");
return txToSend;
};
this.createNewAccount = function (web3 = this.web3) {
return new Promise(function (resolve, reject) {
var accounts = web3.eth.accounts;
var retObj = accounts.create();
if (retObj === null) {
reject({ status: 0, message: "Account create failed" });
} else {
resolve({ address: retObj.address, privateKey: retObj.privateKey });
}
});
};
this.prepareSignSend = async function (
abi,
contractAddress,
functionName,
senderAddress,
privateKey,
valueInWei = "0x00",
params,
gasLimit
) {
try {
var txnData = this.encodeFunctionParams(abi, functionName, params);
var _gasLimit = await this.ContractInstance.methods[functionName](
...params
).estimateGas({ from: senderAddress, gas: gasLimit, value: valueInWei });
var txnRawData = await this.getDefaultTxnAttributes(
"",
senderAddress,
contractAddress,
valueInWei,
txnData,
_gasLimit,
""
);
if (txnRawData) {
var dataToSend = await this.web3.eth.accounts.signTransaction(
txnRawData,
privateKey
);
var txHash = await this.web3.eth.sendSignedTransaction(
dataToSend.rawTransaction
);
return Promise.resolve({
status: 1,
functionName: functionName,
message: txHash,
});
} else {
return Promise.reject({
status: 0,
functionName: functionName,
message: "Error in setting Default Txn Attributes",
});
}
} catch (err) {
return Promise.reject({
status: 0,
functionName: functionName,
message: err,
});
}
};
this.invokeGetTxnReceipt = function (tx_hash, web3 = this.web3) {
return new Promise(async (resolve, reject) => {
try {
var txnInfo = await web3.eth.getTransaction(tx_hash);
if (txnInfo === null) {
reject({ status: 0, message: "Transaction not found" });
} else {
resolve({ status: 1, message: txnInfo });
}
} catch (err) {
reject({ status: 0, message: err });
}
});
};
this.getDefaultTxnAttributes = async function (
nonce,
fromAddress,
toAddress,
valueInWei,
dataAsHex,
gasLimit,
gasPrice
) {
var TxnAttributes = {
nonce: "0x00",
to: "0x00",
value: "0x00",
data: "0x00",
gasLimit: "0x00",
gasPrice: "0x00",
};
try {
if (nonce == "") {
nonce = await this.web3.eth.getTransactionCount(fromAddress, "latest");
}
TxnAttributes.nonce = nonce;
TxnAttributes.to = toAddress;
TxnAttributes.value = valueInWei;
TxnAttributes.data = dataAsHex;
if (gasLimit == "") gasLimit = 750000;
TxnAttributes.gasLimit = Web3Utils.toHex(gasLimit);
if (gasPrice == "") gasPrice = await this.web3.eth.getGasPrice();
TxnAttributes.gasPrice = Web3Utils.toHex(gasPrice);
return TxnAttributes;
} catch (err) {
return null;
}
};
};