Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(embarkjs/blockchain): remove dependency on web3instance.js
- Loading branch information
1 parent
fdd8ad5
commit bd9fc66
Showing
7 changed files
with
194 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const path = require('path'); | ||
|
||
module.exports = (embark) => { | ||
embark.events.on('runcode:ready', () => { | ||
embark.events.emit('runcode:register', '__web3Connector', require('./web3Connector'), false); | ||
}); | ||
|
||
let code = `\nconst __embarkWeb3 = global.__web3Connector || require('${path.join(__dirname, 'web3ConnectorBrowser.js').replace(/\\/g, '/')}').default;`; | ||
|
||
code += "\nEmbarkJS.Blockchain.registerProvider('web3', __embarkWeb3);"; | ||
|
||
// TODO when we refactor code generator, refactor this to actually do something like connect | ||
code += "\nEmbarkJS.Blockchain.setProvider('web3', {});"; | ||
|
||
embark.addCodeToEmbarkJS(code); | ||
|
||
code = "EmbarkJS.Blockchain.setProvider('web3', {});"; | ||
|
||
const shouldInit = (_config) => { | ||
return true; | ||
}; | ||
|
||
embark.addConsoleProviderInit('blockchain', code, shouldInit); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "web3connector", | ||
"version": "1.0.0", | ||
"description": "Web3.js Connector for EmbarkJS", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "embark/packages/Web3Connector" | ||
}, | ||
"keywords": [ | ||
"embark", | ||
"web3", | ||
"node", | ||
"ethereum", | ||
"smart-contract" | ||
], | ||
"dependencies": { | ||
"web3": "1.0.0-beta.37" | ||
}, | ||
"author": "Jonathan Rainville", | ||
"license": "ISC" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
const Web3 = require('web3'); | ||
|
||
const web3Connector = {}; | ||
// const _web3 = require('./web3_instance').default; | ||
// global.wtf = _web3; | ||
|
||
web3Connector.init = function(_config) { | ||
// Check if the global web3 object uses the old web3 (0.x) | ||
if (global.web3 && typeof global.web3.version !== 'string') { | ||
// If so, use a new instance using 1.0, but use its provider | ||
// _web3.setProvider(global.web3.currentProvider); | ||
this.web3 = new Web3(global.web3.currentProvider); | ||
} else { | ||
this.web3 = global.web3 || new Web3(); | ||
} | ||
global.web3 = this.web3; | ||
}; | ||
|
||
web3Connector.getInstance = function () { | ||
return this.web3; | ||
}; | ||
|
||
web3Connector.getAccounts = function () { | ||
return this.web3.eth.getAccounts(...arguments); | ||
}; | ||
|
||
web3Connector.getNewProvider = function (providerName, ...args) { | ||
return new Web3.providers[providerName](...args); | ||
}; | ||
|
||
web3Connector.setProvider = function (provider) { | ||
// _web3.setProvider(provider); | ||
return this.web3.setProvider(provider); | ||
}; | ||
|
||
web3Connector.getCurrentProvider = function () { | ||
return this.web3.currentProvider; | ||
}; | ||
|
||
web3Connector.getDefaultAccount = function () { | ||
return this.web3.eth.defaultAccount; | ||
}; | ||
|
||
web3Connector.setDefaultAccount = function (account) { | ||
this.web3.eth.defaultAccount = account; | ||
}; | ||
|
||
web3Connector.newContract = function (options) { | ||
return new this.web3.eth.Contract(options.abi, options.address); | ||
}; | ||
|
||
web3Connector.send = function () { | ||
return this.web3.eth.sendTransaction(...arguments); | ||
}; | ||
|
||
web3Connector.toWei = function () { | ||
return this.web3.toWei(...arguments); | ||
}; | ||
|
||
web3Connector.getNetworkId = function () { | ||
return this.web3.eth.net.getId(); | ||
}; | ||
|
||
module.exports = web3Connector; | ||
// if (typeof module !== 'undefined' && module.exports) { | ||
// module.exports = web3Connector; | ||
// return; | ||
// } else { | ||
// export default web3Connector; | ||
// } | ||
// exports.default = web3Connector; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
const Web3 = require('web3'); | ||
|
||
const web3Connector = {}; | ||
// const _web3 = require('./web3_instance').default; | ||
// global.wtf = _web3; | ||
|
||
web3Connector.init = function(_config) { | ||
// Check if the global web3 object uses the old web3 (0.x) | ||
if (global.web3 && typeof global.web3.version !== 'string') { | ||
// If so, use a new instance using 1.0, but use its provider | ||
// _web3.setProvider(global.web3.currentProvider); | ||
this.web3 = new Web3(global.web3.currentProvider); | ||
} else { | ||
this.web3 = global.web3 || new Web3(); | ||
} | ||
global.web3 = this.web3; | ||
}; | ||
|
||
web3Connector.getInstance = function () { | ||
return this.web3; | ||
}; | ||
|
||
web3Connector.getAccounts = function () { | ||
return this.web3.eth.getAccounts(...arguments); | ||
}; | ||
|
||
web3Connector.getNewProvider = function (providerName, ...args) { | ||
return new Web3.providers[providerName](...args); | ||
}; | ||
|
||
web3Connector.setProvider = function (provider) { | ||
// _web3.setProvider(provider); | ||
return this.web3.setProvider(provider); | ||
}; | ||
|
||
web3Connector.getCurrentProvider = function () { | ||
return this.web3.currentProvider; | ||
}; | ||
|
||
web3Connector.getDefaultAccount = function () { | ||
return this.web3.eth.defaultAccount; | ||
}; | ||
|
||
web3Connector.setDefaultAccount = function (account) { | ||
this.web3.eth.defaultAccount = account; | ||
}; | ||
|
||
web3Connector.newContract = function (options) { | ||
return new this.web3.eth.Contract(options.abi, options.address); | ||
}; | ||
|
||
web3Connector.send = function () { | ||
return this.web3.eth.sendTransaction(...arguments); | ||
}; | ||
|
||
web3Connector.toWei = function () { | ||
return this.web3.toWei(...arguments); | ||
}; | ||
|
||
web3Connector.getNetworkId = function () { | ||
return this.web3.eth.net.getId(); | ||
}; | ||
|
||
|
||
// if (typeof module !== 'undefined' && module.exports) { | ||
// module.exports = web3Connector; | ||
// return; | ||
// } else { | ||
// export default web3Connector; | ||
// } | ||
export default web3Connector; |