@clober/v2-sdk 0.0.2-b → 0.0.3-a
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/dist/cjs/approval.js +0 -26
- package/dist/cjs/approval.js.map +1 -1
- package/dist/cjs/call.js +0 -208
- package/dist/cjs/call.js.map +1 -1
- package/dist/cjs/constants/fee.js +2 -2
- package/dist/cjs/constants/fee.js.map +1 -1
- package/dist/cjs/index.js +6 -19
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/model/book.js +145 -112
- package/dist/cjs/model/book.js.map +1 -1
- package/dist/cjs/model/fee-policy.js +71 -21
- package/dist/cjs/model/fee-policy.js.map +1 -1
- package/dist/cjs/model/market.js +235 -171
- package/dist/cjs/model/market.js.map +1 -1
- package/dist/cjs/package.json +1 -1
- package/dist/cjs/signature.js +0 -33
- package/dist/cjs/signature.js.map +1 -1
- package/dist/cjs/utils/market.js +0 -3
- package/dist/cjs/utils/market.js.map +1 -1
- package/dist/cjs/utils/math.js +1 -28
- package/dist/cjs/utils/math.js.map +1 -1
- package/dist/cjs/view.js +0 -92
- package/dist/cjs/view.js.map +1 -1
- package/dist/esm/model/book.js +147 -112
- package/dist/esm/model/book.js.map +1 -1
- package/dist/esm/model/fee-policy.js +71 -21
- package/dist/esm/model/fee-policy.js.map +1 -1
- package/dist/esm/model/market.js +241 -171
- package/dist/esm/model/market.js.map +1 -1
- package/dist/esm/package.json +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/package.json +5 -11
- package/dist/cjs/tsconfig.build.tsbuildinfo +0 -1
- package/dist/esm/tsconfig.build.tsbuildinfo +0 -1
package/dist/cjs/view.js
CHANGED
|
@@ -6,24 +6,6 @@ const market_1 = require("./apis/market");
|
|
|
6
6
|
const prices_1 = require("./utils/prices");
|
|
7
7
|
const price_1 = require("./constants/price");
|
|
8
8
|
const open_order_1 = require("./apis/open-order");
|
|
9
|
-
/**
|
|
10
|
-
* Get market information by chain id and token addresses
|
|
11
|
-
* @param chainId - chain id from {@link CHAIN_IDS}
|
|
12
|
-
* @param token0 - token0 address
|
|
13
|
-
* @param token1 - token1 address
|
|
14
|
-
* @param options
|
|
15
|
-
* @param options.rpcUrl - RPC URL of the blockchain
|
|
16
|
-
* @returns A market {@link Market}
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* import { getMarket } from '@clober/v2-sdk'
|
|
20
|
-
*
|
|
21
|
-
* const market = await getMarket(
|
|
22
|
-
* 421614,
|
|
23
|
-
* '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
|
|
24
|
-
* '0x0000000000000000000000000000000000000000',
|
|
25
|
-
* )
|
|
26
|
-
*/
|
|
27
9
|
const getMarket = async (chainId, token0, token1, options) => {
|
|
28
10
|
if ((0, viem_1.isAddressEqual)(token0, token1)) {
|
|
29
11
|
throw new Error('Token0 and token1 must be different');
|
|
@@ -42,27 +24,6 @@ const getMarket = async (chainId, token0, token1, options) => {
|
|
|
42
24
|
};
|
|
43
25
|
};
|
|
44
26
|
exports.getMarket = getMarket;
|
|
45
|
-
/**
|
|
46
|
-
* Calculates the expected output for a given input amount, based on the provided market data.
|
|
47
|
-
*
|
|
48
|
-
* @param chainId The chain ID of the blockchain.
|
|
49
|
-
* @param inputToken The address of the input token.
|
|
50
|
-
* @param outputToken The address of the output token.
|
|
51
|
-
* @param amountIn The amount of expected input amount. (ex 1.2 ETH -> 1.2)
|
|
52
|
-
* @param options
|
|
53
|
-
* @param options.limitPrice The maximum limit price to spend.
|
|
54
|
-
* @param options.rpcUrl The RPC URL of the blockchain.
|
|
55
|
-
* @returns A Promise resolving to an object containing the taken amount, spend amount and result of the calculation.
|
|
56
|
-
* @example
|
|
57
|
-
* import { getExpectedOutput } from '@clober/v2-sdk'
|
|
58
|
-
*
|
|
59
|
-
* const { takenAmount, spendAmount } = await getExpectedOutput(
|
|
60
|
-
* 421614,
|
|
61
|
-
* '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
|
|
62
|
-
* '0x0000000000000000000000000000000000000000',
|
|
63
|
-
* '1000.123', // spend 1000.123 USDC
|
|
64
|
-
* )
|
|
65
|
-
*/
|
|
66
27
|
const getExpectedOutput = async (chainId, inputToken, outputToken, amountIn, options) => {
|
|
67
28
|
const market = await (0, market_1.fetchMarket)(chainId, [inputToken, outputToken], options?.rpcUrl);
|
|
68
29
|
const isBid = (0, viem_1.isAddressEqual)(market.quote.address, inputToken);
|
|
@@ -92,27 +53,6 @@ const getExpectedOutput = async (chainId, inputToken, outputToken, amountIn, opt
|
|
|
92
53
|
};
|
|
93
54
|
};
|
|
94
55
|
exports.getExpectedOutput = getExpectedOutput;
|
|
95
|
-
/**
|
|
96
|
-
* Calculates the expected input for a given output amount, based on the provided market data.
|
|
97
|
-
*
|
|
98
|
-
* @param chainId The chain ID of the blockchain.
|
|
99
|
-
* @param inputToken The address of the input token.
|
|
100
|
-
* @param outputToken The address of the output token.
|
|
101
|
-
* @param amountOut The amount of expected output amount. (ex 1.2 ETH -> 1.2)
|
|
102
|
-
* @param options
|
|
103
|
-
* @param options.limitPrice The maximum limit price to take.
|
|
104
|
-
* @param options.rpcUrl The RPC URL of the blockchain.
|
|
105
|
-
* @returns A Promise resolving to an object containing the taken amount, spend amount and result of the calculation.
|
|
106
|
-
* @example
|
|
107
|
-
* import { getExpectedInput } from '@clober/v2-sdk'
|
|
108
|
-
*
|
|
109
|
-
* const { takenAmount, spendAmount } = await getExpectedInput(
|
|
110
|
-
* 421614,
|
|
111
|
-
* '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
|
|
112
|
-
* '0x0000000000000000000000000000000000000000',
|
|
113
|
-
* '0.1', // take 0.1 ETH
|
|
114
|
-
* )
|
|
115
|
-
*/
|
|
116
56
|
const getExpectedInput = async (chainId, inputToken, outputToken, amountOut, options) => {
|
|
117
57
|
const market = await (0, market_1.fetchMarket)(chainId, [inputToken, outputToken], options?.rpcUrl);
|
|
118
58
|
const isBid = (0, viem_1.isAddressEqual)(market.quote.address, inputToken);
|
|
@@ -142,42 +82,10 @@ const getExpectedInput = async (chainId, inputToken, outputToken, amountOut, opt
|
|
|
142
82
|
};
|
|
143
83
|
};
|
|
144
84
|
exports.getExpectedInput = getExpectedInput;
|
|
145
|
-
/**
|
|
146
|
-
* Retrieves the open order with the specified ID on the given chain.
|
|
147
|
-
*
|
|
148
|
-
* @param {CHAIN_IDS} chainId The chain ID.
|
|
149
|
-
* @param {string} id The ID of the open order.
|
|
150
|
-
* @param options
|
|
151
|
-
* @param options.rpcUrl The RPC URL of the blockchain.
|
|
152
|
-
* @returns {Promise<OpenOrder>} Promise resolving to the open order object, or undefined if not found.
|
|
153
|
-
* @example
|
|
154
|
-
* import { getOpenOrder } from '@clober/v2-sdk'
|
|
155
|
-
*
|
|
156
|
-
* const openOrder = await getOpenOrder(
|
|
157
|
-
* 421614,
|
|
158
|
-
* '46223845323662364279893361453861711542636620039907198451770258805035840307200'
|
|
159
|
-
* )
|
|
160
|
-
*/
|
|
161
85
|
const getOpenOrder = async (chainId, id, options) => {
|
|
162
86
|
return (0, open_order_1.fetchOpenOrder)(chainId, id, options?.rpcUrl);
|
|
163
87
|
};
|
|
164
88
|
exports.getOpenOrder = getOpenOrder;
|
|
165
|
-
/**
|
|
166
|
-
* Retrieves open orders for the specified user on the given chain.
|
|
167
|
-
*
|
|
168
|
-
* @param {CHAIN_IDS} chainId The chain ID.
|
|
169
|
-
* @param {`0x${string}`} userAddress The Ethereum address of the user.
|
|
170
|
-
* @param options
|
|
171
|
-
* @param options.rpcUrl The RPC URL of the blockchain.
|
|
172
|
-
* @returns {Promise<OpenOrder[]>} Promise resolving to an array of open orders.
|
|
173
|
-
* @example
|
|
174
|
-
* import { getOpenOrders } from '@clober/v2-sdk'
|
|
175
|
-
*
|
|
176
|
-
* const openOrders = await getOpenOrders(
|
|
177
|
-
* 421614,
|
|
178
|
-
* '0x5F79EE8f8fA862E98201120d83c4eC39D9468D49'
|
|
179
|
-
* )
|
|
180
|
-
*/
|
|
181
89
|
const getOpenOrders = async (chainId, userAddress, options) => {
|
|
182
90
|
return (0, open_order_1.fetchOpenOrders)(chainId, userAddress, options?.rpcUrl);
|
|
183
91
|
};
|
package/dist/cjs/view.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"view.js","sourceRoot":"","sources":["../../src/view.ts"],"names":[],"mappings":";;;AAAA,+BAA8D;AAE9D,0CAA2C;AAG3C,2CAA2C;AAC3C,6CAA6C;AAC7C,kDAAmE;
|
|
1
|
+
{"version":3,"file":"view.js","sourceRoot":"","sources":["../../src/view.ts"],"names":[],"mappings":";;;AAAA,+BAA8D;AAE9D,0CAA2C;AAG3C,2CAA2C;AAC3C,6CAA6C;AAC7C,kDAAmE;AAqB5D,MAAM,SAAS,GAAG,KAAK,EAC5B,OAAkB,EAClB,MAAqB,EACrB,MAAqB,EACrB,OAA6B,EACZ,EAAE;IACnB,IAAI,IAAA,qBAAc,EAAC,MAAM,EAAE,MAAM,CAAC,EAAE,CAAC;QACnC,MAAM,IAAI,KAAK,CAAC,qCAAqC,CAAC,CAAA;IACxD,CAAC;IACD,MAAM,MAAM,GAAG,MAAM,IAAA,oBAAW,EAAC,OAAO,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE,MAAM,CAAC,CAAA;IAC5E,OAAO;QACL,OAAO;QACP,KAAK,EAAE,MAAM,CAAC,KAAK;QACnB,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,QAAQ,EAAE,MAAM,CAAC,QAAQ;QACzB,QAAQ,EAAE,MAAM,CAAC,QAAQ;QACzB,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,WAAW,EAAE,MAAM,CAAC,WAAW;QAC/B,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,WAAW,EAAE,MAAM,CAAC,WAAW;KAChC,CAAA;AACH,CAAC,CAAA;AArBY,QAAA,SAAS,aAqBrB;AAuBM,MAAM,iBAAiB,G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B,WAAW;YACX,WAAW;SACZ,CAAC,CACH;KACF,CAAA;AACH,CAAC,CAAA;AAzDY,QAAA,gBAAgB,oBAyD5B;AAkBM,MAAM,YAAY,GAAG,KAAK,EAC/B,OAAkB,EAClB,EAAU,EACV,OAA6B,EACT,EAAE;IACtB,OAAO,IAAA,2BAAc,EAAC,OAAO,EAAE,EAAE,EAAE,OAAO,EAAE,MAAM,CAAC,CAAA;AACrD,CAAC,CAAA;AANY,QAAA,YAAY,gBAMxB;AAkBM,MAAM,aAAa,GAAG,KAAK,EAChC,OAAkB,EAClB,WAA0B,EAC1B,OAA6B,EACP,EAAE;IACxB,OAAO,IAAA,4BAAe,EAAC,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,CAAC,CAAA;AAC/D,CAAC,CAAA;AANY,QAAA,aAAa,iBAMzB"}
|
package/dist/esm/model/book.js
CHANGED
|
@@ -3,124 +3,159 @@ import { TAKER_DEFAULT_POLICY } from '../constants/fee';
|
|
|
3
3
|
import { divide } from '../utils/math';
|
|
4
4
|
import { baseToQuote, quoteToBase } from '../utils/decimals';
|
|
5
5
|
export class Book {
|
|
6
|
-
id;
|
|
7
|
-
base;
|
|
8
|
-
unit;
|
|
9
|
-
quote;
|
|
10
|
-
depths;
|
|
11
6
|
constructor({ id, base, quote, unit, depths, }) {
|
|
7
|
+
Object.defineProperty(this, "id", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
configurable: true,
|
|
10
|
+
writable: true,
|
|
11
|
+
value: void 0
|
|
12
|
+
});
|
|
13
|
+
Object.defineProperty(this, "base", {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
configurable: true,
|
|
16
|
+
writable: true,
|
|
17
|
+
value: void 0
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(this, "unit", {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
configurable: true,
|
|
22
|
+
writable: true,
|
|
23
|
+
value: void 0
|
|
24
|
+
});
|
|
25
|
+
Object.defineProperty(this, "quote", {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
configurable: true,
|
|
28
|
+
writable: true,
|
|
29
|
+
value: void 0
|
|
30
|
+
});
|
|
31
|
+
Object.defineProperty(this, "depths", {
|
|
32
|
+
enumerable: true,
|
|
33
|
+
configurable: true,
|
|
34
|
+
writable: true,
|
|
35
|
+
value: void 0
|
|
36
|
+
});
|
|
37
|
+
Object.defineProperty(this, "take", {
|
|
38
|
+
enumerable: true,
|
|
39
|
+
configurable: true,
|
|
40
|
+
writable: true,
|
|
41
|
+
value: ({ limitPrice, amountOut, // quote
|
|
42
|
+
}) => {
|
|
43
|
+
let takenQuoteAmount = 0n;
|
|
44
|
+
let spendBaseAmount = 0n;
|
|
45
|
+
if (this.depths.length === 0) {
|
|
46
|
+
return {
|
|
47
|
+
takenQuoteAmount,
|
|
48
|
+
spendBaseAmount,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
const ticks = this.depths
|
|
52
|
+
.sort((a, b) => Number(b.tick) - Number(a.tick))
|
|
53
|
+
.map((depth) => depth.tick);
|
|
54
|
+
let index = 0;
|
|
55
|
+
let tick = ticks[index];
|
|
56
|
+
while (tick > -8388608n) {
|
|
57
|
+
if (limitPrice > toPrice(tick)) {
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
let maxAmount = TAKER_DEFAULT_POLICY.usesQuote
|
|
61
|
+
? TAKER_DEFAULT_POLICY.calculateOriginalAmount(amountOut - takenQuoteAmount, true)
|
|
62
|
+
: amountOut - takenQuoteAmount;
|
|
63
|
+
maxAmount = divide(maxAmount, this.unit, true);
|
|
64
|
+
if (maxAmount === 0n) {
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
const currentDepth = this.depths.find((depth) => depth.tick === tick);
|
|
68
|
+
let quoteAmount = (currentDepth.rawAmount > maxAmount
|
|
69
|
+
? maxAmount
|
|
70
|
+
: currentDepth.rawAmount) * this.unit;
|
|
71
|
+
let baseAmount = quoteToBase(tick, quoteAmount, true);
|
|
72
|
+
if (TAKER_DEFAULT_POLICY.usesQuote) {
|
|
73
|
+
quoteAmount =
|
|
74
|
+
quoteAmount - TAKER_DEFAULT_POLICY.calculateFee(quoteAmount, false);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
baseAmount =
|
|
78
|
+
baseAmount + TAKER_DEFAULT_POLICY.calculateFee(baseAmount, false);
|
|
79
|
+
}
|
|
80
|
+
if (quoteAmount === 0n) {
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
takenQuoteAmount += quoteAmount;
|
|
84
|
+
spendBaseAmount += baseAmount;
|
|
85
|
+
if (amountOut <= takenQuoteAmount) {
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
index++;
|
|
89
|
+
tick = ticks[index];
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
takenQuoteAmount,
|
|
93
|
+
spendBaseAmount,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
Object.defineProperty(this, "spend", {
|
|
98
|
+
enumerable: true,
|
|
99
|
+
configurable: true,
|
|
100
|
+
writable: true,
|
|
101
|
+
value: ({ limitPrice, amountIn, // base
|
|
102
|
+
}) => {
|
|
103
|
+
let takenQuoteAmount = 0n;
|
|
104
|
+
let spendBaseAmount = 0n;
|
|
105
|
+
if (this.depths.length === 0) {
|
|
106
|
+
return {
|
|
107
|
+
takenQuoteAmount,
|
|
108
|
+
spendBaseAmount,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
const ticks = this.depths
|
|
112
|
+
.sort((a, b) => Number(b.tick) - Number(a.tick))
|
|
113
|
+
.map((depth) => depth.tick);
|
|
114
|
+
let index = 0;
|
|
115
|
+
let tick = ticks[index];
|
|
116
|
+
while (spendBaseAmount <= amountIn && tick > -8388608n) {
|
|
117
|
+
if (limitPrice > toPrice(tick)) {
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
let maxAmount = TAKER_DEFAULT_POLICY.usesQuote
|
|
121
|
+
? amountIn - spendBaseAmount
|
|
122
|
+
: TAKER_DEFAULT_POLICY.calculateOriginalAmount(amountIn - spendBaseAmount, false);
|
|
123
|
+
maxAmount = baseToQuote(tick, maxAmount, false) / this.unit;
|
|
124
|
+
if (maxAmount === 0n) {
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
const currentDepth = this.depths.find((depth) => depth.tick === tick);
|
|
128
|
+
let quoteAmount = (currentDepth.rawAmount > maxAmount
|
|
129
|
+
? maxAmount
|
|
130
|
+
: currentDepth.rawAmount) * this.unit;
|
|
131
|
+
let baseAmount = quoteToBase(tick, quoteAmount, true);
|
|
132
|
+
if (TAKER_DEFAULT_POLICY.usesQuote) {
|
|
133
|
+
quoteAmount =
|
|
134
|
+
quoteAmount - TAKER_DEFAULT_POLICY.calculateFee(quoteAmount, false);
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
baseAmount =
|
|
138
|
+
baseAmount + TAKER_DEFAULT_POLICY.calculateFee(baseAmount, false);
|
|
139
|
+
}
|
|
140
|
+
if (baseAmount === 0n) {
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
takenQuoteAmount += quoteAmount;
|
|
144
|
+
spendBaseAmount += baseAmount;
|
|
145
|
+
index++;
|
|
146
|
+
tick = ticks[index];
|
|
147
|
+
}
|
|
148
|
+
return {
|
|
149
|
+
takenQuoteAmount,
|
|
150
|
+
spendBaseAmount,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
});
|
|
12
154
|
this.id = id;
|
|
13
155
|
this.base = base;
|
|
14
156
|
this.unit = unit;
|
|
15
157
|
this.quote = quote;
|
|
16
158
|
this.depths = depths;
|
|
17
159
|
}
|
|
18
|
-
take = ({ limitPrice, amountOut, // quote
|
|
19
|
-
}) => {
|
|
20
|
-
let takenQuoteAmount = 0n;
|
|
21
|
-
let spendBaseAmount = 0n;
|
|
22
|
-
if (this.depths.length === 0) {
|
|
23
|
-
return {
|
|
24
|
-
takenQuoteAmount,
|
|
25
|
-
spendBaseAmount,
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
const ticks = this.depths
|
|
29
|
-
.sort((a, b) => Number(b.tick) - Number(a.tick))
|
|
30
|
-
.map((depth) => depth.tick);
|
|
31
|
-
let index = 0;
|
|
32
|
-
let tick = ticks[index];
|
|
33
|
-
while (tick > -8388608n) {
|
|
34
|
-
if (limitPrice > toPrice(tick)) {
|
|
35
|
-
break;
|
|
36
|
-
}
|
|
37
|
-
let maxAmount = TAKER_DEFAULT_POLICY.usesQuote
|
|
38
|
-
? TAKER_DEFAULT_POLICY.calculateOriginalAmount(amountOut - takenQuoteAmount, true)
|
|
39
|
-
: amountOut - takenQuoteAmount;
|
|
40
|
-
maxAmount = divide(maxAmount, this.unit, true);
|
|
41
|
-
if (maxAmount === 0n) {
|
|
42
|
-
break;
|
|
43
|
-
}
|
|
44
|
-
const currentDepth = this.depths.find((depth) => depth.tick === tick);
|
|
45
|
-
let quoteAmount = (currentDepth.rawAmount > maxAmount
|
|
46
|
-
? maxAmount
|
|
47
|
-
: currentDepth.rawAmount) * this.unit;
|
|
48
|
-
let baseAmount = quoteToBase(tick, quoteAmount, true);
|
|
49
|
-
if (TAKER_DEFAULT_POLICY.usesQuote) {
|
|
50
|
-
quoteAmount =
|
|
51
|
-
quoteAmount - TAKER_DEFAULT_POLICY.calculateFee(quoteAmount, false);
|
|
52
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
baseAmount =
|
|
55
|
-
baseAmount + TAKER_DEFAULT_POLICY.calculateFee(baseAmount, false);
|
|
56
|
-
}
|
|
57
|
-
if (quoteAmount === 0n) {
|
|
58
|
-
break;
|
|
59
|
-
}
|
|
60
|
-
takenQuoteAmount += quoteAmount;
|
|
61
|
-
spendBaseAmount += baseAmount;
|
|
62
|
-
if (amountOut <= takenQuoteAmount) {
|
|
63
|
-
break;
|
|
64
|
-
}
|
|
65
|
-
index++;
|
|
66
|
-
tick = ticks[index];
|
|
67
|
-
}
|
|
68
|
-
return {
|
|
69
|
-
takenQuoteAmount,
|
|
70
|
-
spendBaseAmount,
|
|
71
|
-
};
|
|
72
|
-
};
|
|
73
|
-
spend = ({ limitPrice, amountIn, // base
|
|
74
|
-
}) => {
|
|
75
|
-
let takenQuoteAmount = 0n;
|
|
76
|
-
let spendBaseAmount = 0n;
|
|
77
|
-
if (this.depths.length === 0) {
|
|
78
|
-
return {
|
|
79
|
-
takenQuoteAmount,
|
|
80
|
-
spendBaseAmount,
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
const ticks = this.depths
|
|
84
|
-
.sort((a, b) => Number(b.tick) - Number(a.tick))
|
|
85
|
-
.map((depth) => depth.tick);
|
|
86
|
-
let index = 0;
|
|
87
|
-
let tick = ticks[index];
|
|
88
|
-
while (spendBaseAmount <= amountIn && tick > -8388608n) {
|
|
89
|
-
if (limitPrice > toPrice(tick)) {
|
|
90
|
-
break;
|
|
91
|
-
}
|
|
92
|
-
let maxAmount = TAKER_DEFAULT_POLICY.usesQuote
|
|
93
|
-
? amountIn - spendBaseAmount
|
|
94
|
-
: TAKER_DEFAULT_POLICY.calculateOriginalAmount(amountIn - spendBaseAmount, false);
|
|
95
|
-
maxAmount = baseToQuote(tick, maxAmount, false) / this.unit;
|
|
96
|
-
if (maxAmount === 0n) {
|
|
97
|
-
break;
|
|
98
|
-
}
|
|
99
|
-
const currentDepth = this.depths.find((depth) => depth.tick === tick);
|
|
100
|
-
let quoteAmount = (currentDepth.rawAmount > maxAmount
|
|
101
|
-
? maxAmount
|
|
102
|
-
: currentDepth.rawAmount) * this.unit;
|
|
103
|
-
let baseAmount = quoteToBase(tick, quoteAmount, true);
|
|
104
|
-
if (TAKER_DEFAULT_POLICY.usesQuote) {
|
|
105
|
-
quoteAmount =
|
|
106
|
-
quoteAmount - TAKER_DEFAULT_POLICY.calculateFee(quoteAmount, false);
|
|
107
|
-
}
|
|
108
|
-
else {
|
|
109
|
-
baseAmount =
|
|
110
|
-
baseAmount + TAKER_DEFAULT_POLICY.calculateFee(baseAmount, false);
|
|
111
|
-
}
|
|
112
|
-
if (baseAmount === 0n) {
|
|
113
|
-
break;
|
|
114
|
-
}
|
|
115
|
-
takenQuoteAmount += quoteAmount;
|
|
116
|
-
spendBaseAmount += baseAmount;
|
|
117
|
-
index++;
|
|
118
|
-
tick = ticks[index];
|
|
119
|
-
}
|
|
120
|
-
return {
|
|
121
|
-
takenQuoteAmount,
|
|
122
|
-
spendBaseAmount,
|
|
123
|
-
};
|
|
124
|
-
};
|
|
125
160
|
}
|
|
126
161
|
//# sourceMappingURL=book.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"book.js","sourceRoot":"","sources":["../../../src/model/book.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AACvC,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAA;AACvD,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAA;AACtC,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AA2B5D,MAAM,OAAO,IAAI;
|
|
1
|
+
{"version":3,"file":"book.js","sourceRoot":"","sources":["../../../src/model/book.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AACvC,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAA;AACvD,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAA;AACtC,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AA2B5D,MAAM,OAAO,IAAI;IAOf,YAAY,EACV,EAAE,EACF,IAAI,EACJ,KAAK,EACL,IAAI,EACJ,MAAM,GAOP;QAlBD;;;;;WAAU;QACV;;;;;WAAc;QACd;;;;;WAAY;QACZ;;;;;WAAe;QACf;;;;;WAAkB;QAsBlB;;;;mBAAO,CAAC,EACN,UAAU,EACV,SAAS,EAAE,QAAQ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AAK,CAAC,CAAA;oBACvE,CAAC;yBAAM,CAAC;wBACN,UAAU;4BACR,UAAU,GAAG,oBAAoB,CAAC,YAAY,CAAC,UAAU,EAAE,KAAK,CAAC,CAAA;oBACrE,CAAC;oBACD,IAAI,UAAU,KAAK,EAAE,EAAE,CAAC;wBACtB,MAAK;oBACP,CAAC;oBAED,gBAAgB,IAAI,WAAW,CAAA;oBAC/B,eAAe,IAAI,UAAU,CAAA;oBAC7B,KAAK,EAAE,CAAA;oBACP,IAAI,GAAG,KAAK,CAAC,KAAK,CAAE,CAAA;gBACtB,CAAC;gBACD,OAAO;oBACL,gBAAgB;oBAChB,eAAe;iBAChB,CAAA;YACH,CAAC;WAAA;QAxIC,IAAI,CAAC,EAAE,GAAG,EAAE,CAAA;QACZ,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;QAClB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;IACtB,CAAC;CAoIF"}
|
|
@@ -1,13 +1,51 @@
|
|
|
1
1
|
import { divide } from '../utils/math';
|
|
2
2
|
export class FeePolicy {
|
|
3
|
-
static MAX_FEE_RATE = 500000n;
|
|
4
|
-
static MIN_FEE_RATE = -500000n;
|
|
5
|
-
static RATE_MASK = 0x7fffffn;
|
|
6
|
-
RATE_PRECISION = 10n ** 6n;
|
|
7
|
-
usesQuote;
|
|
8
|
-
value;
|
|
9
|
-
rate;
|
|
10
3
|
constructor(usesQuote, rate) {
|
|
4
|
+
Object.defineProperty(this, "RATE_PRECISION", {
|
|
5
|
+
enumerable: true,
|
|
6
|
+
configurable: true,
|
|
7
|
+
writable: true,
|
|
8
|
+
value: 10n ** 6n
|
|
9
|
+
});
|
|
10
|
+
Object.defineProperty(this, "usesQuote", {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
configurable: true,
|
|
13
|
+
writable: true,
|
|
14
|
+
value: void 0
|
|
15
|
+
});
|
|
16
|
+
Object.defineProperty(this, "value", {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
configurable: true,
|
|
19
|
+
writable: true,
|
|
20
|
+
value: void 0
|
|
21
|
+
});
|
|
22
|
+
Object.defineProperty(this, "rate", {
|
|
23
|
+
enumerable: true,
|
|
24
|
+
configurable: true,
|
|
25
|
+
writable: true,
|
|
26
|
+
value: void 0
|
|
27
|
+
});
|
|
28
|
+
Object.defineProperty(this, "calculateFee", {
|
|
29
|
+
enumerable: true,
|
|
30
|
+
configurable: true,
|
|
31
|
+
writable: true,
|
|
32
|
+
value: (amount, reverseRounding) => {
|
|
33
|
+
const positive = this.rate > 0n;
|
|
34
|
+
const absRate = positive ? this.rate : -this.rate;
|
|
35
|
+
const absFee = divide(amount * absRate, this.RATE_PRECISION, reverseRounding ? !positive : positive);
|
|
36
|
+
return positive ? absFee : -absFee;
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
Object.defineProperty(this, "calculateOriginalAmount", {
|
|
40
|
+
enumerable: true,
|
|
41
|
+
configurable: true,
|
|
42
|
+
writable: true,
|
|
43
|
+
value: (amount, reverseFee) => {
|
|
44
|
+
const positive = this.rate > 0;
|
|
45
|
+
const divider = this.RATE_PRECISION + (reverseFee ? -this.rate : this.rate);
|
|
46
|
+
return divide(amount * this.RATE_PRECISION, divider, positive);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
11
49
|
this.usesQuote = usesQuote;
|
|
12
50
|
this.rate = rate;
|
|
13
51
|
if (rate > FeePolicy.MAX_FEE_RATE || rate < FeePolicy.MIN_FEE_RATE) {
|
|
@@ -20,19 +58,31 @@ export class FeePolicy {
|
|
|
20
58
|
const usesQuote = value >> 23n > 0n;
|
|
21
59
|
return new FeePolicy(usesQuote, FeePolicy.getRateFromValue(value));
|
|
22
60
|
}
|
|
23
|
-
static getRateFromValue = (value) => {
|
|
24
|
-
return (value & FeePolicy.RATE_MASK) - FeePolicy.MAX_FEE_RATE;
|
|
25
|
-
};
|
|
26
|
-
calculateFee = (amount, reverseRounding) => {
|
|
27
|
-
const positive = this.rate > 0n;
|
|
28
|
-
const absRate = positive ? this.rate : -this.rate;
|
|
29
|
-
const absFee = divide(amount * absRate, this.RATE_PRECISION, reverseRounding ? !positive : positive);
|
|
30
|
-
return positive ? absFee : -absFee;
|
|
31
|
-
};
|
|
32
|
-
calculateOriginalAmount = (amount, reverseFee) => {
|
|
33
|
-
const positive = this.rate > 0;
|
|
34
|
-
const divider = this.RATE_PRECISION + (reverseFee ? -this.rate : this.rate);
|
|
35
|
-
return divide(amount * this.RATE_PRECISION, divider, positive);
|
|
36
|
-
};
|
|
37
61
|
}
|
|
62
|
+
Object.defineProperty(FeePolicy, "MAX_FEE_RATE", {
|
|
63
|
+
enumerable: true,
|
|
64
|
+
configurable: true,
|
|
65
|
+
writable: true,
|
|
66
|
+
value: 500000n
|
|
67
|
+
});
|
|
68
|
+
Object.defineProperty(FeePolicy, "MIN_FEE_RATE", {
|
|
69
|
+
enumerable: true,
|
|
70
|
+
configurable: true,
|
|
71
|
+
writable: true,
|
|
72
|
+
value: -500000n
|
|
73
|
+
});
|
|
74
|
+
Object.defineProperty(FeePolicy, "RATE_MASK", {
|
|
75
|
+
enumerable: true,
|
|
76
|
+
configurable: true,
|
|
77
|
+
writable: true,
|
|
78
|
+
value: 0x7fffffn
|
|
79
|
+
});
|
|
80
|
+
Object.defineProperty(FeePolicy, "getRateFromValue", {
|
|
81
|
+
enumerable: true,
|
|
82
|
+
configurable: true,
|
|
83
|
+
writable: true,
|
|
84
|
+
value: (value) => {
|
|
85
|
+
return (value & FeePolicy.RATE_MASK) - FeePolicy.MAX_FEE_RATE;
|
|
86
|
+
}
|
|
87
|
+
});
|
|
38
88
|
//# sourceMappingURL=fee-policy.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fee-policy.js","sourceRoot":"","sources":["../../../src/model/fee-policy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,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