Skip to content

Commit

Permalink
Merge pull request #35 from Aniket-Engg/userdefinedtype
Browse files Browse the repository at this point in the history
Userdefinedtype tested
  • Loading branch information
Aniket-Engg authored Jun 1, 2019
2 parents 287abce + a233478 commit 66133e9
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 13 deletions.
1 change: 0 additions & 1 deletion lib/solReleases.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ module.exports.getCompilerVersion = async (parsedData, map) => {
if(pragmaVersion.indexOf('<') > -1 || pragmaVersion.indexOf('>') > -1)
throw new Error('Add Specific Compiler Version Pragma !!!');
pragmaVersion = pragmaVersion.replace('^', '');

if(Object.keys(map.compilers).indexOf(pragmaVersion) > -1){
compiler = map.compilers[pragmaVersion];
return compiler;
Expand Down
18 changes: 9 additions & 9 deletions lib/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,20 @@ module.exports.verify = async (data, cli = false) => {
const ast = parser.parse(contractSource);
const nodes = ast.children;
const compiler = await solReleases.getCompilerVersion(nodes, map);
const availableContracts = nodes.filter(e => (e.type == 'ContractDefinition' && e.kind == 'contract'));

const noOfContracts = nodes.filter(e => (e.type == 'ContractDefinition' && e.kind == 'contract')).length;
if( noOfContracts == 1){
name = nodes[1].name;
parsedData = nodes[1];
}else if(noOfContracts > 1){
if( availableContracts.length == 1){
name = availableContracts[0].name;
parsedData = availableContracts[0];
}else if(availableContracts.length > 1){
if(contractName){
name = contractName;
parsedData = nodes.filter(e => (e.type == 'ContractDefinition' && e.kind == 'contract' && e.name == contractName))[0]; // eslint-disable-line max-len
parsedData = availableContracts.filter(e => (e.name == contractName))[0];
}else
throw new Error('More Than One Contracts in File!!! Please Provide --contractName Option');
}

const constructorNode = parsedData.subNodes.filter(obj => (obj.type == 'FunctionDefinition' && obj.isConstructor == true)); // eslint-disable-line max-len

if(constructorNode.length > 0 && constructorNode[0].parameters.parameters.length > 0){
const cParamsArray = constructorNode[0].parameters.parameters;
if(cvalues){
Expand All @@ -57,8 +56,9 @@ module.exports.verify = async (data, cli = false) => {
const cparams = [];
for (const param of cParamsArray){
if(param.typeName.type == 'UserDefinedTypeName')
cparams.push(param.typeName.type.resolvedType.name);
cparams.push(param.typeName.name);
cparams.push(param.typeName.resolvedType.name);
else
cparams.push(param.typeName.name);
}
abiEncodedParams = web3.eth.abi.encodeParameters(cparams, cvalues);
abiEncodedParams = abiEncodedParams.slice(2, abiEncodedParams.length);
Expand Down
5 changes: 5 additions & 0 deletions test/contracts/Itest.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pragma solidity ^0.5.1;

interface Itest {
function set(uint) external;
}
13 changes: 13 additions & 0 deletions test/contracts/Sample2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pragma solidity >=0.4.0 <0.7.0;

contract Sample2 {
uint public n;

function set(uint _n) public returns (uint) {
n = _n;
}

function get() public view returns (uint) {
return n;
}
}
12 changes: 12 additions & 0 deletions test/contracts/SampleWithUserDefinedType.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pragma solidity ^0.5.1;

import "./Itest.sol";

contract SampleWithUserDefinedType {

uint age;
constructor (Itest it, uint _age) public{
Itest iaddr = it;
age = _age;
}
}
14 changes: 14 additions & 0 deletions test/contracts/SampleWithUserDefinedType_merged.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pragma solidity ^0.5.1;

interface Itest {
function set(uint) external;
}

contract SampleWithUserDefinedType {

uint age;
constructor (Itest it, uint _age) public{
Itest iaddr = it;
age = _age;
}
}
26 changes: 26 additions & 0 deletions test/sol-verifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,5 +302,31 @@ describe('sol-verifier', () => {
const response = await Verifier.verifyContract(sampleData);
response.status.should.equal('1');
});

it('Deploys & verifies contract with node_modules file import successfully', async () => {
contractName = 'SampleWithUserDefinedType';
network = 'rinkeby';
const constructParams = [];
constructParams.push('0x0000000000000000000000000000000000000000');
constructParams.push('12345');
const pathToDeploy = __dirname + '/contracts/'+ 'SampleWithUserDefinedType_merged' +'.sol';
const pathToVerify = __dirname + '/contracts/'+ contractName +'.sol';
const pragma = await getPragma(pathToVerify);
const contractSource = await processFile(pathToVerify, true);
const parsedData = parser.parse(pragma + '\n\n' + contractSource).children;
const compiler = await solReleases.getCompilerVersion(parsedData, mockMap);
contractAddress = await deployContract(contractName, network, compiler, pathToDeploy, constructParams);
await sleep(40000); // To make sure that contractCode is stored
sampleData = {
key : process.env.KEY,
path : pathToVerify,
contractAddress: contractAddress,
network : network,
cvalues : constructParams,
contractName : contractName,
};
const response = await Verifier.verifyContract(sampleData);
response.status.should.equal('1');
});
});
});
28 changes: 28 additions & 0 deletions test/solReleases.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ require('chai').should();

describe('sol-Releases', () => {

describe('Verifying compiler versions for listed compiler version', () => {
const contractName = 'Sample';
const path = __dirname + '/contracts/'+ contractName +'.sol';
it(' trying to get compiler version for Sample.sol (should pass)', async () => {

const contractSource = fs.readFileSync(path, 'UTF-8');
const parsedData = parser.parse(contractSource).children;
const compiler = await solReleases.getCompilerVersion(parsedData, mockMap);
compiler.should.equal('v0.5.1+commit.c8a2cb62');
});
});

describe('Verifying compiler versions for upgraded compiler version', () => {
const contractName = 'sampleWithUpdatedPragma';
const path = __dirname + '/contracts/'+ contractName +'.sol';
Expand Down Expand Up @@ -49,4 +61,20 @@ describe('sol-Releases', () => {
}
});
});

describe('Verifying compiler versions of contract with non-required format of pragma', () => {
const contractName = 'Sample2';
const path = __dirname + '/contracts/'+ contractName +'.sol';


it(' trying to get compiler version for Sample2.sol (should fail)', async () => {
const contractSource = fs.readFileSync(path, 'UTF-8');
const parsedData = parser.parse(contractSource).children;
try {
await solReleases.getCompilerVersion(parsedData, mockMap);
} catch(err){
err.message.should.equal('Add Specific Compiler Version Pragma !!!');
}
});
});
});
5 changes: 2 additions & 3 deletions utils/hasFallback.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict';

module.exports = async (contract) => {
module.exports = (contract) => {
for (const node of contract.subNodes) {
if (node.type == 'FunctionDefinition' && node.name === '' && node.stateMutability === 'payable') {
if (node.type == 'FunctionDefinition' && node.name === '' && node.stateMutability === 'payable')
return true;
}
}
return false;
};

0 comments on commit 66133e9

Please sign in to comment.