只有所有者才能提取资金



我需要在solidity中编写一个测试来测试withdrawFunds函数。此函数有一个isOwner修饰符,用于测试发件人的地址是否与作为构造函数一部分存储为所有者的地址相同。问题是,当我试图调用withdrawFunds函数时,该函数失败并表示我不是所有者。有什么方法可以测试这个功能吗?代理合同对这里有帮助吗?任何见解都会很有帮助。

PS:我添加了一个事件来记录作为构造函数(msg.sender(的一部分存储为所有者的地址,并记录函数作为msg.sender接收的地址。这两者都不同,我不知道如何作为所有者调用此函数。

在solidity中,您应该定义一个函数来返回合同所有者:

// assuming you already set the owner in constructor
function getContractOwner() public view returns (address)
{
return owner;
}

然后在测试文件中

// accounts is passed by truffle. this testing in truffle environment
contract("YourContract", (accounts) => {
let _contract = null;
let buyer = null;
// set the contract
before(async () => {
_contract = await YourContractName.deployed();
buyer = accounts[1];
});
describe("Normal withdraw", () => {
let currentOwner = null;
before(async () => {
currentOwner = await _contract.getContractOwner();
});
// now you have got the currentOwner
it("should fail when withdrawing by different than owner address", async () => {
const value = "10000000000000000";
_contract.withdraw(value, { from: buyer });
// add the logic for assertion
});
}
}

相关内容

  • 没有找到相关文章

最新更新