Skip to content

Commit

Permalink
feat(ACI): Refactor ACI module (#505)
Browse files Browse the repository at this point in the history
* feat(Selector): If default account `address` not provided use the first

* feat(ACI): Refactor ACI. Split to separated files. Fix test.

* feat(ACI): Refactor building of contract instance method, add ability to init instance without client

* feat(ACI): Handle ACI without init function

* chore(ACI): Cleanup code

* feat(ACI): Refactor ACI module. Fix bug with empty init function. Automatically decide to use on-chai

* fix(ACI): Fix options composition when building function

* chore(docs): Fix ACI docs example
  • Loading branch information
nduchak authored Jun 22, 2019
1 parent 325cc90 commit fb7bc00
Show file tree
Hide file tree
Showing 17 changed files with 633 additions and 570 deletions.
5 changes: 3 additions & 2 deletions es/account/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@ const DEFAULT_NETWORK_ID = `ae_mainnet`
* @category async
* @rtype (tx: String) => tx: Promise[String], throws: Error
* @param {String} tx - Transaction to sign
* @param {Object} opt - Options
* @return {String} Signed transaction
*/
async function signTransaction (tx) {
async function signTransaction (tx, opt = {}) {
const networkId = this.getNetworkId()
const rlpBinaryTx = Crypto.decodeBase64Check(Crypto.assertedType(tx, 'tx'))
// Prepend `NETWORK_ID` to begin of data binary
const txWithNetworkId = Buffer.concat([Buffer.from(networkId), rlpBinaryTx])

const signatures = [await this.sign(txWithNetworkId)]
const signatures = [await this.sign(txWithNetworkId, opt)]
return buildTx({ encodedTx: rlpBinaryTx, signatures }, TX_TYPE.signed).tx
}

Expand Down
3 changes: 2 additions & 1 deletion es/account/selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async function address () {
* @param {String} address - Address of account to select
* @example selectAccount('ak_xxxxxxxx')
*/
async function selectAccount (address) {
function selectAccount (address) {
this.Selector.address = address
}

Expand All @@ -58,6 +58,7 @@ async function selectAccount (address) {
*/
const Selector = Account.compose({
async init ({ address }) {
if (!address) address = Object.keys(this.accounts)[0]
this.Selector.address = address
},
methods: { sign, address, selectAccount },
Expand Down
3 changes: 2 additions & 1 deletion es/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ async function signWith (address, data) {
return account.sign(data)
}

async function addAccount (account) {
async function addAccount (account, { select } = {}) {
const address = await account.address()
this.accounts[address] = account
if (select) this.selectAccount(address)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion es/ae/aepp.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

import Ae from './'
import Aens from './aens'
import { Contract } from './contract'
import Rpc from '../rpc/client'
import { Contract } from './contract'

/**
* Aepp Stamp
Expand Down
20 changes: 11 additions & 9 deletions es/ae/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import Ae from './'
import * as R from 'ramda'
import { isBase64 } from '../utils/crypto'
import ContractCompilerAPI from '../contract/compiler'
import ContractACI from '../contract/aci'
import ContractBase from '../contract'
import ContractACI from '../contract/aci'

/**
* Handle contract call error
Expand Down Expand Up @@ -108,23 +108,25 @@ async function contractDecodeData (source, fn, callValue, callResult, options) {
*/
async function contractCallStatic (source, address, name, args = [], { top, options = {} } = {}) {
const opt = R.merge(this.Ae.defaults, options)

// Prepare `call` transaction
const tx = await this.contractCallTx(R.merge(opt, {
callerId: await this.address(),
contractId: address,
callData: await this.contractEncodeCall(source, name, args)
}))
const callerId = await this.address()

// Get block hash by height
if (top && !isNaN(top)) {
top = (await this.getKeyBlock(top)).hash
}

// Prepare `call` transaction
const tx = await this.contractCallTx(R.merge(opt, {
callerId,
contractId: address,
callData: await this.contractEncodeCall(source, name, args),
nonce: top ? (await this.getAccount(callerId, { hash: top })).nonce + 1 : undefined
}))

// Dry-run
const [{ result: status, callObj, reason }] = (await this.txDryRun([tx], [{
amount: opt.amount,
pubKey: await this.address()
pubKey: callerId
}], top)).results

// check response
Expand Down
2 changes: 1 addition & 1 deletion es/ae/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import { BigNumber } from 'bignumber.js'
*/
async function send (tx, options) {
const opt = R.merge(this.Ae.defaults, options)
const signed = await this.signTransaction(tx)
const signed = await this.signTransaction(tx, opt)
return this.sendTransaction(signed, opt)
}

Expand Down
11 changes: 9 additions & 2 deletions es/ae/universal.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ import Ae from './'
import Account from '../account/memory'
import Chain from '../chain/node'
import Aens from './aens'
import Contract from './contract'
import Transaction from '../tx/tx'
import Oracle from './oracle'
import Selector from '../account/selector'
import Accounts from '../accounts'
import Contract from './contract'

/**
* Universal Stamp
Expand All @@ -41,7 +43,12 @@ import Oracle from './oracle'
* @param {Object} [options={}] - Initializer object
* @return {Object} Universal instance
*/
const Universal = Ae.compose(Account, Chain, Transaction, Aens, Contract, Oracle, {
export const Universal = Ae.compose(Account, Chain, Transaction, Aens, Contract, Oracle, {
init () {},
props: { process: {} }
})

export const UniversalWithAccounts = Ae.compose(Accounts, Chain, Transaction, Aens, Contract, Oracle, Selector, {
init () {},
props: { process: {} }
})
Expand Down
2 changes: 1 addition & 1 deletion es/ae/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
import Ae from './'
import Account from '../account'
import ContractBase from '../contract'
import Contract from './contract'
import Accounts from '../accounts'
import Chain from '../chain/node'
import Rpc from '../rpc/server'
import Selector from '../account/selector'
import * as R from 'ramda'
import Tx from '../tx/tx'
import Contract from './contract'

const contains = R.flip(R.contains)
const isTxMethod = contains(Tx.compose.deepConfiguration.Ae.methods)
Expand Down
Loading

0 comments on commit fb7bc00

Please sign in to comment.