blockchain - 使用合约 A 将代币从合约 B 转移到用户地址 X
问题描述
我已经部署了合约 A。现在我正在创建网关合约 B,我想使用所有者地址将合约 A 的一些代币发送到用户地址 X。值得一提的是,合同 A 的所有者与合同 B 相同。我执行以下操作
contract A is Ownable { // this one already deployed by owner
constructor() {
owner = msg.sender; // owner address is 0x123
approve(msg.sender, totalSupply); // let's approve all tokens for owner
}
function transferFrom(address from, address to, uint256 value) public returns (bool) {
require(value <= allowed[from][msg.sender], "Not allowed!");
// let's skip other logic
}
}
contract B is Ownable { // gateway contract will be deployed and executed by same owner
A contractA = ETC20(0x111);
address payable X = 0x333;
constructor() {
owner = msg.sender; // owner address is 0x123
}
function giveAwayTokens(uint256 value) {
contractA.transferFrom(owner, X, value);
}
}
当我从所有者地址 (0x123) 执行“giveAwayTokens”函数时,我收到错误“不允许!”。所以我现在有点困惑,因为所有者的所有者津贴是最大供应量。或者 msg.sender 可能是 contractB 本身?请赐教我在这里做错了什么,谢谢
解决方案
ContractB
调用时ContractA
,msg.sender
inContractA
是ContractB
地址。
根据代码和错误消息,owner
( 0x123
) 不允许ContractB
花费他们的代币。
您需要将 的值设置allowed[<owner>][<ContractB>]
为至少要发送的令牌数量。
很可能您有一个可以使用的approve()
函数(在令牌标准中定义)。在链接的示例中,函数的调用者将是 the owner
, thespender
将是 theContractB
并且 thevalue
将是等于或高于您要发送的令牌数量的任何值(注意小数)。
推荐阅读
- php - 是否有类似于 PHP 的 setInterval () (JavaScript) 的函数?
- amazon-web-services - AWS Lex 机器人:使用户能够在页面刷新后继续对话
- javascript - 当我使用 Express 向 Heroku 提供 index.html 文件时,为什么我的 Create-React-App 不显示?
- xamarin.forms - 为什么我的列表 ObservableCollection 永远不会刷新?
- android - Firebase 自定义声明不起作用 - android
- angular - 当我从 MS Doc 正确设置 CORS 设置时出现未知服务器错误
- java - 使用 java 有没有办法从没有典型格式扩展名的网站下载图像?
- kubernetes - 更新 pod 定义以在现有 kubernetes 部署上定义就绪探测
- node.js - 添加 azure 函数代理后,当我在本地运行 azure 函数项目(VSCode)时,出现“Worker 无法加载函数”错误
- python - 需要通过套接字连接接受关闭指令