Hello! Please choose your
desired language:
Dismiss

Scene blockchain operations

By interacting with the Ethereum blockchain you can, for example, require that a user pays a fee before activating something in your scene. The SDK is currently experimenting with different ways to interface with the Ethereum blockchain. Currently there are three ways you can achieve this:

  • Using the Web3 API
  • Using the ethjs library
  • Using the eth-connect library

IMPORTANT: At the present time, none of these solutions are fully supported by the SDK, and the way in which they are implemented is very likely to change. For this reason, we advise that you only try these out for proofs of concept, but not to develop final experiences.

Obtain a user’s public key

You can obtain a user’s public Ethereum key by using getUserPublicKey(). You can then use this information to send payments to the user, or as a way to recognize users.

The example below imports the UserIdentity library and runs getUserPublicKey() to get the public key of the user’s Metamask account and log it to console. The user must be logged into their Metamask account on their browser for this to work.

import { createElement, inject, UserIdentity } from "decentraland-api/src"

export default class Scene extends ScriptableScene<any, any> {
  @inject("Identity")
  userIdentity: UserIdentity

  state = {
    publicKey: ""
  }

  async sceneDidMount() {
    const publicKey = await this.userIdentity!.getUserPublicKey()
    this.setState({ publicKey })
    console.log(this.state.publicKey)
  }

  render() {
    return <scene />
  }
}

Web3 API

The SDK includes an interface for the Web3 API. You can use it to call methods of this API from your scene’s code. The interface includes two methods: send and sendAsync, which can be used to call the methods from the API. We have only whitelisted the following methods from the API, all others are currently not supported:

  • eth_sendTransaction
  • eth_getTransactionReceipt
  • eth_estimateGas
  • eth_call
  • eth_getBalance
  • eth_getStorageAt
  • eth_blockNumber
  • eth_gasPrice
  • eth_protocolVersion
  • net_version
  • eth_getTransactionCount
  • eth_getBlockByNumber

To use it, you must first install web3 in your local machine. To do so, run the following:

npm i web3

IMPORTANT: The SDK works with version 0.20.6 of the Web3 library. It doesn’t currently support newer versions.

Below is a sample that uses this API to get the contents of a block in the blockchain.

import { createElement, ScriptableScene } from "decentraland-api"
import Web3 = require("web3")

export default class EthereumProvider extends ScriptableScene {
  async sceneDidMount() {
    const provider = await this.getEthereumProvider()
    const web3 = new Web3(provider)

    web3.eth.getBlock(48, function(error: Error, result: any) {
      console.log("Eth block 48 (from scene)", result)
    })
  }

  async render() {
    return <scene />
  }
}

For more details and a full reference of what’s possible with this API, see Web3’s documentation

ethjs library

To use it, you must first install ethjs in your local machine. To do so, run the following:

npm install --save ethjs

For more details and a full reference of what’s possible with this library, see ethjs’s documentation