# @dcl/l2-scene-utils

> A collection of helpers to make it easier to build a Decentraland scene using the SDK.

Latest version **2.0.0** (published 2023-01-23) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @dcl/l2-scene-utils
pnpm add @dcl/l2-scene-utils
yarn add @dcl/l2-scene-utils
bun add @dcl/l2-scene-utils
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2023-01-23 |
| First published | 2021-02-12 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 2.4 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 17 |
| Author | Decentraland |
| Maintainers | decentralandbot |

## Links

- npm: https://www.npmjs.com/package/@dcl/l2-scene-utils
- Repository: https://github.com/decentraland/decentraland-l2-utils
- Homepage: https://github.com/decentraland/decentraland-l2-utils#readme
- Issues: https://github.com/decentraland/decentraland-l2-utils/issues
- npm.io page: https://npm.io/package/@dcl/l2-scene-utils

## Dependencies (2)

- [eth-connect](https://npm.io/package/eth-connect.md) ^6.0.3
- [decentraland-transactions](https://npm.io/package/decentraland-transactions.md) ^1.42.0

## Recent versions

- 2.0.0 (latest) — 2023-01-23
- 2.0.1-20230503210854.commit-7534a61 (next) — 2023-05-03
- 0.0.0-20210212191557.commit-93208ec (tag-v1.0.5) — 2021-02-12
- 1.0.8-20230123121733.commit-62cef26 — 2023-01-23
- 1.0.8-20221227092545.commit-81bbb80 — 2022-12-27
- 1.0.8-20220621133639.commit-cdf9545 — 2022-06-21
- 1.0.8-20220613200203.commit-ecdaa25 — 2022-06-13
- 1.0.8-20220613194929.commit-3376673 — 2022-06-13
- 1.0.8-20220613194536.commit-4896fc2 — 2022-06-13
- 1.0.8-20220513212319.commit-d499110 — 2022-05-13
- 1.0.8-20210902153610.commit-ec1ae35 — 2021-09-02
- 1.0.8-20210717115000.commit-cc89e4f — 2021-07-17
- 1.0.8-20210406122258.commit-7e86870 — 2021-04-06
- 1.0.8-20210405162444.commit-08bd3ce — 2021-04-05
- 1.0.7 — 2021-02-22
- … 5 more at https://npm.io/package/@dcl/l2-scene-utils/versions

## README

# decentraland-l2-utils

This library includes a number of helpful pre-built tools that help you deal with common requirements that involve and interacting with data on the second layer blockchain.

- [ERC20](#ERC20)
  - [Get balance](#get-balance)
  - [Deposit MANA](#deposit-MANA)
  - [Send MANA](#send-MANA)
  - [Withdraw MANA](#withdraw-MANA)

## Using the L2 library

To use any of the helpers provided by the utils library

1. Install it as an `npm` package. Run this command in your scene's project folder:

```
npm i @dcl/l2-scene-utils eth-connect decentraland-transactions -B
```

Note: This command also installs the latest version of the decentraland-transactions and eth-connect libraries, that are dependencies of the l2 utils library

2. Run `dcl start` or `dcl build` so the dependencies are correctly installed.

3. Import the library into the scene's script. Add those lines at the start of your `game.ts` file, or any other TypeScript files that require it:

```ts
import * as l2 from '@dcl/l2-scene-utils'
import * as ethConnect from 'eth-connect'
```

```ts
const { mana } = await l2.createComponents()
const balanceWei = await mana.balance('0xFE2d424AF0Df49Bb3316cB2E9f574b0D09cf98Ad')
log(balanceWei)
if (
  new ethConnect.BigNumber(ethConnect.fromWei(balanceWei, 'ether')).comparedTo(
    new ethConnect.BigNumber(1)
  ) >= 0
) {
  mana
    .transfer(
      `0xFE2d424AF0Df49Bb3316cB2E9f574b0D09cf98Ad`,
      new ethConnect.BigNumber(ethConnect.toWei(1, 'ether'))
    )
    .then(
      () => {},
      (error) => {
        log('error', error)
      }
    )
}
```

## MANA Operations

As MANA is Decentraland's main currency, this library provies tools to make it especially easy to use in a scene.

### Get balance of an address

To check the balance of a specific address in MANAwei, use the `balance()` function. This function has an optional argument:

- `address`: Ethereum address to check, if not provided, it will check the balance of the

```ts
executeTask(async (e) => {
  const { mana } = await l2.createComponents()
  const balanceWei = await mana.balance('0xFE2d424AF0Df49Bb3316cB2E9f574b0D09cf98Ad')
  log(balanceWei)
})
```

### Send MANA to an address

To make players in your scene send MANA to a specific address, use the `transfer()` function. This function requires the following arguments:

- `to`: What ethereum address to send the MANA to
- `amount`: How many MANAwei to send

```ts
mana.transfer(`0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee`, 100)
```

For example, your scene can have a button that requests players to make a MANA payment to the scene cretor's personal wallet. The button opens a door, but only once a transaction is sent to pay the fee.

```ts
import * as l2 from '@dcl/l2-scene-utils'
import * as ethConnect from 'eth-connect'

(...)

button.addComponent(new OnPointerDown(async e => {
	const { mana } = await l2.createComponents()
	const tx = await mana.transfer(
		`0x521b0fef9cdcf250abaf8e7bc798cbe13fa98692`,
		new ethConnect.BigNumber(ethConnect.toWei(1, 'ether'))
	)
}))
```

In this scenario, when players click on the button, they are prompted by Metamask to accept the transaction.
Once that transaction is sent on the Matic network, the door opens.

---

## Contribute

In order to test changes made to this repository in active scenes, do the following:

1. Run `npm run link` on this repository
2. On the scene directory, after you installed the dependency, run `npm link @dcl/l2-scene-utils`

## CI/CD

This repository uses `semantic-release` to atumatically release new versions of the package to NPM.

Use the following convention for commit names:

`feat: something`: Minor release, every time you add a feature or enhancement that doesn’t break the api.

`fix: something`: Bug fixing / patch

`chore: something`: Anything that doesn't require a release to npm, like changing the readme. Updating a dependency is **not** a chore if it fixes a bug or a vulnerability, that's a `fix`.

If you break the API of the library, you need to do a major release, and that's done a different way. You need to add a second comment that starts with `BREAKING CHANGE`, like:

```
commit -m "feat: changed the signature of a method" -m "BREAKING CHANGE: this commit breaks the API, changing foo(arg1) to foo(arg1, arg2)"
```

---
_Source: https://npm.io/package/@dcl/l2-scene-utils · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
