solidity-scale-codec 0.3.4 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +37 -3
- package/README.md +10 -28
- package/package.json +2 -2
- package/src/Utils/BytesUtils.sol +26 -0
- package/src/Utils/UnsignedUtils.sol +52 -0
- package/src/Xcm/VersionedXcm/VersionedXcm.sol +4 -1
- package/src/Xcm/VersionedXcm/VersionedXcmCodec.sol +11 -0
- package/src/Xcm/v3/MaybeErrorCode/MaybeErrorCode.sol +8 -7
- package/src/Xcm/v3/MaybeErrorCode/MaybeErrorCodeCodec.sol +18 -19
- package/src/Xcm/v5/AssetFilter/AssetFilter.sol +8 -8
- package/src/Xcm/v5/AssetFilter/AssetFilterCodec.sol +47 -34
- package/src/Xcm/v5/AssetInstance/AssetInstance.sol +13 -12
- package/src/Xcm/v5/AssetInstance/AssetInstanceCodec.sol +53 -56
- package/src/Xcm/v5/AssetTransferFilter/AssetTransferFilter.sol +12 -12
- package/src/Xcm/v5/AssetTransferFilter/AssetTransferFilterCodec.sol +48 -20
- package/src/Xcm/v5/Assets/Assets.sol +16 -0
- package/src/Xcm/v5/Assets/AssetsCodec.sol +3 -3
- package/src/Xcm/v5/BodyId/BodyId.sol +24 -24
- package/src/Xcm/v5/BodyId/BodyIdCodec.sol +41 -48
- package/src/Xcm/v5/BodyPart/BodyPart.sol +44 -28
- package/src/Xcm/v5/BodyPart/BodyPartCodec.sol +70 -37
- package/src/Xcm/v5/Constants.sol +2 -2
- package/src/Xcm/v5/Fungibility/Fungibility.sol +6 -6
- package/src/Xcm/v5/Fungibility/FungibilityCodec.sol +40 -36
- package/src/Xcm/v5/Hint/Hint.sol +5 -5
- package/src/Xcm/v5/Hint/HintCodec.sol +24 -20
- package/src/Xcm/v5/Instruction/Instruction.sol +81 -55
- package/src/Xcm/v5/Instruction/InstructionCodec.sol +1047 -73
- package/src/Xcm/v5/Junction/Junction.sol +55 -69
- package/src/Xcm/v5/Junction/JunctionCodec.sol +72 -135
- package/src/Xcm/v5/Junctions/Junctions.sol +34 -0
- package/src/Xcm/v5/Junctions/JunctionsCodec.sol +0 -18
- package/src/Xcm/v5/Location/Location.sol +8 -0
- package/src/Xcm/v5/NetworkId/NetworkId.sol +15 -16
- package/src/Xcm/v5/NetworkId/NetworkIdCodec.sol +57 -34
- package/src/Xcm/v5/OriginKind/OriginKindCodec.sol +1 -1
- package/src/Xcm/v5/Response/Response.sol +49 -40
- package/src/Xcm/v5/Response/ResponseCodec.sol +64 -54
- package/src/Xcm/v5/Weight/WeightCodec.sol +3 -2
- package/src/Xcm/v5/WeightLimit/WeightLimit.sol +6 -6
- package/src/Xcm/v5/WeightLimit/WeightLimitCodec.sol +32 -23
- package/src/Xcm/v5/WildAsset/WildAsset.sol +17 -25
- package/src/Xcm/v5/WildAsset/WildAssetCodec.sol +35 -38
- package/src/Xcm/v5/WildFungibility/WildFungibilityCodec.sol +6 -6
- package/src/Xcm/v5/Xcm/XcmBuilder.sol +689 -0
- package/src/Xcm/v5/XcmError/XcmError.sol +7 -7
- package/src/Xcm/v5/XcmError/XcmErrorCodec.sol +25 -22
|
@@ -4,7 +4,7 @@ pragma solidity ^0.8.28;
|
|
|
4
4
|
import {LittleEndianU64} from "../../../LittleEndian/LittleEndianU64.sol";
|
|
5
5
|
|
|
6
6
|
/// @notice Error codes used in XCM.
|
|
7
|
-
enum
|
|
7
|
+
enum XcmErrorVariant {
|
|
8
8
|
/// @custom:variant An arithmetic overflow happened.
|
|
9
9
|
Overflow,
|
|
10
10
|
/// @custom:variant The instruction is intentionally unsupported.
|
|
@@ -81,16 +81,16 @@ enum XcmErrorType {
|
|
|
81
81
|
|
|
82
82
|
/// @notice XCM v5 error, containing the error type and an optional payload for `Trap`.
|
|
83
83
|
struct XcmError {
|
|
84
|
-
/// @custom:property The type of the error. See `
|
|
85
|
-
|
|
86
|
-
/// @custom:property The trap code. Only meaningful when `
|
|
84
|
+
/// @custom:property The type of the error. See `XcmErrorVariant` enum for possible values.
|
|
85
|
+
XcmErrorVariant variant;
|
|
86
|
+
/// @custom:property The trap code. Only meaningful when `variant` is `Trap`.
|
|
87
87
|
bytes payload;
|
|
88
88
|
}
|
|
89
89
|
|
|
90
90
|
/// @notice Parameters for unit (payload-less) XCM errors.
|
|
91
91
|
struct UnitParams {
|
|
92
92
|
/// @custom:property The non-trap error discriminant.
|
|
93
|
-
|
|
93
|
+
XcmErrorVariant variant;
|
|
94
94
|
}
|
|
95
95
|
|
|
96
96
|
/// @notice Parameters for the `Trap` error variant.
|
|
@@ -107,7 +107,7 @@ using LittleEndianU64 for uint64;
|
|
|
107
107
|
/// @param params Parameters for the unit error.
|
|
108
108
|
/// @return The `XcmError` struct.
|
|
109
109
|
function unit(UnitParams memory params) pure returns (XcmError memory) {
|
|
110
|
-
return XcmError({
|
|
110
|
+
return XcmError({variant: params.variant, payload: ""});
|
|
111
111
|
}
|
|
112
112
|
|
|
113
113
|
/// @notice Creates a `Trap` error with the given u64 code.
|
|
@@ -116,7 +116,7 @@ function unit(UnitParams memory params) pure returns (XcmError memory) {
|
|
|
116
116
|
function trap(TrapParams memory params) pure returns (XcmError memory) {
|
|
117
117
|
return
|
|
118
118
|
XcmError({
|
|
119
|
-
|
|
119
|
+
variant: XcmErrorVariant.Trap,
|
|
120
120
|
payload: abi.encodePacked(params.code.toLittleEndian())
|
|
121
121
|
});
|
|
122
122
|
}
|
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
pragma solidity ^0.8.28;
|
|
3
3
|
|
|
4
4
|
import {LittleEndianU64} from "../../../LittleEndian/LittleEndianU64.sol";
|
|
5
|
-
import {XcmError,
|
|
5
|
+
import {XcmError, XcmErrorVariant, TrapParams} from "./XcmError.sol";
|
|
6
|
+
import {BytesUtils} from "../../../Utils/BytesUtils.sol";
|
|
6
7
|
|
|
7
8
|
/// @title SCALE Codec for XCM v5 `Error`
|
|
8
9
|
/// @notice SCALE-compliant encoder/decoder for the XCM v5 `Error` type.
|
|
@@ -10,14 +11,14 @@ import {XcmError, XcmErrorType} from "./XcmError.sol";
|
|
|
10
11
|
/// @dev XCM v5 reference: https://paritytech.github.io/polkadot-sdk/master/staging_xcm/v5/traits.rs.html
|
|
11
12
|
library XcmErrorCodec {
|
|
12
13
|
error InvalidXcmErrorLength();
|
|
13
|
-
error
|
|
14
|
+
error InvalidXcmErrorVariant(uint8 variant);
|
|
14
15
|
error InvalidXcmErrorPayload();
|
|
15
16
|
|
|
16
17
|
/// @notice Encodes an `XcmError` into SCALE bytes.
|
|
17
18
|
/// @param e The `XcmError` struct to encode.
|
|
18
19
|
/// @return SCALE-encoded bytes representing the error.
|
|
19
20
|
function encode(XcmError memory e) internal pure returns (bytes memory) {
|
|
20
|
-
return abi.encodePacked(uint8(e.
|
|
21
|
+
return abi.encodePacked(uint8(e.variant), e.payload);
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
/// @notice Returns the number of bytes that an `XcmError` would occupy when SCALE-encoded.
|
|
@@ -30,9 +31,9 @@ library XcmErrorCodec {
|
|
|
30
31
|
) internal pure returns (uint256) {
|
|
31
32
|
if (data.length < offset + 1) revert InvalidXcmErrorLength();
|
|
32
33
|
uint8 raw = uint8(data[offset]);
|
|
33
|
-
if (raw > uint8(
|
|
34
|
-
revert
|
|
35
|
-
if (raw == uint8(
|
|
34
|
+
if (raw > uint8(type(XcmErrorVariant).max))
|
|
35
|
+
revert InvalidXcmErrorVariant(raw);
|
|
36
|
+
if (raw == uint8(XcmErrorVariant.Trap)) {
|
|
36
37
|
if (data.length < offset + 9) revert InvalidXcmErrorLength();
|
|
37
38
|
return 1 + 8; // 1 byte for the error type and 8 bytes for the u64 trap code
|
|
38
39
|
}
|
|
@@ -58,28 +59,30 @@ library XcmErrorCodec {
|
|
|
58
59
|
bytes memory data,
|
|
59
60
|
uint256 offset
|
|
60
61
|
) internal pure returns (XcmError memory e, uint256 bytesRead) {
|
|
61
|
-
if (data.length < offset + 1) revert InvalidXcmErrorLength();
|
|
62
|
-
uint8 raw = uint8(data[offset]);
|
|
63
|
-
if (raw > uint8(XcmErrorType.TooManyAssets))
|
|
64
|
-
revert InvalidXcmError(raw);
|
|
65
62
|
uint256 size = encodedSizeAt(data, offset);
|
|
63
|
+
uint8 raw = uint8(data[offset]);
|
|
66
64
|
uint256 payloadLength = size - 1;
|
|
67
|
-
bytes memory payload =
|
|
68
|
-
|
|
69
|
-
payload[i] = data[offset + 1 + i];
|
|
70
|
-
}
|
|
71
|
-
e = XcmError({eType: XcmErrorType(raw), payload: payload});
|
|
65
|
+
bytes memory payload = BytesUtils.copy(data, offset + 1, payloadLength);
|
|
66
|
+
e = XcmError({variant: XcmErrorVariant(raw), payload: payload});
|
|
72
67
|
bytesRead = size;
|
|
73
68
|
}
|
|
74
69
|
|
|
75
70
|
/// @notice Decodes the trap code from a `Trap` error.
|
|
76
71
|
/// @param e The `XcmError` struct to decode, which must be of type `Trap`.
|
|
77
|
-
/// @return
|
|
78
|
-
function asTrap(
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
72
|
+
/// @return params A `TrapParams` struct containing the decoded trap code.
|
|
73
|
+
function asTrap(
|
|
74
|
+
XcmError memory e
|
|
75
|
+
) internal pure returns (TrapParams memory params) {
|
|
76
|
+
_assertVariant(e, XcmErrorVariant.Trap);
|
|
77
|
+
params.code = LittleEndianU64.fromLittleEndian(e.payload, 0);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function _assertVariant(
|
|
81
|
+
XcmError memory e,
|
|
82
|
+
XcmErrorVariant expected
|
|
83
|
+
) internal pure {
|
|
84
|
+
if (e.variant != expected) {
|
|
85
|
+
revert InvalidXcmErrorVariant(uint8(e.variant));
|
|
86
|
+
}
|
|
84
87
|
}
|
|
85
88
|
}
|