Scene Blockchain Operations
Learn what the SDK offers for performing operations with the Ethereum blockchain
A Decentraland scene can interface with the Ethereum blockchain. This can serve to obtain data about the user's wallet and the tokens in it, or to trigger transactions that could involve any Ethereum token, fungible or non-fungible. This can be used in many ways, for example to sell tokens, to reward tokens as part of a game-mechanic, to change how a player interacts with a scene if they own certain tokens, etc.
Note that all transactions in the Ethereum mainnet that are triggered by a scene will require a player to approve and pay a gas fee.
All blockchain operations also need to be carried out as asynchronous functions, since the timing depends on external events.
Get a player's ethereum account
To get a player's Ethereum account, use the getPlayer() function.
import { getPlayer } from '@dcl/sdk/src/players'
export function main() {
let userData = getPlayer()
if (!userData.isGuest) {
console.log(userData.userId)
} else {
log('Player is not connected with Web3')
}
}Note that if a player has entered Decentraland as a guest, they will not have a connected ethereum wallet. If they are connected as guests, the isGuest field in the response from getPlayer() will be true. If hasConnectedWeb3 is true, then you can obtain the player's address from the field publicKey. Learn more about the data you can obtain from a player in get player data
You should wrap the function in an async() function, learn more about this in async functions
📔 Note: Even though the eth address may contain upper case characters, some browsers convert the returned string to lower case automatically. If you wish compare address values and have it work on all browsers, use the .toLowerCase() method to convert the value into lower case.
Check gas price
After importing the eth-connect library, you must instance a web3 provider and a request manager, which will will allow you to connect via web3 to Metamask in the player's browser.
The function below fetches the current gas price in the Ethereum main network and prints it.
Import a contract ABI
An ABI (Application Binary Interface) describes how to interact with an Ethereum contract, determining what functions are available, what inputs they take and what they output. Each Ethereum contract has its own ABI, you should import the ABIs of all the contracts you wish to use in your project.
For example, here's an example of one function in the MANA ABI:
ABI definitions can be quite lengthy, as they often include a lot of functions, so we recommend pasting the JSON contents of an ABI file into a separate .ts file and importing it into other scene files from there. We also recommend holding all ABI files in a separate folder of your scene, named /contracts.
Here are links to different Decentraland contracts. Obtain the ABI for each contract by clicking Export ABI and choosing JSON Format.
These are the contracts for the various wearable collections: (each collection was emitted as a separate contract)
Configuring TypeScript to be able to import from a JSON file has its difficulties. The recommended easier workaround is to change the ABI.JSON file's extension to .ts and modifying it slightly so that it its content starts with export default.
For example, if the ABI file's contents starts with [{"constant":true,"inputs":[{"internalType":"bytes4" ...etc, modify it so that it starts with export default [{"constant":true,"inputs":[{"internalType":"bytes4" ...etc.
Instance a contract
After importing the eth-connect library and a contract's abi, you must instance several objects that will allow you to use the functions in the contract and connect to Metamask in the player's browser.
You must also import the web3 provider. This is because Metamask in the player's browser uses web3, so we need a way to interact with that.
Call the methods in a contract
Once you've created a contract object, you can easily call the functions that are defined in its ABI, passing it the specified input parameters.
The example above uses the abi for the Ropsten MANA contract and transfers 100 fake MANA to your account in the Ropsten test network.
Other functions
The eth-connect library includes a number of other helpers you can use. For example to:
Get an estimated gas price
Get the balance of a given address
Get a transaction receipt
Get the number of transactions sent from an address
Convert between various formats including hexadecimal, binary, utf8, etc.
Using the Ethereum test network
While testing your scene, to avoid transferring real MANA or other currencies, you can use the Ethereum Sepolia test network and transfer fake testnet MANA instead.
To use the test network you must set your Metamask Chrome extension to use the Sepolia test network instead of Main network.
You must acquire Sepolia Ether, which you can obtain for free from various external faucets like this one.
To preview your scene using the test network, paste the following URL into a browser tab. This will open the scene on the Decentraland desktop client:
decentraland://realm=http://127.0.0.1:8000&local-scene=true&debug=true&dclenv=zone&position=0,0
Any transactions that you accept while viewing the scene in this mode will only occur in the test network and not affect the MANA balance in your real wallet.
If you need to test transactions in the Polygon Testnet and need to have MANA on that testnet, you'll need to swap MANA to that network after acquiring it in Sepolia. To bridge Sepolia MANA to the Polygon Testnet, visit your Decentraland account page in Sepolia and click on ‘swap’ on the Ethereum MANA side.
When running a preview on the legacy web client, of a scene that uses one of the ethereum libraries, you must open the preview in a separate browser window, have Metamask open in your browser, and manually include the string &ENABLE_WEB3.
Send custom RPC messages
Use the function sendAsync() to send messages over RPC protocol.
Decentraland smart contracts
In the following link you can find a list of Etherum smart contracts relevant to the Decentraland ecosystem. The list includes the contracts in mainnet as well as in other Ethereum test networks.
Last updated