1、关于开发环境搭建配置等可参考之前的文章
2、部署合约代码erc20.js
const hre = require("hardhat");
async function main() {
const CONTRACT = await hre.ethers.getContractFactory("ERC20");
const contract = await CONTRACT.deploy();
await contract.init("ERC20Name","ERC20Symbol");
console.log("name:",contract.name(),"symbol:",contract.symbol());
await contract.deployed();
console.log(`contract deployed to ${contract.address}`);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
3、启动并上链
#切换到智能合约项目位置
npx hardhat node
#新开一个窗口,确保localhost已经在hardhat.config.js中配置了,可查看第一步链接对照
npx hardhat run scripts/erc.js --network localhost
4、新建一个文件夹,存放go项目,完成mod初始化等
完整go项目文件目录
5、拷贝智能合约compile产生的ABI,在新文件夹中新建一个erc20.json文件
6、安装abigen
go get github.com/ethereum/go-ethereum
#切换路径 cd $GOPATH/pkg/mod/github.com/ethereum/go-ethereum@v1