我正在尝试为我正在编写的智能合约编写一些单元测试,但在弄清楚如何在创建时将参数传递给合约的构造函数时遇到困难。下面是我的代码:
const assert = require('assert');
const ganache = require('ganache-cli');
const provider = ganache.provider();
const Web3 = require('web3');
const web3 = new Web3(provider);
const { interface, bytecode } = require('../ethereum/compile');
let token;
let accounts;
beforeEach(async () => {
accounts = await web3.eth.getAccounts();
token = await new web3.eth.Contract(JSON.parse(interface))
.deploy({ data: bytecode })
.send({ from: accounts[0], gas: '1000000' });
token.setProvider(provider);
});
describe('Token Contract', () => { ... });
基于此设置,我如何将参数传递给合约?我通过学习 Solidity 类(class)走到了这一步,但是类(class)中的合约没有对其构造函数有任何选项,因此它从未涵盖它们应该去哪里。感谢您的帮助!
最佳答案
好吧,我自己想出来了:向您传递到 deploy
函数的对象添加一个属性作为参数数组。
.deploy({ data: bytecode, arguments: [ ... ] })
关于node.js - Web3 + 坚固性 : Passing in arguments to a contract's constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49696475/