Skip to content

Commit

Permalink
Merge pull request #23 from blockydevs/feat/ibc-transfer-review-impro…
Browse files Browse the repository at this point in the history
…vments

refactor: add changes according to review
  • Loading branch information
KacperKoza343 authored Jan 17, 2025
2 parents e0b95c6 + 14d0d37 commit d356fde
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/plugin-cosmos/src/actions/ibc-transfer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export const createIBCTransferAction = (
const availableChains = runtime.getSetting("COSMOS_AVAILABLE_CHAINS");
const availableChainsArray = availableChains?.split(",");

return !(mnemonic && availableChains && availableChainsArray.length);
return !!(mnemonic && availableChains && availableChainsArray.length);
},
examples: [
[
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-cosmos/src/actions/ibc-transfer/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { z } from "zod";
export const IBCTransferParamsSchema = z.object({
chainName: z.string(),
symbol: z.string(),
amount: z.string(),
toAddress: z.string(),
amount: z.string().regex(/^\d+$/, "Amount must be a numeric string"),
toAddress: z.string().regex(/^[a-z0-9]+$/, "Invalid bech32 address format"),
targetChainName: z.string(),
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,20 @@ export const bridgeDenomProvider: IDenomProvider = async (
sourceAssetChainId
);

const ibcAssetData = bridgeData.dest_assets[destChainId]?.assets?.find(
const destAssets = bridgeData.dest_assets[destChainId];

if (!destAssets?.assets) {
throw new Error(`No assets found for chain ${destChainId}`);
}

const ibcAssetData = destAssets.assets?.find(
({ origin_denom }) => origin_denom === sourceAssetDenom
);

if (!ibcAssetData) {
throw new Error(`No matching asset found for denom ${sourceAssetDenom}`);
}

if (!ibcAssetData.denom) {
throw new Error("No IBC asset data");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,18 @@ export class IBCTransferAction implements ICosmosActionService {
throw new Error("Cannot find destination chain");
}

const { denom: destAssetDenom } = await bridgeDenomProvider(
const bridgeDenomResult = await bridgeDenomProvider(
denom.base,
sourceChain.chain_id,
destChain.chain_id
);

if (!bridgeDenomResult || !bridgeDenomResult.denom) {
throw new Error("Failed to get destination asset denomination");
}

const destAssetDenom = bridgeDenomResult.denom;

const route = await skipClient.route({
destAssetChainID: destChain.chain_id,
destAssetDenom,
Expand Down Expand Up @@ -110,13 +116,22 @@ export class IBCTransferAction implements ICosmosActionService {

let txHash: string | undefined;

await skipClient.executeRoute({
route,
userAddresses,
onTransactionCompleted: async (_, executeRouteTxHash) => {
txHash = executeRouteTxHash;
},
});
try {
await skipClient.executeRoute({
route,
userAddresses,
onTransactionCompleted: async (_, executeRouteTxHash) => {
txHash = executeRouteTxHash;
},
});
} catch (error) {
throw new Error(`Failed to execute route: ${error?.message}`);
}

if (!txHash) {
throw new Error("Transaction hash is undefined after executing route");
}

return {
from: senderAddress,
to: params.toAddress,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export class SkipApiAssetsFromSourceFetcher {
headers: {
"Content-Type": "application/json",
},
timeout: 5000,
});

const validResponse = skipApiAssetsFromSourceResponseSchema.parse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ describe("IBCTransferAction", () => {
const senderAddress = "cosmos1senderaddress";
const targetChainId = "target-chain-id";
const sourceChainId = "source-chain-id";
const mockTxHash = "mock_tx_hash_123";

mockWalletChains.getWalletAddress.mockResolvedValue(senderAddress);
// @ts-expect-error --- ...
Expand All @@ -149,18 +150,15 @@ describe("IBCTransferAction", () => {
getAssetBySymbol.mockReturnValue({
base: "uatom",
});
const params = {
chainName: "test-chain",
targetChainName: "target-chain",
symbol: "ATOM",
amount: "10",
toAddress: "cosmos1receiveraddress",
};

mockBridgeDenomProvider.mockResolvedValue({ denom: "uatom" });
mockSkipClient.route.mockResolvedValue({
requiredChainAddresses: [sourceChainId, targetChainId],
});
mockSkipClient.executeRoute.mockImplementation(async ({ onTransactionCompleted }) => {
await onTransactionCompleted(null, mockTxHash);
});

// @ts-expect-error --- ...
const ibcTransferAction = new IBCTransferAction(mockWalletChains);

Expand All @@ -173,7 +171,7 @@ describe("IBCTransferAction", () => {
expect(result).toEqual({
from: senderAddress,
to: params.toAddress,
txHash: undefined,
txHash: mockTxHash,
});
expect(mockSkipClient.executeRoute).toHaveBeenCalled();
});
Expand Down

0 comments on commit d356fde

Please sign in to comment.