About your Locks and Smart Contracts

Overview

The Lock Smart Contract has multiple capabilities:
  • Administrative: these are the functions which change rights associated to the lock or individual parameters for the lock a such as its name or of course its price. Finally, there is a method to withdraw funds from the lock contract itself.
  • Transferring key ownership: keys can be purchased from the lock smart contract itself or from another user who purchased one previously. Another element is that keys can be purchased on behalf of somebody else (this is important because this lets somebody pay gas fees on behalf of somebody else)
  • Changing key attributes: the keys have an expiration date which can be changed (for an earlier date by the lock owner) as well as data attributes which can be changed to something else.

Upgrades

All locks deployed prior to version 10 (to be released by year end 2022) are NOT upgradable, which means their core logic will remain unchanged. Starting with version 10, locks are upgradable by their lock manager, through the Unlock contract.

Functions

initialize

This function is the initializer and called when the lock is deployed. You don't need to call this function (and calling it will fail).
1
function initialize(
2
address _lockCreator, // The first owner of the lock
3
uint _expirationDuration, // The duration of each membership when they are purchased
4
address _tokenAddress, // The address of the ERC20 token used to price the membership
5
uint _keyPrice, // The price of the membership (beware the number of decimals)
6
uint _maxNumberOfKeys, // The maximum number of NFTs (keys) available for purchase
7
string calldata _lockName // The name of the lock
8
) external;
Copied!
NB: To create non-expiring memberships, just set the _expirationDuration to 0.
Once minted, the address deploying it becomes its first Lock manager. The Unlock contract also keeps track of each locks' address.

purchase

This function can be invoked to purchase a key (NFT membership). To purchase a membership, the caller should pass the number of tokens to be transfered (an ERC20 approval should have been initiated earlier), and this amount can be larger than the actual keyPrice. It is possible to purchase a membership on behalf of another user by setting a different recipient address. The referrer address is the address that will receive some UDT governance tokens as part of this transaction (if applicable). A key manager can also be set directly during the purchase, allowing a different account to manage the key while the recipient still benefit from a full membership - see Access Control for more. Finally an arbitrary data object can be passed and is propagated to hooks and/or emitted as part of events.
This function will throw when the lock has been disabled, or if it is sold out.
1
function purchase(
2
uint256 _value,
3
address _recipient,
4
address _referrer,
5
address _keyManager,
6
bytes calldata _data
7
) external payable;
Copied!

grantKeys

Allows a Lock manager to give a collection of users a key with no charge. This is the mechanism used to "airdrop" memberships. Each key may be assigned a different expiration date and a different key manager.
1
function grantKeys(
2
address[] calldata _recipients,
3
uint[] calldata _expirationTimestamps,
4
address[] calldata _keyManagers
5
) external;
Copied!

shareKey

This function can be called to transfer some of the time on a membership and do a "partial" transfer.
1
function shareKey(
2
address _to,
3
uint _tokenId,
4
uint _timeShared
5
) external;
Copied!

transfer

We implemented an ERC20 style transfer so that keys can be sent directly from wallets that usually support ERC20 transfers but have lower coverage for ERC721 functions
1
function transfer(
2
address _to,
3
uint _value
4
) external
5
returns (bool success);
Copied!

getTokenIdFor

A function that returns the token id for an address (or the Zero address if the address does not own any).
1
function getTokenIdFor(
2
address _account
3
) external view returns (uint);
Copied!

keyExpirationTimestampFor

A function that returns the expiration timestamp for the token owned by one address. It returns 0 if the user does not own a key and a timestamp from the past if the membership has expired.
1
function keyExpirationTimestampFor(
2
address _keyOwner
3
) external view returns (uint timestamp);
Copied!

purchasePriceFor

This function can be used to retrieve the actual price for a specific user as the hook can alter the price and the front-end application might want to show the actual specific price for the user.
1
function purchasePriceFor(
2
address _recipient,
3
address _referrer,
4
bytes calldata _data
5
) external view
6
returns (uint);
Copied!

getTransferFee

Determines how much of a fee a key owner would need to pay in order to transfer the key to another account. This is pro-rated so the fee goes down overtime. It returns the transfer in seconds.
1
function getTransferFee(
2
address _keyOwner,
3
uint _time
4
) external view returns (uint);
Copied!

expireAndRefundFor

A lock manager can invoke this function to expire a key and perform and refund based on the amount specified.
1
function expireAndRefundFor(
2
address _keyOwner,
3
uint amount
4
) external;
Copied!

cancelAndRefund

Allows a key manager to expire and get a refund for their key, based on the cancellation terms.
1
function cancelAndRefund(uint _tokenId) external;
Copied!

updateRefundPenalty

Allow a Lock manager to change the refund penalty. The first param is the duration of the free trial in seconds, and the second is the penalty in basis-points.
1
function updateRefundPenalty(
2
uint _freeTrialLength,
3
uint _refundPenaltyBasisPoints
4
) external;
Copied!

getCancelAndRefundValueFor

Determines how much of a refund a key owner would receive if they issued a cancelAndRefund transaction. Note that due to the time required to mine a tx, the actual refund amount will be lower than what the user reads from this call.
1
function getCancelAndRefundValueFor(
2
address _keyOwner
3
) external view returns (uint refund);
Copied!

disableLock

This function can only be called by the lock manager and is used to completely disable a lock and prevents any key purchase and transfer. This cannot be reversed.
1
function disableLock() external;
Copied!

updateKeyPricing

This function can be called by any lock manager to change the pricing on the lock, including both the amount of currency. For locks priced in the chain's native currency, use the Zero address.
1
function updateKeyPricing( uint _keyPrice, address _tokenAddress ) external;
Copied!

withdraw

This function can be called by a lock manager or the beneficiary to transfer all or some of the funds on the lock to the beneficiary address. The tokenAddress can be specified to transfer funds from an ERC20 when the currency has been changed. Note: consider avoiding full withdrawals as it breaks the cancelAndRefund and expireAndRefundFor methods.
1
function withdraw(
2
address _tokenAddress,
3
uint _amount
4
) external;
Copied!

approveBeneficiary

This function can only be called by a lock manager to approve a 3rd party address to withdraw funds from the lock.
1
function approveBeneficiary(
2
address _spender,
3
uint _amount
4
) external
5
returns (bool);
Copied!

updateBeneficiary

A function that can be called by any lock manager to change the address of the beneficiary address. The beneficiary can be either ad user's address or a contract's address.
1
function updateBeneficiary( address _beneficiary ) external;
Copied!

setKeyManagerOf

This function can be called by a lock manager or the corresponding key manager to set the key manager for a key. This can be used to "loan" a key or collateralize it.
1
function setKeyManagerOf(
2
uint _tokenId,
3
address _keyManager
4
) external;
Copied!

setMaxNumberOfKeys

This function allows any lock manager to change the maximum number of keys a lock can edit.
1
function setMaxNumberOfKeys (
2
uint _maxNumberOfKeys
3
) external;
Copied!

ERC721 functions

The lock implements the ERC721 specification as described there.

balanceOf

This function is an ERC721 (and ERC20 function) that will return 1 if the user has a valid membership, but 0 if not (either no membership or expired).
1
function balanceOf(address _owner) public view returns (uint256 balance);
Copied!

Other methods

1
function ownerOf(uint256 tokenId) public view returns (address _owner);
2
3
function transferFrom(address from, address to, uint256 tokenId) public;
4
5
function safeTransferFrom(address from, address to, uint256 tokenId) public;
6
7
function approve(address to, uint256 tokenId) public;
8
9
function getApproved(uint256 _tokenId) public view returns (address operator);
10
11
function setApprovalForAll(address operator, bool _approved) public;
12
13
function isApprovedForAll(address _owner, address operator) public view returns (bool);
14
15
function safeTransferFrom(address from, address to, uint256 tokenId, bytes memory data) public;
16
17
function totalSupply() public view returns (uint256);
18
19
function tokenOfOwnerByIndex(address _owner, uint256 index) public view returns (uint256 tokenId);
20
21
function tokenByIndex(uint256 index) public view returns (uint256);
Copied!

Setters

Allows a Lock manager to assign a descriptive name for this Lock.
1
function updateLockName(
2
string calldata _lockName
3
) external;
Copied!
Allows a Lock manager to assign a Symbol for this Lock.
1
function updateLockSymbol(
2
string calldata _lockSymbol
3
) external;
Copied!
Allows a Lock manager to update the baseTokenURI for this Lock.
1
function setBaseTokenURI(
2
string calldata _baseTokenURI
3
) external;
Copied!
Lets a lock manager set event hooks to be triggered on key purchases and on cancellation.
1
function setEventHooks(
2
address _onKeyPurchaseHook,
3
address _onKeyCancelHook
4
) external;
Copied!
Allow a Lock manager to change the transfer fee. The transfer fee is expressed in basis-points. For example: 200bps = 2% and means that 2% of the time transfered is "burnt" during the transfer.
1
function updateTransferFee(
2
uint _transferFeeBasisPoints
3
) external;
Copied!
These functions are used to set, unset and check roles on the lock. Note that Lock Manager can only "renounce" their role and cannot be revoked.
1
function addKeyGranter(address account) external;
2
3
function addLockManager(address account) external;
4
5
function isKeyGranter(address account) external view returns (bool);
6
7
function isLockManager(address account) external view returns (bool);
8
9
function revokeKeyGranter(address _granter) external;
10
11
function renounceLockManager() external;
Copied!

Getters

Returns the lock version number:
1
function publicLockVersion() public pure returns (uint);
Copied!
Returns the name of a lock:
1
function name() external view returns (string memory _name);
Copied!
Returns the current number of owners (including expired keys):
1
function numberOfOwners() external view returns (uint);
Copied!
Returns the token symbol
1
function symbol()
2
external view
3
returns(string memory);
Copied!
Yields the tokenURI as a string for a specific tokenId. This is part of the ERC721 spec: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md
1
function tokenURI(
2
uint256 _tokenId
3
) external view returns(string memory);
Copied!
This yields the address of the onKeyPurchaseHook.
1
function onKeyPurchaseHook() external view returns(address);
Copied!
This yields the address of the onKeyCancelHook.
1
function onKeyCancelHook() external view returns(address);
Copied!
This yields the beneficiary who will receive the lock's funds.
1
function beneficiary() external view returns (address );
Copied!
This yields the duration of each key.
1
function expirationDuration() external view returns (uint256 );
Copied!
This yields the duration of the free trial during which keys can cancelled at no cost.
1
function freeTrialLength() external view returns (uint256 );
Copied!
This returns true if the lock is still alive.
1
function isAlive() external view returns (bool );
Copied!
This returns the currency used to charge for keys. If this is the Zero address, the lock uses the chains's native currency.
1
function tokenAddress() external view returns (address );
Copied!
This returns the price of keys.
1
function keyPrice() external view returns (uint256 );
Copied!
This returns the maximum number of keys that can be purchased on that lock.
1
function maxNumberOfKeys() external view returns (uint256 );
Copied!
This retrns the owner of a key (NFT) based on its id. This is an ERC721 method.
1
function ownerOf(uint256 tokenId) public view returns (address _owner);
Copied!
This returns the key manager of a key based on its id.
1
function keyManagerOf(uint) external view returns (address );
Copied!
This returns the refund penalty in basis points.
1
function refundPenaltyBasisPoints() external view returns (uint256 );
Copied!
This returns the transfer fee in basis points.
1
function transferFeeBasisPoints() external view returns (uint256 );
Copied!