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: update sdk to support Amplifier chains #334

Merged
merged 8 commits into from
Oct 18, 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [0.17.0] - 2024-OCTOBER-18

- Fixed the error when trying to add gas for cosmos source chain from the axelarscan ui. #333
- update SDK to support Amplifier chains #334

## [0.16.1] - 2024-MAY-31

- Add Linea and Polygon Sepolia chains to testnet
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@axelar-network/axelarjs-sdk",
"version": "0.16.2",
"version": "0.17.0",
"description": "The JavaScript SDK for Axelar Network",
"repository": {
"type": "git",
Expand Down
8 changes: 6 additions & 2 deletions src/assets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ import { AssetConfig, LoadAssetConfig } from "./types";
import { Environment } from "../libs";

const urlMap: Record<Environment, string> = {
devnet: "https://axelar-testnet.s3.us-east-2.amazonaws.com/devnet-asset-config.json",
"devnet-amplifier": "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: Record<Environment, any> = { devnet: null, testnet: null, mainnet: null };
const assetMap: Record<Environment, any> = {
"devnet-amplifier": null,
testnet: null,
mainnet: null,
};

export async function loadAssets(config: LoadAssetConfig): Promise<AssetConfig[]> {
if (assetMap[config.environment]) return Object.values(assetMap[config.environment]);
Expand Down
29 changes: 27 additions & 2 deletions src/chains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,37 @@ export async function loadChains(config: LoadChainConfig) {
return rawChains;
}

const s3UrlMap: Record<Environment, string> = {
"devnet-amplifier":
"https://axelar-devnet-amplifier.s3.us-east-2.amazonaws.com/configs/devnet-amplifier-config-1.x.json",
testnet: "https://axelar-testnet.s3.us-east-2.amazonaws.com/configs/testnet-config-1.x.json",
mainnet: "https://axelar-mainnet.s3.us-east-2.amazonaws.com/configs/mainnet-config-1.x.json",
};

const urlMap: Record<Environment, string> = {
devnet: "https://axelar-testnet.s3.us-east-2.amazonaws.com/devnet-chain-config.json",
"devnet-amplifier": "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",
};
const chainMap: Record<Environment, any> = { devnet: null, testnet: null, mainnet: null };
const chainMap: Record<Environment, any> = {
"devnet-amplifier": null,
testnet: null,
mainnet: null,
};

const s3Map: Record<Environment, any> = {
"devnet-amplifier": null,
testnet: null,
mainnet: null,
};

export async function importS3Config(environment: Environment): Promise<any> {
if (s3Map[environment]) return s3Map[environment];

s3Map[environment] = await execGet(s3UrlMap[environment]);

return s3Map[environment];
}

export async function importChains(config: LoadChainConfig): Promise<ChainInfo[]> {
if (chainMap[config.environment]) return Object.values(chainMap[config.environment]);
Expand Down
2 changes: 1 addition & 1 deletion src/chains/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface ChainInfo {
module: "axelarnet" | "evm";
confirmLevel?: number;
chainIdentifier: {
devnet: string;
"devnet-amplifier": string;
testnet: string;
mainnet: string;
};
Expand Down
1 change: 0 additions & 1 deletion src/libs/AxelarQueryAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ export class AxelarQueryAPI {
amountInUnits?: BigNumber | string
): Promise<BaseFeeResponse> {
await throwIfInvalidChainIds([sourceChainId, destinationChainId], this.environment);
await this.throwIfInactiveChains([sourceChainId, destinationChainId]);

const params: {
method: string;
Expand Down
12 changes: 5 additions & 7 deletions src/libs/TransactionRecoveryApi/AxelarGMPRecoveryAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -889,17 +889,15 @@ export class AxelarGMPRecoveryAPI extends AxelarRecoveryApi {
};
}

const denomOnSrcChain = getIBCDenomOnSrcChain(
tx.gas_paid?.returnValues?.denom,
chainConfig,
chainConfigs
);
const denom = tx.gas_paid?.returnValues?.asset;

const denomOnSrcChain = getIBCDenomOnSrcChain(denom, chainConfig, chainConfigs);

if (!matchesOriginalTokenPayment(params.token, denomOnSrcChain)) {
return {
success: false,
info: `The token you are trying to send does not match the token originally \
used for gas payment. Please send ${tx.gas_paid?.returnValues?.denom} instead`,
used for gas payment. Please send ${denom} instead`,
};
}

Expand All @@ -913,7 +911,7 @@ export class AxelarGMPRecoveryAPI extends AxelarRecoveryApi {
tx.call.returnValues.destinationChain,
gasLimit,
autocalculateGasOptions?.gasMultipler,
tx.gas_paid?.returnValues?.denom ?? "uaxl"
denom ?? "uaxl"
),
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const COSMOS_GAS_RECEIVER_OPTIONS = {
devnet: "axelar1zl3rxpp70lmte2xr6c4lgske2fyuj3hupcsvcd",
"devnet-amplifier": "axelar1zl3rxpp70lmte2xr6c4lgske2fyuj3hupcsvcd",
testnet: "axelar1zl3rxpp70lmte2xr6c4lgske2fyuj3hupcsvcd",
mainnet: "axelar1aythygn6z5thymj6tmzfwekzh05ewg3l7d6y89",
} as const;
2 changes: 1 addition & 1 deletion src/libs/TransactionRecoveryApi/constants/s3.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default {
mainnet: "https://axelar-mainnet.s3.us-east-2.amazonaws.com/mainnet-config.json",
testnet: "https://axelar-testnet.s3.us-east-2.amazonaws.com/testnet-config.json",
devnet: "https://axelar-testnet.s3.us-east-2.amazonaws.com/testnet-config.json", //same as testnet for now
"devnet-amplifier": "https://axelar-testnet.s3.us-east-2.amazonaws.com/testnet-config.json", //same as testnet for now
};
14 changes: 14 additions & 0 deletions src/libs/test/AxelarQueryAPI.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,20 @@ describe("AxelarQueryAPI", () => {
// gasAmount should be greater than 0.0000001, otherwise we handle decimal conversion incorrectly.
expect(ethers.utils.parseEther("0.0000001").lt(gasAmount as BigNumberish)).toBeTruthy();
});
test("It should return estimated gas amount for an Amplifier chain", async () => {
const gasAmount = await api.estimateGasFee(
CHAINS.TESTNET.AVALANCHE as EvmChain,
"flow",
700000,
1.1,
undefined,
"5000000000"
);

// gasAmount should be greater than 0.0000001, otherwise we handle decimal conversion incorrectly.
expect(ethers.utils.parseEther("0.0000001").lt(gasAmount as BigNumberish)).toBeTruthy();
});

// TODO: fix this test. Potential rounding issue
test.skip("It should use `minGasPrice` if it is greater than the destination chain's gas_price returned from the api", async () => {
const feeStub = getFeeStub();
Expand Down
2 changes: 1 addition & 1 deletion src/libs/test/stubs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export const chainInfoStub = () => ({
txFeeInPercent: 0.1,
module: "axelarnet",
chainIdentifier: {
devnet: "mockedDevnet",
"devnet-amplifier": "mockedDevnet",
testnet: "mockedTestnet",
mainnet: "mockedMainnet",
},
Expand Down
2 changes: 1 addition & 1 deletion src/libs/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { GasToken } from "../../constants/GasToken";
import { EvmChain } from "../../constants/EvmChain";

export enum Environment {
DEVNET = "devnet",
DEVNET = "devnet-amplifier",
TESTNET = "testnet",
MAINNET = "mainnet",
}
Expand Down
28 changes: 26 additions & 2 deletions src/utils/validateChain.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { stringSimilarity } from "string-similarity-js";
import { loadChains } from "../chains";
import { importS3Config, loadChains } from "../chains";
import { Environment } from "../libs";

export async function validateChainIdentifier(chainIdentifier: string, environment: Environment) {
export async function validateChainIdentifierOld(
chainIdentifier: string,
environment: Environment
) {
const chains = await loadChains({
environment,
});
Expand All @@ -18,6 +21,27 @@ export async function validateChainIdentifier(chainIdentifier: string, environme
};
}

export async function validateChainIdentifier(chainIdentifier: string, environment: Environment) {
const s3 = await importS3Config(environment);

if (!s3 || !s3.chains)
return {
foundChain: false,
bestMatch: false,
};

const chainIdentifiers = Object.keys(s3.chains);

const foundChain = chainIdentifiers.find(
(identifier: string) => identifier === chainIdentifier.toLowerCase()
);

return {
foundChain: !!foundChain,
bestMatch: foundChain ? false : findSimilarInArray(chainIdentifiers, chainIdentifier),
};
}

function findSimilarInArray(array: Array<string>, wordsToFind: string) {
let bestMatch = array[0];
let bestScore = 0;
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const getTransferPayload = (
txFeeInPercent: 0.1,
module: "axelarnet",
chainIdentifier: {
devnet: "terra",
"devnet-amplifier": "terra",
testnet: "terra",
mainnet: "terra",
},
Expand All @@ -45,7 +45,7 @@ export const getTransferPayload = (
module: "evm",
confirmLevel: 12,
chainIdentifier: {
devnet: "avalanche",
"devnet-amplifier": "avalanche",
testnet: "avalanche",
mainnet: "avalanche",
},
Expand Down
2 changes: 1 addition & 1 deletion test/integration/parts/01.deposit-address.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe(
"uausdc"
),
axelarAssetTransferTestnet.getDepositAddress(
CHAINS.TESTNET.AURORA,
CHAINS.TESTNET.SEPOLIA,
CHAINS.TESTNET.MOONBEAM,
"0xB8Cd93C83A974649D76B1c19f311f639e62272BC",
"uausdc"
Expand Down
Loading