forked from immutable/imx-core-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
burn.ts
75 lines (66 loc) · 2.32 KB
/
burn.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
import { Signer } from '@ethersproject/abstract-signer';
import {
TransfersApi,
CreateTransferResponseV1,
TransfersApiGetTransferRequest,
} from '../api';
import { serializeSignature, sign, signRaw } from '../utils';
import { BurnAddress } from './constants';
import { GetSignableBurnRequest } from './types';
import { StarkWallet } from '../types';
export async function burnWorkflow(
signer: Signer,
starkWallet: StarkWallet,
request: GetSignableBurnRequest,
transfersApi: TransfersApi,
): Promise<CreateTransferResponseV1> {
// Get signable response for transfer
const signableResult = await transfersApi.getSignableTransferV1({
getSignableTransferRequest: {
sender: request.sender,
token: request.token,
amount: request.amount,
receiver: BurnAddress.BurnEthAddress,
},
});
const { signable_message: signableMessage, payload_hash: payloadHash } =
signableResult.data;
// Sign message with L1 credentials
const ethSignature = await signRaw(signableMessage, signer);
// Sign hash with L2 credentials
const starkSignature = serializeSignature(
sign(starkWallet.starkKeyPair, payloadHash),
);
// Obtain Ethereum Address from signer
const ethAddress = (await signer.getAddress());
// Assemble transfer params
const transferSigningParams = {
sender_stark_key: signableResult.data.sender_stark_key!,
sender_vault_id: signableResult.data.sender_vault_id,
receiver_stark_key: signableResult.data.receiver_stark_key,
receiver_vault_id: signableResult.data.receiver_vault_id,
asset_id: signableResult.data.asset_id,
amount: signableResult.data.amount,
nonce: signableResult.data.nonce,
expiration_timestamp: signableResult.data.expiration_timestamp,
stark_signature: starkSignature,
};
// create transfer
const response = await transfersApi.createTransferV1({
createTransferRequest: transferSigningParams,
xImxEthAddress: ethAddress,
xImxEthSignature: ethSignature,
});
return {
sent_signature: response?.data.sent_signature,
status: response?.data.status?.toString(),
time: response?.data.time,
transfer_id: response?.data.transfer_id,
};
}
export async function getBurnWorkflow(
request: TransfersApiGetTransferRequest,
transfersApi: TransfersApi,
) {
return await transfersApi.getTransfer({ id: request.id });
}