|
| 1 | +## 前言 |
| 2 | + |
| 3 | +通过本样例代码,开发者了解到 `web3.js`和 `ehters.js`的区别 |
| 4 | + |
| 5 | +项目里主要针对 `连接到etherum `,`获取账户`,`部署合约`,`调用合约方法`这些业务做了示例演示。 |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +## 测试流程 |
| 10 | + |
| 11 | +1. 安装依赖 |
| 12 | + |
| 13 | +``` |
| 14 | +yarn install |
| 15 | +``` |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +2. 执行 compile.js 脚本 |
| 20 | + |
| 21 | +``` |
| 22 | +yarn compile |
| 23 | +``` |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +3. 启动本地测试网络 |
| 28 | + |
| 29 | +``` |
| 30 | +yarn chain |
| 31 | +``` |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +4. 测试脚本 |
| 36 | + |
| 37 | +``` |
| 38 | +yarn test |
| 39 | +``` |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +## 示例说明 |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +### 1、connect 到 etherum |
| 48 | + |
| 49 | +```js |
| 50 | +// web3.js |
| 51 | + |
| 52 | +const web3 = new Web3('http://localhost:8545'); |
| 53 | +// or |
| 54 | +const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545')); |
| 55 | + |
| 56 | +// ethers |
| 57 | +const provider = new ethers.providers.JsonRpcProvider('http://localhost:8545'); |
| 58 | +const signer = provider.getSigner(); |
| 59 | +``` |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +### 2、获取账户 |
| 64 | + |
| 65 | +```js |
| 66 | +// ethers |
| 67 | +const provider = new ethers.providers.JsonRpcProvider('http://localhost:8545'); |
| 68 | +const accounts1 = await provider.listAccounts(); |
| 69 | + |
| 70 | +// web3.js |
| 71 | +const web3 = new Web3('http://127.0.0.1:8545'); |
| 72 | +const accounts2 = await web3.eth.getAccounts(); |
| 73 | +``` |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +### 3、发布合约 |
| 78 | + |
| 79 | +```js |
| 80 | +// using ethers |
| 81 | +// ethers部署合约需要abi,bytecode, signer。 |
| 82 | +const factory = new ethers.ContractFactory(abi, bytecode, signer); |
| 83 | +const contractInstance = await factory.deploy(0); |
| 84 | +const tx = await contractInstance.deployTransaction.wait(); |
| 85 | +console.log(tx); |
| 86 | +console.log('Contract deployed at address:', contractInstance.address); |
| 87 | + |
| 88 | +// using web3 |
| 89 | +const accounts = await web3.eth.getAccounts(); |
| 90 | +const contract = new web3.eth.Contract(abi); |
| 91 | +const tx = contract.deploy({ |
| 92 | + data: bytecode, |
| 93 | + arguments: [0], |
| 94 | +}); |
| 95 | +// const deployReceipt = tx.send({ |
| 96 | +// from: accounts[0], |
| 97 | +// gas: gasLimit, |
| 98 | +// gasPrice, |
| 99 | +// }); |
| 100 | +// deployReceipt.on('receipt', function (receipt) { |
| 101 | +// console.log(`Contract deployed at address: ${receipt.contractAddress}`); |
| 102 | +// }); |
| 103 | +const contractInstance = await tx.send({ |
| 104 | + from: accounts[0], |
| 105 | + gas: gasLimit, |
| 106 | + gasPrice, |
| 107 | +}); |
| 108 | +console.log('Contract deployed at address:', contractInstance.options.address); |
| 109 | +``` |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +### 4、调用合约方法: |
| 114 | + |
| 115 | +#### 调用非交易类型的方法: |
| 116 | + |
| 117 | +```js |
| 118 | +// Web3.js |
| 119 | +const contractInstance = new web3.eth.Contract(abi, contractAddress); |
| 120 | +let currentValue = await contractInstance.methods.currentValue().call(); |
| 121 | +console.log('Incrementer Contract currentValue:', currentValue); |
| 122 | + |
| 123 | +// ethers |
| 124 | +const readContract = new ethers.Contract(contractAddress, abi, provider); |
| 125 | +let currentValue = await readContract.currentValue(); |
| 126 | +console.log('Incrementer Contract currentValue:', currentValue.toString()); |
| 127 | +``` |
| 128 | + |
| 129 | +#### 调用交易类型的方法: |
| 130 | + |
| 131 | +```js |
| 132 | +// Web3.js |
| 133 | +const accounts = await web3.eth.getAccounts(); |
| 134 | +const contractInstance = new web3.eth.Contract(abi, contractAddress); |
| 135 | +const tx = contractInstance.methods.descrement(1); |
| 136 | +await tx |
| 137 | + .send({ |
| 138 | + from: accounts[0], |
| 139 | + gas: gasLimit, |
| 140 | + gasPrice, |
| 141 | + }) |
| 142 | + .on('receipt', async (recepit) => { |
| 143 | + currentValue = await contractInstance.methods.currentValue().call(); |
| 144 | + console.log('Incrementer Contract currentValue:', currentValue); |
| 145 | + }); |
| 146 | + |
| 147 | +// ethers |
| 148 | +const writeContract = new ethers.Contract(contractAddress, abi, signer); |
| 149 | +const tx = await writeContract.increment(ethers.BigNumber.from(5)); |
| 150 | +await tx.wait(); |
| 151 | +currentValue = await readContract.currentValue(); |
| 152 | +console.log('Incrementer Contract currentValue:', currentValue.toString()); |
| 153 | +``` |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | +#### 查询event事件 |
| 158 | + |
| 159 | +```js |
| 160 | +// etherjs写法 |
| 161 | +const readContract = new ethers.Contract(contractAddress, abi, provider); |
| 162 | +// filters里传入的参数只能是indexed的参数 |
| 163 | +const filter = readContract.filters.Bid(null, utils.hexlify(BigNumber.from(auction.recordId))); |
| 164 | +// 过滤区块 |
| 165 | +const logsFrom = await readContract.queryFilter(filter, 0, "latest"); |
| 166 | +logsFrom.forEach(item => console.log(items.args)); |
| 167 | + |
| 168 | +// web3的写法 |
| 169 | +const web3 = new Web3(provider); |
| 170 | +const contractInstance = new web3.eth.Contract(abi, contractAddress); |
| 171 | +const logs = await contractInstance.getPastEvents('Descrement', { |
| 172 | + filter: {}, |
| 173 | + fromBlock: 0, |
| 174 | +}); |
| 175 | + |
| 176 | +logs.forEach((item) => { |
| 177 | + console.log('Descrement Event:', item); // same results as the optional callback above |
| 178 | +}); |
| 179 | +``` |
| 180 | + |
| 181 | + |
| 182 | + |
| 183 | +#### 订阅event事件 |
| 184 | + |
| 185 | +```js |
| 186 | + |
| 187 | +const provider = new ethers.providers.JsonRpcProvider('http://localhost:8545'); |
| 188 | +// http provider不支持订阅 |
| 189 | +const web3 = new Web3(new Web3.providers.WebsocketProvider('ws://localhost:8545')); |
| 190 | + |
| 191 | + // using ethers |
| 192 | + async function subByEthers() { |
| 193 | + const readContract = new ethers.Contract(contractAddress, abi, provider); |
| 194 | + let filterForm = readContract.filters.Increment(); |
| 195 | + readContract.on(filterForm, (amount, event) => { |
| 196 | + console.log('Increment events:', event); |
| 197 | + }); |
| 198 | + } |
| 199 | + subByEthers(); |
| 200 | + |
| 201 | + |
| 202 | + // using web3 |
| 203 | + async function subByWeb3() { |
| 204 | + const contractInstance = new web3.eth.Contract(abi, contractAddress); |
| 205 | + contractInstance.events |
| 206 | + .Descrement({ |
| 207 | + fromBlock: 0, |
| 208 | + }) |
| 209 | + .on('data', (event) => { |
| 210 | + console.log('Descrement Event:', event); // same results as the optional callback above |
| 211 | + }) |
| 212 | + .on('error', function (error, receipt) { |
| 213 | + console.error('Descrement Event error:', error); |
| 214 | + }); |
| 215 | + } |
| 216 | + subByWeb3(); |
| 217 | +} |
| 218 | + |
| 219 | +const server = http.createServer(() => {}); |
| 220 | +server.listen(8002); |
| 221 | + |
| 222 | +``` |
| 223 | + |
| 224 | + |
| 225 | + |
| 226 | + |
| 227 | + |
| 228 | +## 参考资料 |
| 229 | + |
| 230 | +[1 - web3-vs-ethers](https://github.com/adrianmcli/web3-vs-ethers) |
| 231 | + |
| 232 | +[2 - web3js文档](https://web3js.readthedocs.io/en/v1.2.11/index.html) |
| 233 | + |
| 234 | +[3 - ethers 文档](https://docs.ethers.io/v5/) |
| 235 | + |
0 commit comments