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

Feature/axelar query migration #416

Merged
merged 17 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions .changeset/cool-foxes-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@axelarjs/api": patch
---

Add getNativeGasBaseFee to the axelar-query API, migrated it from axelarjs-sdk.
9 changes: 9 additions & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@
"devDependencies": {
"@axelarjs/config": "workspace:*",
"@cosmjs/stargate": "^0.31.3",
"@cosmjs/tendermint-rpc": "0.31.0-alpha.1",
"@types/standard-http-error": "^2.0.1",
"@types/node": "^20.11.28",
"@types/clone-deep": "^4.0.1",
"dotenv": "^16.4.5",
"fast-check": "^3.17.0",
"gh-pages": "^6.1.1",
Expand All @@ -105,8 +108,14 @@
"@axelarjs/core": "workspace:*",
"@axelarjs/cosmos": "workspace:*",
"@axelarjs/utils": "workspace:*",
"@axelarjs/proto": "workspace:*",
"clone-deep": "^4.0.1",
"cross-fetch": "^4.0.0",
"ethers": "^6.13.2",
"isomorphic-unfetch": "^4.0.2",
"rambda": "^9.1.1",
"string-similarity-js": "^2.1.4",
"standard-http-error": "^2.0.1",
SGiaccobasso marked this conversation as resolved.
Show resolved Hide resolved
"viem": "^2.8.18"
}
}
20 changes: 20 additions & 0 deletions packages/api/src/axelar-query/AxelarQueryClient/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { QueryClient } from "@cosmjs/stargate";
import { Tendermint34Client } from "@cosmjs/tendermint-rpc";

import { EnvironmentConfigs, getConfigs } from "../constants";
import { AxelarQueryClientConfig } from "../types";
import { AxelarQueryService, setupQueryExtension } from "./types/index";

export type AxelarQueryClientType = QueryClient & AxelarQueryService;

export class AxelarQueryClient extends QueryClient {
static async initOrGetAxelarQueryClient(config: AxelarQueryClientConfig) {
const { axelarRpcUrl, environment } = config;
const links: EnvironmentConfigs = getConfigs(environment);
const rpc: string = axelarRpcUrl || links.axelarRpcUrl;
return QueryClient.withExtensions(
await Tendermint34Client.connect(rpc),
setupQueryExtension
);
}
}
35 changes: 35 additions & 0 deletions packages/api/src/axelar-query/AxelarQueryClient/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
QueryService as AxelarnetQS,
QueryServiceClientImpl as AxelarnetQSCI,
} from "@axelarjs/proto/axelar/axelarnet/v1beta1/service";
import {
QueryService as EvmQS,
QueryServiceClientImpl as EVMQSCI,
} from "@axelarjs/proto/axelar/evm/v1beta1/service";
import {
QueryService as NexusQS,
QueryServiceClientImpl as NexusQSCI,
} from "@axelarjs/proto/axelar/nexus/v1beta1/service";
import {
QueryService as TSSQS,
QueryServiceClientImpl as TSSQSCI,
} from "@axelarjs/proto/axelar/tss/v1beta1/service";

import { createProtobufRpcClient, QueryClient } from "@cosmjs/stargate";

export interface AxelarQueryService {
readonly evm: EvmQS;
readonly axelarnet: AxelarnetQS;
readonly nexus: NexusQS;
readonly tss: TSSQS;
}

export function setupQueryExtension(base: QueryClient): AxelarQueryService {
const client = createProtobufRpcClient(base);
return {
evm: new EVMQSCI(client),
axelarnet: new AxelarnetQSCI(client),
nexus: new NexusQSCI(client),
tss: new TSSQSCI(client),
};
}
51 changes: 51 additions & 0 deletions packages/api/src/axelar-query/assets/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import fetch from "cross-fetch";

import type { Environment } from "../..";
import { AssetConfig, LoadAssetConfig } from "./types";

const urlMap: Record<Environment, string> = {
devnet:
"https://axelar-testnet.s3.us-east-2.amazonaws.com/devnet-asset-config.json",
testnet:
"https://axelar-testnet.s3.us-east-2.amazonaws.com/testnet-asset-config.json",
mainnet:
"https://axelar-mainnet.s3.us-east-2.amazonaws.com/mainnet-asset-config.json",
};
const assetMap: {
[key in Environment]: { [key: string]: AssetConfig } | null;
} = {
devnet: null,
testnet: null,
mainnet: null,
};

export async function loadAssets(
config: LoadAssetConfig
): Promise<AssetConfig[]> {
if (assetMap[config.environment])
return Object.values(
assetMap[config.environment] as { [key: string]: AssetConfig }
);

assetMap[config.environment] = await execGet(urlMap[config.environment]);

return Object.values(
assetMap[config.environment] as { [key: string]: AssetConfig }
);
}

async function execGet(url: string): Promise<{ [key: string]: AssetConfig }> {
try {
const response = await fetch(url, {
method: "GET",
headers: { "Content-Type": "application/json" },
});

const result = (await response.json()) as { [key: string]: AssetConfig };

return result;
} catch (error) {
console.error({ error });
throw error;
}
}
SGiaccobasso marked this conversation as resolved.
Show resolved Hide resolved
48 changes: 48 additions & 0 deletions packages/api/src/axelar-query/assets/test/loadAssets.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { loadAssets } from "..";

const mock = {
loadAssets: loadAssets,
};

describe("loadAssets()", () => {
beforeEach(() => {
vitest.clearAllMocks();
vitest.spyOn(mock, "loadAssets");
});

describe("when loadAssets is called with known env, but not mainnet", () => {
beforeEach(async () => {
await mock.loadAssets({
environment: "testnet",
});
});

test("then it should call loadAssets", () => {
expect(mock.loadAssets).toHaveBeenCalledWith({
environment: "testnet",
});
});

// test("then it should return assets", () => {
// expect(mock.loadAssets).toHaveReturnedWith(Object.values(testnet));
// });
});

describe("when loadAssets is called with mainnet", () => {
beforeEach(async () => {
await mock.loadAssets({
environment: "mainnet",
});
});

test("then it should call loadAssets", () => {
expect(mock.loadAssets).toHaveBeenCalledWith({
environment: "mainnet",
});
});

// test("then it should return assets", () => {
// expect(mock.loadAssets).toHaveReturnedWith(Object.values(mainnet));
// });
});
});
34 changes: 34 additions & 0 deletions packages/api/src/axelar-query/assets/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { Environment } from "../../..";

export interface AssetInfo {
assetSymbol?: string;
assetName?: string;
assetAddress?: string;
common_key?: string;
fullDenomPath?: string;
fullySupported?: boolean;
native_chain?: string;
minDepositAmt?: number;
decimals?: number;
ibcDenom?: string;
tokenAddress?: string;
}

export interface AssetInfoForChain extends AssetInfo {
minDepositAmt: number;
}

export interface AssetConfig {
id: string;
common_key: { [env: string]: string };
native_chain: string;
fully_supported: boolean;
decimals: number;
chain_aliases: { [key: string]: AssetInfoForChain };
wrapped_erc20: string;
is_gas_token: boolean;
}

export type LoadAssetConfig = {
environment: Environment;
};
76 changes: 76 additions & 0 deletions packages/api/src/axelar-query/chains/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import cloneDeep from "clone-deep";
import fetch from "cross-fetch";

import type { Environment } from "../..";
import { loadAssets } from "../assets";
import { AssetConfig, AssetInfo } from "../assets/types";
import { ChainInfo, LoadChainConfig } from "./types";

export * from "./supported-chains-list";

export async function loadChains(config: LoadChainConfig) {
const allAssets = await loadAssets(config);
const _environment = config.environment as Environment;

const rawChains: ChainInfo[] = await importChains({
environment: _environment,
});

/*push assets to supported chains*/
rawChains.forEach((chainInfo) => {
const filteredAssetList: AssetConfig[] = allAssets.filter(
({ chain_aliases }) =>
Object.keys(chain_aliases).indexOf(chainInfo.chainName.toLowerCase()) >
-1
SGiaccobasso marked this conversation as resolved.
Show resolved Hide resolved
);

const assetsList: AssetInfo[] = [];

filteredAssetList.forEach((asset) => {
const assetToPush = cloneDeep(
asset.chain_aliases[chainInfo.chainName.toLowerCase()]
) as AssetInfo;
assetToPush.common_key = asset.common_key[_environment] as string;
assetToPush.native_chain = asset.native_chain;
assetToPush.decimals = asset.decimals;
assetToPush.fullySupported = asset.fully_supported;
assetsList.push(assetToPush);
});

chainInfo.assets = assetsList;
});
SGiaccobasso marked this conversation as resolved.
Show resolved Hide resolved

return rawChains;
}

const urlMap: Record<Environment, string> = {
devnet:
"https://axelar-testnet.s3.us-east-2.amazonaws.com/devnet-chain-config.json",
testnet:
"https://axelar-testnet.s3.us-east-2.amazonaws.com/testnet-chain-config.json",
mainnet:
"https://axelar-mainnet.s3.us-east-2.amazonaws.com/mainnet-chain-config.json",
};
SGiaccobasso marked this conversation as resolved.
Show resolved Hide resolved

export async function importChains(
config: LoadChainConfig
): Promise<ChainInfo[]> {
const chainsForEnv = (await execGet(urlMap[config.environment])) as Record<
Environment,
ChainInfo
>;
if (chainsForEnv) return Object.values(chainsForEnv);

return Object.values(chainsForEnv);
}

async function execGet(url: string) {
return fetch(url, {
method: "GET",
headers: { "Content-Type": "application/json" },
})
.then((res) => res.json())
.catch((error) => {
throw error;
});
}
Loading
Loading