Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jinglescode committed Oct 18, 2024
1 parent 77fd5e9 commit aa7b973
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 5 deletions.
47 changes: 47 additions & 0 deletions packages/mesh-provider/src/blockfrost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('<Your-API-Key>');
*/
export class BlockfrostProvider
implements IFetcher, IListener, ISubmitter, IEvaluator
{
Expand Down Expand Up @@ -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<any> {
try {
const headers = { "Content-Type": "application/cbor" };
Expand Down Expand Up @@ -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<AccountInfo> {
const rewardAddress = address.startsWith("addr")
? resolveRewardAddress(address)
Expand All @@ -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 }> {
Expand Down Expand Up @@ -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 }[]> {
Expand Down Expand Up @@ -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<AssetMetadata> {
try {
const { policyId, assetName } = parseAssetUnit(asset);
Expand All @@ -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<BlockInfo> {
try {
const { data, status } = await this._axiosInstance.get(`blocks/latest`);
Expand Down Expand Up @@ -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;

Expand All @@ -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<string> {
try {
const headers = { "Content-Type": "application/cbor" };
Expand Down
20 changes: 15 additions & 5 deletions packages/mesh-react/src/hooks/useLovelace.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import { useContext, useEffect, useState } from "react";
import { useContext, useEffect, useMemo, useRef, useState } from "react";

import { WalletContext } from "../contexts";

export const useLovelace = () => {
const [lovelace, setLovelace] = useState<string>();
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;
};
11 changes: 11 additions & 0 deletions packages/mesh-wallet/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -147,6 +157,7 @@ export class BrowserWallet implements IInitiator, ISigner, ISubmitter {
* @returns a list of assets and their quantities
*/
async getBalance(): Promise<Asset[]> {
console.log(999)
const balance = await this._walletInstance.getBalance();
return fromValue(deserializeValue(balance));
}
Expand Down

0 comments on commit aa7b973

Please sign in to comment.