Solidity 智能合约可以代表 msg.sender 执行 erc20 合约中的批准功能吗

发布于 2025-01-13 00:27:03 字数 1263 浏览 4 评论 0原文

建立银行合同来执行存款和取款。我想知道银行合约是否可以代表 msg.sender 执行 erc20 合约中他们想要存入的代币的批准功能。 下面是我对银行合约调用 erc20 代币合约批准函数的尝试。然而,msg.sender 不会是银行合约地址而不是原始的 msg.sender (depositToken 函数的第二行)。这听起来很愚蠢,但是有没有办法让合约发送在 msg.senders 中传递的请求地址?如果没有,msg.sender 是否有一种集成的方式来批准银行合约地址和金额,以使银行合约能够调用 TransferFrom 函数并获得津贴。

//best guess on what that would look like inside the function depositTokens
msg.sender = customer;
customer.IER20(usdt).approve.address(this), uint _amount;
address public usdt;
  mapping(address => uint) public bankBalance;
  constructor() public {
      usdt = 0x77c24f0Af71257C0ee26e0E0a108F940D1698d53;
  }
usdt = 0x77c24f0Af71257C0ee26e0E0a108F940D1698d53;
  function depositTokens(uint _amount) public {
      IER20(usdt).approve.address(this), uint _amount;
      // Trasnfer usdt tokens to contract
      IERC20(usdt).transferFrom(msg.sender, address(this), _amount);
      // Update the bank balance in map
      bankBalance[msg.sender] = bankBalance[msg.sender] + _amount;
  }

//approve function in erc20
  function approve(address delegate, uint256 numTokens) public override returns (bool) {
      allowed[msg.sender][delegate] = numTokens;
      emit Approval(msg.sender, delegate, numTokens);
      return true;

Setting up a bank contract to perform deposits and withdraws. I was wondering if its possible for the bank contract can execute the approve function in the erc20 contract on behalf of the msg.sender for the tokens they are wanting to deposit.
Below is my attempt for the bank contract to call the erc20 token contracts approve function. However wouldn't the msg.sender be the bank contract address instead of the original msg.sender (second line of the depositToken function.) This sounds silly but is there a way for the contract to send the request passing in the msg.senders address? If not is there an integrated way for the msg.sender to approve the bank contract address and the amount to enable the bank contract to call the transferFrom function and be provided the allowance.

//best guess on what that would look like inside the function depositTokens
msg.sender = customer;
customer.IER20(usdt).approve.address(this), uint _amount;
address public usdt;
  mapping(address => uint) public bankBalance;
  constructor() public {
      usdt = 0x77c24f0Af71257C0ee26e0E0a108F940D1698d53;
  }
usdt = 0x77c24f0Af71257C0ee26e0E0a108F940D1698d53;
  function depositTokens(uint _amount) public {
      IER20(usdt).approve.address(this), uint _amount;
      // Trasnfer usdt tokens to contract
      IERC20(usdt).transferFrom(msg.sender, address(this), _amount);
      // Update the bank balance in map
      bankBalance[msg.sender] = bankBalance[msg.sender] + _amount;
  }

//approve function in erc20
  function approve(address delegate, uint256 numTokens) public override returns (bool) {
      allowed[msg.sender][delegate] = numTokens;
      emit Approval(msg.sender, delegate, numTokens);
      return true;

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

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

发布评论

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

评论(2

风为裳 2025-01-20 00:27:03

答案是否定的,批准函数将 msg.sender 作为给予津贴的人。您可以查看最常用的 OpenZeppelin 实现 在这里

不过,我可以看到您想要做什么,我想补充一点,有一种方法可以在发送交易之前自动要求 EOA(外部拥有帐户)或拥有钱包的用户批准一些代币到合约中。
我不知道钱包是否会自动执行此操作,或者您必须在用户界面中对其进行编码,但我认为这就是您正在寻找的。

然后,在用户批准您的银行后,您可以删除 deposit 函数中的第一行

The answer is no, the approve function takes the msg.sender as the one that gives allowance. You can see the most used OpenZeppelin implementation here.

However I can see what you are trying to do and I want to add that there is a way to automatically ask an EOA (externally owned account) or a user with a wallet to approve some tokens to the contract before sending a transaction.
I don't know if a wallet does it automatically or you have to code it in the UI but I think that is what you are looking for.

And then after the user already approved your bank, you can remove your first line in the deposit function

同展鸳鸯锦 2025-01-20 00:27:03

我在您的 DepositTokens 函数中发现拼写错误:IER20IERC20

I see a typo in your depositTokens function: IER20 vs IERC20

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文