Skip to content

Commit

Permalink
Revert "Merge branch 'main' into testnet-prod"
Browse files Browse the repository at this point in the history
This reverts commit 00f4318, reversing
changes made to 0c08528.
  • Loading branch information
preethamr committed Sep 14, 2023
1 parent 00f4318 commit 6db06e8
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 360 deletions.
72 changes: 66 additions & 6 deletions packages/adapters/subgraph/.graphclientrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,17 @@ sources:
- name: Connext_Mainnet
handler:
graphql:
strategy: fallback
strategy: highestValue
strategyConfig:
selectionSet: |
{
_meta {
block {
number
}
}
}
value: "_meta.block.number"
sources:
- endpoint: "https://gateway.thegraph.com/api/{env.GRAPH_API_KEY}/subgraphs/id/DfD1tZSmDtjCGC2LeYEQbVzj9j8kNqKAQEsYL27Vg6Sw"
retry: 2
Expand All @@ -373,7 +383,17 @@ sources:
- name: Connext_Optimism
handler:
graphql:
strategy: fallback
strategy: highestValue
strategyConfig:
selectionSet: |
{
_meta {
block {
number
}
}
}
value: "_meta.block.number"
sources:
- endpoint: https://api.thegraph.com/subgraphs/name/connext/amarok-runtime-v0-optimism
retry: 2
Expand All @@ -393,7 +413,17 @@ sources:
- name: Connext_Polygon
handler:
graphql:
strategy: fallback
strategy: highestValue
strategyConfig:
selectionSet: |
{
_meta {
block {
number
}
}
}
value: "_meta.block.number"
sources:
- endpoint: "https://gateway.thegraph.com/api/{env.GRAPH_API_KEY}/subgraphs/id/6NccoNRo22DgB7oCHaG7U5MxvfYXcCERRN1oVtCWm32x"
retry: 5
Expand All @@ -413,7 +443,17 @@ sources:
- name: Connext_ArbitrumOne
handler:
graphql:
strategy: fallback
strategy: highestValue
strategyConfig:
selectionSet: |
{
_meta {
block {
number
}
}
}
value: "_meta.block.number"
sources:
- endpoint: "https://gateway.thegraph.com/api/{env.GRAPH_API_KEY}/subgraphs/id/DNQfRxJou184r5YagTz9HUQST5URsY2WdYkegH9Rrdkf"
retry: 5
Expand All @@ -433,7 +473,17 @@ sources:
- name: Connext_Bnb
handler:
graphql:
strategy: fallback
strategy: highestValue
strategyConfig:
selectionSet: |
{
_meta {
block {
number
}
}
}
value: "_meta.block.number"
sources:
- endpoint: https://api.thegraph.com/subgraphs/name/connext/amarok-runtime-v0-bnb
retry: 2
Expand All @@ -453,7 +503,17 @@ sources:
- name: Connext_Xdai
handler:
graphql:
strategy: fallback
strategy: highestValue
strategyConfig:
selectionSet: |
{
_meta {
block {
number
}
}
}
value: "_meta.block.number"
sources:
- endpoint: "https://gateway.thegraph.com/api/{env.GRAPH_API_KEY}/subgraphs/id/EdS5b1Zbw8A5KHs4JRnWDKdM5K2Hnq97UuE8RG9b61Ey"
retry: 2
Expand Down
4 changes: 0 additions & 4 deletions packages/agents/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

## Next Release

## v2.1.3-alpha.1

- `updateSlippage`: Fix decimal parsing

## v2.1.2

- `options`: SDK takes passed in provider
Expand Down
2 changes: 1 addition & 1 deletion packages/agents/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@connext/sdk-core",
"version": "2.1.3-alpha.1",
"version": "2.1.2",
"description": "Client-side package for interacting with the Connext protocol",
"author": "Connext",
"license": "MIT",
Expand Down
5 changes: 4 additions & 1 deletion packages/agents/sdk/src/sdkBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,10 @@ export class SdkBase extends SdkShared {
canonicalId: transfer.canonical_id,
};

const data = this.contracts.connext.encodeFunctionData("forceUpdateSlippage", [transferInfo, _newSlippage]);
const data = this.contracts.connext.encodeFunctionData("forceUpdateSlippage", [
transferInfo,
utils.parseUnits(_newSlippage),
]);

const txRequest = {
to: ConnextContractAddress,
Expand Down
138 changes: 39 additions & 99 deletions packages/agents/sdk/src/sdkPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,8 @@ export class SdkPool extends SdkShared {
* @param originTokenAddress - The address of the token to be bridged from origin.
* @param amount - The amount of the origin token to bridge, in the origin token's native decimal precision.
* @param receiveLocal - (optional) Whether the desired destination token is the local asset ("nextAsset").
* @param checkFastLiquidity - (optional) Check for fast liquidity availability. False will assume fast liquidity is available.
* @returns
* - amountReceived: Estimated amount received in the decimal precision of the final destination token.
* - originSlippage: Slippage for the origin swap (0 if no swap) in BPS. Negative values indicate positive slippage.
* - destinationSlippage: Slippage for the destination swap (0 if no swap) in BPS. Negative values indicate positive slippage.
* - routerFee: Fee taken by the router in the amount of the local origin asset.
* - isFastPath: Boolean indicating if the fast liquidity path is available.
* @param checkFastLiquidity - (optional) Whether to check for fast liquidity availability.
* @returns Estimated amount received for local/adopted assets, if applicable, in their native decimal precisions.
*/
async calculateAmountReceived(
originDomain: string,
Expand Down Expand Up @@ -253,122 +248,67 @@ export class SdkPool extends SdkShared {
throw new Error("Origin token cannot be bridged to any token on this destination domain");
}

/**
* ------------------------------------
* Origin-side calculations
* ------------------------------------
*/

let originAmountReceived = amount;
let originSlippage = BigNumber.from(0);

// Swap IFF supplied origin token is an adopted asset
let originAmountReceived = amount;
if (!isNextAsset && originPool) {
const originAmountReceivedAfterSwap = await this.calculateSwapLocal(
originAmountReceived = await this.calculateSwapLocal(
originDomain,
originPool,
_originTokenAddress,
originPool.adopted.index,
originPool.local.index,
amount,
);

// Pool assets may have different decimals
const originLocalDecimals = originPool.local.decimals;
const originAdoptedDecimals = originPool.adopted.decimals;

// Convert decimals to the higher precision
const higherPrecisionDecimals = Math.max(originLocalDecimals, originAdoptedDecimals);
const originAmountReceivedConverted = BigNumber.from(originAmountReceived).mul(
BigNumber.from(10).pow(higherPrecisionDecimals - originAdoptedDecimals),
);
const originAmountReceivedAfterSwapConverted = originAmountReceivedAfterSwap.mul(
BigNumber.from(10).pow(higherPrecisionDecimals - originLocalDecimals),
);

originSlippage = BigNumber.from(
originAmountReceivedConverted
.sub(originAmountReceivedAfterSwapConverted ?? originAmountReceivedConverted)
.mul(10000)
.div(originAmountReceivedConverted),
);

// Convert origin amount received post-swap to the local decimal precision
originAmountReceived = originAmountReceivedAfterSwapConverted.div(
BigNumber.from(10).pow(higherPrecisionDecimals - originLocalDecimals),
);
}

/**
* ------------------------------------
* Router / fast liquidity calculations
* ------------------------------------
*/

// Determine if fast liquidity is available (pre-destination-swap amount)
let isFastPath = true;
if (checkFastLiquidity) {
const activeLiquidity = await this.getActiveLiquidity(destinationDomain, destinationAssetData.local);
if (activeLiquidity?.length > 0) {
const total_balance: string = activeLiquidity[0].total_balance.toString();
isFastPath = BigNumber.from(this.scientificToBigInt(total_balance)).mul(70).div(100).gt(originAmountReceived);
}
}

// Subtract router fee if fast liquidity is available
const originSlippage = BigNumber.from(amount).sub(originAmountReceived).mul(10000).div(amount);
const feeBps = BigNumber.from(+DEFAULT_ROUTER_FEE * 100);
const routerFee = BigNumber.from(originAmountReceived).mul(feeBps).div(10000);
if (isFastPath) {
originAmountReceived = BigNumber.from(originAmountReceived).sub(routerFee);
}

/**
* ------------------------------------
* Destination-side calculations
* ------------------------------------
*/

const destinationPool = await this.getPool(destinationDomain, destinationAssetData.local);
let destinationAmountReceived = originAmountReceived;
let destinationSlippage = BigNumber.from(0);
const destinationAmount = BigNumber.from(originAmountReceived).sub(routerFee);
let destinationAmountReceived = destinationAmount;

const promises: Promise<any>[] = [];

// Swap IFF desired destination token is an adopted asset
if (!receiveLocal && destinationPool) {
const destinationAmountReceivedAfterSwap = await this.calculateSwapLocal(
destinationDomain,
destinationPool,
destinationAssetData.local,
destinationPool.local.index,
destinationPool.adopted.index,
destinationAmountReceived,
promises.push(
this.calculateSwapLocal(
destinationDomain,
destinationPool,
destinationAssetData.local,
destinationPool.local.index,
destinationPool.adopted.index,
destinationAmount,
),
);
} else {
promises.push(Promise.resolve(undefined));
}

// Pool assets may have different decimals
const destinationLocalDecimals = destinationPool.local.decimals;
const destinationAdoptedDecimals = destinationPool.adopted.decimals;

// Convert decimals to the higher precision
const higherPrecisionDecimals = Math.max(destinationLocalDecimals, destinationAdoptedDecimals);
const destinationAmountReceivedConverted = BigNumber.from(destinationAmountReceived).mul(
BigNumber.from(10).pow(higherPrecisionDecimals - destinationLocalDecimals),
);
const destinationAmountReceivedAfterSwapConverted = destinationAmountReceivedAfterSwap.mul(
BigNumber.from(10).pow(higherPrecisionDecimals - destinationAdoptedDecimals),
);
// Determine if fast liquidity is available (pre-destination-swap amount)
if (checkFastLiquidity) {
promises.push(this.getActiveLiquidity(destinationDomain, destinationAssetData.local));
}

destinationSlippage = BigNumber.from(
destinationAmountReceivedConverted
.sub(destinationAmountReceivedAfterSwapConverted ?? destinationAmountReceivedConverted)
.mul(10000)
.div(destinationAmountReceivedConverted),
);
const [destinationAmountReceivedSwap, activeLiquidity] = await Promise.all(promises);
destinationAmountReceived = destinationAmountReceivedSwap ?? destinationAmountReceived;

// Convert destination amount received post-swap to the adopted decimal precision
destinationAmountReceived = destinationAmountReceivedAfterSwapConverted.div(
BigNumber.from(10).pow(higherPrecisionDecimals - destinationAdoptedDecimals),
);
// Default true, set to false if fast liquidity is not available
let isFastPath = true;
if (activeLiquidity?.length > 0) {
const total_balance: string = activeLiquidity[0].total_balance.toString();
isFastPath = BigNumber.from(this.scientificToBigInt(total_balance)).mul(70).div(100).gt(destinationAmount);
}

const destinationSlippage = BigNumber.from(
destinationAmount
.sub(destinationAmountReceived ?? destinationAmount)
.mul(10000)
.div(destinationAmount),
);

return {
amountReceived: destinationAmountReceived,
originSlippage,
Expand Down
Loading

0 comments on commit 6db06e8

Please sign in to comment.