如何在 Remix 中测试 Solidity-By-Example Mapping 智能合约?
How to test the Solidity-By-Example Mapping smart contract in Remix?
我想在 Remix 中测试以下代码。但是程序是什么?
我应该在标记为 set 的函数的输入字段中输入什么?
Mapping.sol
// https://solidity-by-example.org/mapping
// Mapping
// Maps are created with the syntax mapping(keyType => valueType).
// The keyType can be any built-in value type, bytes, string, or any contract.
// valueType can be any type including another mapping or an array.
// Mappings are not iterable.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract Mapping {
// Mapping from address to uint
mapping(address => uint) public myMap;
function get(address _addr) public view returns (uint) {
// Mapping always returns a value.
// If the value was never set, it will return the default value.
return myMap[_addr];
}
function set(address _addr, uint _i) public {
// Update the value at this address
myMap[_addr] = _i;
}
function remove(address _addr) public {
// Reset the value to the default value.
delete myMap[_addr];
}
}
contract NestedMapping {
// Nested mapping (mapping from address to another mapping)
mapping(address => mapping(uint => bool)) public nested;
function get(address _addr1, uint _i) public view returns (bool) {
// You can get values from a nested mapping
// even when it is not initialized
return nested[_addr1][_i];
}
function set(
address _addr1,
uint _i,
bool _boo
) public {
nested[_addr1][_i] = _boo;
}
function remove(address _addr1, uint _i) public {
delete nested[_addr1][_i];
}
}
此代码来自 Solidity by Example。
当我在 Remix 上实现它时,我得到以下屏幕。
此时,为了测试它,我想我需要将一个 address
映射到一个 uint256
,所以我在 集旁边的字段中输入以下内容 按钮:
["0xcf646ed6e21fd0756ec45a6be5e1057fc24a1b8308175ff0b9f97f565b594eb3", 7439]
地址的值是随机生成的哈希值(我怀疑随机哈希值 可能 有问题?)
我希望看到 set 函数呈现值 7439
。但是,它会抛出以下错误:
transact to Mapping.set errored: Error encoding arguments: Error: invalid address (argument="address", value=["0xcf646ed6e21fd0756ec45a6be5e1057fc24a1b8308175ff0b9f97f565b594eb3",7439], code=INVALID_ARGUMENT, version=address/5.5.0) (argument=null, value=["0xcf646ed6e21fd0756ec45a6be5e1057fc24a1b8308175ff0b9f97f565b594eb3",7439], code=INVALID_ARGUMENT, version=abi/5.5.0)
我做错了什么?
您生成了一个有效地址格式的随机 SHA-256,但它不作为任何帐户存在于 Remix 的 browser-based(Javascript 虚拟机)链上。如果是这样,你可能只是幸运。
如果您想使用 browser-based VM 链上确实存在的有效地址,请复制并使用帐户部分中的地址。
我想在 Remix 中测试以下代码。但是程序是什么?
我应该在标记为 set 的函数的输入字段中输入什么?
Mapping.sol
// https://solidity-by-example.org/mapping
// Mapping
// Maps are created with the syntax mapping(keyType => valueType).
// The keyType can be any built-in value type, bytes, string, or any contract.
// valueType can be any type including another mapping or an array.
// Mappings are not iterable.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract Mapping {
// Mapping from address to uint
mapping(address => uint) public myMap;
function get(address _addr) public view returns (uint) {
// Mapping always returns a value.
// If the value was never set, it will return the default value.
return myMap[_addr];
}
function set(address _addr, uint _i) public {
// Update the value at this address
myMap[_addr] = _i;
}
function remove(address _addr) public {
// Reset the value to the default value.
delete myMap[_addr];
}
}
contract NestedMapping {
// Nested mapping (mapping from address to another mapping)
mapping(address => mapping(uint => bool)) public nested;
function get(address _addr1, uint _i) public view returns (bool) {
// You can get values from a nested mapping
// even when it is not initialized
return nested[_addr1][_i];
}
function set(
address _addr1,
uint _i,
bool _boo
) public {
nested[_addr1][_i] = _boo;
}
function remove(address _addr1, uint _i) public {
delete nested[_addr1][_i];
}
}
此代码来自 Solidity by Example。
当我在 Remix 上实现它时,我得到以下屏幕。
此时,为了测试它,我想我需要将一个 address
映射到一个 uint256
,所以我在 集旁边的字段中输入以下内容 按钮:
["0xcf646ed6e21fd0756ec45a6be5e1057fc24a1b8308175ff0b9f97f565b594eb3", 7439]
地址的值是随机生成的哈希值(我怀疑随机哈希值 可能 有问题?)
我希望看到 set 函数呈现值 7439
。但是,它会抛出以下错误:
transact to Mapping.set errored: Error encoding arguments: Error: invalid address (argument="address", value=["0xcf646ed6e21fd0756ec45a6be5e1057fc24a1b8308175ff0b9f97f565b594eb3",7439], code=INVALID_ARGUMENT, version=address/5.5.0) (argument=null, value=["0xcf646ed6e21fd0756ec45a6be5e1057fc24a1b8308175ff0b9f97f565b594eb3",7439], code=INVALID_ARGUMENT, version=abi/5.5.0)
我做错了什么?
您生成了一个有效地址格式的随机 SHA-256,但它不作为任何帐户存在于 Remix 的 browser-based(Javascript 虚拟机)链上。如果是这样,你可能只是幸运。
如果您想使用 browser-based VM 链上确实存在的有效地址,请复制并使用帐户部分中的地址。