node.js - Web3 + 坚固性 : Passing in arguments to a contract's constructor

标签 node.js solidity web3js

我正在尝试为我正在编写的智能合约编写一些单元测试,但在弄清楚如何在创建时将参数传递给合约的构造函数时遇到困难。下面是我的代码:

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/

相关文章:

javascript - ES6动态导入功能如何设置当前目录

ethereum - 在solidity中调用 'external view'函数是否会向网络广播 'transaction'?

javascript - 通过Web3访问智能合约成员功能

javascript - CryptoCurrency NODE.js Web3 Ethereum - 无法连接到 TESTRPC

Node.js 使用用户名和密码从 URL 下载文件?

javascript - 如何分析我的 Node 应用程序以查看我在哪里用完了内存和 CPU 资源?

javascript - 如何使用 koa2 运行简单的应用程序?

go - 将参数作为 bytes32 传递给 Solidity 智能合约

javascript - 为什么我们不能从运行在智能合约上的 sendTransaction() 获得返回值?

javascript - 选择 html 元素文本作为 JavaScript 的输入