2.6.1 • Published 10 days ago

decentraland-transactions v2.6.1

Weekly downloads
534
License
-
Repository
github
Last release
10 days ago

Decentraland Transactions

NPM version Install Size

Send meta transactions

Table of contents

API

The API consists of one function at the moment, which is all you need to send meta transactions. You might also choose to import pre-loaded contract configurations or some types.

sendMetaTransaction

Sends a meta transaction using a relay server. It's provider agnostic, so it'll take the providers it needs as parameters. The Provider only has to conform to the required interface found on types. See the Configuration type for information on what values you can override.

The provider argument refers to which network you are connected to and therefore where the meta transaction will be signed. The metaTransactionProvider argument is where the meta transaction will be executed. E.g: If you want to send a meta transaction to a second layer like Matic by staying connected on Ethereum Mainnet, provider could be Metamask connected to the Ethereum Mainnet and metaTransactionProvider could be a provider instantiated with a Matic Mainnet RPC URL.

Definition

async function sendMetaTransaction(
  provider: Provider,
  metaTransactionProvider: Provider,
  functionSignature: string,
  contractData: ContractData,
  partialConfiguration: Partial<Configuration> = {}
): Promise<string>

Usage

Using ethers for the providers

import {
  sendMetaTransaction,
  getContract,
  ContractName,
  ChainId
} from 'decentraland-transactions'
import { ethers } from 'ethers'

const manaConfig = getContract(ContractName.MANAToken, ChainId.MATIC_MUMBAI)
const manaContract = new ethers.Contract(
  manaConfig.address,
  manaConfig.abi,
  provider
)

const txHash = await sendMetaTransaction(
  new ethers.providers.Web3Provider(window.ethereum),
  new ethers.providers.JsonRpcProvider('https://rpc-mumbai.matic.today'),
  manaContract.pupulateTransaction.transfer(to, value),
  manaConfig
  // ,{ serverURL: 'override.url' }
)

getContract

Returns data for a collection of useful Decentraland contracts enum. for the different Ethereum chains. It contains all the information necessary to call sendMetaTransaction.

Definition

function getContract(contractName: ContractName, chainId: ChainId): ContractData

Usage

getContract(ContractName.MANAToken, ChainId.ROPSTEN)

Types

Configuration

type Configuration = {
  serverURL: string
}

Check configuration.ts to get an up to date snapshot of the current configuration values.

Provider

Defines the minimun required interface for a Provider. It tries to accomodate for different lib implementations

export interface EIPProvider {
  request: (reqArgs: { method: string; params?: any[] }) => Promise<any>
  send?: (method: string, params?: any[]) => Promise<any>
}
export interface LegacyProvider {
  send: (method: string, params: any[]) => Promise<any>
}
export type Provider = EIPProvider | LegacyProvider

ContractName

Supported contract names

enum ContractName {
  MANAToken = 'MANAToken'
}

ContractData

Represents all the information necessary to interact with a contract. If the contract is a proxy and doesn't need the address, you can leave it empty.

type ContractData = {
  abi: object[]
  address: string
  name: string
  version: string
  chainId: ChainId
}

DataToSign

type DataToSign = {
  types: {
    EIP712Domain: DomainType[]
    MetaTransaction: MetaTransactionType[]
  }
  domain: DomainData
  primaryType: 'MetaTransaction'
  message: {
    nonce: number
    from: string
    functionSignature: FunctionSignature
  }
}

DomainData

type DomainData = {
  name: string
  version: string
  verifyingContract: string
  salt: string
}

Example

Example use

Example using decentraland-connect and ethers to get the providers

import { connection, ProviderType } from 'decentraland-connect'
import {
  sendMetaTransaction,
  getContract,
  ContractName,
  ChainId
} from 'decentraland-transactions'

async function transferMana() {
  try {
    const { provider } = await connection.connect(ProviderType.INJECTED)

    const txHash = await sendMetaTransaction(
      // Connected wallet provider
      provider,
      // L2 matic provider
      new ethers.providers.JsonRpcProvider('https://rpc-mumbai.matic.today'),
      // Function signature
      '0xa9059cbb000000000000000000000000a8d82b0bf686eee78eb5ec882cac98fdd1335ef50000000000000000000000000000000000000000000000000000000000000001',
      // Mana contract for MATIC_MUMBAI
      getContract(ContractName.MANAToken, ChainId.MATIC_MUMBAI)
    )

    console.log('Result tx hash', txHash)
  } catch (error) {
    console.error('An error occurred sending the meta tx', error)
  }
}

Development

To run the project you need to

npm i
npm run test
npm run build

Copyright

This repository is protected with a standard Apache 2 license. See the terms and conditions in the LICENSE file.

2.6.1

10 days ago

2.6.0

16 days ago

2.5.0

17 days ago

2.4.0

27 days ago

2.3.2

2 months ago

2.3.1

2 months ago

2.3.0

2 months ago

2.2.0

2 months ago

2.1.2

2 months ago

2.1.1

2 months ago

2.1.0

2 months ago

2.0.3

3 months ago

2.0.2

3 months ago

2.0.1

3 months ago

1.50.1

3 months ago

1.50.2

3 months ago

1.48.0

5 months ago

1.49.0

5 months ago

1.50.0

5 months ago

1.46.1

10 months ago

1.47.0

10 months ago

1.44.0

1 year ago

1.45.0

1 year ago

1.46.0

1 year ago

1.43.1

1 year ago

1.43.0

1 year ago

1.40.0

2 years ago

1.41.0

1 year ago

1.42.0

1 year ago

1.38.0

2 years ago

1.38.1

2 years ago

1.39.0

2 years ago

1.37.0

2 years ago

1.32.1

2 years ago

1.34.0

2 years ago

1.33.0

2 years ago

1.35.1

2 years ago

1.35.0

2 years ago

1.36.0

2 years ago

1.32.0

2 years ago

1.31.0

2 years ago

1.29.0

2 years ago

1.29.1

2 years ago

1.30.0

2 years ago

1.25.0

2 years ago

1.27.0

2 years ago

1.26.0

2 years ago

1.28.0

2 years ago

1.24.0

3 years ago

1.23.0

3 years ago

1.23.1

3 years ago

1.22.2

3 years ago

1.12.1

3 years ago

1.12.0

3 years ago

1.11.0

3 years ago

1.10.1

3 years ago

1.10.0

3 years ago

1.9.1

3 years ago

1.9.0

3 years ago

1.8.0

3 years ago

1.7.1

3 years ago

1.5.3

3 years ago

1.7.0

3 years ago

1.6.1

3 years ago

1.6.0

3 years ago

1.5.2

3 years ago

1.5.1

3 years ago

1.5.0

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.3.0

3 years ago

1.2.0

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.4

3 years ago

1.0.0

3 years ago