Skip to content

Commit

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

## 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.2",
"version": "2.1.3-alpha.1",
"description": "Client-side package for interacting with the Connext protocol",
"author": "Connext",
"license": "MIT",
Expand Down
5 changes: 1 addition & 4 deletions packages/agents/sdk/src/sdkBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,10 +485,7 @@ export class SdkBase extends SdkShared {
canonicalId: transfer.canonical_id,
};

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

const txRequest = {
to: ConnextContractAddress,
Expand Down
138 changes: 99 additions & 39 deletions packages/agents/sdk/src/sdkPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,13 @@ 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) Whether to check for fast liquidity availability.
* @returns Estimated amount received for local/adopted assets, if applicable, in their native decimal precisions.
* @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.
*/
async calculateAmountReceived(
originDomain: string,
Expand Down Expand Up @@ -248,66 +253,121 @@ export class SdkPool extends SdkShared {
throw new Error("Origin token cannot be bridged to any token on this destination domain");
}

// Swap IFF supplied origin token is an adopted asset
/**
* ------------------------------------
* Origin-side calculations
* ------------------------------------
*/

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

// Swap IFF supplied origin token is an adopted asset
if (!isNextAsset && originPool) {
originAmountReceived = await this.calculateSwapLocal(
const originAmountReceivedAfterSwap = 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),
);
}

const originSlippage = BigNumber.from(amount).sub(originAmountReceived).mul(10000).div(amount);
/**
* ------------------------------------
* 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 feeBps = BigNumber.from(+DEFAULT_ROUTER_FEE * 100);
const routerFee = BigNumber.from(originAmountReceived).mul(feeBps).div(10000);
if (isFastPath) {
originAmountReceived = BigNumber.from(originAmountReceived).sub(routerFee);
}

const destinationPool = await this.getPool(destinationDomain, destinationAssetData.local);
const destinationAmount = BigNumber.from(originAmountReceived).sub(routerFee);
let destinationAmountReceived = destinationAmount;
/**
* ------------------------------------
* Destination-side calculations
* ------------------------------------
*/

const promises: Promise<any>[] = [];
const destinationPool = await this.getPool(destinationDomain, destinationAssetData.local);
let destinationAmountReceived = originAmountReceived;
let destinationSlippage = BigNumber.from(0);

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

// Determine if fast liquidity is available (pre-destination-swap amount)
if (checkFastLiquidity) {
promises.push(this.getActiveLiquidity(destinationDomain, destinationAssetData.local));
}
// Pool assets may have different decimals
const destinationLocalDecimals = destinationPool.local.decimals;
const destinationAdoptedDecimals = destinationPool.adopted.decimals;

const [destinationAmountReceivedSwap, activeLiquidity] = await Promise.all(promises);
destinationAmountReceived = destinationAmountReceivedSwap ?? destinationAmountReceived;
// 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),
);

// 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);
}
destinationSlippage = BigNumber.from(
destinationAmountReceivedConverted
.sub(destinationAmountReceivedAfterSwapConverted ?? destinationAmountReceivedConverted)
.mul(10000)
.div(destinationAmountReceivedConverted),
);

const destinationSlippage = BigNumber.from(
destinationAmount
.sub(destinationAmountReceived ?? destinationAmount)
.mul(10000)
.div(destinationAmount),
);
// Convert destination amount received post-swap to the adopted decimal precision
destinationAmountReceived = destinationAmountReceivedAfterSwapConverted.div(
BigNumber.from(10).pow(higherPrecisionDecimals - destinationAdoptedDecimals),
);
}

return {
amountReceived: destinationAmountReceived,
Expand Down
Loading

0 comments on commit 00f4318

Please sign in to comment.