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
Install package
First you can to install the @meshsdk/contracts
package:
Initialize the contract
To initialize the contract, we need to initialize a provider, MeshTxBuilder
and MeshGiftCardContract
.
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 function returns a transaction hex if giftcard has been created successfully.
Create a gift card with a given amount of lovelace
Connect wallet to run this demo
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 function returns a transaction hex if the gift card has been redeemed successfully.
We have provided a very handle function, getUtxoByTxHash
, which will return the UTxO object for a given transaction hash. You can always create another function that searches by token name.
Redeem a gift card given the gift card UTxO
Connect wallet to run this demo