@reyaxyz/sdk 0.145.2 → 0.146.0
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/README.md +1 -1
- package/dist/services/lp/encode.js +31 -1
- package/dist/services/lp/encode.js.map +1 -1
- package/dist/services/lp/index.js +2 -0
- package/dist/services/lp/index.js.map +1 -1
- package/dist/services/lp/stakeReya.js +75 -0
- package/dist/services/lp/stakeReya.js.map +1 -0
- package/dist/services/lp/types.js.map +1 -1
- package/dist/services/lp/unstakeStakedReya.js +75 -0
- package/dist/services/lp/unstakeStakedReya.js.map +1 -0
- package/dist/services/margin-accounts/bridgeAndDepositExistingMA.js +2 -6
- package/dist/services/margin-accounts/bridgeAndDepositExistingMA.js.map +1 -1
- package/dist/services/margin-accounts/encode.js +94 -2
- package/dist/services/margin-accounts/encode.js.map +1 -1
- package/dist/services/margin-accounts/index.js +2 -0
- package/dist/services/margin-accounts/index.js.map +1 -1
- package/dist/services/margin-accounts/oftBridgeAndDepositExistingMA.js +102 -0
- package/dist/services/margin-accounts/oftBridgeAndDepositExistingMA.js.map +1 -0
- package/dist/services/margin-accounts/oftWithdrawMAAndBridge.js +89 -0
- package/dist/services/margin-accounts/oftWithdrawMAAndBridge.js.map +1 -0
- package/dist/services/margin-accounts/types.js.map +1 -1
- package/dist/types/services/lp/encode.d.ts +2 -0
- package/dist/types/services/lp/encode.d.ts.map +1 -1
- package/dist/types/services/lp/index.d.ts +2 -0
- package/dist/types/services/lp/index.d.ts.map +1 -1
- package/dist/types/services/lp/stakeReya.d.ts +3 -0
- package/dist/types/services/lp/stakeReya.d.ts.map +1 -0
- package/dist/types/services/lp/types.d.ts +22 -0
- package/dist/types/services/lp/types.d.ts.map +1 -1
- package/dist/types/services/lp/unstakeStakedReya.d.ts +3 -0
- package/dist/types/services/lp/unstakeStakedReya.d.ts.map +1 -0
- package/dist/types/services/margin-accounts/bridgeAndDepositExistingMA.d.ts.map +1 -1
- package/dist/types/services/margin-accounts/encode.d.ts +13 -2
- package/dist/types/services/margin-accounts/encode.d.ts.map +1 -1
- package/dist/types/services/margin-accounts/index.d.ts +2 -0
- package/dist/types/services/margin-accounts/index.d.ts.map +1 -1
- package/dist/types/services/margin-accounts/oftBridgeAndDepositExistingMA.d.ts +3 -0
- package/dist/types/services/margin-accounts/oftBridgeAndDepositExistingMA.d.ts.map +1 -0
- package/dist/types/services/margin-accounts/oftWithdrawMAAndBridge.d.ts +3 -0
- package/dist/types/services/margin-accounts/oftWithdrawMAAndBridge.d.ts.map +1 -0
- package/dist/types/services/margin-accounts/types.d.ts +12 -0
- package/dist/types/services/margin-accounts/types.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/services/lp/encode.ts +40 -0
- package/src/services/lp/index.ts +2 -0
- package/src/services/lp/stakeReya.ts +66 -0
- package/src/services/lp/types.ts +26 -0
- package/src/services/lp/unstakeStakedReya.ts +66 -0
- package/src/services/margin-accounts/bridgeAndDepositExistingMA.ts +0 -10
- package/src/services/margin-accounts/encode.ts +151 -2
- package/src/services/margin-accounts/index.ts +2 -0
- package/src/services/margin-accounts/oftBridgeAndDepositExistingMA.ts +112 -0
- package/src/services/margin-accounts/oftWithdrawMAAndBridge.ts +93 -0
- package/src/services/margin-accounts/types.ts +16 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { UnstakeStakedReyaParams, UnstakeStakedReyaResult } from './types';
|
|
2
|
+
import { getReyaNetwork } from '../../utils/network';
|
|
3
|
+
import {
|
|
4
|
+
scale,
|
|
5
|
+
getTokenInfoByName,
|
|
6
|
+
ContractType,
|
|
7
|
+
getAddress,
|
|
8
|
+
signCoreCommands,
|
|
9
|
+
getCurrentTimestampInSeconds,
|
|
10
|
+
CORE_DEADLINE_IN_SECONDS,
|
|
11
|
+
MultiAction,
|
|
12
|
+
} from '@reyaxyz/common';
|
|
13
|
+
import { encodeSingleWithdraw } from '../encode';
|
|
14
|
+
import { encodeUnstakeStakedReya } from './encode';
|
|
15
|
+
import { signAndBroadcastTransaction } from '../signAndBroadcastTransaction';
|
|
16
|
+
import { ethers } from 'ethers';
|
|
17
|
+
|
|
18
|
+
export const unstakeStakedReya = async (
|
|
19
|
+
params: UnstakeStakedReyaParams,
|
|
20
|
+
): Promise<UnstakeStakedReyaResult> => {
|
|
21
|
+
const chainId = getReyaNetwork();
|
|
22
|
+
|
|
23
|
+
const sreyaTokenInfo = getTokenInfoByName('SREYA', chainId);
|
|
24
|
+
const reyaTokenInfo = getTokenInfoByName('REYA', chainId);
|
|
25
|
+
const shareAmount = scale(sreyaTokenInfo.decimals)(params.shareAmount);
|
|
26
|
+
const minAssetAmount = params.minAssetAmount
|
|
27
|
+
? scale(reyaTokenInfo.decimals)(params.minAssetAmount)
|
|
28
|
+
: BigInt(0);
|
|
29
|
+
|
|
30
|
+
const multiAction = new MultiAction();
|
|
31
|
+
encodeSingleWithdraw(sreyaTokenInfo.address, shareAmount, multiAction);
|
|
32
|
+
|
|
33
|
+
const { signature: withdrawSig } = await signCoreCommands(
|
|
34
|
+
params.signer,
|
|
35
|
+
chainId,
|
|
36
|
+
getAddress(chainId, ContractType.PERIPHERY_PROXY),
|
|
37
|
+
params.marginAccountId,
|
|
38
|
+
multiAction.commands,
|
|
39
|
+
params.owner.coreSigNonce + 1,
|
|
40
|
+
getCurrentTimestampInSeconds() + CORE_DEADLINE_IN_SECONDS,
|
|
41
|
+
ethers.AbiCoder.defaultAbiCoder().encode(
|
|
42
|
+
['string', 'uint256'],
|
|
43
|
+
['unstakeStakedReya', minAssetAmount],
|
|
44
|
+
),
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
const { calldata: data } = encodeUnstakeStakedReya(
|
|
48
|
+
params.marginAccountId,
|
|
49
|
+
shareAmount,
|
|
50
|
+
minAssetAmount,
|
|
51
|
+
withdrawSig,
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
const result = await signAndBroadcastTransaction(
|
|
55
|
+
data,
|
|
56
|
+
chainId,
|
|
57
|
+
ContractType.PERIPHERY_PROXY,
|
|
58
|
+
{},
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
transactionHash: result?.txHash || null,
|
|
63
|
+
coreSigNonce:
|
|
64
|
+
result?.coreSigNonce != null ? Number(result.coreSigNonce) : null,
|
|
65
|
+
};
|
|
66
|
+
};
|
|
@@ -14,8 +14,6 @@ import {
|
|
|
14
14
|
BridgeAndDepositExistingMAParams,
|
|
15
15
|
BridgeAndDepositExistingMAResult,
|
|
16
16
|
} from './types';
|
|
17
|
-
import { encodeSingleDeposit } from '../encode';
|
|
18
|
-
import { MultiAction } from '@reyaxyz/common';
|
|
19
17
|
import { getTransactionModuleClient } from '../../config';
|
|
20
18
|
|
|
21
19
|
export const bridgeAndDepositExistingMA = async (
|
|
@@ -38,13 +36,6 @@ export const bridgeAndDepositExistingMA = async (
|
|
|
38
36
|
? getTokenInfoByName('RUSD', reyaChainId)
|
|
39
37
|
: reyaPeripheryTokenInfo;
|
|
40
38
|
|
|
41
|
-
const multiAction = new MultiAction();
|
|
42
|
-
encodeSingleDeposit(
|
|
43
|
-
reyaCoreTokenInfo.address,
|
|
44
|
-
scale(reyaPeripheryTokenInfo.decimals)(params.amount),
|
|
45
|
-
multiAction,
|
|
46
|
-
);
|
|
47
|
-
|
|
48
39
|
const peripheryAddress = getAddress(
|
|
49
40
|
reyaChainId,
|
|
50
41
|
ContractType.PERIPHERY_PROXY,
|
|
@@ -63,7 +54,6 @@ export const bridgeAndDepositExistingMA = async (
|
|
|
63
54
|
params.socketDepositFees,
|
|
64
55
|
params.marginAccountId,
|
|
65
56
|
reyaPeripheryTokenInfo.address,
|
|
66
|
-
scale(reyaPeripheryTokenInfo.decimals)(params.amount),
|
|
67
57
|
scale(moneyInOutTokenInfo.decimals)(params.amount),
|
|
68
58
|
);
|
|
69
59
|
|
|
@@ -1,8 +1,14 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
MethodParameters,
|
|
3
|
+
MultiAction,
|
|
4
|
+
OFTSendParam,
|
|
5
|
+
OFTMessagingFee,
|
|
6
|
+
} from '@reyaxyz/common';
|
|
2
7
|
import { Interface } from 'ethers';
|
|
3
8
|
import { encodeSingleTransferMargin } from '../encode';
|
|
4
9
|
import {
|
|
5
10
|
CoreAbi as abi,
|
|
11
|
+
OFTAbi as OFTAbi,
|
|
6
12
|
PeripheryAbi,
|
|
7
13
|
Socket_VaultWithPayloadAbi,
|
|
8
14
|
CORE_DEADLINE_IN_SECONDS,
|
|
@@ -15,6 +21,12 @@ import { getCurrentTimestampInSeconds } from '@reyaxyz/common';
|
|
|
15
21
|
import { Signer } from 'ethers';
|
|
16
22
|
import { ethers } from 'ethers';
|
|
17
23
|
|
|
24
|
+
enum LzComposerOperationType {
|
|
25
|
+
NONE = 0,
|
|
26
|
+
DEPOSIT_SPOT_ACCOUNT = 1,
|
|
27
|
+
DEPOSIT_ACCOUNT = 2,
|
|
28
|
+
}
|
|
29
|
+
|
|
18
30
|
export const encodeCreateAccountCall = (
|
|
19
31
|
accountOwner: string,
|
|
20
32
|
): MethodParameters => {
|
|
@@ -105,7 +117,6 @@ export const encodeBridgeAndDepositExistingMA = (
|
|
|
105
117
|
socketFees: bigint,
|
|
106
118
|
accountId: number,
|
|
107
119
|
peripheryTokenAddress: string,
|
|
108
|
-
peripheryTokenAmount: bigint,
|
|
109
120
|
moneyInOutTokenAmount: bigint,
|
|
110
121
|
): MethodParameters => {
|
|
111
122
|
const PERIPHERY_INTERFACE = new Interface(PeripheryAbi);
|
|
@@ -131,3 +142,141 @@ export const encodeBridgeAndDepositExistingMA = (
|
|
|
131
142
|
const calldata = INTERFACE.encodeFunctionData(functionSignature, parameters);
|
|
132
143
|
return { calldata: calldata, value: socketFees.toString(10) };
|
|
133
144
|
};
|
|
145
|
+
|
|
146
|
+
export const encodeWithdrawMALZ = (
|
|
147
|
+
accountId: number,
|
|
148
|
+
corePeripheryTokenAddress: string,
|
|
149
|
+
corePeripheryTokenAmount: bigint,
|
|
150
|
+
sig: EIP712Signature,
|
|
151
|
+
dstEid: number,
|
|
152
|
+
receiver: string,
|
|
153
|
+
): MethodParameters => {
|
|
154
|
+
const functionSignature = 'withdrawMALZ';
|
|
155
|
+
const parameters = [
|
|
156
|
+
{
|
|
157
|
+
accountId,
|
|
158
|
+
token: corePeripheryTokenAddress,
|
|
159
|
+
tokenAmount: corePeripheryTokenAmount,
|
|
160
|
+
sig,
|
|
161
|
+
dstEid,
|
|
162
|
+
receiver,
|
|
163
|
+
},
|
|
164
|
+
];
|
|
165
|
+
const INTERFACE = new Interface(PeripheryAbi);
|
|
166
|
+
const calldata = INTERFACE.encodeFunctionData(functionSignature, parameters);
|
|
167
|
+
return { calldata: calldata, value: BigInt(0).toString(10) };
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
// Gas for lzReceive on destination: token credit + endpoint.sendCompose() queuing
|
|
171
|
+
const LZ_RECEIVE_GAS = 1_000_000;
|
|
172
|
+
// Gas estimate for lzCompose execution on destination chain
|
|
173
|
+
// Covers: abi.decode + createOrGetSpotAccount/depositExistingMA in OftModule.lzCompose
|
|
174
|
+
const LZ_COMPOSE_GAS = 1_000_000;
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Encode LZ v3 extraOptions with both lzReceive and lzCompose options.
|
|
178
|
+
* Required for msgType=2 (SEND_AND_CALL) since enforced options only cover msgType=1.
|
|
179
|
+
*/
|
|
180
|
+
export const encodeLzSendAndComposeExtraOptions = (
|
|
181
|
+
receiveGas: number,
|
|
182
|
+
composeGas: number,
|
|
183
|
+
): string => {
|
|
184
|
+
// Options v3 layout:
|
|
185
|
+
// TYPE_3 header: uint16(3)
|
|
186
|
+
// --- lzReceive option ---
|
|
187
|
+
// WORKER_ID: uint8(1)
|
|
188
|
+
// length: uint16(17) = OPTION_TYPE(1) + GAS(16)
|
|
189
|
+
// OPTION_TYPE_LZRECEIVE: uint8(1)
|
|
190
|
+
// gas: uint128
|
|
191
|
+
// --- lzCompose option ---
|
|
192
|
+
// WORKER_ID: uint8(1)
|
|
193
|
+
// length: uint16(19) = OPTION_TYPE(1) + INDEX(2) + GAS(16)
|
|
194
|
+
// OPTION_TYPE_LZCOMPOSE: uint8(3)
|
|
195
|
+
// index: uint16(0)
|
|
196
|
+
// gas: uint128
|
|
197
|
+
return ethers.solidityPacked(
|
|
198
|
+
[
|
|
199
|
+
'uint16',
|
|
200
|
+
'uint8',
|
|
201
|
+
'uint16',
|
|
202
|
+
'uint8',
|
|
203
|
+
'uint128',
|
|
204
|
+
'uint8',
|
|
205
|
+
'uint16',
|
|
206
|
+
'uint8',
|
|
207
|
+
'uint16',
|
|
208
|
+
'uint128',
|
|
209
|
+
],
|
|
210
|
+
[3, 1, 17, 1, receiveGas, 1, 19, 3, 0, composeGas],
|
|
211
|
+
);
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
export const buildOFTBridgeAndDepositExistingMASendParam = (
|
|
215
|
+
dstEid: number,
|
|
216
|
+
receiverAddress: string,
|
|
217
|
+
accountId: number,
|
|
218
|
+
peripheryTokenAddress: string,
|
|
219
|
+
amountLD: bigint,
|
|
220
|
+
minAmountLD: bigint,
|
|
221
|
+
): { sendParam: OFTSendParam; composeMsg: string } => {
|
|
222
|
+
const opType = LzComposerOperationType.DEPOSIT_ACCOUNT;
|
|
223
|
+
const opData = ethers.AbiCoder.defaultAbiCoder().encode(
|
|
224
|
+
['uint128', 'address'],
|
|
225
|
+
[accountId, peripheryTokenAddress],
|
|
226
|
+
);
|
|
227
|
+
|
|
228
|
+
const composeMsg = ethers.AbiCoder.defaultAbiCoder().encode(
|
|
229
|
+
['uint8', 'bytes'],
|
|
230
|
+
[opType, opData],
|
|
231
|
+
);
|
|
232
|
+
|
|
233
|
+
const receiverBytes32 = ethers.zeroPadValue(receiverAddress, 32);
|
|
234
|
+
const extraOptions = encodeLzSendAndComposeExtraOptions(
|
|
235
|
+
LZ_RECEIVE_GAS,
|
|
236
|
+
LZ_COMPOSE_GAS,
|
|
237
|
+
);
|
|
238
|
+
|
|
239
|
+
const sendParam: OFTSendParam = {
|
|
240
|
+
dstEid,
|
|
241
|
+
to: receiverBytes32,
|
|
242
|
+
amountLD,
|
|
243
|
+
minAmountLD,
|
|
244
|
+
extraOptions,
|
|
245
|
+
composeMsg,
|
|
246
|
+
oftCmd: '0x',
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
return { sendParam, composeMsg };
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
export const encodeOFTBridgeAndDepositExistingMA = (
|
|
253
|
+
dstEid: number,
|
|
254
|
+
receiverAddress: string,
|
|
255
|
+
accountId: number,
|
|
256
|
+
peripheryTokenAddress: string,
|
|
257
|
+
amountLD: bigint,
|
|
258
|
+
minAmountLD: bigint,
|
|
259
|
+
messagingFee: OFTMessagingFee,
|
|
260
|
+
callerAddress: string,
|
|
261
|
+
): MethodParameters => {
|
|
262
|
+
const { sendParam } = buildOFTBridgeAndDepositExistingMASendParam(
|
|
263
|
+
dstEid,
|
|
264
|
+
receiverAddress,
|
|
265
|
+
accountId,
|
|
266
|
+
peripheryTokenAddress,
|
|
267
|
+
amountLD,
|
|
268
|
+
minAmountLD,
|
|
269
|
+
);
|
|
270
|
+
|
|
271
|
+
const INTERFACE = new Interface(OFTAbi);
|
|
272
|
+
const calldata = INTERFACE.encodeFunctionData('send', [
|
|
273
|
+
sendParam,
|
|
274
|
+
messagingFee,
|
|
275
|
+
callerAddress,
|
|
276
|
+
]);
|
|
277
|
+
|
|
278
|
+
return {
|
|
279
|
+
calldata,
|
|
280
|
+
value: messagingFee.nativeFee.toString(10),
|
|
281
|
+
};
|
|
282
|
+
};
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export * from './types';
|
|
2
2
|
export * from './account';
|
|
3
3
|
export * from './bridgeAndDepositExistingMA';
|
|
4
|
+
export * from './oftBridgeAndDepositExistingMA';
|
|
5
|
+
export * from './oftWithdrawMAAndBridge';
|
|
4
6
|
export * from './transferMarginBetweenAccounts';
|
|
5
7
|
export * from './withdrawMAAndBridge';
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import {
|
|
2
|
+
scale,
|
|
3
|
+
MoneyInOutChainId,
|
|
4
|
+
getTokenInfoByAddress,
|
|
5
|
+
getTokenInfoByName,
|
|
6
|
+
getReyaNetworkFromMoneyInOutChainId,
|
|
7
|
+
ContractType,
|
|
8
|
+
getAddress,
|
|
9
|
+
executeTransaction,
|
|
10
|
+
getLzEndpointId,
|
|
11
|
+
OFTMessagingFee,
|
|
12
|
+
OFTAbi as OFTAbi,
|
|
13
|
+
} from '@reyaxyz/common';
|
|
14
|
+
import { ethers } from 'ethers';
|
|
15
|
+
import {
|
|
16
|
+
encodeOFTBridgeAndDepositExistingMA,
|
|
17
|
+
buildOFTBridgeAndDepositExistingMASendParam,
|
|
18
|
+
} from './encode';
|
|
19
|
+
import {
|
|
20
|
+
OFTBridgeAndDepositExistingMAParams,
|
|
21
|
+
OFTBridgeAndDepositExistingMAResult,
|
|
22
|
+
} from './types';
|
|
23
|
+
|
|
24
|
+
export const oftBridgeAndDepositExistingMA = async (
|
|
25
|
+
params: OFTBridgeAndDepositExistingMAParams,
|
|
26
|
+
): Promise<OFTBridgeAndDepositExistingMAResult> => {
|
|
27
|
+
const moneyInOutChainId: MoneyInOutChainId =
|
|
28
|
+
params.moneyInOutChainId ??
|
|
29
|
+
Number((await params.signer.provider?.getNetwork())?.chainId);
|
|
30
|
+
const reyaChainId = getReyaNetworkFromMoneyInOutChainId(moneyInOutChainId);
|
|
31
|
+
|
|
32
|
+
const moneyInOutTokenInfo = getTokenInfoByAddress(
|
|
33
|
+
params.tokenAddress,
|
|
34
|
+
moneyInOutChainId,
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
if (!moneyInOutTokenInfo.isOFT) {
|
|
38
|
+
throw new Error(
|
|
39
|
+
`Token ${moneyInOutTokenInfo.name} at ${params.tokenAddress} is not an OFT token on chain ${moneyInOutChainId}`,
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const reyaPeripheryTokenInfo = getTokenInfoByName(
|
|
44
|
+
moneyInOutTokenInfo.name,
|
|
45
|
+
reyaChainId,
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
const peripheryAddress = getAddress(
|
|
49
|
+
reyaChainId,
|
|
50
|
+
ContractType.PERIPHERY_PROXY,
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
const dstEid = getLzEndpointId(reyaChainId);
|
|
54
|
+
const amountLD = scale(moneyInOutTokenInfo.decimals)(params.amount);
|
|
55
|
+
const minAmountLD = amountLD;
|
|
56
|
+
|
|
57
|
+
const { sendParam } = buildOFTBridgeAndDepositExistingMASendParam(
|
|
58
|
+
dstEid,
|
|
59
|
+
peripheryAddress,
|
|
60
|
+
params.marginAccountId,
|
|
61
|
+
reyaPeripheryTokenInfo.address,
|
|
62
|
+
amountLD,
|
|
63
|
+
minAmountLD,
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
const provider = params.signer.provider;
|
|
67
|
+
if (!provider) {
|
|
68
|
+
throw new Error('Signer must have a provider to quote OFT messaging fees');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const oftContract = new ethers.Contract(
|
|
72
|
+
moneyInOutTokenInfo.address,
|
|
73
|
+
OFTAbi,
|
|
74
|
+
provider,
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
const msgFee = await oftContract.quoteSend.staticCall(sendParam, false);
|
|
78
|
+
const messagingFee: OFTMessagingFee = {
|
|
79
|
+
nativeFee: msgFee.nativeFee,
|
|
80
|
+
lzTokenFee: msgFee.lzTokenFee,
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const callerAddress = await params.signer.getAddress();
|
|
84
|
+
const { calldata: data, value } = encodeOFTBridgeAndDepositExistingMA(
|
|
85
|
+
dstEid,
|
|
86
|
+
peripheryAddress,
|
|
87
|
+
params.marginAccountId,
|
|
88
|
+
reyaPeripheryTokenInfo.address,
|
|
89
|
+
amountLD,
|
|
90
|
+
minAmountLD,
|
|
91
|
+
messagingFee,
|
|
92
|
+
callerAddress,
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
const result = await executeTransaction(
|
|
96
|
+
params.signer,
|
|
97
|
+
data,
|
|
98
|
+
value,
|
|
99
|
+
moneyInOutChainId,
|
|
100
|
+
moneyInOutTokenInfo.address,
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
if (!result?.hash) {
|
|
104
|
+
return {
|
|
105
|
+
transactionHash: null,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
transactionHash: result?.hash,
|
|
111
|
+
};
|
|
112
|
+
};
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getCurrentTimestampInSeconds,
|
|
3
|
+
scale,
|
|
4
|
+
getTokenInfoByAddress,
|
|
5
|
+
getLzEndpointId,
|
|
6
|
+
} from '@reyaxyz/common';
|
|
7
|
+
import { ContractType, getAddress } from '@reyaxyz/common';
|
|
8
|
+
import { signAndBroadcastTransaction } from '../signAndBroadcastTransaction';
|
|
9
|
+
import { signCoreCommands } from '@reyaxyz/common';
|
|
10
|
+
import { encodeWithdrawMALZ } from './encode';
|
|
11
|
+
import {
|
|
12
|
+
OFTWithdrawMAAndBridgeParams,
|
|
13
|
+
OFTWithdrawMAAndBridgeResult,
|
|
14
|
+
} from './types';
|
|
15
|
+
import { BRIDGE_DEADLINE_IN_SECONDS } from '@reyaxyz/common';
|
|
16
|
+
import { encodeSingleWithdraw } from '../encode';
|
|
17
|
+
import { MultiAction } from '@reyaxyz/common';
|
|
18
|
+
import { ethers } from 'ethers';
|
|
19
|
+
import { getReyaNetwork } from '../../utils/network';
|
|
20
|
+
|
|
21
|
+
export const oftWithdrawMAAndBridge = async ({
|
|
22
|
+
signer,
|
|
23
|
+
moneyInOutChainId,
|
|
24
|
+
marginAccountId,
|
|
25
|
+
owner,
|
|
26
|
+
amount: unscaledAmount,
|
|
27
|
+
tokenAddress,
|
|
28
|
+
receiverAddress = owner.address,
|
|
29
|
+
}: OFTWithdrawMAAndBridgeParams): Promise<OFTWithdrawMAAndBridgeResult> => {
|
|
30
|
+
const reyaChainId = getReyaNetwork();
|
|
31
|
+
|
|
32
|
+
const corePeripheryTokenInfo = getTokenInfoByAddress(tokenAddress);
|
|
33
|
+
|
|
34
|
+
if (!corePeripheryTokenInfo.isOFT) {
|
|
35
|
+
throw new Error(
|
|
36
|
+
`Token ${corePeripheryTokenInfo.name} at ${tokenAddress} is not an OFT token`,
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const multiAction = new MultiAction();
|
|
41
|
+
encodeSingleWithdraw(
|
|
42
|
+
corePeripheryTokenInfo.address,
|
|
43
|
+
scale(corePeripheryTokenInfo.decimals)(unscaledAmount),
|
|
44
|
+
multiAction,
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
const dstEid = getLzEndpointId(moneyInOutChainId);
|
|
48
|
+
|
|
49
|
+
const { signature: eip712Signature } = await signCoreCommands(
|
|
50
|
+
signer,
|
|
51
|
+
reyaChainId,
|
|
52
|
+
getAddress(reyaChainId, ContractType.PERIPHERY_PROXY),
|
|
53
|
+
marginAccountId,
|
|
54
|
+
multiAction.commands,
|
|
55
|
+
owner.coreSigNonce + 1,
|
|
56
|
+
getCurrentTimestampInSeconds() + BRIDGE_DEADLINE_IN_SECONDS,
|
|
57
|
+
ethers.AbiCoder.defaultAbiCoder().encode(
|
|
58
|
+
['string', 'address', 'uint32'],
|
|
59
|
+
['withdrawMALZ', receiverAddress, dstEid],
|
|
60
|
+
),
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
const { calldata: data } = encodeWithdrawMALZ(
|
|
64
|
+
marginAccountId,
|
|
65
|
+
corePeripheryTokenInfo.address,
|
|
66
|
+
scale(corePeripheryTokenInfo.decimals)(unscaledAmount),
|
|
67
|
+
eip712Signature,
|
|
68
|
+
dstEid,
|
|
69
|
+
receiverAddress,
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
const result = await signAndBroadcastTransaction(
|
|
73
|
+
data,
|
|
74
|
+
reyaChainId,
|
|
75
|
+
ContractType.PERIPHERY_PROXY,
|
|
76
|
+
{
|
|
77
|
+
destinationType: 'account',
|
|
78
|
+
},
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
if (!result?.txHash) {
|
|
82
|
+
return {
|
|
83
|
+
transactionHash: null,
|
|
84
|
+
coreSigNonce: null,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
transactionHash: result?.txHash || null,
|
|
90
|
+
coreSigNonce:
|
|
91
|
+
result?.coreSigNonce != null ? Number(result.coreSigNonce) : null,
|
|
92
|
+
};
|
|
93
|
+
};
|
|
@@ -60,3 +60,19 @@ export type BridgeAndDepositExistingMAParams = {
|
|
|
60
60
|
export type BridgeAndDepositExistingMAResult = {
|
|
61
61
|
transactionHash: string | null;
|
|
62
62
|
};
|
|
63
|
+
|
|
64
|
+
export type OFTWithdrawMAAndBridgeParams = WithdrawMAAndBridgeParams;
|
|
65
|
+
|
|
66
|
+
export type OFTWithdrawMAAndBridgeResult = WithdrawMAAndBridgeParamsResult;
|
|
67
|
+
|
|
68
|
+
export type OFTBridgeAndDepositExistingMAParams = {
|
|
69
|
+
signer: Signer | JsonRpcSigner;
|
|
70
|
+
marginAccountId: number;
|
|
71
|
+
tokenAddress: TokenEntity['address'];
|
|
72
|
+
amount: number;
|
|
73
|
+
moneyInOutChainId?: MoneyInOutChainId;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export type OFTBridgeAndDepositExistingMAResult = {
|
|
77
|
+
transactionHash: string | null;
|
|
78
|
+
};
|