Created
November 14, 2018 17:40
-
-
Save Ankarrr/3b52432486d6d24b4998d1788b21ad0f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // Workable on Mainnet: | |
| // https://kyber.network/swap/eth_knc | |
| // https://dapp.originprotocol.com | |
| var Web3 = require('web3'); | |
| const ProviderEngine = require('web3-provider-engine') | |
| const RpcSubprovider = require('web3-provider-engine/subproviders/rpc.js') | |
| const Web3Subprovider = require("./web3_subprovider.js"); | |
| const DappSubprovider = require("./dapp_subprovider_new.js"); | |
| const SubscriptionSubprovider = require('web3-provider-engine/subproviders/subscriptions') | |
| const FixtureSubprovider = require('web3-provider-engine/subproviders/fixture.js') | |
| // const VmSubprovider = require('web3-provider-engine/subproviders/vm.js') | |
| // solving postMessage funciton | |
| function awaitPostMessage() { | |
| let isReactNativePostMessageReady = !!window.originalPostMessage; | |
| const queue = []; | |
| let currentPostMessageFn = function store(message) { | |
| if (queue.length > 100) queue.shift(); | |
| queue.push(message); | |
| }; | |
| if (!isReactNativePostMessageReady) { | |
| // const originalPostMessage = window.postMessage; | |
| Object.defineProperty(window, 'postMessage', { | |
| configurable: true, | |
| enumerable: true, | |
| get() { | |
| return currentPostMessageFn; | |
| }, | |
| set(fn) { | |
| currentPostMessageFn = fn; | |
| isReactNativePostMessageReady = true; | |
| setTimeout(sendQueue, 0); | |
| }, | |
| }); | |
| } | |
| function sendQueue() { | |
| while (queue.length > 0) window.postMessage(queue.shift()); | |
| } | |
| } | |
| awaitPostMessage(); | |
| function logger(msg) { | |
| window.postMessage(JSON.stringify({ action: 'log', msg })); | |
| } | |
| var engine = new ProviderEngine(); | |
| var web3 = new Web3(engine); | |
| // engine.addProvider(new FixtureSubprovider({ | |
| // connected: true, | |
| // // net_listening: true, | |
| // // eth_hashrate: '0x00', | |
| // // eth_mining: false, | |
| // // eth_syncing: true, | |
| // })) | |
| engine.addProvider(new DappSubprovider()); | |
| // filters | |
| engine.addProvider(new SubscriptionSubprovider()) | |
| // data source | |
| engine.addProvider(new RpcSubprovider({ | |
| //rpcUrl: 'http://127.0.0.1:8545', | |
| // rpcUrl: 'https://ropsten.infura.io/v3/056c00b3a8d846369185946435ca1ea3', | |
| rpcUrl: 'https://mainnet.infura.io/v3/056c00b3a8d846369185946435ca1ea3', | |
| })) | |
| // web3, 這可能有問題 | |
| // engine.addProvider( | |
| // new Web3Subprovider( | |
| // new Web3.providers.WebsocketProvider("wss://mainnet.infura.io/ws") | |
| // ) | |
| // ); | |
| // This is needed to work on https://dapp.originprotocol.com | |
| engine.connected = true; | |
| engine.isConnected = () => { | |
| return true; | |
| } | |
| // start polling for blocks | |
| engine.start(); | |
| //web3.currentProvider.isMetaMask = true; | |
| // web3.version = { | |
| // getNetwork: (...args) => { | |
| // return web3.eth.net.getId(...args); | |
| // } | |
| // } | |
| web3.version = { | |
| api: "1.0.0-beta.34", | |
| network: "1", | |
| getNetwork: (...args) => { | |
| return web3.eth.net.getId(...args); | |
| }, | |
| } | |
| // 這樣用會壞掉 | |
| // engine.eth.coinbase = "0x685ce4CbDd5c19b64CA008cB85b83947e5318EFA"; | |
| // web3.currentProvider = engine; | |
| // web3.isConnected = function(){ return web3.currentProvider.isConnected() }; | |
| web3.eth.coinbase = "0x685ce4CbDd5c19b64CA008cB85b83947e5318EFA"; // 不設的話是 undefined | |
| // 在這邊設不能用 | |
| web3.givenProvider = engine; // 不設的話是 null | |
| // engine.net.isListening = true; | |
| // --- Static data --- | |
| // var data = { | |
| // version: web3.version, | |
| // coinbase: web3.eth.coinbase, | |
| // defaultAccount: web3.eth.defaultAccount, | |
| // accounts: web3.eth.accounts, | |
| // currentProvider: web3.currentProvider, | |
| // givenProvider: web3.givenProvider, | |
| // } | |
| // window.postMessage(JSON.stringify({ action: 'log', msg: web3.version })); | |
| // window.postMessage(JSON.stringify({ action: 'log', msg: web3.eth.coinbase })); | |
| // window.postMessage(JSON.stringify({ action: 'log', msg: web3.givenProvider.toString() })); | |
| // window.postMessage(JSON.stringify({ action: 'log', msg: web3.currentProvider.toString() })); | |
| // This work | |
| window.web3 = web3; | |
| // Make sure this line is showed, and then web3 is loaded successfully | |
| window.postMessage(JSON.stringify({ action: 'log', msg: 'load web3 done.' })); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment