Skip to content

Commit

Permalink
Feature/add helpers (#265)
Browse files Browse the repository at this point in the history
* Add getAccountNonce function
Make TX Builder u`unpackTx` function also return txObject(transaction properties)

* fix nonce calculation

* Fix getAccountNonce

* debugg test

* debug test

* debug test

* Revert changes

* skip channel tests

* enable channel test

* Change default fee byte size

* revert error in tx builder

* do not throw error in `unpackTx` when deserialization not supported
  • Loading branch information
nduchak authored Feb 27, 2019
1 parent ea2aa9b commit ae360b2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
10 changes: 7 additions & 3 deletions es/tx/builder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,14 +256,16 @@ export function unpackRawTx (binary, schema) {
* @return {Object} { tx, rlpEncoded, binary } Object with tx -> Base64Check transaction hash with 'tx_' prefix, rlp encoded transaction and binary transaction
*/
export function buildTx (params, type, { excludeKeys = [] } = {}) {
if (!TX_SERIALIZATION_SCHEMA[type]) throw new Error('Transaction not yet implemented.')
if (!TX_SERIALIZATION_SCHEMA[type]) {
throw new Error('Transaction serialization not implemented for ' + type)
}
const [schema, tag] = TX_SERIALIZATION_SCHEMA[type]
const binary = buildRawTx({ ...params, VSN, tag }, schema, { excludeKeys }).filter(e => e !== undefined)

const rlpEncoded = rlp.encode(binary)
const tx = encode(rlpEncoded, 'tx')

return { tx, rlpEncoded, binary }
return { tx, rlpEncoded, binary, txObject: unpackRawTx(binary, schema) }
}

/**
Expand All @@ -279,7 +281,9 @@ export function unpackTx (encodedTx, fromRlpBinary = false) {
const binary = rlp.decode(rlpEncoded)

const objId = readInt(binary[0])
if (!TX_DESERIALIZATION_SCHEMA[objId]) throw new Error('Transaction not yet implemented.')
if (!TX_DESERIALIZATION_SCHEMA[objId]) {
return { message: 'Transaction deserialization not implemented for tag ' + objId }
}
const [schema] = TX_DESERIALIZATION_SCHEMA[objId]

return { txType: OBJECT_ID_TX_TYPE[objId], tx: unpackRawTx(binary, schema), rlpEncoded, binary }
Expand Down
2 changes: 1 addition & 1 deletion es/tx/builder/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export const FIELD_TYPES = {
// FEE CALCULATION
export const BASE_GAS = 15000
export const GAS_PER_BYTE = 20
export const FEE_BYTE_SIZE = 1
export const FEE_BYTE_SIZE = 8
export const DEFAULT_FEE = 20000
export const KEY_BLOCK_INTERVAL = 3

Expand Down
16 changes: 14 additions & 2 deletions es/tx/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const Tx = stampit({
methods: [
'spendTx', 'namePreclaimTx', 'nameClaimTx', 'nameTransferTx',
'nameUpdateTx', 'nameRevokeTx', 'contractCreateTx', 'contractCallTx',
'oracleRegisterTx', 'oracleExtendTx', 'oraclePostQueryTx', 'oracleRespondTx'
'oracleRegisterTx', 'oracleExtendTx', 'oraclePostQueryTx', 'oracleRespondTx', 'getAccountNonce'
]
}
}
Expand All @@ -64,7 +64,8 @@ const Tx = stampit({
oracleRegisterTx: required,
oracleExtendTx: required,
oraclePostQueryTx: required,
oracleRespondTx: required
oracleRespondTx: required,
getAccountNonce: required
}
}))

Expand Down Expand Up @@ -200,4 +201,15 @@ const Tx = stampit({
* @return {String} `oracle_respond_tx` transaction
*/

/**
* Get Account Nonce
* @function getAccountNonce
* @instance
* @abstract
* @category async
* @rtype (address) => result: Number
* @param {String} address - Account public key
* @return {Number} Result
*/

export default Tx
18 changes: 7 additions & 11 deletions es/tx/tx.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,15 +271,10 @@ async function calculateTtl (ttl = 0, relative = true) {
* @param {number} nonce
* @return {number} Next Nonce
*/
async function calculateNonce (accountId, nonce) {
if (!nonce) {
try {
return +(await this.api.getAccountByPubkey(accountId)).nonce + 1
} catch (e) {
return 0
}
}
return nonce
async function getAccountNonce (accountId, nonce) {
if (nonce) return nonce
const { nonce: accountNonce } = await this.api.getAccountByPubkey(accountId).catch(() => ({ nonce: 0 }))
return accountNonce + 1
}

/**
Expand All @@ -290,7 +285,7 @@ async function calculateNonce (accountId, nonce) {
* @return {Object} { ttl, nonce, fee } Object with account nonce, absolute ttl and transaction fee
*/
async function prepareTxParams (txType, { senderId, nonce: n, ttl: t, fee: f, gas, absoluteTtl }) {
const nonce = await (calculateNonce.bind(this)(senderId, n))
const nonce = await this.getAccountNonce(senderId, n)
const ttl = await (calculateTtl.bind(this)(t, !absoluteTtl))
const fee = calculateFee(f, txType, { showWarning: this.showWarning, gas, params: R.merge(R.last(arguments), { nonce, ttl }) })
return { fee, ttl, nonce }
Expand Down Expand Up @@ -339,7 +334,8 @@ const Transaction = Node.compose(Tx, {
oracleRegisterTx,
oracleExtendTx,
oraclePostQueryTx,
oracleRespondTx
oracleRespondTx,
getAccountNonce
}
})

Expand Down

0 comments on commit ae360b2

Please sign in to comment.