The Zombies provider will interact with the smart contract using completium's generated bindings. You can generate these bindings with the following command:
Then, the provider needs to check which tokens are registered, and get its corresponding metadata, in order to populate registeredTokenInfo.
This is the best place to show that an indexer service is all but mandatory in some cases. Since there is no contract enumeration on big maps, there is no way to know which token ids are registered, unless either
an array is kept on the contract (increasing storage usage at token registration) or
an indexer keeps track of the registered token ids
Since these two options are not available, we'll hard-code the number of tokens in our client dapp. We assume that we know in advance the tokens that will be registered
useEffect(() => {
if (!fa2) {
return
}
const fetchRegisteredTokens = async () => {
const tokenInfo = new Map()
for (const id of [1, 2]) {
try {
const value = await fa2.get_token_metadata_value(new Nat(id))
const b = value?.token_info.find((info) => info[0] === "")
if (!b || b.length < 2) continue
const info = b[1].hex_decode()
const metadata = await fetchMetadata(info)
tokenInfo.set(id, metadata)
} catch (e) {
console.error(e)
continue
}
}
console.log(tokenInfo)
setRegisteredTokenInfo(tokenInfo)
}
fetchRegisteredTokens()
}, [fa2, fetchMetadata])
Explanation: for token ids 1 and 2, we try to get the big map value, that is a map of an empty string to a byte-encoded string of the IPFS URI. We pass it to the MetadataProvider to translate it to zombie metadata
Fetch inventory
This method iterates over each registered token, to fetch the user's balance. Another example of a concept that can be greatly optimised.
const fetchFa2Balance = useCallback(
async (address: Address) => {
if (!fa2 || registeredTokenInfo.size < 1) {
return new Map()
}
const inventory = new Map()
for (const [id, _] of registeredTokenInfo) {
try {
const value = await fa2.get_ledger_value(
new ledger_key(address, new Nat(id))
)
inventory.set(id, value?.to_number() ?? 0)
} catch (e) {
console.error(e)
}
}
return inventory
},
[fa2, registeredTokenInfo]
)
const fetchInventory = useCallback(async () => {
if (!account) {
setInventory(new Map())
return
}
setInventory(await fetchFa2Balance(new Address(account.address)))
getBalance()
}, [account, fetchFa2Balance, getBalance])
useEffect(() => {
fetchInventory()
}, [fetchInventory])
Mint (claim)
The following exposes the mint entrypoint (and mints a single token)
const freeClaim = useCallback(
async (id: number) => {
if (!fa2 || !account || !account.address) {
return
}
return await fa2.mint(
new Address(account.address),
new Nat(id),
new Nat(1),
{}
)
},
[fa2, account]
)
Transfer
The transfer entrypoint requires FA2 specific parameters, that have been remapped to a friendlier structure TransferParameters.
const transfer = useCallback(
async (params: TransferParameters) => {
if (!fa2 || !account) {
return
}
const dest = new transfer_destination(
new Address(params.to),
new Nat(params.tokenId),
new Nat(params.amount)
)
const args = new transfer_param(new Address(account.address), [dest])
return await fa2.transfer([args], {})
},
[account, fa2]
)
Wrap up
The props are now memoised and passed to the children:
export { TzombiesProvider, useTzombiesContext }
export type { UserInventory }
Include <TzombiesProvider> in the app hierarchy. This provider accesses the Metadata and Wallet context, so be sure to places it below these two providers in the hierarchy.