-
Notifications
You must be signed in to change notification settings - Fork 106
/
delta.ts
127 lines (107 loc) · 3.68 KB
/
delta.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* eslint-disable @typescript-eslint/no-unused-vars */
import axios from 'axios';
import { ethers, Wallet } from 'ethersV5';
import {
constructPartialSDK,
constructEthersContractCaller,
constructAxiosFetcher,
constructAllDeltaOrdersHandlers,
} from '..';
const fetcher = constructAxiosFetcher(axios);
const provider = ethers.getDefaultProvider(1);
const signer = Wallet.createRandom().connect(provider);
const account = signer.address;
const contractCaller = constructEthersContractCaller({
ethersProviderOrSigner: provider,
EthersContract: ethers.Contract,
});
// type AdaptersFunctions & ApproveTokenFunctions<ethers.ContractTransaction>
const deltaSDK = constructPartialSDK(
{
chainId: 1,
fetcher,
contractCaller,
},
constructAllDeltaOrdersHandlers
);
const DAI_TOKEN = '0x6b175474e89094c44da98b954eedeac495271d0f';
const PSP_TOKEN = '0xcafe001067cdef266afb7eb5a286dcfd277f3de5';
async function simpleDeltaFlow() {
const amount = '1000000000000'; // wei
const deltaPrice = await deltaSDK.getDeltaPrice({
srcToken: DAI_TOKEN,
destToken: PSP_TOKEN,
amount,
userAddress: account,
srcDecimals: 18,
destDecimals: 18,
// partner: "..." // if available
});
const DeltaContract = await deltaSDK.getDeltaContract();
// or sign a Permit1 or Permit2 TransferFrom for DeltaContract
const tx = await deltaSDK.approveTokenForDelta(amount, DAI_TOKEN);
await tx.wait();
const slippagePercent = 0.5;
const destAmountAfterSlippage = BigInt(
// get rid of exponential notation
+(+deltaPrice.destAmount * (1 - slippagePercent / 100)).toFixed(0)
// get rid of decimals
).toString(10);
const deltaAuction = await deltaSDK.submitDeltaOrder({
deltaPrice,
owner: account,
// beneficiary: anotherAccount, // if need to send destToken to another account
// permit: "0x1234...", // if signed a Permit1 or Permit2 TransferFrom for DeltaContract
srcToken: DAI_TOKEN,
destToken: PSP_TOKEN,
srcAmount: amount,
destAmount: destAmountAfterSlippage, // minimum acceptable destAmount
});
// poll if necessary
const auction = await deltaSDK.getDeltaOrderById(deltaAuction.id);
if (auction?.status === 'EXECUTED') {
console.log('Auction was executed');
}
}
async function manualDeltaFlow() {
const amount = '1000000000000'; // wei
const deltaPrice = await deltaSDK.getDeltaPrice({
srcToken: DAI_TOKEN,
destToken: PSP_TOKEN,
amount,
userAddress: account,
srcDecimals: 18,
destDecimals: 18,
// partner: "..." // if available
});
const DeltaContract = await deltaSDK.getDeltaContract();
// or sign a Permit1 or Permit2 TransferFrom for DeltaContract
const tx = await deltaSDK.approveTokenForDelta(amount, DAI_TOKEN);
await tx.wait();
const slippagePercent = 0.5;
const destAmountAfterSlippage = (
+deltaPrice.destAmount *
(1 - slippagePercent / 100)
).toString(10);
const signableOrderData = await deltaSDK.buildDeltaOrder({
deltaPrice,
owner: account,
// beneficiary: anotherAccount, // if need to send destToken to another account
// permit: "0x1234...", // if signed a Permit1 or Permit2 TransferFrom for DeltaContract
srcToken: DAI_TOKEN,
destToken: PSP_TOKEN,
srcAmount: amount,
destAmount: destAmountAfterSlippage, // minimum acceptable destAmount
});
const signature = await deltaSDK.signDeltaOrder(signableOrderData);
const deltaAuction = await deltaSDK.postDeltaOrder({
// partner: "..." // if available
order: signableOrderData.data,
signature,
});
// poll if necessary
const auction = await deltaSDK.getDeltaOrderById(deltaAuction.id);
if (auction?.status === 'EXECUTED') {
console.log('Auction was executed');
}
}