Giftcard

Create a giftcard with native tokens.

Giftcard contract allows users to create a transactions to lock assets into the smart contract, which can be redeemed by any user.

Creating a giftcard will mint a token and send the assets to the contract. While redeeming will burn the token and send the assets to the redeemer.

There are 2 actions (or endpoints) available to interact with this smart contract:

  • create giftcard
  • redeem giftcard

To initialize the escrow, we need to initialize a provider, MeshTxBuilder and MeshGiftCardContract.

import { BlockfrostProvider, MeshTxBuilder } from '@meshsdk/core';
import { MeshGiftCardContract } from '@meshsdk/contracts';
import { useWallet } from '@meshsdk/react';

const { connected, wallet } = useWallet();

const blockchainProvider = new BlockfrostProvider(APIKEY);

const meshTxBuilder = new MeshTxBuilder({
  fetcher: blockchainProvider,
  submitter: blockchainProvider,
});

const contract = new MeshGiftCardContract({
  mesh: meshTxBuilder,
  fetcher: blockchainProvider,
  wallet: wallet,
  networkId: 0,
});

Both on-chain and off-chain codes are open-source and available on Mesh Github Repository.

Create Giftcard

createGiftCard() create a gift card. The function accepts the following parameters:

  • tokenName (string) - name of the token
  • giftValue (Asset[]) - a list of assets

The function returns a transaction hash if the gift card is successfully created.

The code snippet below demonstrates how to create a gift card with a value of 20 ADA.

This demo, we will create a giftcard containing 20 ADA.

const tokenName = 'Mesh Gift Card';
const giftValue: Asset[] = [
  {
    unit: 'lovelace',
    quantity: '20000000',
  },
];

const tx = await contract.createGiftCard(tokenName, giftValue);
const signedTx = await wallet.signTx(tx);
const txHash = await wallet.submitTx(signedTx);

Redeem Giftcard

redeemGiftCard() redeem a gift card. The function accepts the following parameters:

  • giftCardUtxo (UTxO) - unspent transaction output in the script

The function returns a transaction hash if the gift card is successfully redeemed. It will burn the gift card and transfer the value to the wallet signing this transaction.

The code snippet below demonstrates how to redeem a gift card.

This demo, we will redeem the giftcard that was created in the previous step. You may connect with another wallet to claim the giftcard

const utxo = await contract.getUtxoByTxHash(txHashToSearchFor);
const tx = await contract.redeemGiftCard(utxo);
const signedTx = await wallet.signTx(tx, true);
const txHash = await wallet.submitTx(signedTx);