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

Prod sync #6047

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions packages/agents/sdk-wrapper/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## Next Release

## 2.5.0-alpha.4

- `getLatestAssetPrice`: Added to fetch prices from Cartographer

## 2.5.0-alpha.3

- Removes use of relayer fee in native fetched from chaindata

## v2.5.0-alpha.2

- `estimateRelayerFee`: Capped by hardcoded gas estimates of execute
Expand Down
2 changes: 1 addition & 1 deletion packages/agents/sdk-wrapper/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@connext/sdk",
"version": "2.5.0-alpha.3",
"version": "2.5.0-alpha.4",
"description": "Client-side package for interacting with the Connext protocol",
"author": "Connext",
"license": "MIT",
Expand Down
10 changes: 10 additions & 0 deletions packages/agents/sdk-wrapper/src/sdkUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,14 @@ export class SdkUtils extends SdkShared {

return BigNumber.from(response.data);
}

async getLatestAssetPrice(domainId: string, asset: string): Promise<BigNumber> {
const params: { domainId: string; asset: string } = {
domainId,
asset,
};
const response = await axiosPost(`${this.baseUri}/getLatestAssetPrice`, params);

return BigNumber.from(response.data);
}
}
26 changes: 26 additions & 0 deletions packages/agents/sdk-wrapper/test/sdkUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
SdkGetRouterLiquidityParams,
SdkGetRoutersDataParams,
SdkGetTransfersParams,
SdkGetLatestAssetPriceParams,
} from "../src/sdk-types";

const mockConfig = mock.config();
Expand Down Expand Up @@ -183,4 +184,29 @@ describe("#SDKUtils", () => {
expect(res).to.be.deep.eq(expectedRes);
});
});

describe("#getLatestAssetPrice", async () => {
it("happy: should send request with correct params", async () => {
const expectedEndpoint = "/getLatestAssetPrice";
const expectedArgs: SdkGetLatestAssetPriceParams = {
domainId: mock.domain.A,
asset: mock.asset.A.address,
};
const mockServerRes = {
type: "BigNumber",
hex: "0x1",
};
const expectedRes = BigNumber.from(mockServerRes);

axiosPostStub.resolves({
data: mockServerRes,
status: 200,
});

const res = await sdkUtils.getLatestAssetPrice(expectedArgs.domainId, expectedArgs.asset);

expect(axiosPostStub).to.have.been.calledWithExactly(expectedBaseUri + expectedEndpoint, expectedArgs);
expect(res).to.be.deep.eq(expectedRes);
});
});
});
8 changes: 8 additions & 0 deletions packages/agents/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## Next Release

## 2.5.0-alpha.4

- `getLatestAssetPrice`: Added to fetch prices from Cartographer

## 2.5.0-alpha.3

- Removes use of relayer fee in native fetched from chaindata

## v2.5.0-alpha.2

- `estimateRelayerFee`: Capped by hardcoded gas estimates of execute
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.5.0-alpha.3",
"version": "2.5.0-alpha.4",
"description": "Client-side package for interacting with the Connext protocol",
"author": "Connext",
"license": "MIT",
Expand Down
7 changes: 7 additions & 0 deletions packages/agents/sdk/src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,13 @@ export const SdkCheckRouterLiquidityParamsSchema = Type.Object({
});
export type SdkCheckRouterLiquidityParams = Static<typeof SdkCheckRouterLiquidityParamsSchema>;

// getLatestAssetPrice
export const SdkGetLatestAssetPriceParamsSchema = Type.Object({
domainId: Type.String(),
asset: Type.String(),
});
export type SdkGetLatestAssetPriceParams = Static<typeof SdkGetLatestAssetPriceParamsSchema>;

/************************************
SDK Router Types
*************************************/
Expand Down
2 changes: 1 addition & 1 deletion packages/agents/sdk/src/sdkShared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ export class SdkShared {
/**
* Returns the canonical ID and canonical domain of a token.
*
* @param domainId The canonical domain ID of the token.
* @param domainId The domain ID of the token.
* @param tokenAddress The address of the token.
*/
async getCanonicalTokenId(domainId: string, tokenAddress: string): Promise<[string, string]> {
Expand Down
25 changes: 25 additions & 0 deletions packages/agents/sdk/src/sdkUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,29 @@ export class SdkUtils extends SdkShared {
.slice(0, _topN)
.reduce((acc, router) => acc.add(BigNumber.from(router.balance.toString())), BigNumber.from(0));
}

/**
* Fetches asset prices that match filter criteria from Cartographer.
*
* @param domainId - The domain ID for the asset.
* @param asset - The address of the asset.
* @returns The last updated price for the asset.
* ```
*/
async getLatestAssetPrice(domainId: string, asset: string): Promise<BigNumber> {
const canonicalId = await this.getCanonicalTokenId(domainId, asset);
const canonicalIdIdentifier = canonicalId[0] !== "0" ? `canonical_id=eq.${canonicalId[1]}&` : "";

const searchIdentifier = canonicalIdIdentifier;
const orderIdentifier = `order=timestamp.desc`;

const uri = formatUrl(
this.config.cartographerUrl!,
"asset_prices?",
searchIdentifier + orderIdentifier + `&limit=1`,
);
validateUri(uri);

return await axiosGetRequest(uri);
}
}
23 changes: 13 additions & 10 deletions packages/agents/sdk/test/sdkUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ describe("SdkUtils", () => {

stub(ConfigFns, "getConfig").resolves({ nxtpConfig: config, chainData: mockChainData });
stub(SharedFns, "domainToChainId").returns(chainId);
stub(SharedFns, "axiosGetRequest").resolves({});

nxtpUtils = await SdkUtils.create(mockConfig, undefined, mockChainData);
});
Expand Down Expand Up @@ -80,15 +79,6 @@ describe("SdkUtils", () => {
});
});

describe("#getAssetsData", () => {
it("happy: should work", async () => {
(nxtpUtils as any).config.cartographerUrl = config.cartographerUrl;
const res = await nxtpUtils.getAssetsData();

expect(res).to.not.be.undefined;
});
});

describe("#getTransfers", () => {
it("happy: should work with userAddress", async () => {
(nxtpUtils as any).config.cartographerUrl = config.cartographerUrl;
Expand Down Expand Up @@ -185,6 +175,19 @@ describe("SdkUtils", () => {
(nxtpUtils as any).config.cartographerUrl = config.cartographerUrl;
const res = await nxtpUtils.getRouterLiquidity();

expect(res).to.not.be.undefined;
});

});

describe("getLatestAssetPrice", () => {
it("happy: should work", async () => {
(nxtpUtils as any).config.cartographerUrl = config.cartographerUrl;
stub(nxtpUtils, "getCanonicalTokenId").resolves(["123", "0xabc"]);
stub(SharedFns, "axiosGetRequest").resolves({});

const res = await nxtpUtils.getLatestAssetPrice(mock.domain.A, mock.asset.A.address);

expect(res).to.not.be.undefined;
});
});
Expand Down
12 changes: 11 additions & 1 deletion packages/examples/sdk-server/examples/index.http
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ GET {{baseUrl}}/calculateCanonicalKey/1735353714/0x0000000000000000000000007ea6e

##############
### GET getCanonicalTokenId
GET {{baseUrl}}/getCanonicalTokenId/9991/0xeDb95D8037f769B72AAab41deeC92903A98C9E16
GET {{baseUrl}}/getCanonicalTokenId/1869640809/0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1

##############
### POST sendTransaction
Expand Down Expand Up @@ -771,6 +771,16 @@ Content-Type: application/json
"topN": 4
}

##############
### POST getLatestAssetPrice
POST {{baseUrl}}/getLatestAssetPrice
Content-Type: application/json

{
"domainId": "1869640809",
"asset": "0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1"
}

##############
### POST addLiquidityForRouter
POST {{baseUrl}}/addLiquidityForRouter
Expand Down
16 changes: 16 additions & 0 deletions packages/examples/sdk-server/src/routes/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
SdkGetTransfersParams,
SdkCheckRouterLiquidityParamsSchema,
SdkCheckRouterLiquidityParams,
SdkGetLatestAssetPriceParamsSchema,
SdkGetLatestAssetPriceParams,
} from "@connext/sdk-core";
import { createLoggingContext, jsonifyError } from "@connext/nxtp-utils";
import { FastifyInstance } from "fastify";
Expand Down Expand Up @@ -55,4 +57,18 @@ export const utilsRoutes = async (server: FastifyInstance, options: UtilsRoutesO
reply.status(200).send(res);
},
);

s.post<{ Body: SdkGetLatestAssetPriceParams }>(
"/getLatestAssetPrice",
{
schema: {
body: SdkGetLatestAssetPriceParamsSchema,
},
},
async (request, reply) => {
const { domainId, asset } = request.body;
const res = await sdkUtilsInstance.getLatestAssetPrice(domainId, asset);
reply.status(200).send(res);
},
);
};
Loading