Remix编辑器:https://remix.ethereum.org/
1.DEPLOY & RUN TRANSACTIONS SOLIDITY COMPILER 激活active (图标插头里的)
2.Enable optimization 勾上 (第二个) 保存即自动编译
3.竖框 第三个 功能 查看相应信息
合约编写 *.sol,参考资料:https://github.com/OpenZeppelin/openzeppelin-contracts
web3.BatchRequest,批量调用
let batch = new web3.BatchRequest();
for (let i = 0; i < tokenList.length; i++) {
batch.add(contract.methods.profits(user, tokenList[i]).call.request({
from: user }, (err: any, res: any) => {
console.log("=====", i, res);
}))
}
batch.execute();
console.log("=====finish");
1.装multicall ethers
npm install --save @indexed-finance/multicall ethers
import {
MultiCall } from '@indexed-finance/multicall'
import {
ethers } from 'ethers'
const multi = new MultiCall(new ethers.providers.Web3Provider(window['ethereum']));//metamask的provide没有call方法
const inputs = [];
for (let i = 0; i < tokenList.length; i++) {
inputs.push({
target: ContractAddress[chainID].Work, function: 'profits', args: [user, tokenList[i]] });
}
const tokenDatas = await multi.multiCall(WORK, inputs);
console.log("multi call ", tokenDatas)
ethers.js
node:
const provider = new ethers.providers.Web3Provider(web3.currentProvider);
const signer = provider.getSigner();
metamask:
//--1--
await window.ethereum.enable()
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
//--2--
let provider;
window.ethereum.enable().then(provider = new ethers.providers.Web3Provider(window.ethereum));
const signer = provider.getSigner();
//--3--
const provider = new ethers.providers.Web3Provider(window.ethereum, "any");
// Prompt user for account connections
await provider.send("eth_requestAccounts", []);
const signer = provider.getSigner();
console.log("Account:", await signer.getAddress());
const balance = await provider.getBalance(address);//eth数量
contract 交互
//1) 只读方式
contract = new ethers.Contract(contAddress,contAbi,provider)
//2) 读写方式
contract = new ethers.Contract(contAddress,contAbi,signer)
let balance = (await Contract.callStatic.balanceOf(userInfo.account)).toString(); //eg