-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsigner.js
More file actions
134 lines (119 loc) · 3.56 KB
/
Copy pathsigner.js
File metadata and controls
134 lines (119 loc) · 3.56 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
require('dotenv').config();
const { ethers } = require('ethers');
//const { ABI } = require('./dummyABIandBytecode');
//const CONTRACT_ADDRESS = process.env.CONTRACT_ADDRESS
const { ABI } = require('./heavyDummyABIandBytecode');
const CONTRACT_ADDRESS = process.env.HEAVY_CONTRACT_ADDRESS
const GAS_PRICE = '0x3B9ACA00';
const GAS_LIMIT = '0xA7D8C0';
const DESTINATION_ACCOUNT = '0x8ba1f109551bD432803012645Ac136ddd64DBA72';
const ETHERS_SENT = '0.00001';
class NonceManager {
constructor() {
this.nonce = 0;
}
getIncreaseNonce() {
const currNonce = this.nonce;
this.nonce += 1;
return currNonce;
}
}
const provider = new ethers.providers.JsonRpcProvider(process.env.NODE_ENDPOINT);
const wallet = new ethers.Wallet(process.env.PRIV_KEY, provider);
let baseNonce;
const nonceManager = new NonceManager();
const contract = new ethers.Contract(CONTRACT_ADDRESS, ABI, wallet);
async function getNonce() {
const nonce = await wallet.getTransactionCount();
return nonce;
}
async function getRawTransactionSigned(txNonce) {
const tx = {
to: DESTINATION_ACCOUNT,
value: ethers.utils.parseEther(ETHERS_SENT),
gasPrice: GAS_PRICE,
gasLimit: GAS_LIMIT,
nonce: txNonce,
};
const rawTx = await wallet.signTransaction(tx);
return rawTx;
}
async function getRawTransactionSignedFromContract(txNonce) {
const tx = {
gasPrice: GAS_PRICE,
gasLimit: GAS_LIMIT,
nonce: txNonce,
};
const txContract = await contract.populateTransaction.doWork(); // Change this method for your method
Object.assign(tx, txContract);
const rawTx = await wallet.signTransaction(tx);
return rawTx;
}
function createRawTx(requestParams, context, ee, next) {
if (baseNonce === undefined) {
getNonce().then((currNonce) => {
baseNonce = currNonce;
getRawTransactionSigned(baseNonce + nonceManager.getIncreaseNonce()).then((rawTx) => {
context.vars.rawTx = rawTx;
return next();
});
});
} else {
getRawTransactionSigned(baseNonce + nonceManager.getIncreaseNonce()).then((rawTx) => {
context.vars.rawTx = rawTx;
return next();
});
}
}
function createContractRawTx(requestParams, context, ee, next) {
if (baseNonce === undefined) {
getNonce().then((currNonce) => {
baseNonce = currNonce;
getRawTransactionSignedFromContract(baseNonce + nonceManager.getIncreaseNonce())
.then((rawTx) => {
context.vars.rawTx = rawTx;
return next();
});
});
} else {
getRawTransactionSignedFromContract(baseNonce + nonceManager.getIncreaseNonce())
.then((rawTx) => {
context.vars.rawTx = rawTx;
return next();
});
}
}
function logBody(requestParams, response, context, ee, next) {
if (response.body.error) {
console.log(response.body);
} else if (process.env.DEBUG === 'true') {
console.log(`Result: ${response.body.result}`);
}
return next();
}
function logBodyForReceipt(requestParams, response, context, ee, next) {
if (response.body.error || response.body.result === null) {
console.log(response.body);
} else if (process.env.DEBUG === 'true') {
console.log(`Blocknumber: ${response.body.result.blockNumber}, Blockhash: ${response.body.result.blockHash}`);
}
return next();
}
function waitForTx(requestParams, context, ee, next) {
provider.waitForTransaction(context.vars.txHash)
.then(() => {
return next();
})
.catch(err => {
console.error(err);
return next();
})
}
module.exports = {
createRawTx,
createContractRawTx,
logBody,
getNonce,
waitForTx,
logBodyForReceipt,
};