我正在使用truffle develop测试我的智能合同。我曾尝试使用web3.eth.defaultAccount = web3.eth.accounts[1]在不同帐户之间切换,但没有成功。我该如何做到这一点?
发布于 2018-11-17 21:27:34
作为一种时尚的东西,我喜欢在前面宣布参与者:
contract('MyContract', function(accounts) {
var owner = accounts[0];
var seller = accounts[1];
var buyer = accounts[2];然后,在运行测试(或UI)时,明确说明谁在说话:
it("should do something", function() {
myContract.method(args, {from: owner}).then(...希望能帮上忙。
accounts数组是钱包中的帐户列表(而不是网络本身),因此测试客户端能够使用其中的任何一个进行签名。
尽管松露使用“干净的房间”方法进行测试,但您可以通过在每次测试之前部署一个新的新合同来避免对它的依赖,并避免可能出现的混乱。该方法忽略迁移,并在“如实”的基础上与网络配置进行竞争。它为您提供了控制部署过程的机会,包括在客户端测试级别部署合同的帐户。
var myContract; // this will be the contract instance
beforeEach("deploy new MyContract", function() {
return MyContract.new(args, { from: owner }) // make a new one and choose the deployer
.then(instance => myContract = instance);
});如果你把这些都放在一起,你就会得到这样的东西
MyContract = artifacts.require(...
contract('MyContract', function(accounts) {
var myContract;
var owner = accounts[0];
var seller = accounts[1];
var buyer = accounts[2];
beforeEach("deploy new MyContract", function() {
return MyContract.new(args, { from: owner }) // control who deployed it
.then(instance => myContract = instance);
});
it("should do something", function() {
myContract.method(args, {from: owner}).then(... // control who accesses it发布于 2018-11-20 06:27:03
您可以从“develop”命令行更改合同使用的默认帐户。
> MetaCoin.defaults({ from: '0x123413413412341234' })这将影响到对该合同执行的所有事务。
正如Rob所指出的,另一个选项是显式地覆盖每个事务中的from。
> MetaCoin.deployed().transfer(to, value,
{ from: '0x13412341234...' })可以在配置文件from中指定truffle-config.js。
这是比较复杂的,但是如果来自develop的东西不适合您的需求,它是很方便的。
您必须定义您自己的网络并运行您自己的ganache实例。
module.exports = {
networks: {
myCustomConfiguration: {
host: "127.0.0.1",
port: 8545,
network_id: "*",
from: '0x11111..111'
}
}
};现在,在另一个控制台中执行ganache,您可以启动truffle console --network myCustomConfiguration。
https://ethereum.stackexchange.com/questions/62555
复制相似问题