Merge pull request #2980 from ethereum/abi-api

Add abi.encode and abi.encodePacked
This commit is contained in:
chriseth
2018-04-12 20:55:03 +02:00
committed by GitHub
17 changed files with 700 additions and 66 deletions
@@ -0,0 +1,13 @@
contract C {
function f() pure public {
abi.encodeWithSelector();
abi.encodeWithSignature();
abi.encodeWithSelector(uint(2), 2);
abi.encodeWithSignature(uint(2), 2);
}
}
// ----
// TypeError: (52-76): Need at least 1 arguments for function call, but provided only 0.
// TypeError: (86-111): Need at least 1 arguments for function call, but provided only 0.
// TypeError: (144-151): Invalid type for argument in function call. Invalid implicit conversion from uint256 to bytes4 requested.
// TypeError: (189-196): Invalid type for argument in function call. Invalid implicit conversion from uint256 to string memory requested.
@@ -0,0 +1,17 @@
contract C {
struct S { uint x; }
S s;
struct T { uint y; }
T t;
function f() public view {
abi.encode(s, t);
}
function g() public view {
abi.encodePacked(s, t);
}
}
// ----
// TypeError: (131-132): This type cannot be encoded.
// TypeError: (134-135): This type cannot be encoded.
// TypeError: (200-201): This type cannot be encoded.
// TypeError: (203-204): This type cannot be encoded.
@@ -0,0 +1,18 @@
pragma experimental ABIEncoderV2;
contract C {
struct S { uint x; }
S s;
struct T { uint y; }
T t;
function f() public view {
abi.encode(s, t);
}
function g() public view {
abi.encodePacked(s, t);
}
}
// ----
// Warning: (0-33): Experimental features are turned on. Do not use experimental features on live deployments.
// TypeError: (235-236): This type cannot be encoded.
// TypeError: (238-239): This type cannot be encoded.
@@ -1,14 +1,13 @@
contract C {
struct S { uint x; }
S s;
struct T { }
struct T { uint y; }
T t;
function f() public pure {
function f() public view {
bytes32 a = sha256(s, t);
a;
}
}
// ----
// Warning: (51-63): Defining empty structs is deprecated.
// TypeError: (131-132): This type cannot be encoded.
// TypeError: (134-135): This type cannot be encoded.
// TypeError: (139-140): This type cannot be encoded.
// TypeError: (142-143): This type cannot be encoded.
@@ -0,0 +1,16 @@
pragma experimental ABIEncoderV2;
contract C {
struct S { uint x; }
S s;
struct T { uint y; }
T t;
function f() public view {
bytes32 a = sha256(s, t);
a;
}
}
// ----
// Warning: (0-33): Experimental features are turned on. Do not use experimental features on live deployments.
// TypeError: (174-175): This type cannot be encoded.
// TypeError: (177-178): This type cannot be encoded.
@@ -0,0 +1,25 @@
contract C {
function f() pure public returns (bytes32) {
return keccak256(1);
}
function g() pure public returns (bytes32) {
return sha3(1);
}
function h() pure public returns (bytes32) {
return sha256(1);
}
function j() pure public returns (bytes32) {
return ripemd160(1);
}
function k() pure public returns (bytes) {
return abi.encodePacked(1);
}
}
// ----
// Warning: (87-88): The type of "int_const 1" was inferred as uint8. This is probably not desired. Use an explicit type to silence this warning.
// Warning: (161-168): "sha3" has been deprecated in favour of "keccak256"
// Warning: (166-167): The type of "int_const 1" was inferred as uint8. This is probably not desired. Use an explicit type to silence this warning.
// Warning: (247-248): The type of "int_const 1" was inferred as uint8. This is probably not desired. Use an explicit type to silence this warning.
// Warning: (331-332): The type of "int_const 1" was inferred as uint8. This is probably not desired. Use an explicit type to silence this warning.
// Warning: (420-421): The type of "int_const 1" was inferred as uint8. This is probably not desired. Use an explicit type to silence this warning.
@@ -0,0 +1,11 @@
contract C {
function f() pure public returns (bytes32) {
return keccak256(uint8(1));
}
function g() pure public returns (bytes) {
return abi.encode(1);
}
function h() pure public returns (bytes) {
return abi.encodePacked(uint8(1));
}
}
@@ -0,0 +1,8 @@
contract C {
function f() pure public returns (bytes r) {
r = abi.encode(1, 2);
r = abi.encodePacked(f());
r = abi.encodeWithSelector(0x12345678, 1);
r = abi.encodeWithSignature("f(uint256)", 4);
}
}
@@ -0,0 +1,36 @@
contract C {
uint x;
function gView() public view returns (uint) { return x; }
function gNonPayable() public returns (uint) { x = 4; return 0; }
function f1() view public returns (bytes) {
return abi.encode(gView());
}
function f2() view public returns (bytes) {
return abi.encodePacked(gView());
}
function f3() view public returns (bytes) {
return abi.encodeWithSelector(0x12345678, gView());
}
function f4() view public returns (bytes) {
return abi.encodeWithSignature("f(uint256)", gView());
}
function g1() public returns (bytes) {
return abi.encode(gNonPayable());
}
function g2() public returns (bytes) {
return abi.encodePacked(gNonPayable());
}
function g3() public returns (bytes) {
return abi.encodeWithSelector(0x12345678, gNonPayable());
}
function g4() public returns (bytes) {
return abi.encodeWithSignature("f(uint256)", gNonPayable());
}
// This will generate the only warning.
function check() public returns (bytes) {
return abi.encode(2);
}
}
// ----
// Warning: (1044-1121): Function state mutability can be restricted to pure