This project implements an Unlock PublicLock Hook that can be used on Locks smart contracts to support a discount code or coupon.
This process is secured and cannot be bypassed by calling the contract directly as the discount code is used to submit the transaction on-chain.
When the user enters a discount code on the frontend application, it is used to generate a private key that is then used to sign the recipient's address. That signature is passed as the data argument on the purchase
call.
A lock manager can add any number of discount code to their lock by calling the function setDiscountCodeForLock
multiple times.
The Unlock Protocol team has deployed and verified a version of this hook on the following networks:
Production networks:
- Optimism:
0x8e0B46ec3B95c81355175693dA0083b00fCc1326
- Polygon:
0x93E160838c529873cB7565106bBb79a3226FE07A
Test networks:
This example lock is deployed on Goerli and uses this discount hook with 2 different discounts:
FRIENDS
for a 50% discountFAMILY
for a 100% discount
This means if you purchase a key through this checkout URL and enter any of the 2 discount codes you will see a discounted price on the final confirmation screen!
If you don't enter a discount code, you will pay the full price of 0.01 Eth.
- First, you need to generate promo codes, then go to this page to generate the corresponding signer address. You can also generate this locally if needed by checking out the repo and switching to the
gh-page
branch.
- Then, click on the network your lock has been deployed on (list above) and head to
Contract
>Write Contract
. Connect your wallet (you need to be connected as one of the lock's manager) and click onsetDiscountForLock
. There, enter the lock address, and then the signer address generated in the previous step and the discount amount to be applied. Since the EVM does not support floating numbers, you have to enter the discount percentage in basis points. For example for a 100% discount, you would enter10000
. For a 3% discount, you would enter300
.
- After that, you need to point your lock to the hook. You can do that by going to your lock's settings page on the Unlock Dashboard. Then check the Advanced tab and the Hooks section. Add the address of the key purchase hook for the network your lock is deployed on. You can find a list of all the key purchase hook addresses listed above (and please get in touch with us if you need it to be deployed on more networks).
- Finally, build a Checkout URL and make sure you tick
Promo Codes
option for the lock to which you are applying a discount!
You can deploy the hook on other chains by adding the chain to the hardhat.config.js
config file and calling:
yarn run hardhat run scripts/deploy.js --network my-network
To verify the contract on block explorers, call :
yarn run hardhat verify --network my-network 0xhook-address
Running tests:
yarn run hardhat test test/sample-test.js