Skip to content

Instantly share code, notes, and snippets.

@w3kim
Created October 28, 2019 05:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save w3kim/41576456333985a1d10688dd24f7b32c to your computer and use it in GitHub Desktop.
Save w3kim/41576456333985a1d10688dd24f7b32c to your computer and use it in GitHub Desktop.
Send Multiple Transactions to a Contract with Modified Nonce
const Caver = require('caver-js');
const caver = new Caver('https://api.baobab.klaytn.net:8651/');
const acct = caver.klay.accounts.privateKeyToAccount('your_private_key')
caver.klay.accounts.wallet.add(acct)
// Refer to count.sol for Solidity code
const COUNT_BYTECODE = {
"linkReferences": {},
"object": "60806040526000805534801561001457600080fd5b5060e8806100236000396000f3fe6080604052348015600f57600080fd5b5060043610603c5760003560e01c806306661abd14604157806342cbb15c14605d578063d14e62b8146079575b600080fd5b604760a4565b6040518082815260200191505060405180910390f35b606360aa565b6040518082815260200191505060405180910390f35b60a260048036036020811015608d57600080fd5b810190808035906020019092919050505060b2565b005b60005481565b600043905090565b806000819055505056fea165627a7a7230582087453d981a85f80c5262508e1fe5abe94dc38b1167c49b6e3477b74293e9e7000029",
"opcodes": "PUSH1 0x80 PUSH1 0x40 MSTORE PUSH1 0x0 DUP1 SSTORE CALLVALUE DUP1 ISZERO PUSH2 0x14 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH1 0xE8 DUP1 PUSH2 0x23 PUSH1 0x0 CODECOPY PUSH1 0x0 RETURN INVALID PUSH1 0x80 PUSH1 0x40 MSTORE CALLVALUE DUP1 ISZERO PUSH1 0xF JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH1 0x4 CALLDATASIZE LT PUSH1 0x3C JUMPI PUSH1 0x0 CALLDATALOAD PUSH1 0xE0 SHR DUP1 PUSH4 0x6661ABD EQ PUSH1 0x41 JUMPI DUP1 PUSH4 0x42CBB15C EQ PUSH1 0x5D JUMPI DUP1 PUSH4 0xD14E62B8 EQ PUSH1 0x79 JUMPI JUMPDEST PUSH1 0x0 DUP1 REVERT JUMPDEST PUSH1 0x47 PUSH1 0xA4 JUMP JUMPDEST PUSH1 0x40 MLOAD DUP1 DUP3 DUP2 MSTORE PUSH1 0x20 ADD SWAP2 POP POP PUSH1 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 RETURN JUMPDEST PUSH1 0x63 PUSH1 0xAA JUMP JUMPDEST PUSH1 0x40 MLOAD DUP1 DUP3 DUP2 MSTORE PUSH1 0x20 ADD SWAP2 POP POP PUSH1 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 RETURN JUMPDEST PUSH1 0xA2 PUSH1 0x4 DUP1 CALLDATASIZE SUB PUSH1 0x20 DUP2 LT ISZERO PUSH1 0x8D JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST DUP2 ADD SWAP1 DUP1 DUP1 CALLDATALOAD SWAP1 PUSH1 0x20 ADD SWAP1 SWAP3 SWAP2 SWAP1 POP POP POP PUSH1 0xB2 JUMP JUMPDEST STOP JUMPDEST PUSH1 0x0 SLOAD DUP2 JUMP JUMPDEST PUSH1 0x0 NUMBER SWAP1 POP SWAP1 JUMP JUMPDEST DUP1 PUSH1 0x0 DUP2 SWAP1 SSTORE POP POP JUMP INVALID LOG1 PUSH6 0x627A7A723058 KECCAK256 DUP8 GASLIMIT RETURNDATASIZE SWAP9 BYTE DUP6 0xf8 0xc MSTORE PUSH3 0x508E1F 0xe5 0xab 0xe9 0x4d 0xc3 DUP12 GT PUSH8 0xC49B6E3477B74293 0xe9 0xe7 STOP STOP 0x29 ",
"sourceMap": "87:354:0:-;;;179:1;156:24;;87:354;8:9:-1;5:2;;;30:1;27;20:12;5:2;87:354:0;;;;;;;"
};
const COUNT_ABI = [
{
"constant": true,
"inputs": [],
"name": "count",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "getBlockNumber",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_count",
"type": "uint256"
}
],
"name": "setCount",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}
]
deploy(test);
async function deploy(runner) {
new caver.klay.Contract(COUNT_ABI)
.deploy({
data: COUNT_BYTECODE.object,
arguments: []
})
.send({
from: acct.address,
gas: 3000000,
value: 0
})
.on('error', console.log)
.on('transactionHash', function (hash) {
console.log(">>> tx_hash for deployment:", hash);
})
.on('receipt', function (receipt) {
console.log(">>> contract deployed at", receipt.contractAddress);
})
.then(runner);
}
async function test(countContract) {
console.log("mutating count 10 times in a row");
const account = await caver.klay.getAccount(acct.address);
const startingNonce = account.account.nonce;
console.log(startingNonce);
const since = new Date();
console.log(since);
for (var i = 0; i < 10; i++) {
const txSetCount = countContract.methods.setCount(i);
txSetCount.send({
nonce: startingNonce + i,
from: acct.address,
gas: 3000000
})
.on('transactionHash', function (hash) {
console.log(">>> tx_hash =", hash);
})
.on('receipt', function (receipt) {
console.log(">>> done, calling count ...");
countContract.methods.count().call({ from: acct.address }, function (err, result) {
if (err == null) {
console.log(">>> got", result);
} else {
console.log(err);
}
});
caver.klay.getAccount(acct.address).then((v) => {
console.log(v.account.nonce);
});
}).on('error', function (err) {
console.log(err);
});
}
const now = new Date();
console.log(now - since + ' ms');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment