Zombies provider

Go fetch these evil creatures

The Zombies provider will interact with the smart contract using completium's generated bindings. You can generate these bindings with the following command:

npx completium-cli generate binding-dapp-ts * --input-path ./contracts --output-path ./contracts/bindings

This command writes all the contracts bindings in ./contracts/bindings so we can easily interact with our contracts.

Provider base

Create ./components/providers/TzombiesProvider.tsx

The imports:

import React, { useCallback, useEffect, useMemo, useState } from 'react'
import {
  Tzombies,
  ledger_key,
  transfer_destination,
  transfer_param,
} from '../../contracts/bindings/tzombies'
import { useWalletContext } from './WalletProvider'
import { Address, CallResult, Nat } from '@completium/archetype-ts-types'
import { ZombieMetadata, useMetadataContext } from './MetadataProvider'

For this provider, we'll expose the contract itself, as well as some methods and contract state:

  • UserInventory is a mapping of each token id and the amount owned in the user's wallet.

  • tokenInfo is a mapping between the token id and the associated metadata.

  • transfer is the simple FA2 transfer function

  • freeClaim is the free mint function

  • fetchInventory and fetchBalance is used to read and update the state from the contract

We need the empty context:

Now let's dig into the implementation. First, the contexts and state:

The fa2 state is loaded as soon as the TezosToolkit becomes available:

Fetch registered tokens

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

  1. an array is kept on the contract (increasing storage usage at token registration) or

  2. 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

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.

Mint (claim)

The following exposes the mint entrypoint (and mints a single token)

Transfer

The transfer entrypoint requires FA2 specific parameters, that have been remapped to a friendlier structure TransferParameters.

Wrap up

The props are now memoised and passed to the children:

Don't forget to export the component.

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.

Last updated