-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathSubstrateTypes.sol
More file actions
156 lines (144 loc) · 5.05 KB
/
SubstrateTypes.sol
File metadata and controls
156 lines (144 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
pragma solidity 0.8.23;
import {ScaleCodec} from "./utils/ScaleCodec.sol";
import {ParaID, Weight, OriginKind} from "./Types.sol";
/**
* @title SCALE encoders for common Substrate types
*/
library SubstrateTypes {
error UnsupportedCompactEncoding();
/**
* @dev Encodes `MultiAddress::Id`: https://crates.parity.io/sp_runtime/enum.MultiAddress.html#variant.Id
* @return bytes SCALE-encoded bytes
*/
// solhint-disable-next-line func-name-mixedcase
function MultiAddressWithID(bytes32 account) internal pure returns (bytes memory) {
return bytes.concat(hex"00", account);
}
/**
* @dev Encodes `H160`: https://crates.parity.io/sp_core/struct.H160.html
* @return bytes SCALE-encoded bytes
*/
// solhint-disable-next-line func-name-mixedcase
function H160(address account) internal pure returns (bytes memory) {
return abi.encodePacked(account);
}
function VecU8(bytes memory input) internal pure returns (bytes memory) {
return bytes.concat(ScaleCodec.checkedEncodeCompactU32(input.length), input);
}
/**
* @dev Encodes `Option::None`: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None
* @return bytes SCALE-encoded bytes
*/
// solhint-disable-next-line func-name-mixedcase
function None() internal pure returns (bytes memory) {
return hex"00";
}
// solhint-disable-next-line func-name-mixedcase
function OptionParaID(ParaID v) internal pure returns (bytes memory) {
if (ParaID.unwrap(v) == 0) {
return hex"00";
} else {
return bytes.concat(bytes1(0x01), ScaleCodec.encodeU32(uint32(ParaID.unwrap(v))));
}
}
/**
* @dev SCALE-encodes `router_primitives::inbound::VersionedMessage` containing payload
* `NativeTokensMessage::Create`
*/
// solhint-disable-next-line func-name-mixedcase
function RegisterToken(address token, uint128 fee) internal view returns (bytes memory) {
return bytes.concat(
bytes1(0x00),
ScaleCodec.encodeU64(uint64(block.chainid)),
bytes1(0x00),
SubstrateTypes.H160(token),
ScaleCodec.encodeU128(fee)
);
}
/**
* @dev SCALE-encodes `router_primitives::inbound::VersionedMessage` containing payload
* `NativeTokensMessage::Mint`
*/
// destination is AccountID32 address on AssetHub
function SendTokenToAssetHubAddress32(address token, bytes32 recipient, uint128 xcmFee, uint128 amount)
internal
view
returns (bytes memory)
{
return bytes.concat(
bytes1(0x00),
ScaleCodec.encodeU64(uint64(block.chainid)),
bytes1(0x01),
SubstrateTypes.H160(token),
bytes1(0x00),
recipient,
ScaleCodec.encodeU128(amount),
ScaleCodec.encodeU128(xcmFee)
);
}
// destination is AccountID32 address
function SendTokenToAddress32(
address token,
ParaID paraID,
bytes32 recipient,
uint128 xcmFee,
uint128 destinationXcmFee,
uint128 amount
) internal view returns (bytes memory) {
return bytes.concat(
bytes1(0x00),
ScaleCodec.encodeU64(uint64(block.chainid)),
bytes1(0x01),
SubstrateTypes.H160(token),
bytes1(0x01),
ScaleCodec.encodeU32(uint32(ParaID.unwrap(paraID))),
recipient,
ScaleCodec.encodeU128(destinationXcmFee),
ScaleCodec.encodeU128(amount),
ScaleCodec.encodeU128(xcmFee)
);
}
// destination is AccountID20 address
function SendTokenToAddress20(
address token,
ParaID paraID,
bytes20 recipient,
uint128 xcmFee,
uint128 destinationXcmFee,
uint128 amount
) internal view returns (bytes memory) {
return bytes.concat(
bytes1(0x00),
ScaleCodec.encodeU64(uint64(block.chainid)),
bytes1(0x01),
SubstrateTypes.H160(token),
bytes1(0x02),
ScaleCodec.encodeU32(uint32(ParaID.unwrap(paraID))),
recipient,
ScaleCodec.encodeU128(destinationXcmFee),
ScaleCodec.encodeU128(amount),
ScaleCodec.encodeU128(xcmFee)
);
}
// Arbitrary transact
function Transact(address sender, bytes1 originKind, uint128 fee, Weight memory weight, bytes memory call)
internal
view
returns (bytes memory)
{
return bytes.concat(
bytes1(0x00),
ScaleCodec.encodeU64(uint64(block.chainid)),
bytes1(0x02),
SubstrateTypes.H160(sender),
originKind,
ScaleCodec.encodeU128(fee),
ScaleCodec.encodeCompactU64(weight.refTime),
ScaleCodec.encodeCompactU64(weight.proofSize),
ScaleCodec.checkedEncodeCompactU32(call.length),
call
);
}
}