Skip to content

Commit

Permalink
fix cosmos client retry; fix rounding mode (#891)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-nax authored Sep 24, 2024
1 parent 17137d9 commit 4be5d85
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 107 deletions.
1 change: 1 addition & 0 deletions spaceward/src/components/ConnectWallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function ConnectWallet() {
const { balance } = useAsset("award");
const wardAmount = BigInt(balance?.amount || "0");
const ward = bigintToFixed(wardAmount, {
ceil: true,
decimals: 18,
display: 2,
});
Expand Down
64 changes: 32 additions & 32 deletions spaceward/src/components/ui/icons-crypto.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,9 @@ export const NetworkIcons: Record<
gradientUnits="userSpaceOnUse"
gradientTransform="translate(22.1185 3.26671) scale(7.79125 7.93393)"
>
<stop stop-color="#FBEAF3" stop-opacity="0.6" />
<stop offset="0.6807" stop-color="#A087BF" />
<stop offset="1" stop-color="#17102E" />
<stop stopColor="#FBEAF3" stopOpacity="0.6" />
<stop offset="0.6807" stopColor="#A087BF" />
<stop offset="1" stopColor="#17102E" />
</radialGradient>
<radialGradient
id="paint1_radial_4125_12557"
Expand All @@ -304,9 +304,9 @@ export const NetworkIcons: Record<
gradientUnits="userSpaceOnUse"
gradientTransform="translate(17.7838 6.64925) scale(19.0623 19.4114)"
>
<stop stop-color="#FBEAF3" stop-opacity="0.6" />
<stop offset="0.6807" stop-color="#A087BF" />
<stop offset="1" stop-color="#17102E" />
<stop stopColor="#FBEAF3" stopOpacity="0.6" />
<stop offset="0.6807" stopColor="#A087BF" />
<stop offset="1" stopColor="#17102E" />
</radialGradient>
<linearGradient
id="paint2_linear_4125_12557"
Expand All @@ -316,8 +316,8 @@ export const NetworkIcons: Record<
y2="3.70373"
gradientUnits="userSpaceOnUse"
>
<stop stop-color="#9AD9E5" />
<stop offset="0.6202" stop-color="white" stop-opacity="0" />
<stop stopColor="#9AD9E5" />
<stop offset="0.6202" stopColor="white" stopOpacity="0" />
</linearGradient>
<linearGradient
id="paint3_linear_4125_12557"
Expand All @@ -327,8 +327,8 @@ export const NetworkIcons: Record<
y2="14.2314"
gradientUnits="userSpaceOnUse"
>
<stop offset="0.2888" stop-color="white" />
<stop offset="0.7796" stop-color="white" stop-opacity="0" />
<stop offset="0.2888" stopColor="white" />
<stop offset="0.7796" stopColor="white" stopOpacity="0" />
</linearGradient>
<linearGradient
id="paint4_linear_4125_12557"
Expand All @@ -338,8 +338,8 @@ export const NetworkIcons: Record<
y2="18.1153"
gradientUnits="userSpaceOnUse"
>
<stop stop-color="#384EA2" />
<stop offset="0.9952" stop-color="#D54799" />
<stop stopColor="#384EA2" />
<stop offset="0.9952" stopColor="#D54799" />
</linearGradient>
<linearGradient
id="paint5_linear_4125_12557"
Expand All @@ -349,8 +349,8 @@ export const NetworkIcons: Record<
y2="23.7993"
gradientUnits="userSpaceOnUse"
>
<stop offset="0.2888" stop-color="white" />
<stop offset="0.7796" stop-color="white" stop-opacity="0" />
<stop offset="0.2888" stopColor="white" />
<stop offset="0.7796" stopColor="white" stopOpacity="0" />
</linearGradient>
<linearGradient
id="paint6_linear_4125_12557"
Expand All @@ -360,8 +360,8 @@ export const NetworkIcons: Record<
y2="18.1153"
gradientUnits="userSpaceOnUse"
>
<stop stop-color="#2B2F82" />
<stop offset="0.9952" stop-color="#72439A" />
<stop stopColor="#2B2F82" />
<stop offset="0.9952" stopColor="#72439A" />
</linearGradient>
<linearGradient
id="paint7_linear_4125_12557"
Expand All @@ -371,8 +371,8 @@ export const NetworkIcons: Record<
y2="14.2684"
gradientUnits="userSpaceOnUse"
>
<stop stop-color="#2B2F82" />
<stop offset="0.9952" stop-color="#A14298" />
<stop stopColor="#2B2F82" />
<stop offset="0.9952" stopColor="#A14298" />
</linearGradient>
<linearGradient
id="paint8_linear_4125_12557"
Expand All @@ -382,8 +382,8 @@ export const NetworkIcons: Record<
y2="6.36639"
gradientUnits="userSpaceOnUse"
>
<stop offset="0.2888" stop-color="white" />
<stop offset="0.7796" stop-color="white" stop-opacity="0" />
<stop offset="0.2888" stopColor="white" />
<stop offset="0.7796" stopColor="white" stopOpacity="0" />
</linearGradient>
<linearGradient
id="paint9_linear_4125_12557"
Expand All @@ -393,8 +393,8 @@ export const NetworkIcons: Record<
y2="20.4008"
gradientUnits="userSpaceOnUse"
>
<stop offset="0.2888" stop-color="white" />
<stop offset="0.7796" stop-color="white" stop-opacity="0" />
<stop offset="0.2888" stopColor="white" />
<stop offset="0.7796" stopColor="white" stopOpacity="0" />
</linearGradient>
<linearGradient
id="paint10_linear_4125_12557"
Expand All @@ -404,8 +404,8 @@ export const NetworkIcons: Record<
y2="20.1522"
gradientUnits="userSpaceOnUse"
>
<stop offset="0.2888" stop-color="white" />
<stop offset="0.7796" stop-color="white" stop-opacity="0" />
<stop offset="0.2888" stopColor="white" />
<stop offset="0.7796" stopColor="white" stopOpacity="0" />
</linearGradient>
<linearGradient
id="paint11_linear_4125_12557"
Expand All @@ -415,8 +415,8 @@ export const NetworkIcons: Record<
y2="21.4365"
gradientUnits="userSpaceOnUse"
>
<stop offset="0.2888" stop-color="white" />
<stop offset="0.7796" stop-color="white" stop-opacity="0" />
<stop offset="0.2888" stopColor="white" />
<stop offset="0.7796" stopColor="white" stopOpacity="0" />
</linearGradient>
<linearGradient
id="paint12_linear_4125_12557"
Expand All @@ -426,8 +426,8 @@ export const NetworkIcons: Record<
y2="21.1703"
gradientUnits="userSpaceOnUse"
>
<stop offset="0.2888" stop-color="white" />
<stop offset="0.7796" stop-color="white" stop-opacity="0" />
<stop offset="0.2888" stopColor="white" />
<stop offset="0.7796" stopColor="white" stopOpacity="0" />
</linearGradient>
<linearGradient
id="paint13_linear_4125_12557"
Expand All @@ -437,8 +437,8 @@ export const NetworkIcons: Record<
y2="19.9226"
gradientUnits="userSpaceOnUse"
>
<stop offset="0.2888" stop-color="white" />
<stop offset="0.7796" stop-color="white" stop-opacity="0" />
<stop offset="0.2888" stopColor="white" />
<stop offset="0.7796" stopColor="white" stopOpacity="0" />
</linearGradient>
<linearGradient
id="paint14_linear_4125_12557"
Expand All @@ -448,8 +448,8 @@ export const NetworkIcons: Record<
y2="17.9167"
gradientUnits="userSpaceOnUse"
>
<stop offset="0.2888" stop-color="white" />
<stop offset="0.7796" stop-color="white" stop-opacity="0" />
<stop offset="0.2888" stopColor="white" />
<stop offset="0.7796" stopColor="white" stopOpacity="0" />
</linearGradient>
</defs>
</svg>
Expand Down
26 changes: 15 additions & 11 deletions spaceward/src/features/actions/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,17 +269,16 @@ export const handleEth = async ({
throw new Error("walletconnect not initialized");
}

return await w
.respondSessionRequest({
topic: walletConnectTopic,
response: {
jsonrpc: "2.0",
id: walletConnectRequestId,
result: res.hash,
},
})
// fixme
.then(() => true);
await w.respondSessionRequest({
topic: walletConnectTopic,
response: {
jsonrpc: "2.0",
id: walletConnectRequestId,
result: res.hash,
},
});

return true;
}

return provider.waitForTransaction(res.hash).then(() => {
Expand Down Expand Up @@ -401,6 +400,7 @@ export const handleCosmos = async ({
}

const client = await StargateClient.connect(rpc);

const res = await client.broadcastTx(
cosmos.tx.v1beta1.TxRaw.encode(txRaw).finish(),
);
Expand All @@ -409,5 +409,9 @@ export const handleCosmos = async ({
throw new Error("broadcast failed");
}

queryClient.invalidateQueries({
queryKey: getBalanceQueryKey("cosmos", chainName, "").slice(0, -1),
});

return true;
};
66 changes: 3 additions & 63 deletions spaceward/src/features/assets/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,81 +1,21 @@
import { walletContext } from "@cosmos-kit/react-lite";
import type { ExtendedHttpEndpoint, WalletManager } from "@cosmos-kit/core";
import { AddressType } from "@wardenprotocol/wardenjs/codegen/warden/warden/v1beta3/key";
import { cosmos } from "@wardenprotocol/wardenjs";
import { useQueries, useQuery } from "@tanstack/react-query";
import { useContext, useEffect, useMemo, useState } from "react";
import { COSMOS_CHAINS } from "@/config/tokens";
import { useContext, useMemo } from "react";
import { useQueryHooks } from "@/hooks/useClient";
import {
balancesQueryCosmos,
balancesQueryEth,
fiatPricesQuery,
queryCosmosClients,
} from "./queries";
import type { CosmosQueryClient, PriceMapSlinky } from "./types";
import type { PriceMapSlinky } from "./types";

const DERIVE_ADDRESSES = [
AddressType.ADDRESS_TYPE_ETHEREUM,
AddressType.ADDRESS_TYPE_OSMOSIS,
];

const queryCosmosClients = (walletManager: WalletManager) => {
const rpcClients: Record<string, CosmosQueryClient | undefined> = {};
const rpcRetry: Record<string, number> = {};

return {
queryKey: ["cosmos", "rpcClients"],
queryFn: async () => {
const clients: [CosmosQueryClient, string][] = [];

for (let i = 0; i < COSMOS_CHAINS.length; i++) {
const { chainName, rpc } = COSMOS_CHAINS[i];
let client = rpcClients[chainName];

if (client) {
// todo implement client health check
clients.push([client, chainName]);
continue;
}

let endpoint: ExtendedHttpEndpoint | string;

if (rpc) {
const retry = rpcRetry[chainName] ?? 0;
endpoint = rpc[retry % rpc.length];
rpcRetry[chainName] = retry + 1;
} else {
const repo = walletManager.getWalletRepo(chainName);
repo.activate();

try {
endpoint = await repo.getRpcEndpoint();
} catch (e) {
console.error(e);
endpoint = `https://rpc.cosmos.directory/${chainName}`;
}
}

try {
const client =
await cosmos.ClientFactory.createRPCQueryClient({
rpcEndpoint:
typeof endpoint === "string"
? endpoint
: endpoint.url,
});

clients.push([client, chainName]);
} catch (e) {
console.error(e);
continue;
}
}

return clients;
},
} as const;
};

export const useAssetQueries = (spaceId?: string | null) => {
const { walletManager } = useContext(walletContext);
const { isReady, useKeysBySpaceId, slinky } = useQueryHooks();
Expand Down
Loading

0 comments on commit 4be5d85

Please sign in to comment.