This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbundle_example.js
193 lines (162 loc) · 12.4 KB
/
bundle_example.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
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
const Web3 = require('@node-real/web3');
const directRouteEndPoint = "https://api.nodereal.io/direct-route";
const rpcEndPoint = "https://bsc-dataseed.binance.org";
const getBundlePriceDemo = async () => {
const directClient = new Web3(directRouteEndPoint);
var price = await directClient.eth.getBundlePrice();
console.log(price);
};
getBundlePriceDemo();
const getValidatorsStatusDemo = async () => {
const directClient = new Web3(directRouteEndPoint);
var status = await directClient.eth.getValidatorStatus();
console.log(status)
}
getValidatorsStatusDemo();
const sendBUSDByBundleDemo = async () => {
const directClient = new Web3(directRouteEndPoint);
const rpcClient = new Web3(rpcEndPoint);
var contractABI = "[{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"spender\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"Approval\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"previousOwner\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"newOwner\",\"type\":\"address\"}],\"name\":\"OwnershipTransferred\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"from\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"Transfer\",\"type\":\"event\"},{\"constant\":true,\"inputs\":[],\"name\":\"_decimals\",\"outputs\":[{\"internalType\":\"uint8\",\"name\":\"\",\"type\":\"uint8\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"_name\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"_symbol\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"spender\",\"type\":\"address\"}],\"name\":\"allowance\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"internalType\":\"address\",\"name\":\"spender\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"approve\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"}],\"name\":\"balanceOf\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"burn\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"decimals\",\"outputs\":[{\"internalType\":\"uint8\",\"name\":\"\",\"type\":\"uint8\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"internalType\":\"address\",\"name\":\"spender\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"subtractedValue\",\"type\":\"uint256\"}],\"name\":\"decreaseAllowance\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"getOwner\",\"outputs\":[{\"internalType\":\"address\",\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"internalType\":\"address\",\"name\":\"spender\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"addedValue\",\"type\":\"uint256\"}],\"name\":\"increaseAllowance\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"mint\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"name\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"owner\",\"outputs\":[{\"internalType\":\"address\",\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[],\"name\":\"renounceOwnership\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"symbol\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"totalSupply\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"internalType\":\"address\",\"name\":\"recipient\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"transfer\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"internalType\":\"address\",\"name\":\"sender\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"recipient\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"transferFrom\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"internalType\":\"address\",\"name\":\"newOwner\",\"type\":\"address\"}],\"name\":\"transferOwnership\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]";
contractABI = JSON.parse(contractABI);
const contractAddr = "0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56";
const contract = new rpcClient.eth.Contract(contractABI, contractAddr);
const account1 = 'input the address of account1';
const account2 = 'input the address of account2';
const amount = 1e15;
const data1 = contract.methods.transfer(account2, amount).encodeABI();
const data2 = contract.methods.transfer(account2, amount).encodeABI();
const bundlePrice = await directClient.eth.getBundlePrice();
var price = bundlePrice.minimalGasPrice;
if (price < bundlePrice.bundlePrice) {
price = bundlePrice.bundlePrice;
}
const tx1 = {
'from': account1,
'to': contractAddr,
'gas': 70000,
'gasPrice': price,
'data': rpcClient.utils.toHex(data1),
'chainId': 56,
};
const tx2 = {
'from': account1,
'to': contractAddr,
'gas': 70000,
'gasPrice': price,
'data': rpcClient.utils.toHex(data2),
'chainId': 56,
};
const privateKey1 = 'input your private of account1';
const privateKey2 = 'input your private of account2';
const signedTx1 = await rpcClient.eth.accounts.signTransaction(tx1, privateKey1);
const signedTx2 = await rpcClient.eth.accounts.signTransaction(tx2, privateKey1);
var myDate = new Date();
const maxTime = Math.floor(myDate.getTime() / 1000) + 80;
const minTime = Math.floor(myDate.getTime() / 1000) + 20;
console.log(maxTime, minTime);
const bundleArgs = {
'txs': [signedTx1.rawTransaction, signedTx2.rawTransaction],
'minTimestamp': minTime,
'maxTimestamp': maxTime,
'revertingTxHashes': [signedTx2.transactionHash],
};
const bundleHash = await directClient.eth.sendBundle(bundleArgs);
console.log(bundleHash);
const queryBundle = await directClient.eth.getBundleByHash(bundleHash);
console.log(queryBundle);
var found = false;
var i;
function sleep (time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
for(i = 0; i < 30; i++) {
const r1 = await rpcClient.eth.getTransactionReceipt(signedTx1.transactionHash);
const r2 = await rpcClient.eth.getTransactionReceipt(signedTx2.transactionHash);
if (r1 != null && r2 != null) {
found = true
break
}
await sleep(3000)
}
if (found) {
console.log("bundle verified on chain");
} else {
console.log("bundle failed to be verified on chain or timeout");
}
};
sendBUSDByBundleDemo();
const sendBNBByBundleWithDepositCoinbaseDemo = async () => {
const directClient = new Web3(directRouteEndPoint);
const rpcClient = new Web3(rpcEndPoint);
var contractABI = "[{\"inputs\":[],\"name\":\"deposit\",\"outputs\":[],\"stateMutability\":\"payable\",\"type\":\"function\"}]"
contractABI = JSON.parse(contractABI);
const contractAddr = "0xB3BB00B9785f35D0BE13B2BD91C8e3742D9Ab03a";
const contract = new rpcClient.eth.Contract(contractABI, contractAddr);
const account1 = 'input the address of account1';
const account2 = 'input the address of account2';
var nonce = await rpcClient.eth.getTransactionCount(account1, 'pending')
const deposit = 1e10;
const amount = 1e15;
const data1 = contract.methods.deposit().encodeABI();
const bundlePrice = await directClient.eth.getBundlePrice();
var price = bundlePrice.minimalGasPrice;
if (price < bundlePrice.bundlePrice) {
price = bundlePrice.bundlePrice;
}
const tx1 = {
'from': account1,
'to': contractAddr,
'gas': 70000,
'nonce': nonce,
'gasPrice': price,
'value': deposit,
'data': rpcClient.utils.toHex(data1),
'chainId': 56,
};
const tx2 = {
'from': account1,
'to': account2,
'gas': 70000,
'nonce': nonce+1,
'gasPrice': price,
'value': amount,
'chainId': 56,
};
const privateKey1 = 'input your private of account1';
const signedTx1 = await rpcClient.eth.accounts.signTransaction(tx1, privateKey1);
const signedTx2 = await rpcClient.eth.accounts.signTransaction(tx2, privateKey1);
var myDate = new Date();
const maxTime = Math.floor(myDate.getTime() / 1000) + 80;
const minTime = Math.floor(myDate.getTime() / 1000) + 20;
console.log(maxTime, minTime);
const bundleArgs = {
'txs': [signedTx1.rawTransaction, signedTx2.rawTransaction],
'minTimestamp': minTime,
'maxTimestamp': maxTime,
'revertingTxHashes': [signedTx2.transactionHash],
};
const bundleHash = await directClient.eth.sendBundle(bundleArgs);
console.log(bundleHash);
const queryBundle = await directClient.eth.getBundleByHash(bundleHash);
console.log(queryBundle);
var found = false;
var i;
function sleep (time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
for(i = 0; i < 30; i++) {
const r1 = await rpcClient.eth.getTransactionReceipt(signedTx1.transactionHash);
const r2 = await rpcClient.eth.getTransactionReceipt(signedTx2.transactionHash);
if (r1 != null && r2 != null) {
found = true
break
}
await sleep(3000)
}
if (found) {
console.log("bundle verified on chain");
} else {
console.log("bundle failed to be verified on chain or timeout");
}
};
sendBNBByBundleWithDepositCoinbaseDemo();