-
Notifications
You must be signed in to change notification settings - Fork 19
/
helpers.ts
228 lines (216 loc) · 7.69 KB
/
helpers.ts
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import { utils, BigNumber, providers } from 'ethers';
import { TypeRegistry } from '@polkadot/types/create';
import { ERC20 } from '@buildwithsygma/sygma-contracts';
const registry = new TypeRegistry();
/**
* Return hex data padded to the number defined as padding
* based on ethers.utils.hexZeroPad
*
* @category Helpers
* @param covertThis - data to convert
* @param padding - number to padd the data
* @returns {string}
*/
export const toHex = (covertThis: string | number | BigNumber, padding: number): string => {
const amount = covertThis instanceof BigNumber ? covertThis : BigNumber.from(covertThis);
return utils.hexZeroPad(utils.hexlify(amount), padding);
};
/**
* Pads the data
* based on ethers.utils.hexZeroPad
*
* @category Helpers
* @param covertThis - data to convert
* @param padding - padding
* @returns {string}
*/
export const addPadding = (covertThis: string | number, padding: number): string => {
return utils.hexZeroPad(`0x${covertThis}`, padding);
};
/**
* Constructs the deposit data for an EVM-Substrate bridge transaction.
*
* @example
* // Amount of tokens:
* const amount = '1';
* // EVM address
* const evmAddress = '0x1234567890123456789012345678901234567890';
* // Decimals of the token
* const decimals = 18;
* createERCDepositData(amount, evmAddress, decimals);
*
* @example
* import { decodeAddress } from '@polkadot/util-crypto';
* // Substrate MultiLocation
* // Decode address to bytes of public key
* const addressPublicKeyInBytes = decodeAddress(
* '5CDQJk6kxvBcjauhrogUc9B8vhbdXhRscp1tGEUmniryF1Vt',
* );
* // Convert bytes of public key to hex string
* const addressPublicKeyHexString = ethers.utils.hexlify(addressPublicKeyInBytes);
* // console.log(addressPublicKeyHexString) => "0x06a220edf5f82b84fc5f9270f8a30a17636bf29c05a5c16279405ca20918aa39"
* const multiLocation = JSON.stringify({
* parents: 0,
* interior: {
* X1: {
* AccountId32: {
* network: { any: null },
* id: addressPublicKeyHexString,
* },
* },
* },
* })
* // Amount of tokens:
* const amount = '2';
* createERCDepositData(amount, multiLocation);
*
* @param {string} tokenAmount - The amount of tokens to be transferred.
* @param {string} recipientAddress - The address of the recipient.
* @param {number} [decimals=18] - The number of decimals of the token.
* @returns {string} The deposit data as hex string
*/
export const createERCDepositData = (
tokenAmount: string,
recipientAddress: string,
decimals = 18,
): string => {
const convertedAmount = utils.parseUnits(tokenAmount, decimals);
const recipientAddressInBytes = getRecipientAddressInBytes(recipientAddress);
const depositDataBytes = constructMainDepositData(convertedAmount, recipientAddressInBytes);
const depositData = utils.hexlify(depositDataBytes);
return depositData;
};
/**
* Converts a recipient address to a Uint8Array of bytes.
*
* @param {string} recipientAddress - The recipient address, either as a string (EVM address) or a JSON object (Substrate multilocation).
* @returns {Uint8Array} The recipient address as a Uint8Array of bytes
*/
export const getRecipientAddressInBytes = (recipientAddress: string): Uint8Array => {
if (utils.isAddress(recipientAddress)) {
// EVM address
return utils.arrayify(recipientAddress);
}
// Substrate multilocation
return registry.createType('MultiLocation', JSON.parse(recipientAddress)).toU8a();
};
/**
* Constructs the main deposit data for a given token and recipient.
*
* @category Helpers
* @param {BigNumber} tokenStats - The amount of ERC20 tokens or the token ID of ERC721 tokens.
* @param {Uint8Array} destRecipient - The recipient address in bytes array
* @returns {Uint8Array} The main deposit data in bytes array
*/
export const constructMainDepositData = (
tokenStats: BigNumber,
destRecipient: Uint8Array,
): Uint8Array => {
const data: Uint8Array = utils.concat([
utils.hexZeroPad(tokenStats.toHexString(), 32), // Amount (ERC20) or Token Id (ERC721)
utils.hexZeroPad(BigNumber.from(destRecipient.length).toHexString(), 32), // length of recipient
destRecipient, // Recipient
]);
return data;
};
/**
* Creates depositData for permissioned generic handler
*
* @category Helpers
* @param hexMetaData
* @returns {string}
*/
export const createPermissionedGenericDepositData = (hexMetaData: string): string => {
const hexMetaDataLength = hexMetaData.substr(2).length / 2;
return '0x' + toHex(hexMetaDataLength, 32).substr(2) + hexMetaData.substr(2);
};
/**
* Creates the data for permissionless generic handler
*
* @category Helpers
* @param executeFunctionSignature - execution function signature
* @param executeContractAddress - execution contract address
* @param maxFee - max fee defined
* @param depositor - address of depositor on destination chain
* @param executionData - the data to pass as parameter of the function being called on destination chain
* @param depositorCheck - true if you want to check depositor
* @returns {string}
*/
export const createPermissionlessGenericDepositData = (
executeFunctionSignature: string,
executeContractAddress: string,
maxFee: string,
depositor: string,
executionData: string,
depositorCheck: boolean = true,
): string => {
if (depositorCheck) {
// if "depositorCheck" is true -> append depositor address for destination chain check
executionData = executionData.concat(toHex(depositor, 32).substr(2));
}
return (
'0x' +
toHex(maxFee, 32).substr(2) + // uint256
toHex(executeFunctionSignature.substr(2).length / 2, 2).substr(2) + // uint16
executeFunctionSignature.substr(2) + // bytes
toHex(executeContractAddress.substr(2).length / 2, 1).substr(2) + // uint8
executeContractAddress.substr(2) + // bytes
toHex(32, 1).substr(2) + // uint8
toHex(depositor, 32).substr(2) + // bytes32
executionData.substr(2)
) // bytes
.toLowerCase();
};
/**
* Gets the number of decimals for an ERC20 token.
*
* @category Helpers
* @param {ERC20} tokenInstance - An instance of an ERC20 token.
* @returns {Promise<number>} - A promise that resolves with the number of decimals for the token.
* @throws Error if the input token instance is not an ERC20 token.
*/
export async function getTokenDecimals(tokenInstance: ERC20): Promise<number> {
if (isERC20(tokenInstance)) {
return await tokenInstance.decimals();
} else {
throw new Error('Token instance is not ERC20');
}
}
/**
* Type guard function that determines if a given object is an instance of the ERC20 interface.
*
* @category Helpers
* @param {ERC20} tokenInstance - The object to be checked.
* @returns {boolean} - Returns `true` if the object is an instance of ERC20, `false` otherwise.
*/
export function isERC20(tokenInstance: ERC20): tokenInstance is ERC20 {
return 'decimals' in tokenInstance;
}
/**
* Checks if a given value is a number within the range of 0 and 255.
*
* @category Helpers
* @param {unknown} value - The value to check.
* @returns {boolean} - `true` if the number is within the range of 0 and 255, otherwise `false`.
*/
export const isUint8 = (value: unknown): boolean => {
const bn = BigNumber.from(value);
return bn.gte(0) && bn.lte(255);
};
/**
* Check the fee data of the provider and returns the gas price if the node is not EIP1559
*
* @category Helpers
* @param provider - JsonRpcProvider | Web3Provider
* @returns {Promise<BigNumber | boolean>}
*/
export async function isEIP1559MaxFeePerGas(provider: providers.Provider): Promise<BigNumber> {
try {
const feeData = await provider.getFeeData();
const { gasPrice } = feeData;
return gasPrice as BigNumber;
} catch (error) {
console.error('error getting EIP 1559', error);
return Promise.reject(error);
}
}