diff --git a/packages/mesh-provider/src/blockfrost.ts b/packages/mesh-provider/src/blockfrost.ts index 5c86d2ce..5606b125 100644 --- a/packages/mesh-provider/src/blockfrost.ts +++ b/packages/mesh-provider/src/blockfrost.ts @@ -30,6 +30,15 @@ import { parseAssetUnit } from "./utils/parse-asset-unit"; export type BlockfrostSupportedNetworks = "mainnet" | "preview" | "preprod"; +/** + * Blockfrost provides restful APIs which allows your app to access information stored on the blockchain. + * + * Usage: + * ``` + * import { BlockfrostProvider } from "@meshsdk/core"; + * + * const blockchainProvider = new BlockfrostProvider(''); + */ export class BlockfrostProvider implements IFetcher, IListener, ISubmitter, IEvaluator { @@ -69,6 +78,10 @@ export class BlockfrostProvider } } + /** + * Evaluates the resources required to execute the transaction + * @param tx - The transaction to evaluate + */ async evaluateTx(cbor: string): Promise { try { const headers = { "Content-Type": "application/cbor" }; @@ -108,6 +121,10 @@ export class BlockfrostProvider } } + /** + * Obtain information about a specific stake account. + * @param address - Wallet address to fetch account information + */ async fetchAccountInfo(address: string): Promise { const rewardAddress = address.startsWith("addr") ? resolveRewardAddress(address) @@ -133,6 +150,11 @@ export class BlockfrostProvider } } + /** + * Fetches the assets for a given address. + * @param address - The address to fetch assets for + * @returns A map of asset unit to quantity + */ async fetchAddressAssets( address: string, ): Promise<{ [key: string]: string }> { @@ -174,6 +196,10 @@ export class BlockfrostProvider } } + /** + * Fetches the asset addresses for a given asset. + * @param asset - The asset to fetch addresses for + */ async fetchAssetAddresses( asset: string, ): Promise<{ address: string; quantity: string }[]> { @@ -201,6 +227,11 @@ export class BlockfrostProvider } } + /** + * Fetches the metadata for a given asset. + * @param asset - The asset to fetch metadata for + * @returns The metadata for the asset + */ async fetchAssetMetadata(asset: string): Promise { try { const { policyId, assetName } = parseAssetUnit(asset); @@ -222,6 +253,11 @@ export class BlockfrostProvider } } + /** + * Fetches the metadata for a given asset. + * @param asset - The asset to fetch metadata for + * @returns The metadata for the asset + */ async fetchLatestBlock(): Promise { try { const { data, status } = await this._axiosInstance.get(`blocks/latest`); @@ -432,6 +468,12 @@ export class BlockfrostProvider } } + /** + * Allow you to listen to a transaction confirmation. Upon confirmation, the callback will be called. + * @param txHash - The transaction hash to listen for confirmation + * @param callback - The callback function to call when the transaction is confirmed + * @param limit - The number of blocks to wait for confirmation + */ onTxConfirmed(txHash: string, callback: () => void, limit = 100): void { let attempts = 0; @@ -457,6 +499,11 @@ export class BlockfrostProvider }, 5_000); } + /** + * Submit a serialized transaction to the network. + * @param tx - The serialized transaction in hex to submit + * @returns The transaction hash of the submitted transaction + */ async submitTx(tx: string): Promise { try { const headers = { "Content-Type": "application/cbor" }; diff --git a/packages/mesh-react/src/hooks/useLovelace.ts b/packages/mesh-react/src/hooks/useLovelace.ts index bbea1908..fd98ed15 100644 --- a/packages/mesh-react/src/hooks/useLovelace.ts +++ b/packages/mesh-react/src/hooks/useLovelace.ts @@ -1,4 +1,4 @@ -import { useContext, useEffect, useState } from "react"; +import { useContext, useEffect, useMemo, useRef, useState } from "react"; import { WalletContext } from "../contexts"; @@ -6,12 +6,22 @@ export const useLovelace = () => { const [lovelace, setLovelace] = useState(); const { hasConnectedWallet, connectedWalletName, connectedWalletInstance } = useContext(WalletContext); + const hasFetchedLovelace = useRef(false); useEffect(() => { - if (hasConnectedWallet) { - connectedWalletInstance.getLovelace().then(setLovelace); + async function getLovelace() { + console.log(333, lovelace, hasConnectedWallet, connectedWalletName); + setLovelace(await connectedWalletInstance.getLovelace()); } - }, [connectedWalletName]); + if (hasConnectedWallet && !hasFetchedLovelace.current) { + getLovelace(); + hasFetchedLovelace.current = true; + } + }, [hasConnectedWallet, connectedWalletInstance]); + + const _lovelace = useMemo(() => { + return lovelace; + }, [lovelace]); - return lovelace; + return _lovelace; }; diff --git a/packages/mesh-wallet/src/browser/index.ts b/packages/mesh-wallet/src/browser/index.ts index 1143c042..29515afd 100644 --- a/packages/mesh-wallet/src/browser/index.ts +++ b/packages/mesh-wallet/src/browser/index.ts @@ -42,6 +42,16 @@ declare global { } } +/** + * Browser Wallet provides a set of APIs to interact with the blockchain. This wallet is compatible with Mesh transaction builders. + * + * These wallets APIs are in accordance to CIP-30, which defines the API for dApps to communicate with the user's wallet. Additional utility functions provided for developers that are useful for building dApps. + * ```javascript + * import { BrowserWallet } from '@meshsdk/core'; + * + * const wallet = await BrowserWallet.enable('eternl'); + * ``` + */ export class BrowserWallet implements IInitiator, ISigner, ISubmitter { walletInstance: WalletInstance; @@ -147,6 +157,7 @@ export class BrowserWallet implements IInitiator, ISigner, ISubmitter { * @returns a list of assets and their quantities */ async getBalance(): Promise { + console.log(999) const balance = await this._walletInstance.getBalance(); return fromValue(deserializeValue(balance)); }