-
Notifications
You must be signed in to change notification settings - Fork 1
/
transferFt.ts
101 lines (87 loc) · 2.16 KB
/
transferFt.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
import {invariant} from '@spinfi/shared';
import {FinalExecutionOutcome} from 'near-api-js/lib/providers';
import {selectorErrorMessage} from './messages';
import {createUnit} from '../utils/createUnit';
/**
* @category transferFt
*/
export type TransferFtResponse = FinalExecutionOutcome | void;
/**
* @category transferFt
*/
export interface TransferFtRequest {
/**
* Receiver ID
*/
receiverId: string;
/**
* Token Address
*/
tokenAddress: string;
/**
* Amount in decimal
*/
amount: BigInt;
/**
* Message
*/
message?: string;
}
/**
* @category transferFt
*/
export interface TransferFtConfig {
/**
* Gas in decimal
*/
gas?: BigInt;
/**
* Attached Deposit in decimal
*/
deposit?: BigInt;
}
const GAS = '300000000000000';
const DEPOSIT = '1';
const METHOD_NAME = 'ft_transfer_call';
export const transferFt = createUnit<TransferFtRequest, TransferFtConfig>(
({paramsify, methodify}) => {
const createGetParams = paramsify(() => {
return async (request, unitConfig) => {
return {
receiverId: request.tokenAddress,
actions: [
{
type: 'FunctionCall',
params: {
methodName: METHOD_NAME,
args: {
receiver_id: request.receiverId,
amount: request.amount.toString(),
msg: request.message ?? '',
},
gas: unitConfig?.gas?.toString() ?? GAS,
deposit: unitConfig?.deposit?.toString() ?? DEPOSIT,
},
},
],
};
};
});
const createMethod = methodify((config) => {
const getParams = createGetParams(config);
return async (request, unitConfig): Promise<TransferFtResponse> => {
invariant(config.selector, selectorErrorMessage('transferFt'));
const wallet = await config.selector.wallet();
const params = await getParams(request, unitConfig);
if (!params) {
return;
}
return await wallet.signAndSendTransaction(params);
};
});
return {
createGetParams,
createMethod,
};
},
);