|
|
@ -1,5 +1,9 @@ |
|
|
|
import { PluginClient } from '@remixproject/plugin'; |
|
|
|
import { |
|
|
|
import { createClient } from '@remixproject/plugin-iframe'; |
|
|
|
PluginClient |
|
|
|
|
|
|
|
} from '@remixproject/plugin'; |
|
|
|
|
|
|
|
import { |
|
|
|
|
|
|
|
createClient |
|
|
|
|
|
|
|
} from '@remixproject/plugin-webview'; |
|
|
|
import WalletConnectProvider from "@walletconnect/web3-provider"; |
|
|
|
import WalletConnectProvider from "@walletconnect/web3-provider"; |
|
|
|
import Torus from "@toruslabs/torus-embed"; |
|
|
|
import Torus from "@toruslabs/torus-embed"; |
|
|
|
import Authereum from "authereum"; |
|
|
|
import Authereum from "authereum"; |
|
|
@ -43,13 +47,13 @@ export class RemixClient extends PluginClient { |
|
|
|
console.log("Opening a dialog", this.web3Modal); |
|
|
|
console.log("Opening a dialog", this.web3Modal); |
|
|
|
try { |
|
|
|
try { |
|
|
|
this.provider = await this.web3Modal.connect(); |
|
|
|
this.provider = await this.web3Modal.connect(); |
|
|
|
} catch(e) { |
|
|
|
} catch (e) { |
|
|
|
console.log("Could not get a wallet connection", e); |
|
|
|
console.log("Could not get a wallet connection", e); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
this.internalEvents.emit('accountsChanged', this.provider.accounts || []) |
|
|
|
this.internalEvents.emit('accountsChanged', this.provider.accounts || []) |
|
|
|
this.internalEvents.emit('chainChanged', this.provider.chainId) |
|
|
|
this.internalEvents.emit('chainChanged', await this.detectNetwork(this.provider.chainId)) |
|
|
|
|
|
|
|
|
|
|
|
// Subscribe to accounts change
|
|
|
|
// Subscribe to accounts change
|
|
|
|
this.provider.on("accountsChanged", (accounts) => { |
|
|
|
this.provider.on("accountsChanged", (accounts) => { |
|
|
@ -57,8 +61,8 @@ export class RemixClient extends PluginClient { |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// Subscribe to chainId change
|
|
|
|
// Subscribe to chainId change
|
|
|
|
this.provider.on("chainChanged", (chainId) => { |
|
|
|
this.provider.on("chainChanged", async (chainId) => { |
|
|
|
this.internalEvents.emit('chainChanged', chainId) |
|
|
|
this.internalEvents.emit('chainChanged', await this.detectNetwork(chainId)) |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// Subscribe to networkId change
|
|
|
|
// Subscribe to networkId change
|
|
|
@ -72,12 +76,49 @@ export class RemixClient extends PluginClient { |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
getProviderOptions () { |
|
|
|
async detectNetwork(id) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let networkName = null; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md
|
|
|
|
|
|
|
|
if (id === 1) networkName = "Main"; |
|
|
|
|
|
|
|
else if (id === 2) networkName = "Morden (deprecated)"; |
|
|
|
|
|
|
|
else if (id === 3) networkName = "Ropsten"; |
|
|
|
|
|
|
|
else if (id === 4) networkName = "Rinkeby"; |
|
|
|
|
|
|
|
else if (id === 5) networkName = "Goerli"; |
|
|
|
|
|
|
|
else if (id === 42) networkName = "Kovan"; |
|
|
|
|
|
|
|
else networkName = "Custom"; |
|
|
|
|
|
|
|
return networkName |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Disconnect wallet button pressed. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
async onDisconnect() { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Which providers have close method?
|
|
|
|
|
|
|
|
if (this.provider && this.provider.close) { |
|
|
|
|
|
|
|
await this.provider.close(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If the cached provider is not cleared,
|
|
|
|
|
|
|
|
// WalletConnect will default to the existing session
|
|
|
|
|
|
|
|
// and does not allow to re-scan the QR code with a new wallet.
|
|
|
|
|
|
|
|
// Depending on your use case you may want or want not his behavir.
|
|
|
|
|
|
|
|
await this.web3Modal.clearCachedProvider(); |
|
|
|
|
|
|
|
this.provider = null; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
this.internalEvents.emit('disconnect') |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getProviderOptions() { |
|
|
|
const providerOptions = { |
|
|
|
const providerOptions = { |
|
|
|
walletconnect: { |
|
|
|
walletconnect: { |
|
|
|
package: WalletConnectProvider, |
|
|
|
package: WalletConnectProvider, |
|
|
|
options: { |
|
|
|
options: { |
|
|
|
infuraId: '83d4d660ce3546299cbe048ed95b6fad' |
|
|
|
infuraId: '83d4d660ce3546299cbe048ed95b6fad', |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
}, |
|
|
|
torus: { |
|
|
|
torus: { |
|
|
@ -150,4 +191,4 @@ export class RemixClient extends PluginClient { |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |