Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add enoughRouterLiquidity #6018

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: add more filters to getRoutersData
just-a-node committed Apr 24, 2024
commit 5fdd3f0a06b165b1dfbf7570335fa9b662dcbea1
45 changes: 33 additions & 12 deletions packages/agents/sdk/src/sdkUtils.ts
Original file line number Diff line number Diff line change
@@ -94,11 +94,26 @@ export class SdkUtils extends SdkShared {
*}
* ```
*/
async getRoutersData(params?: {
async getRoutersData(params?: {
domain?: string;
localAsset?: string;
adoptedAsset?: string;
canonicalId?: string;
order?: { orderBy?: string; ascOrDesc?: "asc" | "desc" };
limit?: number;
}): Promise<RouterBalance[]> {
const { order, limit } = params ?? {};
const { domain, localAsset, adoptedAsset, canonicalId, order, limit } = params ?? {};

const domainIdentifier = domain ? `domain=eq.${domain}&` : "";
const localAssetIdentifier = localAsset ? `local=eq.${localAsset}&` : "";
const adoptedAssetIdentifier = adoptedAsset ? `adopted=eq.${adoptedAsset}&` : "";
const canonicalIdIdentifier = canonicalId ? `canonical_id=eq.${canonicalId}&` : "";

const searchIdentifier =
domainIdentifier +
localAssetIdentifier +
adoptedAssetIdentifier +
canonicalIdIdentifier;

const orderBy = order?.orderBy || "";
const ascOrDesc = order?.ascOrDesc ? `.${order.ascOrDesc}` : "";
@@ -108,7 +123,7 @@ export class SdkUtils extends SdkShared {
const uri = formatUrl(
this.config.cartographerUrl!,
"routers_with_balances?",
orderIdentifier + limitIdentifier
searchIdentifier + orderIdentifier + limitIdentifier
);
validateUri(uri);

@@ -319,29 +334,35 @@ export class SdkUtils extends SdkShared {
* Checks if enough router liquidity is available for a specific asset.
*
* @param domainId - The domain ID where the asset exists.
* @param asset - The address of the asset.
* @param asset - The address of the local asset.
* @param minLiquidity - The minimum liquidity to check against the sum of max N routers.
* @param maxN - (optional) The max N routers, should match the auction round depth (N = 2^(depth-1).
* @returns The total router liquidity available for the asset.
*
*/
async enoughRouterLiquidity(domainId: string, asset: string, minLiquidity: BigNumberish, maxN?: number): Promise<boolean> {
const _asset = utils.getAddress(asset);
async enoughRouterLiquidity(
domainId: string,
asset: string,
minLiquidity: BigNumberish,
maxN?: number
): Promise<boolean> {
const _asset = asset.toLowerCase();
const _maxN = maxN ?? 4;
const _minLiquidityBN = BigNumber.from(this.scientificToBigInt(minLiquidity.toString()));

const routersByLargestBalance = await this.getRoutersData({ order: { orderBy: "balance", ascOrDesc: "desc" } });
const routersByLargestBalance = await this.getRoutersData({
domain: domainId,
localAsset: _asset,
order: { orderBy: "balance", ascOrDesc: "desc" },
limit: _maxN
});

let totalLiquidity = BigNumber.from(0);
let processedRouters = 0;

for (let routerBalance of routersByLargestBalance) {
if (routerBalance.domain == domainId && utils.getAddress(routerBalance.local) == _asset) {
if (routerBalance.domain == domainId && routerBalance.local == _asset) {
const balanceBN = BigNumber.from(this.scientificToBigInt(routerBalance.balance.toString()));
totalLiquidity = totalLiquidity.add(balanceBN);
processedRouters += 1;
if (totalLiquidity.gte(_minLiquidityBN)) return true;
if (processedRouters == _maxN) break;
}
}

50 changes: 49 additions & 1 deletion packages/agents/sdk/test/sdkUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -101,6 +101,54 @@ describe("SdkUtils", () => {
).to.be.true;
});

it("happy: should work with domain", async () => {
(nxtpUtils as any).config.cartographerUrl = config.cartographerUrl;

await nxtpUtils.getRoutersData({
domain: mock.domain.A
});

expect(stubAxiosGetRequest.calledWith(
config.cartographerUrl + `/routers_with_balances?domain=eq.${mock.domain.A}&`)
).to.be.true;
});

it("happy: should work with localAsset", async () => {
(nxtpUtils as any).config.cartographerUrl = config.cartographerUrl;

await nxtpUtils.getRoutersData({
localAsset: mock.asset.A.address
});

expect(stubAxiosGetRequest.calledWith(
config.cartographerUrl + `/routers_with_balances?local=eq.${mock.asset.A.address}&`)
).to.be.true;
});

it("happy: should work with adoptedAsset", async () => {
(nxtpUtils as any).config.cartographerUrl = config.cartographerUrl;

await nxtpUtils.getRoutersData({
adoptedAsset: mock.asset.A.address
});

expect(stubAxiosGetRequest.calledWith(
config.cartographerUrl + `/routers_with_balances?adopted=eq.${mock.asset.A.address}&`)
).to.be.true;
});

it("happy: should work with canonicalId", async () => {
(nxtpUtils as any).config.cartographerUrl = config.cartographerUrl;

await nxtpUtils.getRoutersData({
canonicalId: "1"
});

expect(stubAxiosGetRequest.calledWith(
config.cartographerUrl + `/routers_with_balances?canonical_id=eq.1&`)
).to.be.true;
});

it("should error if validateUri fails", async () => {
(nxtpUtils as any).config.cartographerUrl = "invalidUrl";

@@ -118,7 +166,7 @@ describe("SdkUtils", () => {
});

describe("#enoughRouterLiquidity", () => {
it("should be true when enough liquidity between <N routers", async () => {
it.only("should be true when enough liquidity between <N routers", async () => {
(nxtpUtils as any).config.cartographerUrl = config.cartographerUrl;

restore();