Skip to content

Commit

Permalink
chore: cleanup part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszjasiuk committed Oct 7, 2024
1 parent f46e58f commit a313da4
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 227 deletions.
2 changes: 1 addition & 1 deletion apps/namadillo/src/atoms/proposals/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import { TransactionPair, buildTxPair } from "lib/query";
import { GasConfig } from "types";

import { fromHex } from "@cosmjs/encoding";
import { getSdkInstance } from "hooks";
import { ChainSettings } from "types";
import { getSdkInstance } from "utils/sdk";

// TODO: this function is way too big
const decodeProposalType = (
Expand Down
16 changes: 0 additions & 16 deletions apps/namadillo/src/atoms/staking/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
BondMsgValue,
ClaimRewardsMsgValue,
RedelegateMsgValue,
ShieldingTransferMsgValue,
UnbondMsgValue,
WithdrawMsgValue,
} from "@namada/types";
Expand All @@ -22,7 +21,6 @@ import {
createClaimAndStakeTx,
createClaimTx,
createReDelegateTx,
createShieldingTx,
createUnbondTx,
createWithdrawTx,
fetchClaimableRewards,
Expand All @@ -40,20 +38,6 @@ export const getStakingTotalAtom = atomWithQuery<StakingTotals>((get) => {
};
});

export const createShieldingTxAtom = atomWithMutation((get) => {
const chain = get(chainAtom);
return {
mutationKey: ["create-shielding-tx"],
enabled: chain.isSuccess,
mutationFn: async ({
params,
gasConfig,
account,
}: BuildTxAtomParams<ShieldingTransferMsgValue>) =>
createShieldingTx(chain.data!, account, params, gasConfig),
};
});

export const createBondTxAtom = atomWithMutation((get) => {
const chain = get(chainAtom);
return {
Expand Down
22 changes: 1 addition & 21 deletions apps/namadillo/src/atoms/staking/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@ import {
ClaimRewardsMsgValue,
ClaimRewardsProps,
RedelegateMsgValue,
ShieldingTransferMsgValue,
ShieldingTransferProps,
TxMsgValue,
UnbondMsgValue,
WithdrawMsgValue,
WithdrawProps,
WrapperTxProps,
} from "@namada/types";
import { queryClient } from "App/Common/QueryProvider";
import { getSdkInstance } from "hooks";
import { TransactionPair, buildTxPair } from "lib/query";
import { Address, AddressBalance, ChainSettings, GasConfig } from "types";
import { getSdkInstance } from "utils/sdk";

export const fetchClaimableRewards = async (
api: DefaultApi,
Expand Down Expand Up @@ -45,24 +43,6 @@ export const createBondTx = async (
return transactionPairs;
};

export const createShieldingTx = async (
chain: ChainSettings,
account: Account,
shiedlingProps: ShieldingTransferMsgValue[],
gasConfig: GasConfig
): Promise<TransactionPair<ShieldingTransferProps> | undefined> => {
const { tx } = await getSdkInstance();
const transactionPairs = await buildTxPair(
account,
gasConfig,
chain,
shiedlingProps,
tx.buildShieldingTransfer,
shiedlingProps[0].data[0].source
);
return transactionPairs;
};

export const createUnbondTx = async (
chain: ChainSettings,
account: Account,
Expand Down
39 changes: 3 additions & 36 deletions apps/namadillo/src/hooks/useSdk.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import initSdk from "@heliax/namada-sdk/inline-init";
import { getSdk, Sdk } from "@heliax/namada-sdk/web";
import { Sdk } from "@heliax/namada-sdk/web";
import { QueryStatus, useQuery } from "@tanstack/react-query";
import { nativeTokenAddressAtom } from "atoms/chain";
import { maspIndexerUrlAtom, rpcUrlAtom } from "atoms/settings";
import { getDefaultStore, useAtomValue } from "jotai";
import { useAtomValue } from "jotai";
import {
createContext,
FunctionComponent,
Expand All @@ -12,6 +10,7 @@ import {
useEffect,
useState,
} from "react";
import { getSdkInstance } from "utils/sdk";
import Proxies from "../../scripts/proxies.json";

type SdkContext = {
Expand All @@ -29,38 +28,6 @@ const { VITE_PROXY: isProxied } = import.meta.env;
const paramsUrl =
isProxied ? `http://localhost:${Proxies[0].proxyPort}/proxy/` : undefined;

const initializeSdk = async (): Promise<Sdk> => {
const { cryptoMemory } = await initSdk();
const store = getDefaultStore();
const rpcUrl = store.get(rpcUrlAtom);
const maspIndexerUrl = store.get(maspIndexerUrlAtom);
const nativeToken = store.get(nativeTokenAddressAtom);

if (!nativeToken.isSuccess) {
throw "Native token not loaded";
}

const sdk = getSdk(
cryptoMemory,
rpcUrl,
maspIndexerUrl,
"",
nativeToken.data
);
return sdk;
};

// Global instance of initialized SDK
let sdkInstance: Promise<Sdk>;

// Helper to access SDK instance
export const getSdkInstance = async (): Promise<Sdk> => {
if (!sdkInstance) {
sdkInstance = initializeSdk();
}
return sdkInstance;
};

export const SdkProvider: FunctionComponent<PropsWithChildren> = ({
children,
}) => {
Expand Down
113 changes: 0 additions & 113 deletions apps/namadillo/src/lib/build.ts

This file was deleted.

9 changes: 6 additions & 3 deletions apps/namadillo/src/lib/query.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getIntegration } from "@namada/integrations";
import { Sdk } from "@heliax/namada-sdk/web";
import { getIntegration } from "@namada/integrations/utils";
import {
Account,
AccountType,
Expand Down Expand Up @@ -85,13 +86,14 @@ export const isPublicKeyRevealed = async (
* @param {(WrapperTxProps, T) => Promise<TxMsgValue>} txFn - Function to build each transaction.
*/
export const buildTx = async <T>(
sdk: Sdk,
account: Account,
gasConfig: GasConfig,
chain: ChainSettings,
queryProps: T[],
txFn: (wrapperTxProps: WrapperTxProps, props: T) => Promise<TxMsgValue>
): Promise<EncodedTxData<T>> => {
const { tx } = await getSdkInstance();
const { tx } = sdk;
const wrapperTxProps = getTxProps(account, gasConfig, chain);
const txs: TxMsgValue[] = [];
const txProps: TxProps[] = [];
Expand All @@ -115,7 +117,6 @@ export const buildTx = async <T>(
txProps.push(tx.buildBatch(txs));
}

const sdk = await getSdkInstance();
return {
txs: txProps.map(({ args, hash, bytes, signingData }) => {
const innerTxHashes = sdk.tx.getInnerTxHashes(bytes);
Expand Down Expand Up @@ -184,7 +185,9 @@ export const buildTxPair = async <T>(
txFn: (wrapperTxProps: WrapperTxProps, props: T) => Promise<TxMsgValue>,
owner: string
): Promise<TransactionPair<T>> => {
const sdk = await getSdkInstance();
const encodedTxData = await buildTx<T>(
sdk,
account,
gasConfig,
chain,
Expand Down
1 change: 0 additions & 1 deletion apps/namadillo/src/utils/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { nativeTokenAddressAtom } from "atoms/chain";
import { maspIndexerUrlAtom, rpcUrlAtom } from "atoms/settings";
import { getDefaultStore } from "jotai";

// TODO: temp import fix
const initializeSdk = async (): Promise<Sdk> => {
const { cryptoMemory } = await initSdk();
const store = getDefaultStore();
Expand Down
6 changes: 2 additions & 4 deletions apps/namadillo/src/workers/ShieldWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { initMulticore } from "@heliax/namada-sdk/inline-init";
import { getSdk } from "@heliax/namada-sdk/web";
import { Account, ShieldingTransferMsgValue } from "@namada/types";
import BigNumber from "bignumber.js";
import { buildTx2, EncodedTxData } from "lib/build";
import { buildTx, EncodedTxData } from "lib/query";
import { ChainSettings } from "types";

export type ShieldPayload = {
Expand Down Expand Up @@ -51,7 +51,6 @@ async function shield(
cryptoMemory: WebAssembly.Memory,
payload: ShieldPayload
): Promise<void> {
console.log("payload", payload);
const { rpcUrl, token, account, gasConfig, chain, shieldingProps } = payload;
const www = {
target: shieldingProps[0].target,
Expand All @@ -71,10 +70,9 @@ async function shield(
// Not really used, but required by the SDK, as long as it's valid address it's fine
token
);
console.log("sdk", sdk);

await sdk.masp.loadMaspParams("");
const encodedTxData = await buildTx2<ShieldingTransferMsgValue>(
const encodedTxData = await buildTx<ShieldingTransferMsgValue>(
sdk,
account,
// TODO: focking prototype xddd
Expand Down
35 changes: 5 additions & 30 deletions packages/integrations/src/hooks/useIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,19 @@ import {
useState,
} from "react";

import { chains } from "@namada/chains";
import { useUntil } from "@namada/hooks";
import { Keplr, Metamask, Namada } from "@namada/integrations";
import { ExtensionKey } from "@namada/types";
import {
IntegrationFromExtensionKey,
integrations,
Integrations,
} from "../utils";

type ExtensionConnection<T, U> = (
onSuccess: () => T,
onFail?: () => U
) => Promise<void>;

type IntegrationFromExtensionKey<K extends ExtensionKey> =
K extends "namada" ? Namada
: K extends "keplr" ? Keplr
: K extends "metamask" ? Metamask
: never;

type Integrations = {
[K in ExtensionKey]: IntegrationFromExtensionKey<K>;
};

export const integrations: Integrations = {
namada: new Namada(chains.namada),
keplr: new Keplr(chains.cosmos),
metamask: new Metamask(chains.ethereum),
};

export const IntegrationsContext = createContext<Integrations>(integrations);

/**
Expand Down Expand Up @@ -137,15 +124,3 @@ export const useUntilIntegrationAttached = (
export const getIntegrations = (): Integrations => {
return integrations;
};

/**
* Returns integration by chainId. To be used outside react components.
*
* @param {ExtensionKey} extensionKey - Key of the wallet
* @returns {InstanceType<Integration>} Integration API
*/
export const getIntegration = <K extends ExtensionKey>(
extensionKey: K
): IntegrationFromExtensionKey<K> => {
return integrations[extensionKey];
};
Loading

0 comments on commit a313da4

Please sign in to comment.