Skip to content
This repository has been archived by the owner on Apr 18, 2021. It is now read-only.

Commit

Permalink
feat: add receipt.logs parser
Browse files Browse the repository at this point in the history
  • Loading branch information
junbeomlee committed Jan 23, 2019
1 parent bf34413 commit e0e6822
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/vvisp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"chalk": "^2.4.1",
"commander": "^2.17.1",
"dotenv": "^5.0.1",
"ethereum-event-logs": "^1.3.0",
"ethereumjs-tx": "^1.3.7",
"ethereumjs-wallet": "^0.6.2",
"find-up": "^3.0.0",
Expand Down
43 changes: 38 additions & 5 deletions packages/vvisp/scripts/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const stringArgv = require('string-argv');
const { parseLog } = require('ethereum-event-logs');

if (!String.prototype.format) {
String.prototype.format = function() {
Expand Down Expand Up @@ -33,7 +34,7 @@ const defaultStateFile = 'state.vvisp.json';
*/

module.exports = async function(scriptPath, options) {
let apis = extractContractsApi(scriptPath);
let apis = setApi(scriptPath);
if (fs.existsSync(defaultStateFile)) {
apis = setApiAddress(apis, defaultStateFile);
} else {
Expand Down Expand Up @@ -234,9 +235,30 @@ async function call(args, apis) {
});

try {
const receiptFilter = [
'transactionHash',
'transactionIndex',
'blockNumber',
'from',
'to',
'gasUsed',
'logs',
'name',
'args'
];
const contract = new apis[contractName](apis[contractName]['address']);
const receipt = await contract.methods[methodName].apply(undefined, params);
console.log(receipt);
const events = parseLog(receipt.logs, apis[contractName]['abi']);
const logs = [];
for (const key in events) {
logs.push({
transactionHash: events[key].transactionHash,
name: events[key].name,
args: events[key].args
});
}
receipt.logs = logs;
console.log(JSON.stringify(receipt, receiptFilter, 2));
} catch (e) {
console.error(e);
}
Expand Down Expand Up @@ -283,18 +305,29 @@ function getApiInfo(apis) {

/**
*
* Get the script api of a smart contract from contractApis
* Get the script api and abi of a smart contract from contractApis
* @param {string} scriptPath is a path to contractApi which is generated
* from vvisp abi-to-script command
* @return {object} object has an api of smart contracts
*/
function extractContractsApi(scriptPath) {
function setApi(scriptPath) {
const defaultScriptPath = process.cwd() + '/contractApis';
if (scriptPath === undefined || scriptPath === '') {
scriptPath = defaultScriptPath;
}

return require(path.join(scriptPath, 'back') + '/index.js');
// set script api
const apis = require(path.join(scriptPath, 'back') + '/index.js');

// set abi
for (const key of Object.keys(apis)) {
apis[key]['abi'] = require(path.join(scriptPath, 'back', 'abi') +
'/' +
key +
'.json');
}

return apis;
}

/**
Expand Down
18 changes: 13 additions & 5 deletions packages/vvisp/test/scripts/console.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const chai = require('chai');
const path = require('path');
const stdMocks = require('std-mocks');
const bddStdin = require('bdd-stdin');
const deployContract = require('../../scripts/deploy-contract');

chai.use(require('chai-as-promised')).should();

Expand Down Expand Up @@ -34,8 +35,8 @@ describe('# console script test', async function() {
});
});

describe('extractContractsApi', function() {
const extractContractsApi = consoleTest.__get__('extractContractsApi');
describe('setApi', function() {
const setApi = consoleTest.__get__('setApi');

before('create apis', function() {
const testScriptPath = path.join(
Expand All @@ -44,7 +45,7 @@ describe('# console script test', async function() {
'dummy',
'testContractApis'
);
this.apis = extractContractsApi(testScriptPath);
this.apis = setApi(testScriptPath);
});

it('should have contract functions', function() {
Expand All @@ -65,6 +66,13 @@ describe('# console script test', async function() {
it('should have right number of contracts', function() {
Object.keys(this.apis).length.should.be.equal(1);
});

it('should have abi', function() {
for (const key of Object.keys(this.apis)) {
this.apis[key]['abi'].should.not.be.undefined;
this.apis[key]['abi'].length.should.be.equal(11);
}
});
});

describe('setApiAddress', function() {
Expand Down Expand Up @@ -257,7 +265,7 @@ describe('# console script test', async function() {
});

describe('show', function() {
const extractContractsApi = consoleTest.__get__('extractContractsApi');
const setApi = consoleTest.__get__('setApi');
const show = consoleTest.__get__('show');

before('create apis', function() {
Expand All @@ -267,7 +275,7 @@ describe('# console script test', async function() {
'dummy',
'testContractApis'
);
this.apis = extractContractsApi(testScriptPath);
this.apis = setApi(testScriptPath);
});

it('should print api method and args', async function() {
Expand Down

0 comments on commit e0e6822

Please sign in to comment.