Skip to content

Commit

Permalink
chore: fix commandId generation
Browse files Browse the repository at this point in the history
  • Loading branch information
npty committed Apr 11, 2024
1 parent 2e45927 commit 81d68c4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 27 deletions.
9 changes: 6 additions & 3 deletions src/libs/TransactionRecoveryApi/AxelarGMPRecoveryAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,9 @@ export class AxelarGMPRecoveryAPI extends AxelarRecoveryApi {
});
}

public getCidFromSrcTxHash(destChainId: string, txHash: string, eventIndex: number) {
return getCommandId(destChainId, txHash, eventIndex, this.environment, rpcInfo);
public getCidFromSrcTxHash(destChainId: string, messageId: string, eventIndex: number) {
const chainId = rpcInfo[this.environment].networkInfo[destChainId.toLowerCase()]?.chainId;
return getCommandId(messageId, eventIndex, chainId);
}

public async doesTxMeetConfirmHt(chain: string, txHash: string, provider?: JsonRpcProvider) {
Expand Down Expand Up @@ -244,8 +245,10 @@ export class AxelarGMPRecoveryAPI extends AxelarRecoveryApi {
};
}

const commandId = this.getCidFromSrcTxHash(destChainId, srcTxHash, eventIndex);
const eventResponse = await this.axelarQueryApi.getEVMEvent(srcChainId, srcTxHash, eventIndex);
const isCallContract = eventResponse?.event?.contractCall ? true : false;
const messageId = isCallContract ? `${srcTxHash}-${eventIndex}` : srcTxHash;
const commandId = this.getCidFromSrcTxHash(destChainId, messageId, eventIndex);

if (!eventResponse || this.isEVMEventFailed(eventResponse)) {
const errorMessage = this.isEVMEventFailed(eventResponse)
Expand Down
38 changes: 16 additions & 22 deletions src/libs/TransactionRecoveryApi/helpers/getCommandId.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
import { arrayify, keccak256 } from "ethers/lib/utils";
import { Environment } from "src/libs/types";
import { RPCInfoType } from "../constants/chain";
import { arrayify, concat, hexlify, hexZeroPad, keccak256 } from "ethers/lib/utils";

export const getCommandId = (
chainName: string,
txHash: string,
sourceEventIndex: number,
environment: Environment,
rpcInfo: RPCInfoType
) => {
const chainID: number = rpcInfo[environment].networkInfo[chainName.toLowerCase()]?.chainId;
if (!chainID) return "";
const seiArr = arrayify(sourceEventIndex).reverse();
const txHashWithEventIndex = new Uint8Array([
...arrayify(txHash),
...new Uint8Array(8).map((a, i) => seiArr[i] || a),
]);
const chainIdByteArray = arrayify(chainID);
const dataToHash = new Uint8Array(txHashWithEventIndex.length + chainIdByteArray.length);
dataToHash.set(txHashWithEventIndex, 0);
dataToHash.set(chainIdByteArray, txHashWithEventIndex.length);
return keccak256(dataToHash).slice(2); // remove 0x prefix
const stringToCharcodeArray = (text: string) => Array.from(text, (char) => char.charCodeAt(0));

// This function should be called from evm source chain only. It doesn't work properly if it's called from cosmos-based or others chains.
export const getCommandId = (messageId: string, sourceEventIndex: number, chainId: number) => {
if (messageId.includes("-")) {
return keccak256(concat([stringToCharcodeArray(messageId), hexlify(chainId)]));
} else {
return keccak256(
concat([
messageId,
arrayify(hexZeroPad(hexlify(sourceEventIndex), 8)).reverse(),
hexlify(chainId),
])
);
}
};
26 changes: 26 additions & 0 deletions src/libs/test/TransactionRecoveryAPI/AxelarGMPRecoveryAPI.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,32 @@ describe("AxelarGMPRecoveryAPI", () => {
});
});

describe("getCidFromSrcTxHash", () => {
const mainnetApi = new AxelarGMPRecoveryAPI({ environment: Environment.MAINNET });

// https://axelarscan.io/gmp/0x3828bf893801f337e08d15b89efc9c3c2d9196fe7f83f3b7640425b24d122cb2:12
it("should return the correct commandId from evm -> evm for ContractCallWithToken event", () => {
expect(
mainnetApi.getCidFromSrcTxHash(
"celo",
"0x3828bf893801f337e08d15b89efc9c3c2d9196fe7f83f3b7640425b24d122cb2",
8
)
).toEqual("0xa45da101fcfed541b8251cb8a288b5b7dd84086377eb9cf3f8d4a99f11e062e0");
});

// https://axelarscan.io/gmp/0x92f676751feccab46a048a16aaf81b26620a3683933b56a722ce742de8ea7429:349
it("should return the correct commandId from evm -> evm for ContractCall event", () => {
expect(
mainnetApi.getCidFromSrcTxHash(
"blast",
"0x92f676751feccab46a048a16aaf81b26620a3683933b56a722ce742de8ea7429-5",
5
)
).toEqual("0xe6868c6e94240fa6a37cc71d265106a00ad8fa0652319f145e3235f703046574");
});
});

describe.skip("calculateNativeGasFee", () => {
const api = new AxelarGMPRecoveryAPI({ environment: Environment.TESTNET });

Expand Down
2 changes: 0 additions & 2 deletions src/libs/test/TransactionRecoveryAPI/EncodingTests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ describe("AxelarDepositRecoveryAPI", () => {
const txHash = "0x2c9083bebd1f82b86b7b0d3298885f90767b584742df9ec3a9c9f15872a1fff9";
const eventIndex = await api.getEventIndex("ethereum-2" as EvmChain, txHash);
const res = await api.getCidFromSrcTxHash(EvmChain.MOONBEAM, txHash, eventIndex as number);
console.log("eventIndex", eventIndex);
console.log("res", res);
expect(res).toEqual("58c46960e6483f61bf206d1bd1819917d2b009f58d7050e05b4be1d13247b4ed");
}, 60000);
});
Expand Down

0 comments on commit 81d68c4

Please sign in to comment.