solidity - solidity abi.encodeWithSignature 不适用于 diffienet 参数
问题描述
pragma solidity ^0.5.3;
contract SimpleFallback{
event FallbackCalledEvent(bytes data);
event AddEvent(uint a, uint b, uint result);
event DoubleEvent(uint a, uint b);
event GetNameEvent(string);
function() external{
emit FallbackCalledEvent(msg.data);
}
function add(uint a, uint b) public returns(uint){
// assert(a >= 0);
// assert(b >= 0);
// assert(a + b > a && a + b > b);
uint _result = a + b;
emit AddEvent(a, b, _result);
return _result;
}
function double(uint a) public returns(uint){
// assert(a >= 0);
// assert(2*a >= 0 && 2*a >= a);
uint _result = 2*a;
emit DoubleEvent(a, _result);
return _result;
}
function getName(string memory name) public returns(string memory){
emit GetNameEvent(name);
return name;
}
}
contract RunTest{
function callAddlTest(address other) public {
// bytes4 messageId = bytes4(keccak256("add(uint, uint)"));
// other.call(messageId, 5, 60);
other.call(abi.encodeWithSignature("add(uint,uint)", 85, 60));
}
function callDoublelTest(address other) public {
// bytes4 messageId = bytes4(keccak256("add(uint, uint)"));
// other.call(messageId, 5, 60);
other.call(abi.encodeWithSignature("double(uint)", 100));
}
function callgetNameTest(address other) public{
other.call(abi.encodeWithSignature("getName(string)", "hello"));
}
}
操作如下。
- Step 1. 部署2个合约;
- Step 2. 调用
callAddlTest
合约SimpleFallback的地址,事件日志如下,显示调用add()失败,触发了fallback函数。
[
{
"from": "0x038f160ad632409bfb18582241d9fd88c1a072ba",
"topic": "0x5cd57a899be814fb3a40e18f9d1ba77420bbd22073d35165511f750aa70538b6",
"event": "FallbackCalledEvent",
"args": {
"0": "0xb89663520000000000000000000000000000000000000000000000000000000000000055000000000000000000000000000000000000000000000000000000000000003c",
"data": "0xb89663520000000000000000000000000000000000000000000000000000000000000055000000000000000000000000000000000000000000000000000000000000003c",
"length": 1
}
}
]
- Step 3.调用
callDoubleTest
whit合约SimpleFallback的地址,事件日志如下,显示调用double()失败,触发了fallback函数。
[
{
"from": "0x038f160ad632409bfb18582241d9fd88c1a072ba",
"topic": "0x5cd57a899be814fb3a40e18f9d1ba77420bbd22073d35165511f750aa70538b6",
"event": "FallbackCalledEvent",
"args": {
"0": "0xd524bc570000000000000000000000000000000000000000000000000000000000000064",
"data": "0xd524bc570000000000000000000000000000000000000000000000000000000000000064",
"length": 1
}
}
]
- 第四步,调用
callgetNameTest
合约SimpleFallback的地址,事件日志如下,显示调用getName()成功。
{
"from": "0x038f160ad632409bfb18582241d9fd88c1a072ba",
"topic": "0x26c73f7f14382f5db0b9f94dd29ff8938f2e4be69fb13e0825ece287e8e538d5",
"event": "GetNameEvent",
"args": {
"0": "hello",
"length": 1
}
}
]
问题:对于 call(abi.encodeWithSignature()),为什么:
1)
other.call(abi.encodeWithSignature("add(uint,uint)", 85, 60));
没有工作?2)
other.call(abi.encodeWithSignature("double(uint)", 100));
没有工作?- 3)
other.call(abi.encodeWithSignature("getName(string)", "hello"));
工作了吗? - 4)不
abi.encodeWithSignature
支持多参数,对比1)和3)?并且不abi.encodeWithSignature
支持 uint 参数,比较 2) 和 3)?
解决方案
我已经找到了根本原因。它应该使用类型的全名,而不是它们的别名,所以它应该uint256
代替uint
.
推荐阅读
- python - 列表Python中的比较总和
- java - ServletRequest getAsyncContext() 上的 IllegalStateException
- python - 如何将python字符串转换为数组
- arrays - 如何检查多维数组是否为空
- django - 如何使用 django-partial-date 和 SelectDateWidget 管理部分日期?
- r - 运行一段代码多次更改 R 中的某些参数(如 SAS 中的宏)
- regex - 用于连接正则表达式和变量的 PowerShell 脚本
- nestjs - 在 Nestjs 问题中读取对象值
- windows - 用于将代理部署到 Windows 操作系统的 Azure 自定义脚本扩展问题
- python-3.x - 如何使用存储在数据库中的内容?SQLite/Python