Using an existing contract
Your lock is an NFT contract, but you can also plug-in an existing contract (ERC721, ERC1155 or ERC20) to make sure any visitor is treated as a valid member
Locks are membership contracts that can "mint" their own NFT (ERC721) as membership cards. However in some cases, you may want to consider other users as "members" even though they might not have one of the actual membership cards from the lock itself (we call them keys). In that case, you would hook your lock to an external contract that would itself provide its own membership cards.
Note: the Lock contract and the pre-existing contract NFT (ERC721 or ERC1155) or ERC20 need to be on the same network...
We have deployed these hook contracts on each of the networks that we currently support:
Using an ERC20
Using an ERC721
Using an ERC1155
When someone visits a site or application that offers member benefits, it uses a function called
hasValidKeyon the smart-contract (or
balanceOf). Internally, the lock contract keeps track of the list of members thru the expiration of their membership. So when
hasValidKeyis checked, it simply verifies that the expiration for that specific address is in the future.
However, we also added a hook to that the
hasValidKeymethod so that a 3rd party application could also "override" the default behavior of the
hasValidKeyfunction. If the hook is set, then the contract will call it and use the hook's response to determine if indeed the address should be treated as having a valid key.
The first step is to register your lock on the hook by mapping it to the existing NFT contract.
Using the block explorer (linked from the contract addresses above) for the mapping contract, click on the
Contractand then the
Now, connect your wallet by clicking on the
Connect to Web3button in the block explorer. It is important that you do that from an address that is a Lock Manager on your lock (the account you used to deploy the lock is a lock manager).
Then, in the
createMappingform, enter the following values:
_lockAddress: this is the address for your lock (you can get it from your Unlock Dashboard)
_nftAddress: that is the address for your pre-existing NFT contract (ERC721 or ERC1155). Note that this field is called
_tokenAddressin the case of an ERC20.
_tokenTypeId: (only for ERC1155) the id of the type of token to check
_minAmount: (only for ERC20) the minimum amount of ERC20 that should be held
Then click on
Write. You will be prompted to send a transaction by your wallet. Validate and wait for this to have been executed.
Once done, head to your lock in the block explorer. You can click on the
button on the dashboard, or just type your lock's address in the search box of the block explorer. Once on your lock's page, follow the same steps as above by clicking on
Write as Proxy. Proceed to connect your wallet.
Then, look for the
Hooks are 3rd party contracts that can be called when your lock itself is called. Here we are interested in changing the behavior of the
validKeyfunction, so you will enter the address of the hook contract for your network (see list above!) in the
_onValidKeyHookfield. Make sure you add
0x0000000000000000000000000000000000000000in all the other fields to leave them unset.
Write, confirm the transaction in your wallet and you should be all set!
Here you are hooking a special "hook" contract that we created to match existing ERC721 contracts, but you could actually create your own custom hook for a dedicated contract! You could for example check that an URL owns a specific balance for an ERC20 token, or even create a hook where every address is considered to be a valid member on special days... etc.
Check our Developer Docs on the hook for more details!