Skip to content

Commit c1bed28

Browse files
committed
code and test importing the http contract
1 parent 9bf06ae commit c1bed28

File tree

7 files changed

+77
-17
lines changed

7 files changed

+77
-17
lines changed

lib/core/file.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,11 @@ class File {
2020
callback = isHttpContract;
2121
isHttpContract = false;
2222
}
23-
if (self.parsedImports) {
24-
// We already parsed this file
25-
return callback();
26-
}
27-
self.parsedImports = true;
2823
if (self.filename.indexOf('.sol') < 0) {
2924
// Only supported in Solidity
30-
return callback();
25+
return callback(null, content);
3126
}
32-
const regex = /import "([a-zA-Z0-9_\-.\\\/:]+)";/g;
27+
const regex = /import ["|']([a-zA-Z0-9_\-.\\\/:]+)";/g;
3328
let matches;
3429
const filesToDownload = [];
3530
const pathWithoutFile = path.dirname(self.path);
@@ -40,6 +35,9 @@ class File {
4035
url: `${pathWithoutFile}/${matches[1]}`
4136
};
4237
if (httpFileObj) {
38+
// Replace http import by filePath import in content
39+
content = content.replace(matches[1], httpFileObj.filePath);
40+
4341
fileObj.fileRelativePath = httpFileObj.filePath;
4442
fileObj.url = httpFileObj.url;
4543
} else if (!isHttpContract) {
@@ -49,11 +47,18 @@ class File {
4947
filesToDownload.push(fileObj);
5048
}
5149

50+
if (self.downloadedImports) {
51+
// We already parsed this file
52+
return callback(null, content);
53+
}
54+
self.downloadedImports = true;
5255
async.each(filesToDownload, ((fileObj, eachCb) => {
5356
self.downloadFile(fileObj.fileRelativePath, fileObj.url, (_content) => {
5457
eachCb();
5558
});
56-
}), callback);
59+
}), (err) => {
60+
callback(err, content);
61+
});
5762
}
5863

5964
downloadFile (filename, url, callback) {
@@ -103,8 +108,8 @@ class File {
103108
content = fs.readFileSync(this.path).toString();
104109
} else if (this.type === File.types.custom) {
105110
return this.resolver((theContent) => {
106-
this.parseFileForImport(content, () => {
107-
callback(theContent);
111+
this.parseFileForImport(theContent, (err, newContent) => {
112+
callback(newContent);
108113
});
109114
});
110115
} else if (this.type === File.types.http) {
@@ -119,8 +124,8 @@ class File {
119124
} else {
120125
throw new Error("unknown file: " + this.filename);
121126
}
122-
return this.parseFileForImport(content, () => {
123-
callback(content);
127+
return this.parseFileForImport(content, (err, newContent) => {
128+
callback(newContent);
124129
});
125130
}
126131

lib/modules/solidity/solcP.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ let solc;
33
const fs = require('fs-extra');
44
const path = require('path');
55
const constants = require('../../constants');
6+
const Utils = require('../../utils/utils');
67

78
function findImports(filename) {
9+
if (filename.startsWith('http') || filename.startsWith('git')) {
10+
const fileObj = Utils.getExternalContractUrl(filename);
11+
filename = fileObj.filePath;
12+
}
813
if (fs.existsSync(filename)) {
914
return {contents: fs.readFileSync(filename).toString()};
1015
}

test/contracts/contract_with_http_import.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
pragma solidity ^0.4.7;
2+
import "https://github.com/embark-framework/embark/blob/develop/test_apps/contracts_app/contracts/contract_args.sol";
23
contract SimpleStorage {
34
uint public storedData;
4-
import "https://github.com/embark-framework/embark/blob/develop/test_apps/contracts_app/contracts/contract_args.sol";
55

66
function SimpleStorage(uint initialValue) {
77
storedData = initialValue;

test/contracts/contract_with_import.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
pragma solidity ^0.4.7;
2+
import "./ownable.sol";
23
contract SimpleStorage {
34
uint public storedData;
4-
import "./ownable.sol";
55

66
function SimpleStorage(uint initialValue) {
77
storedData = initialValue;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
pragma solidity ^0.4.17;
2+
3+
import "https://github.com/embark-framework/embark/blob/develop/test_apps/contracts_app/contracts/ownable.sol";
4+
5+
6+
contract SimpleStorageWithHttpImport is Ownable {
7+
uint public storedData;
8+
9+
function() public payable { }
10+
11+
function SimpleStorageWithHttpImport(uint initialValue) public {
12+
storedData = initialValue;
13+
}
14+
15+
function set(uint x) public {
16+
storedData = x;
17+
for(uint i = 0; i < 1000; i++) {
18+
storedData += i;
19+
}
20+
}
21+
22+
function set2(uint x, uint unusedGiveWarning) public onlyOwner {
23+
storedData = x;
24+
}
25+
26+
function get() public view returns (uint retVal) {
27+
return storedData;
28+
}
29+
30+
function getS() public pure returns (string d) {
31+
return "hello";
32+
}
33+
34+
}

test_apps/test_app/config/contracts.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@
7474
},
7575
"Identity": {
7676
"file": "https://github.com/status-im/contracts/blob/master/contracts/identity/Identity.sol"
77+
},
78+
"SimpleStorageWithHttpImport": {
79+
"fromIndex": 0,
80+
"args": [
81+
100
82+
]
7783
}
7884
},
7985
"afterDeploy": [

test_apps/test_app/test/http_contract_test.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ const fs = require('fs-extra');
33
const assert = require('assert');
44

55
describe('http contracts', () => {
6-
const contractPath = '.embark/contracts/status-im/contracts/master/contracts/identity/Identity.sol';
7-
const contractImportPath = '.embark/contracts/status-im/contracts/master/contracts/identity/ERC725.sol';
86

97
it('should have downloaded the file in .embark/contracts', (done) => {
8+
const contractPath = '.embark/contracts/status-im/contracts/master/contracts/identity/Identity.sol';
109
fs.access(contractPath, (err) => {
1110
if (err) {
1211
assert.fail(contractPath + ' was not downloaded');
@@ -16,9 +15,20 @@ describe('http contracts', () => {
1615
});
1716

1817
it('should have downloaded the file import file too', (done) => {
18+
const contractImportPath = '.embark/contracts/status-im/contracts/master/contracts/identity/ERC725.sol';
1919
fs.access(contractImportPath, (err) => {
2020
if (err) {
21-
assert.fail(contractPath + ' was not downloaded');
21+
assert.fail(contractImportPath + ' was not downloaded');
22+
}
23+
done();
24+
});
25+
});
26+
27+
it('should have downloaded the http import in SimpleStorageWithHttpImport', (done) => {
28+
const contractImportPath = '.embark/contracts/embark-framework/embark/develop/test_apps/contracts_app/contracts/ownable.sol';
29+
fs.access(contractImportPath, (err) => {
30+
if (err) {
31+
assert.fail(contractImportPath + ' was not downloaded');
2232
}
2333
done();
2434
});

0 commit comments

Comments
 (0)