参考自:
https://www.trufflesuite.com/boxes/polygon
https://www.qikegu.com/docs/5124
合约的编译和部署:
使用truffle中的polygon truffle unbox,通过命令truffle unbox polygon下载框架,若出现unbox failed!报错,需要在etc/hosts中手动添加github.com的IP;
安装好框架后需要touch .env,以存储
MNEMONIC="<Your Mnemonic>"
INFURA_PROJECT_ID="<Your Infura Project ID>"
编译时,输入指令npm run compile:polygon,polygon unbox分开管理polygon合约与ethereum合约,因此在调用指令时需指明调用的是何种合约(更多细节参考官方文档);
部署时,输入指令npm run migrate:polygon --network=polygon_testnet,网络选择参考truffle-config.polygon.js或官方文档;
对链上合约的调用:
通过web3.js调用,
//实现查询余额功能的示例
const Web3 = require('web3')
const rpcURL = 'https://rpc-mumbai.matic.today' //链的地址
const web3 = new Web3(rpcURL)
const address = '' //钱包地址
web3.eth.getBalance(address, (err, wei) => {
balance = web3.utils.fromWei(wei, 'ether');
console.log("balance: " + balance);
})
//调用call函数
const Web3 = require('web3')
const rpcURL = 'https://rpc-mumbai.matic.today'
const web3 = new Web3(rpcURL)
const abi = [] //合约abi
const address = '' //合约地址
const contract = new web3.eth.Contract(abi, address)
contract.methods.functionGetNumber().call((err, result) => { console.log(result) })