Blockchain based simple redeemable coupons
- Load the file.
- Convert the file into _JSON object
- Check the json object and finally convert it into _coupon object.
- Load the coupon into the UI and asynchronously use the _coupons method
- Show the output from coupons method to the UI.
- Give buttons options for redeeming the coupon.
- Read the coupon.
- Use the redeemCoupon of the smart contract.
- Show in the UI that it's waiting for the transaction to confirm.
- Once confirmed, show that coupon is redeemed.
- If there was an error, display transaction failed.
- Take amount from the user.
- Check if user has enough balance. Code reference
- Generate Coupon Bytes Code reference.
- Generate a file for user or QR code Code reference
- Check for allowance Code reference.
- If allowance not there then make user call the _approve method.
- Once allowance is there, make user proceed to final step and give button to call _newCoupon.
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
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
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
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
This method is used when redeeming a new coupon.
couponDappInstance.functions.balanceOf(
userAddress: string, /* 20 bytes hex string */
) => Promise<BigNumber>Useful links: BigNumber
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
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
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
This method is used when redeeming a new coupon.
couponDappInstance.functions.redeemCoupon(
couponBytes: string /* 64 bytes hex string */
) => Promise<TransactionObject>