eris-db-promise
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -42,2 +42,118 @@ 'use strict' | ||
/** | ||
* Will wait for transaction will be validated | ||
* @param {Object} tx CallTx object | ||
* @param {Object} result broadcastTx result | ||
* @return {Promise} | ||
*/ | ||
_waitForConfirmation (tx, result) { | ||
return Promise.reject(new Error('Not implemented yet !')) | ||
return new Promise((resolve, reject) => { | ||
const checkBlock = (step = 0) => { | ||
setTimeout(() => { | ||
this._eris | ||
.blockchain | ||
.getLatestBlock() | ||
.then((block) => { | ||
if (block.data.txs && block.data.txs.length > 0) | ||
console.log(new Buffer(block.data.txs[0], 'hex').toString()) | ||
console.log('==========================') | ||
console.log(block) | ||
console.log('==========================') | ||
step++ | ||
if (step > 2) | ||
resolve(block) | ||
else | ||
checkBlock(step) | ||
}) | ||
.catch(reject) | ||
}, 500) | ||
} | ||
checkBlock() | ||
}) | ||
} | ||
/** | ||
* System will try to set all missing fields for given tx | ||
* - Use the other parameters to create a CallTx object | ||
* - Sign the transaction. | ||
* - Broadcast the transaction | ||
* - Wait until the transaction is fully processed | ||
* | ||
* @param {Object} tx | ||
* @param {String} privKey | ||
* @return {Promise} | ||
*/ | ||
sendTransaction (tx, privKey) { | ||
if (!tx || !util.isCallTx(tx)) | ||
return Promise.reject(new Error('Transaction is required parameter')) | ||
if (!privKey || !util.isPrivKey(privKey)) | ||
return Promise.reject(new Error('PrivKey is required parameter')) | ||
let chainId | ||
let account | ||
return this._eris | ||
.blockchain | ||
.getChainId() | ||
.then((currentChainId) => { | ||
chainId = currentChainId | ||
return this._eris | ||
.accounts | ||
.getAccount(tx.input.address) | ||
}) | ||
.then((loadedAccount) => { | ||
account = loadedAccount | ||
if (!tx.address) | ||
tx.address = '' | ||
if (!tx.input.sequence) | ||
tx.input.sequence = (account && account.sequence) ? account.sequence + 1 : 1 | ||
if (!tx.fee) | ||
tx.fee = 0 | ||
if (!tx.gas_limit) | ||
tx.gas_limit = 100000 | ||
if (!tx.input.amount) | ||
tx.input.amount = 10 | ||
// Sign transaction | ||
const txForSigning = { | ||
chain_id: chainId, | ||
tx: [ | ||
2, { | ||
address: tx.address, | ||
data: tx.data, | ||
fee: tx.fee, | ||
gas_limit: tx.gas_limit, | ||
input: { | ||
address: tx.input.address, | ||
amount: tx.input.amount, | ||
sequence: tx.input.sequence | ||
} | ||
} | ||
] | ||
} | ||
return this.sign(txForSigning, privKey) | ||
}) | ||
.then((signed) => { | ||
tx.input.signature = signed | ||
tx.input.pub_key = account.pub_key | ||
return this.broadcastTx(tx) | ||
}) | ||
.then((info) => { | ||
if (!info.tx_hash) | ||
return Promise.reject(new Error('No tx_hash received')) | ||
return this._waitForConfirmation(tx, info) | ||
}) | ||
} | ||
/** | ||
* Broadcast a given (signed) transaction to the node. | ||
@@ -44,0 +160,0 @@ * It will be added to the tx pool if there are no issues, and if it is |
@@ -47,7 +47,7 @@ 'use strict' | ||
} | ||
if (fee && _.isNumber(fee)) | ||
if (_.isNumber(fee)) | ||
params.fee = fee | ||
if (gasLimit && _.isNumber(gasLimit)) | ||
params.gasLimit = gasLimit | ||
if (_.isNumber(gasLimit)) | ||
params.gas_limit = gasLimit | ||
@@ -89,7 +89,7 @@ return this._eris.request | ||
} | ||
if (fee && _.isNumber(fee)) | ||
if (_.isNumber(fee)) | ||
params.fee = fee | ||
if (gasLimit && _.isNumber(gasLimit)) | ||
params.gasLimit = gasLimit | ||
if (_.isNumber(gasLimit)) | ||
params.gas_limit = gasLimit | ||
@@ -96,0 +96,0 @@ return this._eris.request |
@@ -15,2 +15,24 @@ 'use strict' | ||
/** | ||
* Check if given Object is CallTx | ||
* @see {@link https://monax.io/docs/documentation/db/latest/specifications/api/#calltx} | ||
* @param {Object} tx | ||
* @return {Boolean} | ||
*/ | ||
function isCallTx (tx) { | ||
if (!_.isObject(tx)) | ||
return false | ||
if (!isHex(tx.data)) | ||
return false | ||
if (!_.isObject(tx.input)) | ||
return false | ||
if (!isAddress(tx.input.address)) | ||
return false | ||
return true | ||
} | ||
/** | ||
* Check if given operation allowed for Filters in RPC | ||
@@ -81,2 +103,7 @@ * @param {String} operation | ||
/** | ||
* Check is given obj is CallTx | ||
* @type {isCallTx} | ||
*/ | ||
exports.isCallTx = isCallTx | ||
/** | ||
* Filter operations | ||
@@ -83,0 +110,0 @@ * @type {filterOperations} |
{ | ||
"name": "eris-db-promise", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -6,4 +6,2 @@ 'use strict' | ||
const crypto = require('tendermint-crypto') | ||
const config = require('../config') | ||
const _ = require('lodash') | ||
@@ -22,4 +20,2 @@ describe('Transactions :: ', () => { | ||
const wrongAccount = chance.string({ length: 40, 'pool': 'abcdef1234567890' }) | ||
it('reject without tx', () => { | ||
@@ -69,37 +65,37 @@ return global.erisdb | ||
let accounts | ||
let chainId | ||
let user | ||
// let accounts | ||
// let chainId | ||
// let user | ||
// | ||
// before(() => { | ||
// return global.erisdb | ||
// .unsafe | ||
// .genPrivAccount() | ||
// .then((newAcc) => { | ||
// expect(newAcc).to.be.an('object') | ||
// .and.to.contain.all.keys([ | ||
// 'address', 'pub_key', 'priv_key' | ||
// ]) | ||
// | ||
// user = newAcc | ||
// }) | ||
// }) | ||
before(() => { | ||
return global.erisdb | ||
.unsafe | ||
.genPrivAccount() | ||
.then((newAcc) => { | ||
expect(newAcc).to.be.an('object') | ||
.and.to.contain.all.keys([ | ||
'address', 'pub_key', 'priv_key' | ||
]) | ||
// before(() => { | ||
// return global.erisdb | ||
// .accounts | ||
// .getAccounts() | ||
// .then((list) => { | ||
// expect(list).to.be.an('array') | ||
// .and.to.have.length.above(1) | ||
// | ||
// accounts = list | ||
// }) | ||
// .then(() => global.erisdb.blockchain.getChainId()) | ||
// .then((chain) => { | ||
// expect(chain).to.be.a('string') | ||
// chainId = chain | ||
// }) | ||
// }) | ||
user = newAcc | ||
}) | ||
}) | ||
before(() => { | ||
return global.erisdb | ||
.accounts | ||
.getAccounts() | ||
.then((list) => { | ||
expect(list).to.be.an('array') | ||
.and.to.have.length.above(1) | ||
accounts = list | ||
}) | ||
.then(() => global.erisdb.blockchain.getChainId()) | ||
.then((chain) => { | ||
expect(chain).to.be.a('string') | ||
chainId = chain | ||
}) | ||
}) | ||
it('reject without transaction', () => { | ||
@@ -116,47 +112,2 @@ return global.erisdb | ||
xit('should broadcast tx', () => { | ||
const tx = { | ||
inputs: [ | ||
{ | ||
address: user.address, | ||
amount: 101, | ||
sequence: 1, | ||
pub_key: user.pub_key | ||
} | ||
], | ||
outputs: [ | ||
{ | ||
address: accounts[0].address, | ||
amount: 100 | ||
} | ||
] | ||
} | ||
const txForSign = { | ||
chain_id: chainId, | ||
tx: [ | ||
2, | ||
_.cloneDeep(tx) | ||
] | ||
} | ||
return global.erisdb | ||
.transactions | ||
.sign(txForSign, config.account.privKey) | ||
.then((signed) => { | ||
tx.inputs[0].signature = signed | ||
}) | ||
.then(() => { | ||
return global.erisdb | ||
.transactions | ||
.broadcastTx(tx) | ||
}) | ||
.then((data) => { | ||
expect(data).to.be.an('object') | ||
.and.to.contain.all.keys([ | ||
'tx_hash', | ||
'creates_contract', | ||
'contract_addr' | ||
]) | ||
}) | ||
}) | ||
}) | ||
@@ -163,0 +114,0 @@ |
@@ -156,3 +156,3 @@ 'use strict' | ||
xit('should make a transaction', () => { | ||
it('should make a transaction', () => { | ||
const contract = ` | ||
@@ -168,11 +168,16 @@ contract Sample { | ||
.unsafe | ||
.transactAndHold(config.account.privKey, compiled.bytecode, config.account.address) | ||
.transactAndHold(config.account.privKey, compiled.bytecode, '') | ||
.then((info) => { | ||
console.log('==========================') | ||
console.log(info) | ||
console.log('==========================') | ||
expect(info).to.be.an('object') | ||
.and.to.contain.all.keys([ | ||
'tx_hash', 'creates_contract', 'contract_addr' | ||
'call_data', 'tx_id', 'return', 'origin' | ||
]) | ||
expect(info.call_data).to.be.an('object') | ||
.and.to.contain.all.keys([ | ||
'caller', 'callee', 'data', 'value', 'gas' | ||
]) | ||
expect(info.call_data.data).to.be | ||
.eq(compiled.bytecode.toUpperCase()) | ||
}) | ||
@@ -179,0 +184,0 @@ }) |
3706097
38
107406