首先准备ganache工具,可以快速搭建一条本地链。
在vscode终端安装yarn add 添加ethers,fs-sxtra,solc,用solc命令编译后下面文件生成abi和bin文件
// I'm a comment! // SPDX-License-Identifier: MIT pragma solidity >=0.8.0 <0.9.0; // pragma solidity ^0.8.0; // pragma solidity >=0.8.0 <0.9.0;\ contract SimpleStorage { uint256 favoriteNumber; struct People { uint256 favoriteNumber; string name; } // uint256[] public anArray; People[] public people; mapping(string => uint256) public nameToFavoriteNumber; function store(uint256 _favoriteNumber) public { favoriteNumber = _favoriteNumber; } function retrieve() public view returns (uint256) { return favoriteNumber; } function addPerson(string memory _name, uint256 _favoriteNumber) public { people.push(People(_favoriteNumber, _name)); nameToFavoriteNumber[_name] = _favoriteNumber; } }
const ethers = require("ethers"); const fs = require("fs-extra"); //how to send a transaction //首先签名,,然后与提供者一起发送,在发送交易之前会先签署交易 async function main() { console.log("hi"); let variable = 5; console.log(variable); //deploy a contract? Wait for it to be deployed //contract.deploy -> would wait for it to finish // http://0.0.0.0:8545 const provider = new ethers.providers.JsonRpcBatchProvider( "HTTP://127.0.0.1:7545" ); const wallet = new ethers.Wallet( "5c44a7ac1026215f1833fcbd95abc9bff61d34b1b1460029814a031558378cf0", provider ); const abi = fs.readFileSync( "./abi/ethers-simple-storage_SimpleStorage_sol_SimpleStorage.abi", "utf-8" ); const binary = fs.readFileSync( "./abi/ethers-simple-storage_SimpleStorage_sol_SimpleStorage.bin", "utf-8" ); //factory contract just you can use object,deploying contract. const contractFactory = new ethers.ContractFactory(abi, binary, wallet); console.log("Deploying, please wait..."); const contract = await contractFactory.deploy( //{gasLimit:100000000 } ); //Stop here,Wait for contract to deep.Await mean wait return object //factory deploy just return promise //console.log(contract); const transactionReceipt = await contract.deployTransaction.wait(1); /*console.log("Here is the deployment transaction:"); console.log(transactionReceipt); conslog.log("contract is the transaction receipt:"); console.log(deploymentReceipt);*/ console.log("Let's deploy with only transaction data!"); const nonce = await wallet.getTransactionCount(); const tx = { nonce: nonce, gasPrice: 2000000000, gasLimit: 1000000, to: null, value: 0, data: "0x608060405234801561001057600080fd5b5061057f806100206000396000f3fe608060405234801561001057600080fd5b50600436106100575760003560e01c80632e64cec11461005c5780636057361d146100735780636f760f41146100885780638bab8dd51461009b5780639e7a13ad146100c6575b600080fd5b6000545b6040519081526020015b60405180910390f35b610086610081366004610248565b600055565b005b610086610096366004610304565b6100e7565b6100606100a9366004610349565b805160208183018101805160028252928201919093012091525481565b6100d96100d4366004610248565b610190565b60405161006a9291906103aa565b6040805180820190915281815260208101838152600180548082018255600091909152825160029091027fb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf68101918255915190917fb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf70190610168908261046d565b5050508060028360405161017c919061052d565b908152604051908190036020019020555050565b600181815481106101a057600080fd5b600091825260209091206002909102018054600182018054919350906101c5906103e4565b80601f01602080910402602001604051908101604052809291908181526020018280546101f1906103e4565b801561023e5780601f106102135761010080835404028352916020019161023e565b820191906000526020600020905b81548152906001019060200180831161022157829003601f168201915b5050505050905082565b60006020828403121561025a57600080fd5b5035919050565b634e487b7160e01b600052604160045260246000fd5b600082601f83011261028857600080fd5b813567ffffffffffffffff808211156102a3576102a3610261565b604051601f8301601f19908116603f011681019082821181831017156102cb576102cb610261565b816040528381528660208588010111156102e457600080fd5b836020870160208301376000602085830101528094505050505092915050565b6000806040838503121561031757600080fd5b823567ffffffffffffffff81111561032e57600080fd5b61033a85828601610277565b95602094909401359450505050565b60006020828403121561035b57600080fd5b813567ffffffffffffffff81111561037257600080fd5b61037e84828501610277565b949350505050565b60005b838110156103a1578181015183820152602001610389565b50506000910152565b82815260406020820152600082518060408401526103cf816060850160208701610386565b601f01601f1916919091016060019392505050565b600181811c908216806103f857607f821691505b60208210810361041857634e487b7160e01b600052602260045260246000fd5b50919050565b601f82111561046857600081815260208120601f850160051c810160208610156104455750805b601f850160051c820191505b8181101561046457828155600101610451565b5050505b505050565b815167ffffffffffffffff81111561048757610487610261565b61049b8161049584546103e4565b8461041e565b602080601f8311600181146104d057600084156104b85750858301515b600019600386901b1c1916600185901b178555610464565b600085815260208120601f198616915b828110156104ff578886015182559484019460019091019084016104e0565b508582101561051d5787850151600019600388901b60f8161c191681555b5050505050600190811b01905550565b6000825161053f818460208701610386565b919091019291505056fea26469706673582212202d850b1b08f3a17fe0175402c0846c255608f1e797c6d59b47f42145d88cf54364736f6c63430008100033", chainId: 5777, }; const sendTxResponse = await wallet.sendTransaction(tx);//send response await sendTxResponse.wait(1);//confirm sendtransaction ok //console.log(signedTxResponse); const } main() .then(() => process.exit(0)) .catch((error) => { console.log(error); }); main() .then(() => process.exit(0)) .catch((error) => { console.log(error); process.exit(1); });
运行后会获得类似打印,再去ganche工具中插件查看blocks
> node deploy.js hi 5 Deploying, please wait... hi 5 Deploying, please wait... Error: the tx doesn't have the correct nonce. account has nonce of: 6 tx has nonce of: 5 at D:\SolidityObject\hh-fcc\node_modules\@ethersproject\providers\lib\json-rpc-batch-provider.js:72:41 at Array.forEach (<anonymous>) at D:\SolidityObject\hh-fcc\node_modules\@ethersproject\providers\lib\json-rpc-batch-provider.js:69:27 at processTicksAndRejections (node:internal/process/task_queues:96:5) { code: -32000, data: { stack: "TXRejectedError: the tx doesn't have the correct nonce. account has nonce of: 6 tx has nonce of: 5\n" + ' at validateNonce (C:\\Program Files\\WindowsApps\\GanacheUI_2.5.4.0_x64__5dg5pnz03psnj\\app\\resources\\static\\node\\node_modules\\ganache-core\\lib\\statemanager.js:1017:11)\n' + ' at C:\\Program Files\\WindowsApps\\GanacheUI_2.5.4.0_x64__5dg5pnz03psnj\\app\\resources\\static\\node\\node_modules\\ganache-core\\lib\\statemanager.js:1026:7\n' + ' at C:\\Program Files\\WindowsApps\\GanacheUI_2.5.4.0_x64__5dg5pnz03psnj\\app\\resources\\static\\node\\node_modules\\ganache-core\\lib\\blockchain_double.js:444:5\n' + ' at C:\\Program Files\\WindowsApps\\GanacheUI_2.5.4.0_x64__5dg5pnz03psnj\\app\\resources\\static\\node\\node_modules\\ganache-core\\node_modules\\merkle-patricia-tree\\baseTrie.js:97:9\n' + ' at C:\\Program Files\\WindowsApps\\GanacheUI_2.5.4.0_x64__5dg5pnz03psnj\\app\\resources\\static\\node\\node_modules\\ganache-core\\node_modules\\merkle-patricia-tree\\baseTrie.js:520:18\n' + ' at Object._return [as return] (C:\\Program Files\\WindowsApps\\GanacheUI_2.5.4.0_x64__5dg5pnz03psnj\\app\\resources\\static\\node\\node_modules\\ganache-core\\node_modules\\merkle-patricia-tree\\baseTrie.js:545:13)\n' + ' at processNode (C:\\Program Files\\WindowsApps\\GanacheUI_2.5.4.0_x64__5dg5pnz03psnj\\app\\resources\\static\\node\\node_modules\\ganache-core\\node_modules\\merkle-patricia-tree\\baseTrie.js:325:34)\n' + ' at processNode (C:\\Program Files\\WindowsApps\\GanacheUI_2.5.4.0_x64__5dg5pnz03psnj\\app\\resources\\static\\node\\node_modules\\ganache-core\\node_modules\\merkle-patricia-tree\\baseTrie.js:579:9)\n' + ' at C:\\Program Files\\WindowsApps\\GanacheUI_2.5.4.0_x64__5dg5pnz03psnj\\app\\resources\\static\\node\\node_modules\\ganache-core\\node_modules\\merkle-patricia-tree\\baseTrie.js:574:17\n' + ' at C:\\Program Files\\WindowsApps\\GanacheUI_2.5.4.0_x64__5dg5pnz03psnj\\app\\resources\\static\\node\\node_modules\\ganache-core\\node_modules\\merkle-patricia-tree\\baseTrie.js:212:11\n' + ' at processTicksAndRejections (internal/process/task_queues.js:75:11)', name: 'TXRejectedError' }, transaction: { nonce: 5, gasPrice: BigNumber { _hex: '0x04a817c800', _isBigNumber: true }, gasLimit: BigNumber { _hex: '0x057292', _isBigNumber: true }, to: null, value: BigNumber { _hex: '0x00', _isBigNumber: true }, data: '0x608060405234801561001057600080fd5b5061057f806100206000396000f3fe608060405234801561001057600080fd5b50600436106100575760003560e01c80632e64cec11461005c5780636057361d146100735780636f760f41146100885780638bab8dd51461009b5780639e7a13ad146100c6575b600080fd5b6000545b6040519081526020015b60405180910390f35b610086610081366004610248565b600055565b005b610086610096366004610304565b6100e7565b6100606100a9366004610349565b805160208183018101805160028252928201919093012091525481565b6100d96100d4366004610248565b610190565b60405161006a9291906103aa565b6040805180820190915281815260208101838152600180548082018255600091909152825160029091027fb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf68101918255915190917fb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf70190610168908261046d565b5050508060028360405161017c919061052d565b908152604051908190036020019020555050565b600181815481106101a057600080fd5b600091825260209091206002909102018054600182018054919350906101c5906103e4565b80601f01602080910402602001604051908101604052809291908181526020018280546101f1906103e4565b801561023e5780601f106102135761010080835404028352916020019161023e565b820191906000526020600020905b81548152906001019060200180831161022157829003601f168201915b5050505050905082565b60006020828403121561025a57600080fd5b5035919050565b634e487b7160e01b600052604160045260246000fd5b600082601f83011261028857600080fd5b813567ffffffffffffffff808211156102a3576102a3610261565b604051601f8301601f19908116603f011681019082821181831017156102cb576102cb610261565b816040528381528660208588010111156102e457600080fd5b836020870160208301376000602085830101528094505050505092915050565b6000806040838503121561031757600080fd5b823567ffffffffffffffff81111561032e57600080fd5b61033a85828601610277565b95602094909401359450505050565b60006020828403121561035b57600080fd5b813567ffffffffffffffff81111561037257600080fd5b61037e84828501610277565b949350505050565b60005b838110156103a1578181015183820152602001610389565b50506000910152565b82815260406020820152600082518060408401526103cf816060850160208701610386565b601f01601f1916919091016060019392505050565b600181811c908216806103f857607f821691505b60208210810361041857634e487b7160e01b600052602260045260246000fd5b50919050565b601f82111561046857600081815260208120601f850160051c810160208610156104455750805b601f850160051c820191505b8181101561046457828155600101610451565b5050505b505050565b815167ffffffffffffffff81111561048757610487610261565b61049b8161049584546103e4565b8461041e565b602080601f8311600181146104d057600084156104b85750858301515b600019600386901b1c1916600185901b178555610464565b600085815260208120601f198616915b828110156104ff578886015182559484019460019091019084016104e0565b508582101561051d5787850151600019600388901b60f8161c191681555b5050505050600190811b01905550565b6000825161053f818460208701610386565b919091019291505056fea26469706673582212202d850b1b08f3a17fe0175402c0846c255608f1e797c6d59b47f42145d88cf54364736f6c63430008100033', chainId: 1337, v: 2709, r: '0xac06cb91a0a7e9cc94033795de71560507adcafc7f5387b1c7ef59b808e6da80', s: '0x4f4a3d4e2c8896353414dc48a669704cf8e681c000cb261072e3505cb93dcec2', from: '0x1c41eE85180F4307011FC6f0c87bd5aB7d286D2C', hash: '0xbf6a1142ebdcab11affe40dafe3b0d9bc82bd7e318bf554a6df5f3396c88dab7', type: null, confirmations: 0 }, transactionHash: '0xbf6a1142ebdcab11affe40dafe3b0d9bc82bd7e318bf554a6df5f3396c88dab7' }
标签:node,const,log,modules,js,发送,一笔,console From: https://www.cnblogs.com/zaleswift/p/16590210.html