-
Notifications
You must be signed in to change notification settings - Fork 1
/
swap.ts
80 lines (71 loc) · 1.72 KB
/
swap.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
import {FinalExecutionOutcome} from 'near-api-js/lib/providers';
import {isNear} from '@spinfi/shared';
import {swapNear} from './swapNear';
import {swapFt} from './swapFt';
import {createUnit} from '../../utils/createUnit';
/**
* @category swap
*/
export type SwapResponse = FinalExecutionOutcome | void;
/**
* @category swap
*/
export interface SwapRequest {
/**
* Market identifier to exchange
*/
marketId: number;
/**
* Token Address From
*/
tokenAddress: string;
/**
* Market order stop price
*/
price: BigInt;
/**
* Amount in decimal
*/
amount: BigInt;
}
/**
* @category swap
*/
export interface SwapConfig {
/**
* Gas in decimal
*/
gas?: BigInt;
/**
* Attached Deposit in decimal
*/
deposit?: BigInt;
}
export const swap = createUnit<SwapRequest, SwapConfig>(({paramsify, methodify}) => {
const createGetParams = paramsify((config) => {
const getSwapNearParams = swapNear.createGetParams(config);
const getSwapFtParams = swapFt.createGetParams(config);
return async (request, unitConfig) => {
if (isNear(request.tokenAddress)) {
return await getSwapNearParams(request, unitConfig);
} else {
return await getSwapFtParams(request, unitConfig);
}
};
});
const createMethod = methodify((config) => {
const swapNearMethod = swapNear.createMethod(config);
const swapFtMethod = swapFt.createMethod(config);
return async (request, unitConfig): Promise<SwapResponse> => {
if (isNear(request.tokenAddress)) {
return await swapNearMethod(request, unitConfig);
} else {
return await swapFtMethod(request, unitConfig);
}
};
});
return {
createGetParams,
createMethod,
};
});