diff --git a/apps/namadillo/src/atoms/proposals/functions.ts b/apps/namadillo/src/atoms/proposals/functions.ts index 95b628319..0b291b82c 100644 --- a/apps/namadillo/src/atoms/proposals/functions.ts +++ b/apps/namadillo/src/atoms/proposals/functions.ts @@ -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 = ( diff --git a/apps/namadillo/src/atoms/staking/atoms.ts b/apps/namadillo/src/atoms/staking/atoms.ts index fd1209aa9..8e9e5796b 100644 --- a/apps/namadillo/src/atoms/staking/atoms.ts +++ b/apps/namadillo/src/atoms/staking/atoms.ts @@ -3,7 +3,6 @@ import { BondMsgValue, ClaimRewardsMsgValue, RedelegateMsgValue, - ShieldingTransferMsgValue, UnbondMsgValue, WithdrawMsgValue, } from "@namada/types"; @@ -22,7 +21,6 @@ import { createClaimAndStakeTx, createClaimTx, createReDelegateTx, - createShieldingTx, createUnbondTx, createWithdrawTx, fetchClaimableRewards, @@ -40,20 +38,6 @@ export const getStakingTotalAtom = atomWithQuery((get) => { }; }); -export const createShieldingTxAtom = atomWithMutation((get) => { - const chain = get(chainAtom); - return { - mutationKey: ["create-shielding-tx"], - enabled: chain.isSuccess, - mutationFn: async ({ - params, - gasConfig, - account, - }: BuildTxAtomParams) => - createShieldingTx(chain.data!, account, params, gasConfig), - }; -}); - export const createBondTxAtom = atomWithMutation((get) => { const chain = get(chainAtom); return { diff --git a/apps/namadillo/src/atoms/staking/services.ts b/apps/namadillo/src/atoms/staking/services.ts index 9e4af6b3a..48849f5b4 100644 --- a/apps/namadillo/src/atoms/staking/services.ts +++ b/apps/namadillo/src/atoms/staking/services.ts @@ -6,8 +6,6 @@ import { ClaimRewardsMsgValue, ClaimRewardsProps, RedelegateMsgValue, - ShieldingTransferMsgValue, - ShieldingTransferProps, TxMsgValue, UnbondMsgValue, WithdrawMsgValue, @@ -15,9 +13,9 @@ import { 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, @@ -45,24 +43,6 @@ export const createBondTx = async ( return transactionPairs; }; -export const createShieldingTx = async ( - chain: ChainSettings, - account: Account, - shiedlingProps: ShieldingTransferMsgValue[], - gasConfig: GasConfig -): Promise | 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, diff --git a/apps/namadillo/src/hooks/useSdk.tsx b/apps/namadillo/src/hooks/useSdk.tsx index e168632a9..eab53df47 100644 --- a/apps/namadillo/src/hooks/useSdk.tsx +++ b/apps/namadillo/src/hooks/useSdk.tsx @@ -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, @@ -12,6 +10,7 @@ import { useEffect, useState, } from "react"; +import { getSdkInstance } from "utils/sdk"; import Proxies from "../../scripts/proxies.json"; type SdkContext = { @@ -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 => { - 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; - -// Helper to access SDK instance -export const getSdkInstance = async (): Promise => { - if (!sdkInstance) { - sdkInstance = initializeSdk(); - } - return sdkInstance; -}; - export const SdkProvider: FunctionComponent = ({ children, }) => { diff --git a/apps/namadillo/src/lib/build.ts b/apps/namadillo/src/lib/build.ts deleted file mode 100644 index 303b89a12..000000000 --- a/apps/namadillo/src/lib/build.ts +++ /dev/null @@ -1,113 +0,0 @@ -import { Sdk } from "@heliax/namada-sdk/web"; -import { - Account, - AccountType, - TxMsgValue, - TxProps, - WrapperTxProps, -} from "@namada/types"; -import { getIndexerApi } from "atoms/api"; -import invariant from "invariant"; -import { Address, ChainSettings, GasConfig } from "types"; - -export type EncodedTxData = { - type: string; - txs: TxProps[] & - { - innerTxHashes: string[]; - }[]; - wrapperTxProps: WrapperTxProps; - meta?: { - props: T[]; - }; -}; - -export const isPublicKeyRevealed = async ( - address: Address -): Promise => { - const api = getIndexerApi(); - console.log("address", address); - let publicKey: string | undefined; - try { - publicKey = (await api.apiV1RevealedPublicKeyAddressGet(address)).data - ?.publicKey; - console.log("publicKey", publicKey); - } catch {} - return Boolean(publicKey); -}; - -const getTxProps = ( - account: Account, - gasConfig: GasConfig, - chain: ChainSettings -): WrapperTxProps => { - invariant( - !!account.publicKey, - "Account doesn't contain a publicKey attached to it" - ); - - return { - token: chain.nativeTokenAddress, - feeAmount: gasConfig.gasPrice, - gasLimit: gasConfig.gasLimit, - chainId: chain.chainId, - publicKey: account.publicKey!, - memo: "", - }; -}; - -export const buildTx2 = async ( - sdk: Sdk, - account: Account, - gasConfig: GasConfig, - chain: ChainSettings, - queryProps: T[], - txFn: (wrapperTxProps: WrapperTxProps, props: T) => Promise -): Promise> => { - const { tx } = sdk; - const wrapperTxProps = getTxProps(account, gasConfig, chain); - const txs: TxMsgValue[] = []; - const txProps: TxProps[] = []; - console.log("wrapperTxProps", wrapperTxProps); - - // Determine if RevealPK is needed: - // const publicKeyRevealed = await isPublicKeyRevealed(account.address); - // console.log("publicKeyRevealed", publicKeyRevealed, account.address); - // if (!publicKeyRevealed) { - // const revealPkTx = await tx.buildRevealPk(wrapperTxProps); - // txs.push(revealPkTx); - // } - - console.log("queryProps", queryProps); - - const encodedTxs = await Promise.all( - queryProps.map((props) => txFn.apply(tx, [wrapperTxProps, props])) - ); - console.log("encodedTxs", encodedTxs); - - txs.push(...encodedTxs); - - if (account.type === AccountType.Ledger) { - txProps.push(...txs); - } else { - txProps.push(tx.buildBatch(txs)); - } - - return { - txs: txProps.map(({ args, hash, bytes, signingData }) => { - const innerTxHashes = tx.getInnerTxHashes(bytes); - return { - args, - hash, - bytes, - signingData, - innerTxHashes, - }; - }), - wrapperTxProps, - type: txFn.name, - meta: { - props: queryProps, - }, - }; -}; diff --git a/apps/namadillo/src/lib/query.ts b/apps/namadillo/src/lib/query.ts index 240f67b9c..4fce8e689 100644 --- a/apps/namadillo/src/lib/query.ts +++ b/apps/namadillo/src/lib/query.ts @@ -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, @@ -85,13 +86,14 @@ export const isPublicKeyRevealed = async ( * @param {(WrapperTxProps, T) => Promise} txFn - Function to build each transaction. */ export const buildTx = async ( + sdk: Sdk, account: Account, gasConfig: GasConfig, chain: ChainSettings, queryProps: T[], txFn: (wrapperTxProps: WrapperTxProps, props: T) => Promise ): Promise> => { - const { tx } = await getSdkInstance(); + const { tx } = sdk; const wrapperTxProps = getTxProps(account, gasConfig, chain); const txs: TxMsgValue[] = []; const txProps: TxProps[] = []; @@ -115,7 +117,6 @@ export const buildTx = async ( txProps.push(tx.buildBatch(txs)); } - const sdk = await getSdkInstance(); return { txs: txProps.map(({ args, hash, bytes, signingData }) => { const innerTxHashes = sdk.tx.getInnerTxHashes(bytes); @@ -184,7 +185,9 @@ export const buildTxPair = async ( txFn: (wrapperTxProps: WrapperTxProps, props: T) => Promise, owner: string ): Promise> => { + const sdk = await getSdkInstance(); const encodedTxData = await buildTx( + sdk, account, gasConfig, chain, diff --git a/apps/namadillo/src/utils/sdk.ts b/apps/namadillo/src/utils/sdk.ts index 28dc03599..4fc2c1d4e 100644 --- a/apps/namadillo/src/utils/sdk.ts +++ b/apps/namadillo/src/utils/sdk.ts @@ -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 => { const { cryptoMemory } = await initSdk(); const store = getDefaultStore(); diff --git a/apps/namadillo/src/workers/ShieldWorker.ts b/apps/namadillo/src/workers/ShieldWorker.ts index 3875601cd..42998c49b 100644 --- a/apps/namadillo/src/workers/ShieldWorker.ts +++ b/apps/namadillo/src/workers/ShieldWorker.ts @@ -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 = { @@ -51,7 +51,6 @@ async function shield( cryptoMemory: WebAssembly.Memory, payload: ShieldPayload ): Promise { - console.log("payload", payload); const { rpcUrl, token, account, gasConfig, chain, shieldingProps } = payload; const www = { target: shieldingProps[0].target, @@ -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( + const encodedTxData = await buildTx( sdk, account, // TODO: focking prototype xddd diff --git a/packages/integrations/src/hooks/useIntegration.ts b/packages/integrations/src/hooks/useIntegration.ts index b23b56554..d4db23e31 100644 --- a/packages/integrations/src/hooks/useIntegration.ts +++ b/packages/integrations/src/hooks/useIntegration.ts @@ -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 = ( onSuccess: () => T, onFail?: () => U ) => Promise; -type IntegrationFromExtensionKey = - K extends "namada" ? Namada - : K extends "keplr" ? Keplr - : K extends "metamask" ? Metamask - : never; - -type Integrations = { - [K in ExtensionKey]: IntegrationFromExtensionKey; -}; - -export const integrations: Integrations = { - namada: new Namada(chains.namada), - keplr: new Keplr(chains.cosmos), - metamask: new Metamask(chains.ethereum), -}; - export const IntegrationsContext = createContext(integrations); /** @@ -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 API - */ -export const getIntegration = ( - extensionKey: K -): IntegrationFromExtensionKey => { - return integrations[extensionKey]; -}; diff --git a/packages/integrations/src/index.ts b/packages/integrations/src/index.ts index 9652afe39..5c56d84f7 100644 --- a/packages/integrations/src/index.ts +++ b/packages/integrations/src/index.ts @@ -1,7 +1,8 @@ -import Namada from "./Namada"; import Keplr from "./Keplr"; import Metamask from "./Metamask"; +import Namada from "./Namada"; -export { Namada, Keplr, Metamask }; export * from "./hooks"; export type { MetamaskWindow } from "./Metamask"; +export * from "./utils"; +export { Keplr, Metamask, Namada }; diff --git a/packages/integrations/src/utils.ts b/packages/integrations/src/utils.ts new file mode 100644 index 000000000..8db546290 --- /dev/null +++ b/packages/integrations/src/utils.ts @@ -0,0 +1,33 @@ +import { chains } from "@namada/chains"; +import { ExtensionKey } from "@namada/types"; +import Keplr from "./Keplr"; +import Metamask from "./Metamask"; +import Namada from "./Namada"; + +export type IntegrationFromExtensionKey = + K extends "namada" ? Namada + : K extends "keplr" ? Keplr + : K extends "metamask" ? Metamask + : never; + +export type Integrations = { + [K in ExtensionKey]: IntegrationFromExtensionKey; +}; + +export const integrations: Integrations = { + namada: new Namada(chains.namada), + keplr: new Keplr(chains.cosmos), + metamask: new Metamask(chains.ethereum), +}; + +/** + * Returns integration by chainId. To be used outside react components. + * + * @param {ExtensionKey} extensionKey - Key of the wallet + * @returns {InstanceType} Integration API + */ +export const getIntegration = ( + extensionKey: K +): IntegrationFromExtensionKey => { + return integrations[extensionKey]; +};