Skip to content

Commit

Permalink
Merge pull request #127 from bancorprotocol/extend-trade-by-source-am…
Browse files Browse the repository at this point in the history
…ount-input-range

Extend the supported input range of trade-by-source-amount
  • Loading branch information
zavelevsky committed Mar 28, 2024
2 parents 5c007e0 + e69030e commit 5ab9616
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 26 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const init = async (

## Notes

- The SDK Logger supports 3 verbosity levels:
### 1. The SDK Logger supports 3 verbosity levels:
- `0` (default) only prints errors and important logs.
- `1` (debug) prints highly verbose logs.
- `2` (debug readable) is same as `1` but also converts any BigNumber to an easy to read string (impacting performance).
Expand All @@ -91,6 +91,12 @@ const init = async (
window.CARBON_DEFI_SDK_VERBOSITY = 2;
```

### 2. For usage with contracts version < 5, without the enhanced range for trade by source:

```js
window.LEGACY_TRADE_BY_SOURCE_RANGE = true;
```

## Authors

- [@zavelevsky](https://www.github.com/zavelevsky)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@bancor/carbon-sdk",
"type": "module",
"source": "src/index.ts",
"version": "0.0.96-DEV",
"version": "0.0.97-DEV",
"description": "The SDK is a READ-ONLY tool, intended to facilitate working with Carbon contracts. It's a convenient wrapper around our matching algorithm, allowing programs and users get a ready to use transaction data that will allow them to manage strategies and fulfill trades",
"main": "dist/index.cjs",
"module": "dist/index.js",
Expand Down
24 changes: 2 additions & 22 deletions src/common/logger.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@
import { BigNumber } from '../utils/numerics';
import { getRuntimeConfig } from '../runtime-config';

const globalObject = (() => {
try {
return self;
} catch (e) {
try {
return window;
} catch (e) {
return global;
}
}
})();

function getVerbosityLevel(): number {
if (globalObject !== undefined) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return Number((globalObject as any).CARBON_DEFI_SDK_VERBOSITY) || 0;
}

return 0;
}

const verbosity = getVerbosityLevel();
const verbosity = getRuntimeConfig().logVerbosityLevel;

function isVerbose(): boolean {
return verbosity >= 1;
Expand Down
48 changes: 48 additions & 0 deletions src/runtime-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* This module provides a way to read runtime configuration passed by means of global object.
* This is useful in case the configuration is not available at compile time, such as log verbosity.
*/

const globalObject = (() => {
try {
return self;
} catch (e) {
try {
return window;
} catch (e) {
return global;
}
}
})();

export type RuntimeConfig = {
logVerbosityLevel: number;
legacyTradeBySourceRange: boolean;
};

function getVerbosityLevel(): number {
if (globalObject !== undefined) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return Number((globalObject as any).CARBON_DEFI_SDK_VERBOSITY) || 0;
}

return 0;
}

function getLegacyTradeBySourceRange(): boolean {
if (globalObject !== undefined) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return Boolean((globalObject as any).LEGACY_TRADE_BY_SOURCE_RANGE);
}

return false;
}

export function getRuntimeConfig(): RuntimeConfig {
const logVerbosityLevel = getVerbosityLevel();
const legacyTradeBySourceRange = getLegacyTradeBySourceRange();
return {
logVerbosityLevel,
legacyTradeBySourceRange,
};
}
20 changes: 18 additions & 2 deletions src/trade-matcher/trade.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { ONE, Decimal, BigNumber, BigNumberMax } from '../utils/numerics';
import { EncodedOrder, DecodedOrder } from '../common/types';
import { decodeFloat } from '../utils/encoders';
import { getRuntimeConfig } from '../runtime-config';

const isLegacyTradeBySourceRange = getRuntimeConfig().legacyTradeBySourceRange;

const C = BigNumber.from(ONE);

Expand Down Expand Up @@ -33,8 +36,12 @@ const getEncodedTradeBySourceAmount = (
y: BigNumber,
z: BigNumber,
A: BigNumber,
B: BigNumber
B: BigNumber,
forceLegacyTradeBySourceRange: boolean = false
): BigNumber => {
const legacyTradeBySourceRange =
forceLegacyTradeBySourceRange || isLegacyTradeBySourceRange;

if (A.eq(0)) {
return mulDivF(x, mul(B, B), mul(C, C));
}
Expand All @@ -49,7 +56,16 @@ const getEncodedTradeBySourceAmount = (

const temp4 = mulDivC(temp1, temp1, factor);
const temp5 = mulDivC(temp3, A, factor);
return mulDivF(temp2, temp3.div(factor), add(temp4, temp5));
// newer versions of the CarbonController contract can handle extended range
// trades by source. In case the SDK is using an older version of the
// contract, handle the trade "same as before" in here.
// Before - in a case of overflow it would end up with a 0 rate and not get
// picked up for trade. This added check is to avoid the order being picked up
// for trade - just to be reverted in the case of overflow by the contract.
if (legacyTradeBySourceRange || temp4.add(temp5).lte(MAX_UINT256)) {
return mulDivF(temp2, temp3.div(factor), temp4.add(temp5));
}
return temp2.div(add(A, mulDivC(temp1, temp1, temp3)));
};

//
Expand Down
92 changes: 92 additions & 0 deletions tests/data/SpecialMatch.json
Original file line number Diff line number Diff line change
Expand Up @@ -871,5 +871,97 @@
}
]
}
},
{
"method": "matchBySourceAmount",
"amount": "10000000000000000000000",
"orders": [
{
"liquidity": "9694677352470453892074",
"lowestRate": "33333333333333.01391180980424255864136284799315035343170166015625",
"highestRate": "198019801980197.408654344705869476683801622129976749420166015625",
"marginalRate": "104452559155538.8882958589671163947507815520699425817338168004935"
},
{
"liquidity": "96946773524704538920743",
"lowestRate": "33333333333333.01391180980424255864136284799315035343170166015625",
"highestRate": "198019801980197.408654344705869476683801622129976749420166015625",
"marginalRate": "104452559155538.8882958589671163947507815520699425817338168004935"
},
{
"liquidity": "969467735247045389207437",
"lowestRate": "33333333333333.01391180980424255864136284799315035343170166015625",
"highestRate": "198019801980197.408654344705869476683801622129976749420166015625",
"marginalRate": "104452559155538.8882958589671163947507815520699425817338168004935"
},
{
"liquidity": "9694677352470453892074378",
"lowestRate": "33333333333333.01391180980424255864136284799315035343170166015625",
"highestRate": "198019801980197.408654344705869476683801622129976749420166015625",
"marginalRate": "104452559155538.8882958589671163947507815520699425817338168004935"
},
{
"liquidity": "96946773524704538920743789",
"lowestRate": "33333333333333.01391180980424255864136284799315035343170166015625",
"highestRate": "198019801980197.408654344705869476683801622129976749420166015625",
"marginalRate": "104452559155538.8882958589671163947507815520699425817338168004935"
}
],
"actions": {
"Fast": [
{
"id": "0",
"input": "164298820",
"output": "9694677346078191181485"
},
{
"id": "1",
"input": "1642988201",
"output": "96946773494115245173215"
},
{
"id": "2",
"input": "16429882019",
"output": "969467735241152451806436"
},
{
"id": "3",
"input": "164298820191",
"output": "9694677352444857851397115"
},
{
"id": "4",
"input": "1642988201917",
"output": "96946773524681911847310712"
}
],
"Best": [
{
"id": "0",
"input": "164298820",
"output": "9694677346078191181485"
},
{
"id": "1",
"input": "1642988201",
"output": "96946773494115245173215"
},
{
"id": "2",
"input": "16429882019",
"output": "969467735241152451806436"
},
{
"id": "3",
"input": "164298820191",
"output": "9694677352444857851397115"
},
{
"id": "4",
"input": "1642988201917",
"output": "96946773524681911847310712"
}
]
}
}
]

0 comments on commit 5ab9616

Please sign in to comment.