This repository was archived by the owner on Feb 26, 2024. It is now read-only.
Description
Environment:
[root@zhangxf55 unirouter1]# truffle --version
Truffle v5.11.4 (core: 5.11.4)
Ganache v7.9.1
Solidity - 0.8.21 (solc-js)
Node v18.17.1
Web3.js v1.10.0
Fork the mainnet with command:
ganache -h 127.0.0.1 -p 9000 --fork https://eth-mainnet.g.alchemy.com/v2/KW8************************ Zttj0 -D
The contract I deployed:
// SPDX-License-Identifier: MIT
pragma solidity >= 0.4.22 < 0.9.0 ;
contract Authorizor {
address internal admin;
mapping (address => bool ) internal users;
constructor () {
admin = msg .sender ;
}
modifier onlyadmin () {
require (msg .sender == admin, "UniverseRouter02: ONLY_ADMIN " );
_;
}
modifier onlyuser () {
require (users[msg .sender ], "ONLY_USER " );
_;
}
function getAdmin () external view returns (address result ) {
result = admin;
}
function setAdmin (address _admin ) onlyadmin external {
require (_admin != admin, "ADMIN_NOT_CHANGE " );
admin = _admin;
}
function addUser (address _user ) onlyadmin external {
require (! users[_user], "USER_EXIST " );
users[_user] = true ;
}
function deleteUser (address _user ) onlyadmin external {
require (users[_user], "USER_NOT_EXIST " );
users[_user] = false ;
}
function checkUser (address _user ) external view returns (bool result ) {
result = users[_user];
}
}
Use ethers.js(6.7.1) to call addUser and deleteUser in RPC (Not truffle test mode):
const { ethers } = require ( 'ethers' ) ;
const private = require ( '../privatekey.json' ) ;
const routerabi = require ( '../build/contracts/UniverseRouter02.json' ) . abi ;
const erc20abi = require ( '../build/contracts/IERC20.json' ) . abi ;
const routerAddress = '0x548De337842AF790d92dDf93F52B3E9A87E1Da0B' ;
const distributorAddress = '0xC9412b0791F10320e9Deb867F2a046D84E5Ae8C5' ;
const usdtAddress = '0xdac17f958d2ee523a2206206994597c13d831ec7' ;
const wethAddress = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2' ;
const pepeAddress = '0x6982508145454ce325ddbe47a25d4ec3d2311933' ;
const v3pool_wethusdt = '0x4e68ccd3e89f51c3074ca5072bbac773960dfa36' ;
const v3pool_pepeweth = '0x11950d141ecb863f01007add7d1a342041227b58' ;
const v2pool_wethusdt = '0x0d4a11d5eeaac28ec3f61d100daf4d40471f1852' ;
const v2pool_pepeweth = '0xa43fe16908251ee70ef74718545e4fe6c5ccec9f' ;
const provider = new ethers . JsonRpcProvider ( "http://127.0.0.1:9000/" ) ;
const wallet = new ethers . Wallet ( private . zhangxf55 , provider ) ;
const contract = new ethers . Contract ( routerAddress , routerabi , wallet ) ;
async function testUser ( ) {
const tx = await contract . addUser ( wallet . address ) ;
let result = await contract . checkUser ( wallet . address ) ;
console . log ( result ) ;
await contract . deleteUser ( wallet . address ) ;
result = await contract . checkUser ( wallet . address ) ;
console . log ( result ) ;
}
testUser ( ) ;
The result:
[root@zhangxf55 unirouter1]# node local/testweb3.js
true
/data/unirouter1/node_modules/ethers/lib.commonjs/utils/errors.js:125
error = new Error(message);
^
Error: could not coalesce error (error={ " code" : -32000, " message" : " the tx doesn't have the correct nonce. account has nonce of: 44 tx has nonce of: 43" , " stack" : " Error: the tx doesn't have the correct nonce. account has nonce of: 44 tx has nonce of: 43\n at TransactionPool.prepareTransaction (/root/.nvm/versions/node/v18.17.1/lib/node_modules/ganache/dist/node/1.js:2:188489)" }, payload={ " id" : 18, " jsonrpc" : " 2.0" , " method" : " eth_sendRawTransaction" , " params" : [ " 0x02f8908205392b843b9aca0084f8cde7b282730094548de337842af790d92ddf93f52b3e9a87e1da0b80a45c60f226000000000000000000000000d3dfa878b32204cb7de5297a3d785b4bd38f995ec080a0b567e71468c74b71693239a722d3c5215eac1d19643a80e8982fe6b71c654fcda072d6169dc2146fbafc961d9f1756078df9766434a1b456cf857034c6c43f11b0" ] }, code=UNKNOWN_ERROR, version=6.7.1)
at makeError (/data/unirouter1/node_modules/ethers/lib.commonjs/utils/errors.js:125:21)
at JsonRpcProvider.getRpcError (/data/unirouter1/node_modules/ethers/lib.commonjs/providers/provider-jsonrpc.js:653:41)
at /data/unirouter1/node_modules/ethers/lib.commonjs/providers/provider-jsonrpc.js:268:45
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
code: ' UNKNOWN_ERROR' ,
error: {
message: " the tx doesn't have the correct nonce. account has nonce of: 44 tx has nonce of: 43" ,
stack: " Error: the tx doesn't have the correct nonce. account has nonce of: 44 tx has nonce of: 43\n" +
' at TransactionPool.prepareTransaction (/root/.nvm/versions/node/v18.17.1/lib/node_modules/ganache/dist/node/1.js:2:188489)' ,
code: -32000
},
payload: {
method: ' eth_sendRawTransaction' ,
params: [
' 0x02f8908205392b843b9aca0084f8cde7b282730094548de337842af790d92ddf93f52b3e9a87e1da0b80a45c60f226000000000000000000000000d3dfa878b32204cb7de5297a3d785b4bd38f995ec080a0b567e71468c74b71693239a722d3c5215eac1d19643a80e8982fe6b71c654fcda072d6169dc2146fbafc961d9f1756078df9766434a1b456cf857034c6c43f11b0'
],
id: 18,
jsonrpc: ' 2.0'
}
}
Node.js v18.17.1 Reactions are currently unavailable
[root@zhangxf55 unirouter1]# truffle --version Truffle v5.11.4 (core: 5.11.4) Ganache v7.9.1 Solidity - 0.8.21 (solc-js) Node v18.17.1 Web3.js v1.10.0ganache -h 127.0.0.1 -p 9000 --fork https://eth-mainnet.g.alchemy.com/v2/KW8************************Zttj0 -D