如何直接与已经部署的智能合约交互?

发布于 2025-01-17 15:09:45 字数 639 浏览 2 评论 0原文

我的任务是与已部署的智能合约中的函数进行交互。我知道合约地址和函数签名,并且我有一个使用接口的解决方案。但是,我正在与之交互的这个函数会向 msg.sender 发送一个 NFT,在本例中,这是与 interface 解决方案(而不是我的个人帐户)签订的合同。

//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.4;

interface ITargetContract {
    function addWhitelist(bytes32 _something, string memory _id)
        external;
}

contract MyContract {
    function addWhitelist(
        address _t,
        bytes32 _something,
        string memory _id
    ) public {
        ITargetContract(_t).addWhitelist(_something, _id);
    }
}

那么为了避免这个NFT被发送到我的合约中,如果我只知道合约地址和函数签名,我可以直接用我的个人账户与已部署的合约进行交互吗?

I am tasked with interacting with a function in a smart contract that has already been deployed. I know the contract address and the function signature and I have a solution that uses interfaces. However, this function I am interacting with then sends an NFT to msg.sender which in this case is the contract with the interface solution as opposed to my personal account.

//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.4;

interface ITargetContract {
    function addWhitelist(bytes32 _something, string memory _id)
        external;
}

contract MyContract {
    function addWhitelist(
        address _t,
        bytes32 _something,
        string memory _id
    ) public {
        ITargetContract(_t).addWhitelist(_something, _id);
    }
}

So to avoid this NFT being sent to my contract, can I interact with the deployed contract directly with my personal account if I only know the contract address and function signature?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赴月观长安 2025-01-24 15:09:45

msg.sender中继的唯一情况是delegatecall。但是,它还使用呼叫者合同的存储(而不是被调用的),因此主要用于代理合同。

既无法传递msg.sender>,也无法使用所谓的合同的存储。


假设目标合同实施 erc721 标准,您可以实现onerc> onercfore> onerc721recepeired( )函数并在收到最终用户后将令牌重新发送给该令牌。

contract MyContract {
    mapping (string => address) tokenUsers;

    function addWhitelist(
        address _t,
        bytes32 _something,
        string memory _id
    ) public {
        // store the end user address by the token ID
        tokenUsers[_id] = msg.sender;
        ITargetContract(_t).addCandidateIdToWhitelist(_something, _id);
    }

    function onERC721Received(address _operator, address _from, uint256 _tokenId, bytes _data) external returns(bytes4) {
        // resend the token from your contract to the user
        ITargetContract.safeTransferFrom(
            address(this),
            tokenUsers[_tokenId],
            // TODO: transfer functions accept the token ID as uint, not as string
            _stringToUint(_tokenId)
        );
    }
}

The only case when msg.sender is relayed is delegatecall. However, it also uses storage of the caller contract (and not of the called one), so it's mostly used in proxy contracts.

There's no way to both relay the msg.sender and use the storage of the called contract.


Assuming the target contract implements the ERC721 standard, you can implement the onERC721Received() function and resend the token to the end user after you've received it.

contract MyContract {
    mapping (string => address) tokenUsers;

    function addWhitelist(
        address _t,
        bytes32 _something,
        string memory _id
    ) public {
        // store the end user address by the token ID
        tokenUsers[_id] = msg.sender;
        ITargetContract(_t).addCandidateIdToWhitelist(_something, _id);
    }

    function onERC721Received(address _operator, address _from, uint256 _tokenId, bytes _data) external returns(bytes4) {
        // resend the token from your contract to the user
        ITargetContract.safeTransferFrom(
            address(this),
            tokenUsers[_tokenId],
            // TODO: transfer functions accept the token ID as uint, not as string
            _stringToUint(_tokenId)
        );
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文