我在测试授权人的弱点。我创造了两个契约来实现这个目标。
WalletLibrary.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract MyWalletLibrary {
uint private value = 0;
function setValue(uint a) public {
value = a;
}
function getValue() public view returns (uint) {
return value;
}
}和MyWallet.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract MyWallet {
uint private value = 0;
address private walletLibrary;
constructor(address a) {
walletLibrary = a;
}
fallback () external payable {
(bool success, ) = walletLibrary.delegatecall(msg.data);
if (! success) revert();
}
}现在,我想通过MyWalletLibrary.setValue中的回退函数调用MyWallet。所以我创建了一个块菌单元测试文件:
let MyWallet = artifacts.require("MyWallet")
it('delegate-test', async () => {
let accounts = await web3.eth.getAccounts()
let MyWalletDeployed = await MyWallet.deployed()
web3.eth.sendTransaction({
from: accounts[0],
to: MyWalletDeployed.address,
data: '123'
});
})根据文档,我应该使用sendTransaction(),但是这个sendTransaction()没有指定要调用的方法,也就是"setValue“。
我该怎么做?
发布于 2022-01-20 06:47:05
您的数据应该是函数签名和传递给它的参数。
这就是您如何在坚实的情况下这样做的:walletLibrary.delegatecall(abi.encodePacked("setValue(uint256)", "123"))
对于web3,您将希望使用此文档。
const myData = web3.eth.abi.encodeFunctionCall({
name: 'setValue',
type: 'function',
inputs: [{
type: 'uint256',
name: 'a'
}]
}, ['123']);
web3.eth.sendTransaction({
from: accounts[0],
to: MyWalletDeployed.address,
data: myData
});https://ethereum.stackexchange.com/questions/119413
复制相似问题