Created
October 30, 2019 20:18
-
-
Save julien51/18361565bf589086441b2826f194eed6 to your computer and use it in GitHub Desktop.
unlock-js wallet example : deploys a lock, purchases a key
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const HDWalletProvider = require("truffle-hdwallet-provider") | |
const { WalletService } = require("@unlock-protocol/unlock-js") | |
const provider = new HDWalletProvider( | |
mnemonic, | |
endpoint, | |
accountIndex | |
) | |
async function run() { | |
/** | |
* Unlock Contract: | |
* mainnet: 0x3d5409cce1d45233de1d4ebdee74b8e004abdd13 | |
* rinkeby: 0xd8c88be5e8eb88e38e6ff5ce186d764676012b0b | |
*/ | |
const service = new WalletService({ | |
unlockAddress: "0xd8c88be5e8eb88e38e6ff5ce186d764676012b0b" | |
}) | |
// Connects | |
await service.connect(provider) | |
// Creates a lock | |
const lockAddress = await service.createLock( | |
{ | |
expirationDuration: 60 * 60 * 24 * 10, | |
keyPrice: "1", | |
maxNumberOfKeys: -1, // Unlimited | |
name: "Weenus lock", | |
currencyContractAddress: "0xaFF4481D10270F50f203E0763e2597776068CBc5" // https://github.com/bokkypoobah/WeenusTokenFaucet | |
}, | |
(error, hash) => { | |
console.log(`Lock creation transaction hash ${hash}`) | |
} | |
) | |
console.log(`Lock deployed at ${lockAddress}`) | |
// Purchases a key for owner | |
const owner = "0x33ab07dF7f09e793dDD1E9A25b079989a557119A" | |
const tokenId = await service.purchaseKey( | |
{ | |
lockAddress, | |
owner, | |
keyPrice: "1" | |
}, | |
(error, hash) => { | |
console.log(`Key purchase transaction hash ${hash}`) | |
} | |
) | |
console.log(`New key sold: ${tokenId}`) | |
} | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment