首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用sendTransaction调用回退方法?

如何使用sendTransaction调用回退方法?
EN

Ethereum用户
提问于 2022-01-20 06:22:55
回答 1查看 281关注 0票数 1

我在测试授权人的弱点。我创造了两个契约来实现这个目标。

WalletLibrary.sol

代码语言:javascript
复制
// 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

代码语言:javascript
复制
// 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。所以我创建了一个块菌单元测试文件:

代码语言:javascript
复制
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“。

我该怎么做?

EN

回答 1

Ethereum用户

回答已采纳

发布于 2022-01-20 06:47:05

您的数据应该是函数签名和传递给它的参数。

这就是您如何在坚实的情况下这样做的:walletLibrary.delegatecall(abi.encodePacked("setValue(uint256)", "123"))

对于web3,您将希望使用此文档

代码语言:javascript
复制
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
    });
票数 2
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/119413

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档