Skip to content

Commit

Permalink
Handle chains that don't support memos
Browse files Browse the repository at this point in the history
  • Loading branch information
thal0x committed Jul 31, 2023
1 parent 006ecd4 commit 21dd8ac
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 18 deletions.
1 change: 1 addition & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"dependencies": {
"@cosmjs/cosmwasm-stargate": "^0.31.0",
"@cosmjs/encoding": "^0.31.0",
"@cosmjs/math": "^0.30.1",
"@cosmjs/proto-signing": "^0.30.1",
"@cosmjs/stargate": "^0.30.1",
Expand Down
30 changes: 12 additions & 18 deletions src/solve/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import { SkipClient, MsgsRequest } from "./client";
import { trackRoute } from "@/analytics";
import { KeplrClient, KeplrExtensionWallet } from "@cosmos-kit/keplr-extension";
import { MsgTransfer } from "cosmjs-types/ibc/applications/transfer/v1/tx";
import { useQuery } from "@tanstack/react-query";
import { getNumberOfTransactionsFromRoute } from "./utils";

const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

Expand Down Expand Up @@ -218,25 +220,17 @@ export function useSolveForm() {
routeResponse,
]);

const numberOfTransactions = useMemo(() => {
if (!routeResponse) {
return 0;
}

let n = 1;

routeResponse.operations.forEach((hop, i) => {
if (isSwapOperation(hop)) {
return;
const { data: numberOfTransactions } = useQuery({
queryKey: ["solve-number-of-transactions", routeResponse],
queryFn: () => {
if (!routeResponse) {
return 0;
}

if (i !== 0 && !hop.transfer.pfm_enabled) {
n += 1;
}
});

return n;
}, [routeResponse]);
return getNumberOfTransactionsFromRoute(routeResponse);
},
enabled: !!routeResponse,
});

const route = useMemo(() => {
if (
Expand All @@ -259,7 +253,7 @@ export function useSolveForm() {
destinationAsset: formValues.destinationAsset,
destinationChain: formValues.destinationChain,
operations: routeResponse?.operations ?? [],
transactionCount: numberOfTransactions,
transactionCount: numberOfTransactions ?? 0,
actionType,
rawRoute: routeResponse,
};
Expand Down
35 changes: 35 additions & 0 deletions src/solve/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { toBech32 } from "@cosmjs/encoding";
import { getChainByID } from "@/utils/utils";
import { MsgsRequest, RouteResponse, SkipClient } from "./client";
import { Asset, AssetWithMetadata } from "./types";

export function assetHasMetadata(asset: Asset) {
Expand Down Expand Up @@ -27,3 +30,35 @@ export function isAssetWithMetadata(asset: Asset): asset is AssetWithMetadata {
export function filterAssetsWithMetadata(assets: Asset[]) {
return assets.filter(isAssetWithMetadata);
}

export async function getNumberOfTransactionsFromRoute(route: RouteResponse) {
const userAddresses: Record<string, string> = {};
for (const chainID of route.chain_ids) {
const chain = getChainByID(chainID);

// fake address
userAddresses[chainID] = toBech32(
chain.bech32_prefix,
Uint8Array.from(Array.from({ length: 20 }))
);
}

const msgRequest: MsgsRequest = {
source_asset_denom: route.source_asset_denom,
source_asset_chain_id: route.source_asset_chain_id,
dest_asset_denom: route.dest_asset_denom,
dest_asset_chain_id: route.dest_asset_chain_id,
amount_in: route.amount_in,
operations: route.operations,

estimated_amount_out: route.estimated_amount_out,
chain_ids_to_addresses: userAddresses,
slippage_tolerance_percent: "0.05",
};

const skipClient = new SkipClient();

const msgsResponse = await skipClient.fungible.getMessages(msgRequest);

return msgsResponse.msgs.length;
}

0 comments on commit 21dd8ac

Please sign in to comment.