hydro-meta-tx
Hydro Meta Transactions
Last updated 4 months ago by rmagier .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install hydro-meta-tx 
SYNC missed versions from official npm registry.

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

HYDRO META TRANSACTIONS

Hydro-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 Hydro 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 Hydro-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/hydro-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('hydro-meta-tx').abi.registry

  • Fund relayer account with some ether. In other case relayer will not be able to execute transactions

  • Create HYDRO_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/HydroMetaTx/test/2_relayer.js

HOW TO INSTALL

npm install hydro-meta-tx

HOW TO USE API

const HydroTxAPI = require('hydro-meta-tx').api
const HYDRO_RELAYER = require('hydro-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 HYDRO_RELAYER()
    await RELAYER.start(relayerOptions)

####CREATE HYDRO SMART WALLET
hydro = new HydroTxAPI({
    relayHost: 'http://127.0.0.1:4000'
})
const privateKey = '0xeaf7032cbe0ab9e317bde8cdca5d508abe054d9a28778354c74fc39464038b6b'
clientSmartWallet = await hydro.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)


HydroTxAPI.constructor(options)

Creates HydroTxAPI 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 @hydro-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 - Hydro 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 HYDRO_RELAYER = require('hydro-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 HYDRO_RELAYER()
    await RELAYER.start(relayerOptions)
    await RELAYER.stop()

HYDRO_RELAYER.constructor()

Returns relayer instance

HYDRO_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.

HYDRO_RELAYER.stop()

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

Chart

Chart

Current Tags

  • 0.1.37                                ...           latest (4 months ago)

46 Versions

  • 0.1.37                                ...           4 months ago
  • 0.1.36                                ...           4 months ago
  • 0.1.35                                ...           4 months ago
  • 0.1.34                                ...           4 months ago
  • 0.1.33                                ...           4 months ago
  • 0.1.32                                ...           4 months ago
  • 0.1.31                                ...           4 months ago
  • 0.1.30                                ...           4 months ago
  • 0.1.29                                ...           4 months ago
  • 0.1.28                                ...           4 months ago
  • 0.1.27                                ...           4 months ago
  • 0.1.26                                ...           4 months ago
  • 0.1.25                                ...           4 months ago
  • 0.1.24                                ...           4 months ago
  • 0.1.23                                ...           4 months ago
  • 0.1.22                                ...           4 months ago
  • 0.1.20                                ...           4 months ago
  • 0.1.19                                ...           4 months ago
  • 0.1.18                                ...           4 months ago
  • 0.1.17                                ...           5 months ago
  • 0.1.16                                ...           5 months ago
  • 0.1.15                                ...           5 months ago
  • 0.1.14                                ...           5 months ago
  • 0.1.13                                ...           5 months ago
  • 0.1.12                                ...           5 months ago
  • 0.1.11                                ...           5 months ago
  • 0.1.10                                ...           5 months ago
  • 0.1.9                                ...           5 months ago
  • 0.1.8                                ...           5 months ago
  • 0.1.7                                ...           5 months ago
  • 0.1.6                                ...           5 months ago
  • 0.1.5                                ...           5 months ago
  • 0.1.4                                ...           5 months ago
  • 0.1.3                                ...           5 months ago
  • 0.1.2                                ...           5 months ago
  • 0.1.1                                ...           5 months ago
  • 0.1.0                                ...           5 months ago
  • 0.0.9                                ...           5 months ago
  • 0.0.8                                ...           5 months ago
  • 0.0.7                                ...           5 months ago
  • 0.0.6                                ...           5 months ago
  • 0.0.5                                ...           5 months ago
  • 0.0.4                                ...           5 months ago
  • 0.0.3                                ...           5 months ago
  • 0.0.2                                ...           5 months ago
  • 0.0.1                                ...           5 months ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (13)
Dev Dependencies (6)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |