Skip to content

Commit

Permalink
feat: check if given chains are actived
Browse files Browse the repository at this point in the history
  • Loading branch information
npty committed Jan 17, 2024
1 parent 6eeee7e commit 490b09b
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 7 deletions.
9 changes: 9 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,12 @@ export const DEPOSIT_SERVICE_API_URLS = {
testnet: "https://deposit-service.testnet.axelar.dev",
mainnet: "https://deposit-service.mainnet.axelar.dev",
} as const;

export type DepositServiceAPIUrl = keyof typeof DEPOSIT_SERVICE_API_URLS;

export const AXELAR_RPC_URLS = {
testnet: "https://axelartest-rpc.quickapi.com",
mainnet: "https://axelar-rpc.quickapi.com",
} as const;

export type AxelarRPCUrl = keyof typeof AXELAR_RPC_URLS;
1 change: 1 addition & 0 deletions packages/cosmos/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"long": "^5.2.3"
},
"devDependencies": {
"@axelarjs/core": "workspace:*",
"@tsconfig/strictest": "^2.0.2",
"cosmjs-types": "^0.8.0",
"rambda": "^8.6.0",
Expand Down
1 change: 0 additions & 1 deletion packages/cosmos/src/stargate/spec/constants.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/cosmos/src/stargate/spec/queryClient.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { AXELAR_RPC_URLS } from "@axelarjs/core";
import { TokenType } from "@axelarjs/proto/axelar/evm/v1beta1/query";

import { createAxelarQueryClient } from "../stargateClient";
import { AXELAR_RPC_URL } from "./constants";

describe("query client", () => {
test("query erc20Tokens", async () => {
const client = await createAxelarQueryClient(AXELAR_RPC_URL);
const client = await createAxelarQueryClient(AXELAR_RPC_URLS.testnet);

const erc20Tokens = await client.evm.eRC20Tokens({
chain: "fantom",
Expand Down
7 changes: 4 additions & 3 deletions packages/cosmos/src/stargate/spec/stargateClient.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { AXELAR_RPC_URLS } from "@axelarjs/core";

import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
import { toAccAddress } from "@cosmjs/stargate/build/queryclient/utils";

Expand All @@ -6,7 +8,6 @@ import {
createAxelarSigningClient,
getAxelarSigningClientOptions,
} from "../stargateClient";
import { AXELAR_RPC_URL } from "./constants";
import { MOCK_BROADCAST_RESPONSE } from "./mock";

describe("stargate client", () => {
Expand All @@ -28,7 +29,7 @@ describe("stargate client", () => {
);

const client = await createAxelarSigningClient(
AXELAR_RPC_URL,
AXELAR_RPC_URLS.testnet,
offlineSigner
);

Expand Down Expand Up @@ -66,7 +67,7 @@ describe("stargate client", () => {
);

const client = await createAxelarSigningClient(
AXELAR_RPC_URL,
AXELAR_RPC_URLS.testnet,
offlineSigner
);

Expand Down
1 change: 1 addition & 0 deletions packages/deposit-address/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"dependencies": {
"@axelarjs/api": "workspace:*",
"@axelarjs/core": "workspace:*",
"@axelarjs/cosmos": "workspace:*",
"@axelarjs/utils": "workspace:*",
"bech32": "^2.0.0",
"viem": "1.21.4"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createAxelarConfigClient } from "@axelarjs/api";
import { ENVIRONMENTS } from "@axelarjs/core";

import { unwrappable } from "./depositService";
import { getActiveChains, unwrappable } from "./depositService";

describe("depositService - helper", () => {
describe("unwrappable", () => {
Expand All @@ -22,4 +22,12 @@ describe("depositService - helper", () => {
});
});
});

describe("getActiveChains", () => {
test.only("should return a list of active chains", async () => {
const env = ENVIRONMENTS.testnet;
const activeChains = await getActiveChains(env);
expect(activeChains.length).toBeGreaterThan(0);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { ChainConfigsResponse } from "@axelarjs/api";
import { AXELAR_RPC_URLS, Environment } from "@axelarjs/core";
import { createAxelarQueryClient } from "@axelarjs/cosmos";

import { encodeAbiParameters, keccak256 } from "viem";

import { ChainStatus } from "../../../../proto/build/module/axelar/nexus/v1beta1/query";

export function unwrappable(
destinationChain: string,
asset: string,
Expand All @@ -23,6 +27,19 @@ export function unwrappable(
return false;
}

export async function getActiveChains(
environment: Environment
// chainConfigs: ChainConfig[]
) {
const axelarQueryClient = await createAxelarQueryClient(
AXELAR_RPC_URLS[environment]
);

return axelarQueryClient.nexus
.chains({ status: ChainStatus.CHAIN_STATUS_ACTIVATED })
.then(({ chains }) => chains.map((chain) => chain.toLowerCase()));
}

export function generateRandomSalt(destinationAddress: string) {
return keccak256(
encodeAbiParameters(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ export function validateChainIds(
});
}

export function validateActiveChains(
chainIds: string[],
activeChains: string[]
) {
chainIds.forEach((chainId) => {
if (!activeChains.includes(chainId.toLowerCase()))
throw new Error(`chain ID ${chainId} is not active`);
});
}

export function validateAsset(
chainIds: string[],
assetId: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@ describe("Deposit Address", () => {
expect(depositAddress).toBeDefined();
});

test("should get inactive chain error", async () => {
const params: DepositNativeWrapOptions = {
sourceChain: "terra",
destinationChain: "Fantom",
destinationAddress: "0xB8Cd93C83A974649D76B1c19f311f639e62272BC",
refundAddress: "0xB8Cd93C83A974649D76B1c19f311f639e62272BC",
environment: ENVIRONMENTS.testnet,
};

await expect(getNativeWrapDepositAddress(params)).rejects.toThrowError();
});

test("should throw error when passing invalid source chain or dest chain to wrap deposit address", async () => {
const invalidSourceParams: DepositNativeWrapOptions = {
sourceChain: "Avax",
Expand Down
14 changes: 14 additions & 0 deletions packages/deposit-address/src/get-deposit-address/isomorphic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import type { ChainConfig } from "@axelarjs/api";

import {
generateRandomSalt,
getActiveChains,
getDepositAddressFromAxelarNetwork,
unwrappable,
validateActiveChains,
validateAddress,
validateAsset,
validateChainIds,
Expand Down Expand Up @@ -133,6 +135,12 @@ export async function getNativeWrapDepositAddress(
chainConfigs.chains[params.destinationChain.toLowerCase()] as ChainConfig
);

const activeChains = await getActiveChains(params.environment);
validateActiveChains(
[params.sourceChain, params.destinationChain],
activeChains
);

const { address } =
await dependencies.depositServiceClient.getDepositAddressForNativeWrap({
refundAddress: params.refundAddress,
Expand All @@ -159,6 +167,12 @@ export async function getNativeUnwrapDepositAddress(
chainConfigs.chains[params.destinationChain.toLowerCase()] as ChainConfig
);

const activeChains = await getActiveChains(params.environment);
validateActiveChains(
[params.sourceChain, params.destinationChain],
activeChains
);

const { address } =
await dependencies.depositServiceClient.getDepositAddressForNativeUnwrap({
refundAddress: params.refundAddress,
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 490b09b

Please sign in to comment.