@orbs-network/twap 1.5.12 → 1.5.14
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 +1 -1
- package/dist/src/lib.d.ts +1 -0
- package/dist/src/lib.js +14 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -99,7 +99,7 @@ As a result of this process, an `Order` is generated and held in the `TWAP` cont
|
|
|
99
99
|
* `maker`: order creator (`msg.sender`)
|
|
100
100
|
* `exchange`: swap only on this exchange, or zero for any exchange
|
|
101
101
|
* `srcToken`: input token, required
|
|
102
|
-
* `dstToken`: output token, required
|
|
102
|
+
* `dstToken`: output token, required. If zero address, will unwrap to native token on each chunk.
|
|
103
103
|
* `srcAmount`: input total order amount in `srcToken`, required
|
|
104
104
|
* `srcBidAmount`: input chunk size in `srcToken`, required
|
|
105
105
|
* `dstMinAmount`: minimum output chunk size, in `dstToken`, required. Can be higher than market output (implies a limit order), or as low as 1 wei
|
package/dist/src/lib.d.ts
CHANGED
|
@@ -26,6 +26,7 @@ export declare class TWAPLib {
|
|
|
26
26
|
status: (order: Order) => Status;
|
|
27
27
|
makerBalance(token: TokenData): Promise<BN>;
|
|
28
28
|
wrapNativeToken(amount: BN.Value, priorityFeePerGas?: BN.Value, maxFeePerGas?: BN.Value): Promise<any>;
|
|
29
|
+
unwrapNativeToken(amount: BN.Value, priorityFeePerGas?: BN.Value, maxFeePerGas?: BN.Value): Promise<any>;
|
|
29
30
|
waitForConfirmation<T>(fn: () => Promise<T>): Promise<T>;
|
|
30
31
|
hasAllowance(srcToken: TokenData, amount: BN.Value): Promise<boolean>;
|
|
31
32
|
approve(srcToken: TokenData, amount: BN.Value, priorityFeePerGas?: BN.Value, maxFeePerGas?: BN.Value): Promise<void>;
|
package/dist/src/lib.js
CHANGED
|
@@ -29,7 +29,7 @@ class TWAPLib {
|
|
|
29
29
|
this.provider = provider;
|
|
30
30
|
this.dstAmount = (srcToken, dstToken, srcAmount, srcUsdMarket, dstUsdMarket, limitDstPriceFor1Src, isMarketOrder) => (0, web3_candies_1.convertDecimals)(isMarketOrder ? (0, bignumber_js_1.default)(srcAmount).times(srcUsdMarket).div(dstUsdMarket) : (0, bignumber_js_1.default)(srcAmount).times(limitDstPriceFor1Src), srcToken.decimals, dstToken.decimals).integerValue(bignumber_js_1.default.ROUND_FLOOR);
|
|
31
31
|
this.isNativeToken = (token) => (0, configs_1.isNativeAddress)(token.address);
|
|
32
|
-
this.isWrappedToken = (token) => (0, web3_candies_1.eqIgnoreCase)(token.address, this.config.wToken.address);
|
|
32
|
+
this.isWrappedToken = (token) => (0, web3_candies_1.eqIgnoreCase)(token.address, this.config.wToken.address);
|
|
33
33
|
this.isValidChain = (chainId) => chainId === this.config.chainId;
|
|
34
34
|
this.maxPossibleChunks = (srcToken, srcAmount, srcUsd) => bignumber_js_1.default.max(1, (0, bignumber_js_1.default)(srcAmount).div((0, bignumber_js_1.default)(10).pow(srcToken.decimals).div(srcUsd).times(this.config.minChunkSizeUsd)))
|
|
35
35
|
.integerValue(bignumber_js_1.default.ROUND_FLOOR)
|
|
@@ -72,6 +72,11 @@ class TWAPLib {
|
|
|
72
72
|
return yield this.sendTx((0, web3_candies_1.erc20)(this.config.wToken.symbol, this.config.wToken.address, this.config.wToken.decimals, web3_candies_1.iwethabi).methods.deposit(), priorityFeePerGas, maxFeePerGas, amount);
|
|
73
73
|
});
|
|
74
74
|
}
|
|
75
|
+
unwrapNativeToken(amount, priorityFeePerGas, maxFeePerGas) {
|
|
76
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
77
|
+
return yield this.sendTx((0, web3_candies_1.erc20)(this.config.wToken.symbol, this.config.wToken.address, this.config.wToken.decimals, web3_candies_1.iwethabi).methods.withdraw((0, bignumber_js_1.default)(amount).toFixed(0)), priorityFeePerGas, maxFeePerGas);
|
|
78
|
+
});
|
|
79
|
+
}
|
|
75
80
|
waitForConfirmation(fn) {
|
|
76
81
|
return __awaiter(this, void 0, void 0, function* () {
|
|
77
82
|
const nonceBefore = yield (0, web3_candies_1.web3)().eth.getTransactionCount(this.maker);
|
|
@@ -84,6 +89,8 @@ class TWAPLib {
|
|
|
84
89
|
}
|
|
85
90
|
hasAllowance(srcToken, amount) {
|
|
86
91
|
return __awaiter(this, void 0, void 0, function* () {
|
|
92
|
+
if (this.isNativeToken(srcToken))
|
|
93
|
+
return true;
|
|
87
94
|
const token = (0, web3_candies_1.erc20)(srcToken.symbol, srcToken.address, srcToken.decimals);
|
|
88
95
|
const allowance = (0, bignumber_js_1.default)(yield token.methods.allowance(this.maker, this.config.twapAddress).call());
|
|
89
96
|
return allowance.gte(amount);
|
|
@@ -96,28 +103,22 @@ class TWAPLib {
|
|
|
96
103
|
});
|
|
97
104
|
}
|
|
98
105
|
validateTokens(srcToken, dstToken) {
|
|
99
|
-
if (lodash_1.default.isEqual(srcToken, dstToken))
|
|
106
|
+
if (lodash_1.default.isEqual(srcToken, dstToken) || (this.isNativeToken(srcToken) && this.isNativeToken(dstToken)))
|
|
100
107
|
return TokensValidation.invalid;
|
|
101
|
-
if (
|
|
102
|
-
if ((0, configs_1.isNativeAddress)(dstToken.address))
|
|
103
|
-
return TokensValidation.invalid;
|
|
108
|
+
if (this.isNativeToken(srcToken)) {
|
|
104
109
|
if (this.isWrappedToken(dstToken))
|
|
105
110
|
return TokensValidation.wrapOnly;
|
|
106
111
|
return TokensValidation.wrapAndOrder;
|
|
107
112
|
}
|
|
108
|
-
if (this.isWrappedToken(srcToken))
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
return TokensValidation.valid;
|
|
112
|
-
}
|
|
113
|
-
if ((0, configs_1.isNativeAddress)(dstToken.address)) {
|
|
113
|
+
if (this.isWrappedToken(srcToken) && this.isNativeToken(dstToken))
|
|
114
|
+
return TokensValidation.unwrapOnly;
|
|
115
|
+
if (this.isNativeToken(dstToken))
|
|
114
116
|
return TokensValidation.dstTokenZero;
|
|
115
|
-
}
|
|
116
117
|
return TokensValidation.valid;
|
|
117
118
|
}
|
|
118
119
|
validateOrderInputs(srcToken, dstToken, srcAmount, srcChunkAmount, dstMinChunkAmountOut, deadline, fillDelaySeconds, srcUsd) {
|
|
119
120
|
if (this.validateTokens(srcToken, dstToken) !== TokensValidation.valid)
|
|
120
|
-
return OrderInputValidation.invalidTokens;
|
|
121
|
+
return OrderInputValidation.invalidTokens;
|
|
121
122
|
if ((0, bignumber_js_1.default)(srcAmount).lte(0))
|
|
122
123
|
return OrderInputValidation.invalidSrcAmount;
|
|
123
124
|
if ((0, bignumber_js_1.default)(srcChunkAmount).lte(0) || (0, bignumber_js_1.default)(srcChunkAmount).gt(srcAmount))
|