logo

(Un)Locking Decentraland!

We're very excited to introduce the Unlock Decentraland integration! It lets Decentralandd developers easily create private spaces that only users who own a specific NFT can access!

By Julien Genestoux on

The past 18 months have proven that "real life" is also very much happening online. The metaverse has been taking a significantly bigger place in our lives! Among all platforms, Decentraland is one built on top of the Ethereum network. For example, the parcels are NFT.

When we started our grants program a few months ago, one of our community members, Henry, reached out. He's a gaming expert and wanted to showcase what it would mean to build a private club in Decentraland. His grant application was approved, and today, we're very excited to show his work: he made it simple for developers to build memberships on their parcels.

Want to try it? Here's a saloon, where only members can enter :) The membership costs 0.1 MANA!

Enter the saloon

Play Unlock Decentraland demo

We really love this integration because it shows how versatile the protocol is: memberships are everyone, and the Unlock Protocol makes it easy to deploy them for creators!

Building your own!

Henry went as far as creating a 2nd scene that developers can use as a reference implementation. Let's review the code and see how you, a developer, can add a lock to a scene where users are allowed to move!

The first step is to install a npm module to your project.

yarn add @thehen/decentraland-unlock-integration

Then, you need to configure Unlock with the following command (make sure you replace the lock address!).

const decentralandLock = new unlock.Lock(
  '0x9625Bc447d23117e22105B77FAC015F6B970f0C0'
)

After this, you're ready to handle events and trigger things. In this demo, the door migt be open or closed, based on events triggered in the scene

sceneMessageBus.on('openDoor', ({ sender }) => {
  if (!door.isOpen) {
    door.toggle(true)
  }
})
sceneMessageBus.on('closeDoor', ({ sender }) => {
  if (door.isOpen) {
    door.toggle(false)
  }
})

Once Unlock is initialized, an event will be triggered indicating whether the current user is indeed a member and the door should be open, or if they are not... In the latter case, we allow the user to purchase a membership when they click on the door!

unlock.eventManager.addListener(
  unlock.LockInitialised,
  'unlockInit',
  ({ lock, hasValidKey }) => {
    if (hasValidKey) {
      // already owns key so open the door
      sceneMessageBus.emit('openDoor', {})
    } else {
      // doesn't own key

      // Instantiate Unlock UI object
      const unlockPurchaseUI = new unlock.UnlockPurchaseUI(
        decentralandLock,
        'https://raw.githubusercontent.com/thehen/decentraland-unlock-integration/master/images/unlock-logo-black.png',
        'Unlock lets you easily offer paid memberships to your \n website or application. \n It is free to try! Just click "purchase" below.'
      )

      // Show UI when cube is clicked
      door.addComponent(
        new OnClick(
          () => {
            unlockPurchaseUI.show()
          },
          {
            button: ActionButton.ANY,
            showFeedback: true,
            hoverText: 'Unlock Door',
          }
        )
      )
    }
  }
)

Finally, if the purchase is succesful, we open the door, letting the user in:

unlock.eventManager.addListener(
  unlock.PurchaseSuccess,
  'purchase success',
  ({ lock }) => {
    sceneMessageBus.emit('openDoor', {})
  }
)

You can find all of the code detailed above in this file.

Unlocking games!

In a game, a membership can let the user access specific private spaces, like a saloon, a concert, a lecture… etc, but they can also let users unlock features or particular features.

We're really excited to see what developers can build on top of this integration! And one that we're interested in beyond this is the idea that a single membership can be used by multiple games or even entirely outside of games. For example, someone could set up a Discord server that uses the same membership and create member-only channels mapped to the games' memberships!

Your turn!

The Unlock protocol is a protocol for memberships that is collectively governed through our governance token UDT. Unlock Inc. distributes some of the pre-mined tokens in the form of grants.

Like Henry, you can apply for a grant to receive UDT!