OfflineFetcher
An offline blockchain data provider for testing, development and offline scenarios.
The OfflineFetcher provides access to blockchain data without requiring network connectivity. It's ideal for testing, development, and scenarios where you need to work with pre-loaded blockchain data offline.
Initialize the fetcher:
import { OfflineFetcher } from "@meshsdk/core";
// Create a new instance
const fetcher = new OfflineFetcher();
// Create with specified network
const fetcherWithNetwork = new OfflineFetcher("mainnet");
Before you can fetch data, you need to add it to the fetcher. Here are examples of adding different types of blockchain data:
// Add account information
fetcher.addAccount("addr1...", {
balance: "1000000",
rewards: "500000",
withdrawals: "100000",
poolId: "pool1..." // optional
});
// Add UTXOs
fetcher.addUTxOs([
{
input: {
txHash: "1234...",
outputIndex: 0
},
output: {
address: "addr1...",
amount: [{ unit: "lovelace", quantity: "1000000" }],
// Optional fields for script UTXOs:
scriptHash: "abcd...",
dataHash: "ef12...",
plutusData: "...",
scriptRef: "..."
}
}
]);
// Add asset addresses
fetcher.addAssetAddresses("policyID.assetName", [
{ address: "addr1...", quantity: "1" }
]);
// Add asset metadata
fetcher.addAssetMetadata("policyID.assetName", {
name: "Asset Name",
image: "ipfs://...",
// Any other metadata attributes
});
// Add protocol parameters
fetcher.addProtocolParameters({
epoch: 290,
minFeeA: 44,
minFeeB: 155381,
maxBlockSize: 73728,
maxTxSize: 16384,
maxBlockHeaderSize: 1100,
keyDeposit: 2000000,
poolDeposit: 500000000,
minPoolCost: "340000000",
// Other parameters...
});
// Add serilized transaction
fetcher.addSerializedTransaction("txHash");
The fetcher's state can be saved and loaded, making it easy to persist data between sessions:
// Save state
const state = fetcher.toJSON();
localStorage.setItem('fetcher-state', state);
// Load state
const savedState = localStorage.getItem('fetcher-state');
const fetcher = OfflineFetcher.fromJSON(savedState);
Once data is added, you can use the fetch* methods just like with other providers such as BlockfrostProvider. This makes OfflineFetcher a drop-in replacement for testing and offline scenarios.
Get data from URL
You can fetch any data from the blockchain by providing the URL path.
// Params: URL
await provider.get('/addresses/addr_test1qpvx0sacufuypa2k4sngk7q40zc5c4npl337uusdh64kv0uafhxhu32dys6pvn6wlw8dav6cmp4pmtv7cc3yel9uu0nq93swx9/transactions')
Fetch Account Info
Obtain information about a specific stake account.
// Params: Stake Address
await provider.fetchAccountInfo('stake_test1uzw5mnt7g4xjgdqkfa80hrk7kdvds6sa4k0vvgjvlj7w8eskffj2n')
Fetch Address Assets
Fetch assets from an address.
// Params: Address
await provider.fetchAddressAssets('addr_test1qpvx0sacufuypa2k4sngk7q40zc5c4npl337uusdh64kv0uafhxhu32dys6pvn6wlw8dav6cmp4pmtv7cc3yel9uu0nq93swx9')
Fetch Address UTxOs
Fetch UTxOs from address
await provider.fetchAddressAssets(
'addr_test1qpvx0sacufuypa2k4sngk7q40zc5c4npl337uusdh64kv0uafhxhu32dys6pvn6wlw8dav6cmp4pmtv7cc3yel9uu0nq93swx9'
);
Fetch assets from address
Fetch assets given an address
await provider.fetchAddressAssets(
'addr_test1qpvx0sacufuypa2k4sngk7q40zc5c4npl337uusdh64kv0uafhxhu32dys6pvn6wlw8dav6cmp4pmtv7cc3yel9uu0nq93swx9',
);
Fetch Address UTxOs
Fetch UTxOs controlled by an address.
// Params: Address
await provider.fetchAddressUTxOs('addr_test1qpvx0sacufuypa2k4sngk7q40zc5c4npl337uusdh64kv0uafhxhu32dys6pvn6wlw8dav6cmp4pmtv7cc3yel9uu0nq93swx9')
Optionally, you can filter UTXOs containing a particular asset by providing asset
, where it is the concatenation of policy ID and asset.
await fetchAddressUTxOs(address: string, asset?: string)
Fetch Address UTxOs
Fetch UTxOs from address
await provider.fetchAddressUTxOs(
'addr_test1qpvx0sacufuypa2k4sngk7q40zc5c4npl337uusdh64kv0uafhxhu32dys6pvn6wlw8dav6cmp4pmtv7cc3yel9uu0nq93swx9'
);
Fetch UTxOs with Asset
Fetch UTxOs from address with asset
await provider.fetchAddressUTxOs(
'addr_test1qpvx0sacufuypa2k4sngk7q40zc5c4npl337uusdh64kv0uafhxhu32dys6pvn6wlw8dav6cmp4pmtv7cc3yel9uu0nq93swx9',
'd9312da562da182b02322fd8acb536f37eb9d29fba7c49dc172555274d657368546f6b656e'
);
Fetch Asset Addresses
Fetch a list of a addresses containing a specific asset where it is the concatenation of policy ID and asset.
// Params: Asset Unit
await provider.fetchAssetAddresses('d9312da562da182b02322fd8acb536f37eb9d29fba7c49dc172555274d657368546f6b656e')
Fetch Asset Metadata
Fetch the asset metadata by providing asset's unit
, which is the concatenation of policy ID and asset name in hex.
// Params: Asset Unit
await provider.fetchAssetMetadata('d9312da562da182b02322fd8acb536f37eb9d29fba7c49dc172555274d657368546f6b656e')
Fetch Block Info
Fetch block infomation. You can get the hash from fetchTxInfo()
.
// Params: Block hash
await provider.fetchBlockInfo('79f60880b097ec7dabb81f75f0b52fedf5e922d4f779a11c0c432dcf22c56089')
Fetch Collection Assets
Fetch a list of assets belonging to a collection by providing its Policy ID.
await provider.fetchCollectionAssets('d9312da562da182b02322fd8acb536f37eb9d29fba7c49dc17255527')
The API will return a list of assets
and a cursor next
. If the cursor is not null, you can use it to fetch the next page of results. Here is an example of the response.
{
"assets": [
{
"unit": "d9312da562da182b02322fd8acb536f37eb9d29fba7c49dc17255527",
"quantity": "1"
},
],
"next": 2
}
The fetchCollectionAssets
function also accepts an optional cursor
parameter to fetch the next page of results. The default value is 1
.
await fetchCollectionAssets(
policyId: string,
cursor = 1
)
Fetch Handle Address
ADA Handle allows users to use a human-readable "Handle" to associate an address.
Each Handle is a unique NFT, minted and issued on the Cardano blockchain. These NFTs act as unique identifiers for the UTXO that they reside in.
We can resolve the handle's address with fetchHandleAddress
.
// Params: Handle
await provider.fetchHandleAddress('meshsdk')
Fetch Handle
ADA Handle allows users to use a human-readable "Handle" to associate an address.
Each Handle is a unique NFT, minted and issued on the Cardano blockchain. These NFTs act as unique identifiers for the UTXO that they reside in.
ADA Handle also released a CIP68 handle and this function will fetch the metadata of the handle.
// Params: Handle
await provider.fetchHandle('meshsdk')
Fetch Protocol Parameters
Fetch the latest protocol parameters.
await provider.fetchProtocolParameters()
Optionally, you can provide an epoch number to fetch the protocol parameters of that epoch.
Fetch Transaction Info
Fetch transaction infomation. Only confirmed transaction can be retrieved.
await provider.fetchTxInfo('f4ec9833a3bf95403d395f699bc564938f3419537e7fb5084425d3838a4b6159')
Fetch UTxOs
Get UTxOs for a given hash.
// Params: Hash
await provider.fetchUTxOs('dfd2a2616e6154a092807b1ceebb9ddcadc0f22cf5c8e0e6b0757815083ccb70')
Optionally, you can specify the index of the index output.
await provider.fetchUTxOs('hash_here', 0)
Fetch Proposal Info
Get information for a given governance proposal, identified by the txHash and proposal index
// Params: TxHash, CertIndex
await provider.fetchGovernanceProposal('372d688faa77e146798b581b322c0f2981a9023764736ade5d12e0e4e796af8c', 0)