0.0.1 • Published 4 years ago

numio-meta-tx v0.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

Based on https://github.com/Lamarkaz/Metacash-contracts

NUMIO META TRANSACTIONS

Numio-Meta-Tx is an API module to run gasless transaction using Meta-Tx concept introduced by Lamarkaz team. This module was modified and updated to suit Numio team requirements.

There are three parts in this module:

  • api - API module which will create ethers wallets and call gasless transactions on SmartWallet contract.
  • relayer - Backend process processing transactions from API module and executing them on blockchain as relayer account in the name of an account which used API module.
  • abi - returns ABI for all Numio-Meta-Tx Smart Contracts: registry,factory,smartwallet,erc20 erc20 is a simple mintable token. This can be used for testing.

Correct scenario to be able to use whole meta transaction sollution is as follows:

  • Deploy Smart Contracts using truffle migrate --reset --network ropsten This will deploy smart contracts in following order:

    • RelayRegistry - this smart contract keeps information about accounts and smart contract addressess allowed to act as relayer.It needs initial relayer address as constructor parameter.
    • SmartWallet - this is Smart Wallet template which will be used by all deployed Smart Wallets.
    • Factory - this is the main smart contract which deploys all new Smart Wallets. It needs Smart Wallet template and RelayRegistry addressess as constructor parameters.

    Smart Contracts are here: https://github.com/robertmagier/numio-meta-tx/tree/master/packages/SmartContracts

  • Create account which will be used as relayer account. This account can be used only for this purpose. Do not use it for anything else

  • Add relayer account address to registry contract. Call the function registryContract.triggerRelay(relayerWallet.address,true) You can get registry contract ABI by require('numio-meta-tx').abi.registry
  • Fund relayer account with some ether. In other case relayer will not be able to execute transactions
  • Create NUMIO_RELAYER instance and start relayer. Read more below.
  • Create new SmartWallet instance for your client. You will need private key to do it. Read below.
  • Once Smart Wallet instance is created you have to deploy Smart Wallet contract to blockchain. Call deployWallet function. Read below.
  • Once it is deployed you can use transfer function to transfer tokens. Read below.

Working testing example is here: ./packages/NumioMetaTx/test/2_relayer.js

HOW TO INSTALL

npm install numio-meta-tx

HOW TO USE API

const NumioTxAPI = require('numio-meta-tx').api
const NUMIO_RELAYER = require('numio-meta-tx').relayer
const ethers = require('ethers')

####CREATE RELAYER

const relayerOptions = {
      port: 4000,
      privateKey: "0x023940abcd",
      providerAddress: "http://localhost:8545",
      infuraNetwork: "ropsten",
      infuraAccessToken: "32423423",
      factoryAddress:"0x43df4c58bef08a5cca6229fa5d0d28ffdd5c953c"
    }

    relayerInstance = new NUMIO_RELAYER()
    await RELAYER.start(relayerOptions)

####CREATE NUMIO SMART WALLET
numio = new NumioTxAPI({
    relayHost: 'http://127.0.0.1:4000'
})
const privateKey = '0xeaf7032cbe0ab9e317bde8cdca5d508abe054d9a28778354c74fc39464038b6b'
clientSmartWallet = await numio.importPrivateKey(privateKey)

let value = 100
let to = accounts[3]
let token = tokenInstance.address
let fee = '10'
let gaspriceInGwei = 1

let result = await clientSmartWallet.deployWallet({token, fee,gasprice:gaspriceInGwei})
console.log('TX Hash:',result.tx)
await provider.waitForTransaction(result.tx)

let deployed =  await clinetSmartWallet.isDeployed()
console.log('Smart Is Deployed:',deployed)
let swAddress = await clientSmartWallet.getSWAddress()
console.log('This is Smart Wallet address. You can mint tokens to this address:',swAddress)

result = await clientSmartWallet.transfer({ token,to,value,fee,gasprice:gaspriceInGwei })
await provider.waitForTransaction(result.tx)

NumioTxAPI.constructor(options)

Creates NumioTxAPI instance. This object allows to create and import existing instances of ethers wallet and SmartWallet Following options are required:

  • relayHost - hostname and port number of relayer backend process. Relayer is an instance of @numio-meta-tx.relayer Example: 'http://localhost:4000'

async importPrivateKey(privateKey: string)

Returns SmartWallet wallet instance based on provided privatekey value. This instance can be used to communicate with relayer to execute deployment and token transfers

async createSmartWallet(password: string)

Creates new Ethers wallet account and new Smart Wallet instance for this account.

  • password - string used to encrypt new account keystore. It will be impossible to decrypt account keystore without it and access to Smart Wallet contract and tokens will be lost.

Returns result object:

  • result.keystore - encrypted ethers wallet keystore
  • result.smartWallet - Numio Smart Wallet instance to be used to call transfer function
  • result.account - ethers wallet instance

async importKeystore(keystore: string,password: string)

Returns SmartWallet instance based on provided keystore and password

async createAccount(password: string)

Creates new random blockchain account and returns ethers wallet instance and encrypted keystore

Returns:

  • result.account - ethers wallet instance
  • result.keystore - encrypted keystore

async SmartWallet.deployWallet({token,fee,gasprice})

Deploys Smart Wallet contract on blockchain. This transaction must be called before it is possible to make token transfer.

Input:

  • token - token address which will be used to cover deployment fee. Optional
  • fee - token amount to be transfered to relayer as a transaction fee. Optional
  • gasprice - gas price in gwei to be used for blockchain transaction by relayer. Optional.

    Returns answer from relayer backend server.

  • failed - true if transaction failed

  • reason - failure reason if transaction failed
  • tx - transaction hash.

async SmartWallet.transfer({token,to,value,fee,gasprice})

Sends transfers transaction to relayerAPI. This function will sign function call message using this instance ethers wallet.

Input:

  • token - token address for which to execute a transfer
  • to - address of the receiving account
  • value - token amount to be transfered to to address
  • fee - token amount to be transfered to relayer as a transaction fee
  • gasprice - gas price in gwei to be used for blockchain transaction by relayer. Optional

    Returns answer from relayer backend server.

  • failed - true if transaction failed

  • reason - failure reason if transaction failed
  • tx - transaction hash.

async SmartWallet.transferETH({to,value,fee,gasprice})

Sends transfers transaction to relayerAPI. This function will sign function call message using this instance ethers wallet.

Input:

  • to - address of the receiving account
  • value - ETH amount to be transfered to to address
  • fee - ETH amount to be transfered to relayer as a transaction fee
  • gasprice - gas price in gwei to be used for blockchain transaction by relayer. Optional

    Returns answer from relayer backend server.

  • failed - true if transaction failed

  • reason - failure reason if transaction failed
  • tx - transaction hash.

async SmartWallet.isDeployed()

Returns true if Smart Wallet was deployed on blockchain.

async SmartWallet.getSWAddress()

Returns SmartWallet contract address. Smart Contract doesn't have to be deployed. It is possible to calculate smart contract address before deployment

async SmartWallet.getRelayer()

Returns blockchain address of the relayer account used to execute transactions on blockchain. This can be used to check relayer ETH balance or to fund account or to check token balance to make sure fees were received

HOW TO USE RELAYER

const NUMIO_RELAYER = require('numio-meta-tx').relayer
const ethers = require('ethers')

const relayerOptions = {
      port: 4000,
      privateKey: "0x023940abcd2314233426353abcdf",
      providerAddress: "http://localhost:8545",
      infuraNetwork: "ropsten",
      infuraAccessToken: "32423423",
      factoryAddress:"0x43df4c58bef08a5cca6229fa5d0d28ffdd5c953c"
    }

    relayerInstance = new NUMIO_RELAYER()
    await RELAYER.start(relayerOptions)
    await RELAYER.stop()

NUMIO_RELAYER.constructor()

Returns relayer instance

NUMIO_RELAYER.start({port,privateKey,providerAddress,infuraNetwork,infuraAccessToken,factoryAddress})

This create relayer instance Input:

  • port - tcp port on which relayer will listen for incoming requests
  • privateKey - private key of blockchain account which will be used by Relayer to execute transactions on chain
  • providerAddress - RPC Provider address. It will be used only if infuraNetwork is not defined. If you use ganache on port 8545 you can define it as 'http://localhost:8545'
  • infuraNetwork - network name if you want to use infura provider: ropsten, rinkeby, kovan, main
  • infuraAccessToken - infura access token
  • factoryAddress - Factory Smart contract address to use for SmartWallet deployment.

NUMIO_RELAYER.stop()

This will stop relayer execution. Can be used at the end of tests in mocha.

Chart

Chart