KMPARDS / coupon-dapp-react

By June 24, 2020DApps
Click here to view original web page at github.com

CouponÐApp

Blockchain based simple redeemable coupons

JavaScript Integraton documentation

Flow

Reading a Coupon

  1. Load the file.
  2. Convert the file into _JSON object
  3. Check the json object and finally convert it into _coupon object.
  4. Load the coupon into the UI and asynchronously use the _coupons method
  5. Show the output from coupons method to the UI.
  6. Give buttons options for redeeming the coupon.

Redeeming the Coupon

  1. Read the coupon.
  2. Use the redeemCoupon of the smart contract.
  3. Show in the UI that it's waiting for the transaction to confirm.
  4. Once confirmed, show that coupon is redeemed.
  5. If there was an error, display transaction failed.

New coupon

  1. Take amount from the user.
  2. Check if user has enough balance. Code reference
  3. Generate Coupon Bytes Code reference.
  4. Generate a file for user or QR code Code reference
  5. Check for allowance Code reference.
  6. If allowance not there then make user call the _approve method.
  7. Once allowance is there, make user proceed to final step and give button to call _newCoupon.

File content

When you load a coupon file, you will get the following object encoded as JSON.

interface CouponFile {
 version: number; // version of coupon
 data: string; // base58 encoded data
 keccak256: string; // 32 bytes hex string
}

Useful links: Code reference, bs58, Ethers.js docs

Coupon Object

Once you load the coupon file, you have to decode it into a coupon object for further working with the coupon.

interface CouponObject {
 keccak256: string; // 32 bytes hex string
 couponBytes: string; // 64 bytes hex string
}

Useful links: Code reference

Contract Instances

Once you load the coupon, you will need to work with contract instances to check status of the coupon.

const esInstance = new ethers.Contract(
 esContract.address, // kovan testnet: 0x53e750ee41c562c171d65bcb51405b16a56cf676
 esContract.abi, // import from config
 wallet
);

const couponDappInstance = new ethers.Contract(
 couponDappContract.address, // kovan testnet: 0x255faf8e0e6c51558396215beb0e00ab8f12c982
 couponDappContract.abi,
 wallet
);

Useful links: Ethers.js docs, Code Reference

Contract Config

You need to json files available here. You have to import these json files to get abi and use this abi while creating a contract instance.

Useful links: Code reference

ES Contract methods:

balanceOf method

This method is used when redeeming a new coupon.

couponDappInstance.functions.balanceOf(
 userAddress: string, /* 20 bytes hex string */
) => Promise<BigNumber>

Useful links: BigNumber

approve method

This method is used when redeeming a new coupon.

couponDappInstance.functions.approve(
 spenderAddress: string, /* 20 bytes hex string */
 amount: BigNumber // use parseEther to convert from input
) => Promise<TransactionObject>

Useful links: parseEther

CouponDApp Contract methods:

coupons method

This method is used to check for a coupon if it is available, redeemed or not yet created.

couponDappInstance.functions.coupons(
 couponHash: string /* 32 byte hex string */
) => Promise<{
 amount: BigNumber; // decode using lessDecimals
 status: BigNumber; // .toNumber()
}>;

Convert the status into number and check it's value for below:

Status:
0: NOT_CREATED
1: ACTIVE
2: REDEEMED

Useful links: lessDecimals reference

newCoupon method

This method is used to create a new coupon. When before approve method with esInstance needs to be called.

couponDappInstance.functions.newCoupon(
 couponHash: string /* 32 bytes hex string */,
 amount: BigNumber // use parseEther to convert from input
) => Promise<TransactionObject>

The resolved transaction object contains hash property that you can show in the UI.

Useful links: parseEther

redeemCoupon method

This method is used when redeeming a new coupon.

couponDappInstance.functions.redeemCoupon(
 couponBytes: string /* 64 bytes hex string */
) => Promise<TransactionObject>

Blockchain based simple redeemable coupons JavaScript Integraton documentation

Flow Reading a Coupon> Load the file. Convert the file into _JSON object Check the json object and […]

Leave a Reply