Unlock
Search…
Using Unlock with Ethers
In this tutorial, we will see how to use Unlock's contract with the popular Ethers library.

Ethers

Ethers is a populer Ethereum library written in Javascript. It lets developers easily integrate Ethereum smart contracts in their applications.
In order to "read" state from the blockchain, Ethers requires the use of a provider. Based on the network that the contracts you are trying to interface with are deployed, you will have to use a different provider.
Similarly, if you are trying to modify the state of a contract, you will need to use a "signer", connected to a provider.

Accessing the contracts ABI

The ABI is a description of all functions supported by a smart contract. Ethers uses ABI to "construct" JavaScript objects that map with the smart contracts. All of the Unlock contracts are not only open source, but also verified on Etherscan. This means you can easily retrieve their ABI from there. (You can also use our @unlock-protocol/contracts package).

Basic Example

In this first example, we want to read state from a specific lock deployed on Rinkeby
1
const ethers = require('ethers')
2
const abis = require('@unlock-protocol/contracts')
3
4
// Wrapping all calls in an async block
5
const run = async () => {
6
// Here we use a Rinkeby provider. We will be able to read the state, but not send transactions.
7
const provider = new ethers.providers.JsonRpcProvider("https://rinkeby.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161")
8
9
// We will interact with a lock deployed on rinkeby at this address 0xafa8fE6D93174D17D98E7A539A90a2EFBC0c0Fc1
10
// Using Etherscan, we know that this is a lock of version 8, so we will load the corresponding ABI.
11
const address = '0xafa8fE6D93174D17D98E7A539A90a2EFBC0c0Fc1'
12
13
const lock = new ethers.Contract(address, abis.PublicLockV8.abi, provider)
14
15
// After that we can read the state of the lock, using methods from its ABI:
16
console.log(await lock.symbol()) // => "KEY"
17
console.log(await lock.name()) // => "Test Oct 20"
18
}
19
run()
Copied!

Finding the lock version

Even though the ABI differs between every version, some functions are shared between all lock versions in order to simplify compatinility. One of them is publicLockVersion. You can then easily retrieve the version of a lock with the following.
1
const ethers = require('ethers')
2
3
// Simplest version of a lock's ABI
4
const PublicLockAbi =[
5
{
6
"constant": true,
7
"inputs": [],
8
"name": "publicLockVersion",
9
"outputs": [
10
{
11
"internalType": "uint256",
12
"name": "",
13
"type": "uint256"
14
}
15
],
16
"payable": false,
17
"stateMutability": "pure",
18
"type": "function"
19
}
20
]
21
22
// Wrapping all calls in an async block
23
const run = async () => {
24
const provider = new ethers.providers.JsonRpcProvider("https://rinkeby.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161")
25
const address = '0xafa8fE6D93174D17D98E7A539A90a2EFBC0c0Fc1'
26
const lock = new ethers.Contract(address, PublicLockAbi, provider)
27
28
console.log((await lock.publicLockVersion()).toString()) // => 8
29
}
30
run()
Copied!
Note in the example above that await lock.publicLockVersion() returns a BigNumber which we convert to a string using Ethers' helper `toString().

Modifying the state

When writing an application, you may want to modify the state of Lock. The simplest and most commone state modification is to purchase a membership. Here is a detailed example.
1
const ethers = require('ethers')
2
const abis = require('@unlock-protocol/contracts')
3
4
// Wrapping all calls in an async block
5
const run = async () => {
6
// Here we use a Rinkeby provider. We will be able to read the state, but not send transactions.
7
const provider = new ethers.providers.JsonRpcProvider("https://rinkeby.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161")
8
9
// This time, we also need a signer.
10
// Note: we sent some fake Eth to this address, but please replace with your own!
11
const wallet = ethers.Wallet.fromMnemonic('seed cube fiction obvious cover riot edge beauty pelican radio useful strong')
12
const signer = wallet.connect(provider)
13
14
// We will interact with a lock deployed on rinkeby at this address 0xafa8fE6D93174D17D98E7A539A90a2EFBC0c0Fc1
15
const address = '0xafa8fE6D93174D17D98E7A539A90a2EFBC0c0Fc1'
16
17
// Let's go purchase the membership for this lock
18
const lock = new ethers.Contract(address, abis.PublicLockV8.abi, signer)
19
20
// If the lock was using an ERC20 as currency, we would need to send an approval transaction on the ERC20 contract first...
21
22
// Let's get the key price so we know how much we need to send (we could send more!)
23
const amount = await lock.keyPrice()
24
25
// Purchase params:
26
const purchaseParams = [
27
amount,
28
wallet.address, // This is the recipient of the membership (us!)
29
wallet.address, // The is the referrer who will earn UDT tokens (we'd like this to be us!)
30
[], // empty data object (not used here)
31
]
32
33
const gasPrice = await provider.getGasPrice() // Let's get the current gas price
34
const options = {
35
gasPrice,
36
value: amount // This is a lock that uses Ether, so it means we need send value. If it was an ERC20 we could set this to 0 and just use the amount on purchase's first argument
37
}
38
39
// Important: we need to compute the gasLimit ourselves because it is a funcion of gasPrice
40
// For safety we could also bump it (the user is refunded the difference anyway)
41
const gasEstimate = await lock.estimateGas.purchase(...purchaseParams, options)
42
options.gasLimit = gasEstimate
43
44
// We can now send transactions to modify the state of the lock, like purchase a key!
45
const transaction = await lock.purchase(...purchaseParams, options)
46
const receipt = await transaction.wait()
47
}
48
run()
Copied!
All in all, if you are already using Ethers, all of this should be pretty familiar!
Last modified 10d ago