-
Notifications
You must be signed in to change notification settings - Fork 1
/
withdraw.ts
95 lines (80 loc) · 2.11 KB
/
withdraw.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
import {invariant} from '@spinfi/shared';
import {FinalExecutionOutcome} from 'near-api-js/lib/providers';
import {isNear} from '@spinfi/shared';
import {selectorErrorMessage} from './messages';
import {createUnit} from '../../utils/createUnit';
/**
* @category withdraw
*/
export type WithdrawResponse = FinalExecutionOutcome | void;
/**
* @category withdraw
*/
export interface WithdrawRequest {
/**
* Could be near.near for native NEAR withdrawal or any Fungible Token currency existing on contract market
*/
tokenAddress: string;
/**
* Transfer amount
*/
amount: BigInt;
}
/**
* @category withdraw
*/
export interface WithdrawConfig {
/**
* Gas in decimal
*/
gas?: BigInt;
/**
* Deposit in decimal
*/
deposit?: BigInt;
}
const GAS = '50000000000000';
const NEAR_DEPOSIT = '0';
const FT_DEPOSIT = '1';
const METHOD_NAME = 'withdraw';
export const withdraw = createUnit<WithdrawRequest, WithdrawConfig>(({paramsify, methodify}) => {
const createGetParams = paramsify(() => {
return async (request, unitConfig) => {
return {
actions: [
{
type: 'FunctionCall',
params: {
methodName: METHOD_NAME,
args: {
token: request.tokenAddress,
amount: request.amount.toString(),
},
gas: unitConfig?.gas?.toString() ?? GAS,
deposit:
unitConfig?.deposit?.toString() ?? isNear(request.tokenAddress)
? NEAR_DEPOSIT
: FT_DEPOSIT,
},
},
],
};
};
});
const createMethod = methodify((config) => {
const getParams = createGetParams(config);
return async (request, unitConfig): Promise<WithdrawResponse> => {
invariant(config.selector, selectorErrorMessage('withdraw'));
const wallet = await config.selector.wallet();
const params = await getParams(request, unitConfig);
if (!params) {
return;
}
return await wallet.signAndSendTransaction(params);
};
});
return {
createGetParams,
createMethod,
};
});