@velora-dex/sdk 9.3.6 → 9.4.1-dev.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/dist/examples/externalDelta.d.ts +2 -0
- package/dist/examples/externalDelta.d.ts.map +1 -0
- package/dist/examples/helpers/delta.d.ts +2 -2
- package/dist/examples/helpers/delta.d.ts.map +1 -1
- package/dist/index.d.ts +17 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/methods/delta/buildDeltaOrder.d.ts +4 -10
- package/dist/methods/delta/buildDeltaOrder.d.ts.map +1 -1
- package/dist/methods/delta/buildExternalDeltaOrder.d.ts +46 -0
- package/dist/methods/delta/buildExternalDeltaOrder.d.ts.map +1 -0
- package/dist/methods/delta/buildTWAPDeltaOrder.d.ts +65 -0
- package/dist/methods/delta/buildTWAPDeltaOrder.d.ts.map +1 -0
- package/dist/methods/delta/getDeltaOrders.d.ts +14 -6
- package/dist/methods/delta/getDeltaOrders.d.ts.map +1 -1
- package/dist/methods/delta/helpers/abi.d.ts +16 -0
- package/dist/methods/delta/helpers/abi.d.ts.map +1 -0
- package/dist/methods/delta/helpers/buildDeltaOrderData.d.ts +0 -1
- package/dist/methods/delta/helpers/buildDeltaOrderData.d.ts.map +1 -1
- package/dist/methods/delta/helpers/buildExternalOrderData.d.ts +34 -0
- package/dist/methods/delta/helpers/buildExternalOrderData.d.ts.map +1 -0
- package/dist/methods/delta/helpers/buildTWAPOrderData.d.ts +74 -0
- package/dist/methods/delta/helpers/buildTWAPOrderData.d.ts.map +1 -0
- package/dist/methods/delta/helpers/misc.d.ts +51 -0
- package/dist/methods/delta/helpers/misc.d.ts.map +1 -1
- package/dist/methods/delta/helpers/types.d.ts +116 -4
- package/dist/methods/delta/helpers/types.d.ts.map +1 -1
- package/dist/methods/delta/index.d.ts +34 -3
- package/dist/methods/delta/index.d.ts.map +1 -1
- package/dist/methods/delta/postDeltaOrder.d.ts +4 -9
- package/dist/methods/delta/postDeltaOrder.d.ts.map +1 -1
- package/dist/methods/delta/postExternalDeltaOrder.d.ts +11 -0
- package/dist/methods/delta/postExternalDeltaOrder.d.ts.map +1 -0
- package/dist/methods/delta/postTWAPDeltaOrder.d.ts +16 -0
- package/dist/methods/delta/postTWAPDeltaOrder.d.ts.map +1 -0
- package/dist/methods/delta/preSignDeltaOrder.d.ts.map +1 -1
- package/dist/methods/delta/preSignExternalDeltaOrder.d.ts +17 -0
- package/dist/methods/delta/preSignExternalDeltaOrder.d.ts.map +1 -0
- package/dist/methods/delta/preSignTWAPDeltaOrder.d.ts +20 -0
- package/dist/methods/delta/preSignTWAPDeltaOrder.d.ts.map +1 -0
- package/dist/methods/delta/signExternalDeltaOrder.d.ts +9 -0
- package/dist/methods/delta/signExternalDeltaOrder.d.ts.map +1 -0
- package/dist/methods/delta/signTWAPDeltaOrder.d.ts +9 -0
- package/dist/methods/delta/signTWAPDeltaOrder.d.ts.map +1 -0
- package/dist/methods/limitOrders/approveForOrder.d.ts +2 -0
- package/dist/methods/limitOrders/approveForOrder.d.ts.map +1 -1
- package/dist/methods/limitOrders/buildOrder.d.ts +3 -0
- package/dist/methods/limitOrders/buildOrder.d.ts.map +1 -1
- package/dist/methods/limitOrders/cancelOrder.d.ts +2 -0
- package/dist/methods/limitOrders/cancelOrder.d.ts.map +1 -1
- package/dist/methods/limitOrders/fillOrderDirectly.d.ts +2 -0
- package/dist/methods/limitOrders/fillOrderDirectly.d.ts.map +1 -1
- package/dist/methods/limitOrders/getOrders.d.ts +2 -0
- package/dist/methods/limitOrders/getOrders.d.ts.map +1 -1
- package/dist/methods/limitOrders/getOrdersContract.d.ts +2 -0
- package/dist/methods/limitOrders/getOrdersContract.d.ts.map +1 -1
- package/dist/methods/limitOrders/index.d.ts +7 -1
- package/dist/methods/limitOrders/index.d.ts.map +1 -1
- package/dist/methods/limitOrders/postOrder.d.ts +2 -0
- package/dist/methods/limitOrders/postOrder.d.ts.map +1 -1
- package/dist/methods/limitOrders/signOrder.d.ts +2 -0
- package/dist/methods/limitOrders/signOrder.d.ts.map +1 -1
- package/dist/methods/limitOrders/transaction.d.ts +2 -0
- package/dist/methods/limitOrders/transaction.d.ts.map +1 -1
- package/dist/methods/nftOrders/approveForOrder.d.ts +2 -0
- package/dist/methods/nftOrders/approveForOrder.d.ts.map +1 -1
- package/dist/methods/nftOrders/buildOrder.d.ts +3 -0
- package/dist/methods/nftOrders/buildOrder.d.ts.map +1 -1
- package/dist/methods/nftOrders/cancelOrder.d.ts +2 -0
- package/dist/methods/nftOrders/cancelOrder.d.ts.map +1 -1
- package/dist/methods/nftOrders/getOrders.d.ts +2 -0
- package/dist/methods/nftOrders/getOrders.d.ts.map +1 -1
- package/dist/methods/nftOrders/getOrdersContract.d.ts +2 -0
- package/dist/methods/nftOrders/getOrdersContract.d.ts.map +1 -1
- package/dist/methods/nftOrders/helpers/misc.d.ts +1 -0
- package/dist/methods/nftOrders/helpers/misc.d.ts.map +1 -1
- package/dist/methods/nftOrders/index.d.ts +7 -1
- package/dist/methods/nftOrders/index.d.ts.map +1 -1
- package/dist/methods/nftOrders/postOrder.d.ts +2 -0
- package/dist/methods/nftOrders/postOrder.d.ts.map +1 -1
- package/dist/methods/nftOrders/signOrder.d.ts +2 -0
- package/dist/methods/nftOrders/signOrder.d.ts.map +1 -1
- package/dist/methods/nftOrders/transaction.d.ts +2 -0
- package/dist/methods/nftOrders/transaction.d.ts.map +1 -1
- package/dist/sdk/full.d.ts +2 -0
- package/dist/sdk/full.d.ts.map +1 -1
- package/dist/sdk/partial.d.ts +5 -1
- package/dist/sdk/partial.d.ts.map +1 -1
- package/dist/sdk/simple.d.ts +6 -0
- package/dist/sdk/simple.d.ts.map +1 -1
- package/dist/sdk.cjs.development.js +1357 -210
- package/dist/sdk.cjs.development.js.map +1 -1
- package/dist/sdk.cjs.production.min.js +1 -1
- package/dist/sdk.cjs.production.min.js.map +1 -1
- package/dist/sdk.esm.js +1348 -211
- package/dist/sdk.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/constants.js +3 -0
- package/src/examples/delta.ts +9 -20
- package/src/examples/externalDelta.ts +162 -0
- package/src/examples/helpers/delta.ts +8 -4
- package/src/gas.js +1 -0
- package/src/index.js +85 -0
- package/src/index.ts +103 -3
- package/src/methods/delta/buildDeltaOrder.ts +13 -61
- package/src/methods/delta/buildExternalDeltaOrder.ts +124 -0
- package/src/methods/delta/buildTWAPDeltaOrder.ts +181 -0
- package/src/methods/delta/getDeltaOrders.ts +31 -13
- package/src/methods/delta/helpers/abi.ts +20 -0
- package/src/methods/delta/helpers/buildDeltaOrderData.ts +3 -35
- package/src/methods/delta/helpers/buildExternalOrderData.ts +129 -0
- package/src/methods/delta/helpers/buildTWAPOrderData.ts +227 -0
- package/src/methods/delta/helpers/misc.ts +244 -0
- package/src/methods/delta/helpers/types.ts +138 -9
- package/src/methods/delta/index.ts +159 -3
- package/src/methods/delta/postDeltaOrder.ts +9 -11
- package/src/methods/delta/postExternalDeltaOrder.ts +45 -0
- package/src/methods/delta/postTWAPDeltaOrder.ts +57 -0
- package/src/methods/delta/preSignDeltaOrder.ts +1 -21
- package/src/methods/delta/preSignExternalDeltaOrder.ts +141 -0
- package/src/methods/delta/preSignTWAPDeltaOrder.ts +168 -0
- package/src/methods/delta/signExternalDeltaOrder.ts +35 -0
- package/src/methods/delta/signTWAPDeltaOrder.ts +32 -0
- package/src/methods/limitOrders/approveForOrder.ts +2 -0
- package/src/methods/limitOrders/buildOrder.ts +3 -0
- package/src/methods/limitOrders/cancelOrder.ts +2 -0
- package/src/methods/limitOrders/fillOrderDirectly.ts +2 -0
- package/src/methods/limitOrders/getOrders.ts +2 -0
- package/src/methods/limitOrders/getOrdersContract.ts +2 -0
- package/src/methods/limitOrders/index.ts +7 -1
- package/src/methods/limitOrders/postOrder.ts +2 -0
- package/src/methods/limitOrders/signOrder.ts +2 -0
- package/src/methods/limitOrders/transaction.ts +2 -0
- package/src/methods/nftOrders/approveForOrder.ts +2 -0
- package/src/methods/nftOrders/buildOrder.ts +3 -0
- package/src/methods/nftOrders/cancelOrder.ts +2 -0
- package/src/methods/nftOrders/getOrders.ts +2 -0
- package/src/methods/nftOrders/getOrdersContract.ts +2 -0
- package/src/methods/nftOrders/helpers/misc.ts +1 -0
- package/src/methods/nftOrders/index.ts +7 -1
- package/src/methods/nftOrders/postOrder.ts +2 -0
- package/src/methods/nftOrders/signOrder.ts +2 -0
- package/src/methods/nftOrders/transaction.ts +2 -0
- package/src/sdk/full.ts +2 -0
- package/src/sdk/partial.ts +5 -1
- package/src/sdk/simple.ts +6 -0
- package/src/types.js +1 -0
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { Domain } from '../../common/orders/buildOrderData';
|
|
2
|
+
import {
|
|
3
|
+
Bridge,
|
|
4
|
+
TWAPDeltaOrder,
|
|
5
|
+
TWAPBuyDeltaOrder,
|
|
6
|
+
TWAPOnChainOrderType,
|
|
7
|
+
OnChainOrderMap,
|
|
8
|
+
} from './types';
|
|
9
|
+
import { DELTA_DEFAULT_EXPIRY, producePartnerAndFee } from './misc';
|
|
10
|
+
|
|
11
|
+
const BRIDGE_EIP_712_TYPE = [
|
|
12
|
+
{ name: 'protocolSelector', type: 'bytes4' },
|
|
13
|
+
{ name: 'destinationChainId', type: 'uint256' },
|
|
14
|
+
{ name: 'outputToken', type: 'address' },
|
|
15
|
+
{ name: 'scalingFactor', type: 'int8' },
|
|
16
|
+
{ name: 'protocolData', type: 'bytes' },
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
const TWAP_ORDER_EIP_712_TYPES = {
|
|
20
|
+
TWAPOrder: [
|
|
21
|
+
{ name: 'owner', type: 'address' },
|
|
22
|
+
{ name: 'beneficiary', type: 'address' },
|
|
23
|
+
{ name: 'srcToken', type: 'address' },
|
|
24
|
+
{ name: 'destToken', type: 'address' },
|
|
25
|
+
{ name: 'nonce', type: 'uint256' },
|
|
26
|
+
{ name: 'partnerAndFee', type: 'uint256' },
|
|
27
|
+
{ name: 'deadline', type: 'uint64' },
|
|
28
|
+
{ name: 'interval', type: 'uint64' },
|
|
29
|
+
{ name: 'numSlices', type: 'uint32' },
|
|
30
|
+
{ name: 'destAmountPerSlice', type: 'uint256' },
|
|
31
|
+
{ name: 'totalSrcAmount', type: 'uint256' },
|
|
32
|
+
{ name: 'permit', type: 'bytes' },
|
|
33
|
+
{ name: 'metadata', type: 'bytes' },
|
|
34
|
+
{ name: 'bridge', type: 'Bridge' },
|
|
35
|
+
],
|
|
36
|
+
Bridge: BRIDGE_EIP_712_TYPE,
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const TWAP_BUY_ORDER_EIP_712_TYPES = {
|
|
40
|
+
TWAPBuyOrder: [
|
|
41
|
+
{ name: 'owner', type: 'address' },
|
|
42
|
+
{ name: 'beneficiary', type: 'address' },
|
|
43
|
+
{ name: 'srcToken', type: 'address' },
|
|
44
|
+
{ name: 'destToken', type: 'address' },
|
|
45
|
+
{ name: 'nonce', type: 'uint256' },
|
|
46
|
+
{ name: 'partnerAndFee', type: 'uint256' },
|
|
47
|
+
{ name: 'deadline', type: 'uint64' },
|
|
48
|
+
{ name: 'interval', type: 'uint64' },
|
|
49
|
+
{ name: 'numSlices', type: 'uint32' },
|
|
50
|
+
{ name: 'totalDestAmount', type: 'uint256' },
|
|
51
|
+
{ name: 'maxSrcAmount', type: 'uint256' },
|
|
52
|
+
{ name: 'permit', type: 'bytes' },
|
|
53
|
+
{ name: 'metadata', type: 'bytes' },
|
|
54
|
+
{ name: 'bridge', type: 'Bridge' },
|
|
55
|
+
],
|
|
56
|
+
Bridge: BRIDGE_EIP_712_TYPE,
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export type SignableTWAPSellOrderData = {
|
|
60
|
+
types: typeof TWAP_ORDER_EIP_712_TYPES;
|
|
61
|
+
domain: Domain;
|
|
62
|
+
data: TWAPDeltaOrder;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export type SignableTWAPBuyOrderData = {
|
|
66
|
+
types: typeof TWAP_BUY_ORDER_EIP_712_TYPES;
|
|
67
|
+
domain: Domain;
|
|
68
|
+
data: TWAPBuyDeltaOrder;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export type SignableTWAPOrderData =
|
|
72
|
+
| SignableTWAPSellOrderData
|
|
73
|
+
| SignableTWAPBuyOrderData;
|
|
74
|
+
|
|
75
|
+
type ProduceTWAPOrderTypedDataInput<T extends TWAPOnChainOrderType> = {
|
|
76
|
+
orderInput: OnChainOrderMap[T];
|
|
77
|
+
paraswapDeltaAddress: string;
|
|
78
|
+
chainId: number;
|
|
79
|
+
onChainOrderType: T;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export function produceTWAPOrderTypedData(
|
|
83
|
+
params: ProduceTWAPOrderTypedDataInput<'TWAPOrder'>
|
|
84
|
+
): SignableTWAPSellOrderData;
|
|
85
|
+
export function produceTWAPOrderTypedData(
|
|
86
|
+
params: ProduceTWAPOrderTypedDataInput<'TWAPBuyOrder'>
|
|
87
|
+
): SignableTWAPBuyOrderData;
|
|
88
|
+
export function produceTWAPOrderTypedData({
|
|
89
|
+
orderInput,
|
|
90
|
+
chainId,
|
|
91
|
+
paraswapDeltaAddress,
|
|
92
|
+
onChainOrderType,
|
|
93
|
+
}: ProduceTWAPOrderTypedDataInput<TWAPOnChainOrderType>) {
|
|
94
|
+
const domain: Domain = {
|
|
95
|
+
name: 'Portikus',
|
|
96
|
+
version: '2.0.0',
|
|
97
|
+
chainId,
|
|
98
|
+
verifyingContract: paraswapDeltaAddress,
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
if (onChainOrderType === 'TWAPOrder') {
|
|
102
|
+
return {
|
|
103
|
+
types: TWAP_ORDER_EIP_712_TYPES,
|
|
104
|
+
domain,
|
|
105
|
+
data: orderInput,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
types: TWAP_BUY_ORDER_EIP_712_TYPES,
|
|
111
|
+
domain,
|
|
112
|
+
data: orderInput,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
type TWAPOrderCommonInput = {
|
|
117
|
+
owner: string;
|
|
118
|
+
beneficiary?: string;
|
|
119
|
+
srcToken: string;
|
|
120
|
+
destToken: string;
|
|
121
|
+
nonce?: string;
|
|
122
|
+
deadline?: number;
|
|
123
|
+
permit?: string;
|
|
124
|
+
metadata?: string;
|
|
125
|
+
interval: number;
|
|
126
|
+
numSlices: number;
|
|
127
|
+
bridge: Bridge;
|
|
128
|
+
|
|
129
|
+
partnerAddress: string;
|
|
130
|
+
paraswapDeltaAddress: string;
|
|
131
|
+
partnerFeeBps: number;
|
|
132
|
+
partnerTakesSurplus?: boolean;
|
|
133
|
+
capSurplus?: boolean;
|
|
134
|
+
chainId: number;
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
export type BuildTWAPSellOrderDataInput = TWAPOrderCommonInput & {
|
|
138
|
+
onChainOrderType: 'TWAPOrder';
|
|
139
|
+
destAmountPerSlice: string;
|
|
140
|
+
totalSrcAmount: string;
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
export type BuildTWAPBuyOrderDataInput = TWAPOrderCommonInput & {
|
|
144
|
+
onChainOrderType: 'TWAPBuyOrder';
|
|
145
|
+
totalDestAmount: string;
|
|
146
|
+
maxSrcAmount: string;
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
export type BuildTWAPOrderDataInput =
|
|
150
|
+
| BuildTWAPSellOrderDataInput
|
|
151
|
+
| BuildTWAPBuyOrderDataInput;
|
|
152
|
+
|
|
153
|
+
export function buildTWAPSignableOrderData(
|
|
154
|
+
input: BuildTWAPOrderDataInput
|
|
155
|
+
): SignableTWAPOrderData {
|
|
156
|
+
const {
|
|
157
|
+
owner,
|
|
158
|
+
beneficiary = owner,
|
|
159
|
+
srcToken,
|
|
160
|
+
destToken,
|
|
161
|
+
nonce = Date.now().toString(10),
|
|
162
|
+
deadline = Math.floor(Date.now() / 1000 + DELTA_DEFAULT_EXPIRY),
|
|
163
|
+
permit = '0x',
|
|
164
|
+
metadata = '0x',
|
|
165
|
+
interval,
|
|
166
|
+
numSlices,
|
|
167
|
+
bridge,
|
|
168
|
+
|
|
169
|
+
partnerAddress,
|
|
170
|
+
partnerFeeBps,
|
|
171
|
+
partnerTakesSurplus = false,
|
|
172
|
+
capSurplus = true,
|
|
173
|
+
chainId,
|
|
174
|
+
paraswapDeltaAddress,
|
|
175
|
+
onChainOrderType,
|
|
176
|
+
} = input;
|
|
177
|
+
|
|
178
|
+
const partnerAndFee = producePartnerAndFee({
|
|
179
|
+
partnerFeeBps,
|
|
180
|
+
partnerAddress,
|
|
181
|
+
partnerTakesSurplus,
|
|
182
|
+
capSurplus,
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
const commonFields = {
|
|
186
|
+
owner,
|
|
187
|
+
beneficiary,
|
|
188
|
+
srcToken,
|
|
189
|
+
destToken,
|
|
190
|
+
nonce,
|
|
191
|
+
partnerAndFee,
|
|
192
|
+
deadline,
|
|
193
|
+
interval,
|
|
194
|
+
numSlices,
|
|
195
|
+
permit,
|
|
196
|
+
metadata,
|
|
197
|
+
bridge,
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
if (onChainOrderType === 'TWAPOrder') {
|
|
201
|
+
const orderInput: TWAPDeltaOrder = {
|
|
202
|
+
...commonFields,
|
|
203
|
+
destAmountPerSlice: input.destAmountPerSlice,
|
|
204
|
+
totalSrcAmount: input.totalSrcAmount,
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
return produceTWAPOrderTypedData({
|
|
208
|
+
orderInput,
|
|
209
|
+
chainId,
|
|
210
|
+
paraswapDeltaAddress,
|
|
211
|
+
onChainOrderType: 'TWAPOrder',
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const orderInput: TWAPBuyDeltaOrder = {
|
|
216
|
+
...commonFields,
|
|
217
|
+
totalDestAmount: input.totalDestAmount,
|
|
218
|
+
maxSrcAmount: input.maxSrcAmount,
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
return produceTWAPOrderTypedData({
|
|
222
|
+
orderInput,
|
|
223
|
+
chainId,
|
|
224
|
+
paraswapDeltaAddress,
|
|
225
|
+
onChainOrderType: 'TWAPBuyOrder',
|
|
226
|
+
});
|
|
227
|
+
}
|
|
@@ -1,4 +1,180 @@
|
|
|
1
|
+
import { ZERO_ADDRESS } from '../../common/orders/buildOrderData';
|
|
1
2
|
import type { SignableDeltaOrderData } from './buildDeltaOrderData';
|
|
3
|
+
import type { SignableExternalOrderData } from './buildExternalOrderData';
|
|
4
|
+
import type { SignableTWAPOrderData } from './buildTWAPOrderData';
|
|
5
|
+
import type { GetPartnerFeeFunctions } from '../getPartnerFee';
|
|
6
|
+
import type { RequestParameters } from '../../../types';
|
|
7
|
+
import type { AmountsWithSlippage, SwapSideUnion } from './types';
|
|
8
|
+
import { SwapSide } from '../../../constants';
|
|
9
|
+
import { assert } from 'ts-essentials';
|
|
10
|
+
|
|
11
|
+
// default deadline = 10 min
|
|
12
|
+
export const DELTA_DEFAULT_EXPIRY = 10 * 60; // seconds
|
|
13
|
+
|
|
14
|
+
type ProducePartnerAndFeeInput = {
|
|
15
|
+
partnerFeeBps: number;
|
|
16
|
+
partnerAddress: string;
|
|
17
|
+
partnerTakesSurplus: boolean;
|
|
18
|
+
capSurplus: boolean;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// fee and address are encoded together
|
|
22
|
+
export function producePartnerAndFee({
|
|
23
|
+
partnerFeeBps,
|
|
24
|
+
partnerAddress,
|
|
25
|
+
partnerTakesSurplus,
|
|
26
|
+
capSurplus,
|
|
27
|
+
}: ProducePartnerAndFeeInput): string {
|
|
28
|
+
const capSurplusShifted = BigInt(capSurplus) << BigInt(9);
|
|
29
|
+
if (partnerAddress === ZERO_ADDRESS) {
|
|
30
|
+
return capSurplusShifted.toString(10);
|
|
31
|
+
} else {
|
|
32
|
+
const partnerAndFee =
|
|
33
|
+
(BigInt(partnerAddress) << BigInt(96)) |
|
|
34
|
+
BigInt(partnerFeeBps.toFixed(0)) |
|
|
35
|
+
(BigInt(partnerTakesSurplus) << BigInt(8)) |
|
|
36
|
+
capSurplusShifted;
|
|
37
|
+
|
|
38
|
+
return partnerAndFee.toString(10);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
type ApplySlippageInput = {
|
|
43
|
+
amount: string;
|
|
44
|
+
slippageBps: number;
|
|
45
|
+
increase: boolean;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export function applySlippage({
|
|
49
|
+
amount,
|
|
50
|
+
slippageBps,
|
|
51
|
+
increase,
|
|
52
|
+
}: ApplySlippageInput): string {
|
|
53
|
+
assert(
|
|
54
|
+
Number.isInteger(slippageBps) && slippageBps >= 0 && slippageBps <= 10_000,
|
|
55
|
+
'slippageBps must be an integer between 0 and 10_000'
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
const BPS_BASE = 10_000n;
|
|
59
|
+
const amt = BigInt(amount);
|
|
60
|
+
const bps = BigInt(slippageBps);
|
|
61
|
+
|
|
62
|
+
return increase
|
|
63
|
+
? ((amt * (BPS_BASE + bps)) / BPS_BASE).toString(10)
|
|
64
|
+
: ((amt * (BPS_BASE - bps)) / BPS_BASE).toString(10);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export type ResolvePartnerFeeInput = {
|
|
68
|
+
partnerAddress?: string;
|
|
69
|
+
partnerFeeBps?: number;
|
|
70
|
+
partnerTakesSurplus?: boolean;
|
|
71
|
+
partner?: string;
|
|
72
|
+
deltaPrice: { partner?: string; partnerFee?: number };
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export type ResolvedPartnerFee = {
|
|
76
|
+
partnerAddress: string;
|
|
77
|
+
partnerFeeBps: number;
|
|
78
|
+
partnerTakesSurplus: boolean;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export async function resolvePartnerFee(
|
|
82
|
+
options: ResolvePartnerFeeInput,
|
|
83
|
+
getPartnerFee: GetPartnerFeeFunctions['getPartnerFee'],
|
|
84
|
+
requestParams?: RequestParameters
|
|
85
|
+
): Promise<ResolvedPartnerFee> {
|
|
86
|
+
// externally supplied partner fee data takes precedence
|
|
87
|
+
let partnerAddress = options.partnerAddress;
|
|
88
|
+
let partnerFeeBps =
|
|
89
|
+
options.partnerFeeBps ??
|
|
90
|
+
(options.deltaPrice.partnerFee
|
|
91
|
+
? options.deltaPrice.partnerFee * 100
|
|
92
|
+
: undefined);
|
|
93
|
+
let partnerTakesSurplus = options.partnerTakesSurplus;
|
|
94
|
+
|
|
95
|
+
// if fee given, takeSurplus is ignored
|
|
96
|
+
const feeOrTakeSurplusSupplied =
|
|
97
|
+
partnerFeeBps !== undefined || partnerTakesSurplus !== undefined;
|
|
98
|
+
|
|
99
|
+
if (partnerAddress === undefined || feeOrTakeSurplusSupplied) {
|
|
100
|
+
const partner = options.partner || options.deltaPrice.partner;
|
|
101
|
+
if (!partner) {
|
|
102
|
+
// if no partner given in options or deltaPrice, default partnerAddress to zero,
|
|
103
|
+
// unless supplied explicitly
|
|
104
|
+
partnerAddress = partnerAddress ?? ZERO_ADDRESS;
|
|
105
|
+
} else {
|
|
106
|
+
const partnerFeeResponse = await getPartnerFee(
|
|
107
|
+
{ partner },
|
|
108
|
+
requestParams
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
partnerAddress = partnerAddress ?? partnerFeeResponse.partnerAddress;
|
|
112
|
+
// deltaPrice.partnerFee and partnerFeeResponse.partnerFee should be the same, but give priority to externally provided
|
|
113
|
+
partnerFeeBps = partnerFeeBps ?? partnerFeeResponse.partnerFee * 100;
|
|
114
|
+
partnerTakesSurplus =
|
|
115
|
+
partnerTakesSurplus ?? partnerFeeResponse.takeSurplus;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
partnerAddress: partnerAddress!,
|
|
121
|
+
partnerFeeBps: partnerFeeBps ?? 0,
|
|
122
|
+
partnerTakesSurplus: partnerTakesSurplus ?? false,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export type ResolveAmountsInput = AmountsWithSlippage & {
|
|
127
|
+
deltaPrice: { destAmount: string; srcAmount: string };
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
export type ResolvedAmounts = {
|
|
131
|
+
srcAmount: string;
|
|
132
|
+
destAmount: string;
|
|
133
|
+
expectedAmount: string;
|
|
134
|
+
swapSide: SwapSideUnion;
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
export function resolveAmounts(options: ResolveAmountsInput): ResolvedAmounts {
|
|
138
|
+
let srcAmount: string;
|
|
139
|
+
let destAmount: string;
|
|
140
|
+
|
|
141
|
+
const swapSide: SwapSideUnion =
|
|
142
|
+
options.slippage !== undefined
|
|
143
|
+
? options.srcAmount !== undefined
|
|
144
|
+
? SwapSide.SELL
|
|
145
|
+
: SwapSide.BUY
|
|
146
|
+
: options.side ?? SwapSide.SELL;
|
|
147
|
+
|
|
148
|
+
if (options.slippage !== undefined) {
|
|
149
|
+
if (options.srcAmount !== undefined) {
|
|
150
|
+
// SELL with slippage: destAmount auto-computed
|
|
151
|
+
srcAmount = options.srcAmount;
|
|
152
|
+
destAmount = applySlippage({
|
|
153
|
+
amount: options.deltaPrice.destAmount,
|
|
154
|
+
slippageBps: options.slippage,
|
|
155
|
+
increase: false,
|
|
156
|
+
});
|
|
157
|
+
} else {
|
|
158
|
+
// BUY with slippage: srcAmount auto-computed
|
|
159
|
+
destAmount = options.destAmount;
|
|
160
|
+
srcAmount = applySlippage({
|
|
161
|
+
amount: options.deltaPrice.srcAmount,
|
|
162
|
+
slippageBps: options.slippage,
|
|
163
|
+
increase: true,
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
} else {
|
|
167
|
+
srcAmount = options.srcAmount;
|
|
168
|
+
destAmount = options.destAmount;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const expectedAmount =
|
|
172
|
+
swapSide === SwapSide.SELL
|
|
173
|
+
? options.deltaPrice.destAmount
|
|
174
|
+
: options.deltaPrice.srcAmount;
|
|
175
|
+
|
|
176
|
+
return { srcAmount, destAmount, expectedAmount, swapSide };
|
|
177
|
+
}
|
|
2
178
|
|
|
3
179
|
export function sanitizeDeltaOrderData({
|
|
4
180
|
owner,
|
|
@@ -34,3 +210,71 @@ export function sanitizeDeltaOrderData({
|
|
|
34
210
|
metadata,
|
|
35
211
|
};
|
|
36
212
|
}
|
|
213
|
+
|
|
214
|
+
export function sanitizeExternalOrderData({
|
|
215
|
+
owner,
|
|
216
|
+
handler,
|
|
217
|
+
srcToken,
|
|
218
|
+
destToken,
|
|
219
|
+
srcAmount,
|
|
220
|
+
destAmount,
|
|
221
|
+
expectedAmount,
|
|
222
|
+
deadline,
|
|
223
|
+
nonce,
|
|
224
|
+
permit,
|
|
225
|
+
partnerAndFee,
|
|
226
|
+
kind,
|
|
227
|
+
metadata,
|
|
228
|
+
data,
|
|
229
|
+
}: SignableExternalOrderData['data'] &
|
|
230
|
+
Record<string, any>): SignableExternalOrderData['data'] {
|
|
231
|
+
return {
|
|
232
|
+
owner,
|
|
233
|
+
handler,
|
|
234
|
+
srcToken,
|
|
235
|
+
destToken,
|
|
236
|
+
srcAmount,
|
|
237
|
+
destAmount,
|
|
238
|
+
expectedAmount,
|
|
239
|
+
deadline,
|
|
240
|
+
nonce,
|
|
241
|
+
permit,
|
|
242
|
+
partnerAndFee,
|
|
243
|
+
kind,
|
|
244
|
+
metadata,
|
|
245
|
+
data,
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export function sanitizeTWAPOrderData(
|
|
250
|
+
orderData: SignableTWAPOrderData['data']
|
|
251
|
+
): SignableTWAPOrderData['data'] {
|
|
252
|
+
const common = {
|
|
253
|
+
owner: orderData.owner,
|
|
254
|
+
beneficiary: orderData.beneficiary,
|
|
255
|
+
srcToken: orderData.srcToken,
|
|
256
|
+
destToken: orderData.destToken,
|
|
257
|
+
nonce: orderData.nonce,
|
|
258
|
+
partnerAndFee: orderData.partnerAndFee,
|
|
259
|
+
deadline: orderData.deadline,
|
|
260
|
+
interval: orderData.interval,
|
|
261
|
+
numSlices: orderData.numSlices,
|
|
262
|
+
permit: orderData.permit,
|
|
263
|
+
metadata: orderData.metadata,
|
|
264
|
+
bridge: orderData.bridge,
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
if ('destAmountPerSlice' in orderData) {
|
|
268
|
+
return {
|
|
269
|
+
...common,
|
|
270
|
+
destAmountPerSlice: orderData.destAmountPerSlice,
|
|
271
|
+
totalSrcAmount: orderData.totalSrcAmount,
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return {
|
|
276
|
+
...common,
|
|
277
|
+
totalDestAmount: orderData.totalDestAmount,
|
|
278
|
+
maxSrcAmount: orderData.maxSrcAmount,
|
|
279
|
+
};
|
|
280
|
+
}
|
|
@@ -1,5 +1,44 @@
|
|
|
1
|
+
import type { EnumerateLiteral } from '../../../types';
|
|
1
2
|
import { SwapSide } from '../../../constants';
|
|
2
3
|
|
|
4
|
+
export type SwapSideUnion = EnumerateLiteral<typeof SwapSide>;
|
|
5
|
+
|
|
6
|
+
/** @description SELL with slippage: srcAmount provided, destAmount auto-computed from deltaPrice.destAmount */
|
|
7
|
+
export type AmountsSellSlippage = {
|
|
8
|
+
/** @description Slippage in basis points (bps). 10000 = 100%, 50 = 0.5% */
|
|
9
|
+
slippage: number;
|
|
10
|
+
/** @description The amount of src token to swap */
|
|
11
|
+
srcAmount: string;
|
|
12
|
+
destAmount?: never;
|
|
13
|
+
/** @description The side of the order */
|
|
14
|
+
side?: 'SELL';
|
|
15
|
+
};
|
|
16
|
+
/** @description BUY with slippage: destAmount provided, srcAmount auto-computed from deltaPrice.srcAmount */
|
|
17
|
+
export type AmountsBuySlippage = {
|
|
18
|
+
/** @description Slippage in basis points (bps). 10000 = 100%, 50 = 0.5% */
|
|
19
|
+
slippage: number;
|
|
20
|
+
/** @description The minimum amount of dest token to receive */
|
|
21
|
+
destAmount: string;
|
|
22
|
+
srcAmount?: never;
|
|
23
|
+
/** @description The side of the order */
|
|
24
|
+
side?: 'BUY';
|
|
25
|
+
};
|
|
26
|
+
/** @description Explicit amounts, no slippage (backward-compatible) */
|
|
27
|
+
export type AmountsExplicit = {
|
|
28
|
+
slippage?: never;
|
|
29
|
+
/** @description The amount of src token to swap */
|
|
30
|
+
srcAmount: string;
|
|
31
|
+
/** @description The minimum amount of dest token to receive */
|
|
32
|
+
destAmount: string;
|
|
33
|
+
/** @description The side of the order. Default is SELL */
|
|
34
|
+
side?: SwapSideUnion;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export type AmountsWithSlippage =
|
|
38
|
+
| AmountsSellSlippage
|
|
39
|
+
| AmountsBuySlippage
|
|
40
|
+
| AmountsExplicit;
|
|
41
|
+
|
|
3
42
|
enum OrderKind {
|
|
4
43
|
Sell = 0,
|
|
5
44
|
Buy = 1,
|
|
@@ -52,6 +91,79 @@ export type Bridge = {
|
|
|
52
91
|
protocolData: string; // Hex string
|
|
53
92
|
};
|
|
54
93
|
|
|
94
|
+
export type ExternalDeltaOrder = {
|
|
95
|
+
/** @description The address of the order owner */
|
|
96
|
+
owner: string;
|
|
97
|
+
/** @description The address of the external handler contract */
|
|
98
|
+
handler: string;
|
|
99
|
+
/** @description The address of the src token */
|
|
100
|
+
srcToken: string;
|
|
101
|
+
/** @description The address of the dest token */
|
|
102
|
+
destToken: string;
|
|
103
|
+
/** @description The amount of src token to swap */
|
|
104
|
+
srcAmount: string;
|
|
105
|
+
/** @description The minimum amount of dest token to receive */
|
|
106
|
+
destAmount: string;
|
|
107
|
+
/** @description The expected amount of token to receive */
|
|
108
|
+
expectedAmount: string;
|
|
109
|
+
/** @description The kind of the order */
|
|
110
|
+
kind: OrderKind;
|
|
111
|
+
/** @description Metadata for the order, hex string */
|
|
112
|
+
metadata: string;
|
|
113
|
+
/** @description The deadline for the order */
|
|
114
|
+
deadline: number;
|
|
115
|
+
/** @description The nonce of the order */
|
|
116
|
+
nonce: string;
|
|
117
|
+
/** @description Optional permit signature for the src token */
|
|
118
|
+
permit: string;
|
|
119
|
+
/** @description Encoded partner address, fee bps, and flags for the order */
|
|
120
|
+
partnerAndFee: string;
|
|
121
|
+
/** @description Protocol-specific encoded bytes for the external handler */
|
|
122
|
+
data: string;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
type TWAPDeltaOrderBase = {
|
|
126
|
+
/** @description The address of the order owner */
|
|
127
|
+
owner: string;
|
|
128
|
+
/** @description The address of the order beneficiary */
|
|
129
|
+
beneficiary: string; // beneficiary==owner if no transferTo
|
|
130
|
+
/** @description The address of the src token */
|
|
131
|
+
srcToken: string; // lowercase
|
|
132
|
+
/** @description The address of the dest token */
|
|
133
|
+
destToken: string; // lowercase
|
|
134
|
+
/** @description The nonce of the order */
|
|
135
|
+
nonce: string; // can be random, can even be Date.now()
|
|
136
|
+
/** @description Encoded partner address, fee bps, and flags for the order. partnerAndFee = (partner << 96) | (partnerTakesSurplus << 8) | fee in bps (max fee is 2%) */
|
|
137
|
+
partnerAndFee: string;
|
|
138
|
+
/** @description The deadline for the order */
|
|
139
|
+
deadline: number; // seconds
|
|
140
|
+
/** @description The interval between each slice execution */
|
|
141
|
+
interval: number; // seconds
|
|
142
|
+
/** @description The number of slices to execute */
|
|
143
|
+
numSlices: number;
|
|
144
|
+
/** @description Optional permit signature for the src token */
|
|
145
|
+
permit: string; //can be "0x"
|
|
146
|
+
/** @description Metadata for the order, hex string */
|
|
147
|
+
metadata: string;
|
|
148
|
+
/** @description The bridge input */
|
|
149
|
+
bridge: Bridge;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
export type TWAPDeltaOrder = TWAPDeltaOrderBase & {
|
|
153
|
+
/** @description The amount of dest token to receive per slice */
|
|
154
|
+
destAmountPerSlice: string; // wei
|
|
155
|
+
/** @description The total amount of src token to swap */
|
|
156
|
+
totalSrcAmount: string; // wei
|
|
157
|
+
/** @description Optional permit signature for the src token */
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
export type TWAPBuyDeltaOrder = TWAPDeltaOrderBase & {
|
|
161
|
+
/** @description The total amount of dest token to buy across all slices */
|
|
162
|
+
totalDestAmount: string; // wei
|
|
163
|
+
/** @description The maximum amount of src token willing to spend */
|
|
164
|
+
maxSrcAmount: string; // wei
|
|
165
|
+
};
|
|
166
|
+
|
|
55
167
|
export type DeltaAuctionStatus =
|
|
56
168
|
| 'NOT_STARTED'
|
|
57
169
|
| 'AWAITING_PRE_SIGNATURE'
|
|
@@ -65,7 +177,7 @@ export type DeltaAuctionStatus =
|
|
|
65
177
|
| 'SUSPENDED'
|
|
66
178
|
| 'REFUNDED';
|
|
67
179
|
|
|
68
|
-
type DeltaAuctionTransaction = {
|
|
180
|
+
export type DeltaAuctionTransaction = {
|
|
69
181
|
id: string;
|
|
70
182
|
hash: string;
|
|
71
183
|
orderId: string;
|
|
@@ -92,13 +204,18 @@ type DeltaAuctionTransaction = {
|
|
|
92
204
|
auctionId: string;
|
|
93
205
|
};
|
|
94
206
|
|
|
95
|
-
export type
|
|
207
|
+
export type OnChainOrderMap = {
|
|
208
|
+
Order: DeltaAuctionOrder;
|
|
209
|
+
ExternalOrder: ExternalDeltaOrder;
|
|
210
|
+
TWAPOrder: TWAPDeltaOrder;
|
|
211
|
+
TWAPBuyOrder: TWAPBuyDeltaOrder;
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
type DeltaAuctionBase = {
|
|
96
215
|
id: string;
|
|
97
216
|
deltaVersion: string; // 1.0 or 2.0 currently
|
|
98
217
|
user: string;
|
|
99
|
-
signature: string;
|
|
100
218
|
status: DeltaAuctionStatus;
|
|
101
|
-
order: DeltaAuctionOrder;
|
|
102
219
|
orderHash: string | null; // not available on old Orders only
|
|
103
220
|
transactions: DeltaAuctionTransaction[];
|
|
104
221
|
chainId: number;
|
|
@@ -115,13 +232,17 @@ export type DeltaAuction = {
|
|
|
115
232
|
bridgeMetadata: BridgeMetadata | null;
|
|
116
233
|
bridgeStatus: BridgeStatus | null;
|
|
117
234
|
|
|
118
|
-
|
|
119
|
-
// orderVersion: string; // "2.0.0"
|
|
120
|
-
// deltaGasOverhead: number;
|
|
121
|
-
|
|
122
|
-
type: 'MARKET' | 'LIMIT'; // @TODO when available in API for individual /order/:hash|:id
|
|
235
|
+
type: 'MARKET' | 'LIMIT';
|
|
123
236
|
};
|
|
124
237
|
|
|
238
|
+
export type DeltaAuction<T extends OnChainOrderType = OnChainOrderType> =
|
|
239
|
+
T extends T
|
|
240
|
+
? DeltaAuctionBase & {
|
|
241
|
+
onChainOrderType: T;
|
|
242
|
+
order: OnChainOrderMap[T];
|
|
243
|
+
}
|
|
244
|
+
: never;
|
|
245
|
+
|
|
125
246
|
export type BridgeMetadata = {
|
|
126
247
|
/** @description The amount that user should expect to get */
|
|
127
248
|
outputAmount: string;
|
|
@@ -138,6 +259,14 @@ export type BridgeMetadata = {
|
|
|
138
259
|
// refunded is basically failed
|
|
139
260
|
export type BridgeStatus = 'pending' | 'filled' | 'expired' | 'refunded';
|
|
140
261
|
|
|
262
|
+
export type OnChainOrderType =
|
|
263
|
+
| 'Order'
|
|
264
|
+
| 'ExternalOrder'
|
|
265
|
+
| 'TWAPOrder'
|
|
266
|
+
| 'TWAPBuyOrder';
|
|
267
|
+
|
|
268
|
+
export type TWAPOnChainOrderType = 'TWAPOrder' | 'TWAPBuyOrder';
|
|
269
|
+
|
|
141
270
|
//// available on BridgePrice ////
|
|
142
271
|
|
|
143
272
|
type BridgeQuoteFee = {
|