Skip to content

Commit

Permalink
Merge pull request #5207 from swaponline/wcv2
Browse files Browse the repository at this point in the history
WalletConnectV2
  • Loading branch information
shendel authored Jul 19, 2023
2 parents 508163c + 7cf346b commit 64a16a1
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Node CI
name: Pullrequest build & test

on: [pull_request]

Expand Down
33 changes: 17 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,23 @@
"zip:chrome-extension-eth-testnet": "cd build-chrome-extension/ && zip -r zipped-chrome-extension-eth-testnet.zip ./* && cd - && echo 'Extension is zipped! (Ethereum / TESTNET)'",
"zip:chrome-extension-eth-mainnet": "cd build-chrome-extension/ && zip -r zipped-chrome-extension-eth.zip ./* && cd - && echo 'Extension is zipped! (Ethereum / MAINNET)'",
"build:pages": "npm run clean:pages && cross-env DEBUG=app:*,swap.core:* CONFIG=mainnet.pages.prod node ./src/front/bin/compile",
"build:testnet": "npm run clean:testnet && cross-env DEBUG=app:* CONFIG=testnet.prod node --max-old-space-size=2048 ./src/front/bin/compile",
"build:testnet-tests": "npm run clean:testnet && cross-env DEBUG=app:* IS_TEST=true CONFIG=testnet.prod node --max-old-space-size=2048 ./src/front/bin/compile",
"build:testnet-widget": "npm run clean:testnet-widget && cross-env BUILD_TYPE=testnet DEBUG=app:* CONFIG=testnet.widget.prod node --max-old-space-size=2048 ./src/front/bin/compile",
"build:mainnet": "npm run clean:mainnet && cross-env NODE_ENV=production DEBUG=app:* CONFIG=mainnet.prod node --max-old-space-size=2048 ./src/front/bin/compile",
"build:mainnet-widget": "npm run clean:mainnet-widget && cross-env NODE_ENV=production BUILD_TYPE=mainnet DEBUG=app:* CONFIG=mainnet.widget.prod node --max-old-space-size=2048 ./src/front/bin/compile",
"build:testnet-local": "npm run clean:testnet-local && cross-env DEBUG=app:* CONFIG=testnet-local.prod node --max-old-space-size=2048 ./src/front/bin/compile",
"build:mainnet-local": "npm run clean:mainnet-local && cross-env NODE_ENV=production DEBUG=app:* CONFIG=mainnet-local.prod node --max-old-space-size=2048 ./src/front/bin/compile",
"build:chrome-extension-eth-testnet": "npm run clean:chrome-extension && cross-env DEBUG=app:* CONFIG=chrome-extension-testnet.prod node --max-old-space-size=2048 ./src/front/bin/compile && npm run copy:chrome-extension-eth && npm run zip:chrome-extension-eth-testnet",
"build:chrome-extension-eth-mainnet": "npm run clean:chrome-extension && cross-env NODE_ENV=production DEBUG=app:* CONFIG=chrome-extension-mainnet.prod node --max-old-space-size=2048 ./src/front/bin/compile && npm run copy:chrome-extension-eth && npm run zip:chrome-extension-eth-mainnet",
"build:testnet": "npm run clean:testnet && cross-env DEBUG=app:* CONFIG=testnet.prod node --max-old-space-size=4096 ./src/front/bin/compile",
"build:testnet-tests": "npm run clean:testnet && cross-env DEBUG=app:* IS_TEST=true CONFIG=testnet.prod node --max-old-space-size=8192 ./src/front/bin/compile",
"build:testnet-widget": "npm run clean:testnet-widget && cross-env BUILD_TYPE=testnet DEBUG=app:* CONFIG=testnet.widget.prod node --max-old-space-size=4096 ./src/front/bin/compile",
"build:mainnet": "npm run clean:mainnet && cross-env NODE_ENV=production DEBUG=app:* CONFIG=mainnet.prod node --max-old-space-size=4096 ./src/front/bin/compile",
"build:mainnet-widget": "npm run clean:mainnet-widget && cross-env NODE_ENV=production BUILD_TYPE=mainnet DEBUG=app:* CONFIG=mainnet.widget.prod node --max-old-space-size=4096 ./src/front/bin/compile",
"build:testnet-local": "npm run clean:testnet-local && cross-env DEBUG=app:* CONFIG=testnet-local.prod node --max-old-space-size=4096 ./src/front/bin/compile",
"build:mainnet-local": "npm run clean:mainnet-local && cross-env NODE_ENV=production DEBUG=app:* CONFIG=mainnet-local.prod node --max-old-space-size=4096 ./src/front/bin/compile",
"build:chrome-extension-eth-testnet": "npm run clean:chrome-extension && cross-env DEBUG=app:* CONFIG=chrome-extension-testnet.prod node --max-old-space-size=4096 ./src/front/bin/compile && npm run copy:chrome-extension-eth && npm run zip:chrome-extension-eth-testnet",
"build:chrome-extension-eth-mainnet": "npm run clean:chrome-extension && cross-env NODE_ENV=production DEBUG=app:* CONFIG=chrome-extension-mainnet.prod node --max-old-space-size=4096 ./src/front/bin/compile && npm run copy:chrome-extension-eth && npm run zip:chrome-extension-eth-mainnet",
"messages:extract": "babel-node src/front/tools/run messages",
"release": "git commit -am v%npm_package_version% && git tag v%npm_package_version% && git push && git push --tags",
"bot": "babel-node --extensions .ts,.js --config-file=./babel.bot.config.js --max-old-space-size=2048 ./src/bot",
"bot:test": "cross-env TEST_STARTUP=true babel-node --extensions .ts,.js --config-file=./babel.bot.config.js --max-old-space-size=2048 ./src/bot",
"marketmaker": "cross-env USE_JSON_CONFIG=true NETWORK=mainnet babel-node --extensions .ts,.js --config-file=./babel.bot.config.js --max-old-space-size=2048 ./src/bot",
"marketmaker:testnet": "cross-env USE_JSON_CONFIG=true NETWORK=testnet babel-node --extensions .ts,.js --config-file=./babel.bot.config.js --max-old-space-size=2048 ./src/bot",
"marketmaker:test": "cross-env TEST_STARTUP=true USE_JSON_CONFIG=true NETWORK=testnet babel-node --extensions .ts,.js --config-file=./babel.bot.config.js --max-old-space-size=2048 ./src/bot",
"bot:debug": "cross-env DEBUG=swap.core* babel-node --extensions .ts,.js --config-file=./babel.bot.config.js --max-old-space-size=2048 ./src/bot"
"bot": "babel-node --extensions .ts,.js --config-file=./babel.bot.config.js --max-old-space-size=4096 ./src/bot",
"bot:test": "cross-env TEST_STARTUP=true babel-node --extensions .ts,.js --config-file=./babel.bot.config.js --max-old-space-size=4096 ./src/bot",
"marketmaker": "cross-env USE_JSON_CONFIG=true NETWORK=mainnet babel-node --extensions .ts,.js --config-file=./babel.bot.config.js --max-old-space-size=4096 ./src/bot",
"marketmaker:testnet": "cross-env USE_JSON_CONFIG=true NETWORK=testnet babel-node --extensions .ts,.js --config-file=./babel.bot.config.js --max-old-space-size=4096 ./src/bot",
"marketmaker:test": "cross-env TEST_STARTUP=true USE_JSON_CONFIG=true NETWORK=testnet babel-node --extensions .ts,.js --config-file=./babel.bot.config.js --max-old-space-size=4096 ./src/bot",
"bot:debug": "cross-env DEBUG=swap.core* babel-node --extensions .ts,.js --config-file=./babel.bot.config.js --max-old-space-size=4096 ./src/bot"
},
"devDependencies": {
"@babel/cli": "^7.12.10",
Expand Down Expand Up @@ -112,6 +112,8 @@
"@types/node": "^14.14.20",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"@web3-react/core": "^8.2.0",
"@web3-react/walletconnect-v2": "^8.3.7",
"@welldone-software/why-did-you-render": "^6.2.1",
"abortcontroller-polyfill": "^1.5.0",
"app-module-path": "^2.2.0",
Expand Down Expand Up @@ -212,7 +214,6 @@
"@walletconnect/socket-transport": "^1.3.3",
"@walletconnect/utils": "^1.3.3",
"@walletconnect/web3-provider": "^1.3.3",
"@web3-react/core": "^6.0.9",
"@web3-react/injected-connector": "6.0.7",
"@web3-react/walletconnect-connector": "6.2.8",
"asciichart": "^1.5.25",
Expand Down
45 changes: 24 additions & 21 deletions src/common/web3connect/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-nocheck
import { EventEmitter } from 'events'
import { ConnectorEvent } from '@web3-react/types'
import { BigNumber } from 'bignumber.js'
Expand Down Expand Up @@ -48,28 +49,29 @@ export default class Web3Connect extends EventEmitter {
const cachedProviderName = localStorage.getItem(`WEB3CONNECT:PROVIDER`)

if (cachedProviderName) {
const lsProvider = getProviderByName(this, cachedProviderName)

if (lsProvider) {
lsProvider.isConnected().then(async (isConnected) => {
if (isConnected) {
if (await lsProvider.Connect()) {
this._cachedProviderName = cachedProviderName
this._cachedProvider = lsProvider
this._setupEvents()
await this._cacheProviderData()
this._isConnected = true
this._inited = true
return
getProviderByName(this, cachedProviderName).then((lsProvider) => {

if (lsProvider) {
lsProvider.isConnected().then(async (isConnected) => {
if (isConnected) {
if (await lsProvider.Connect()) {
this._cachedProviderName = cachedProviderName
this._cachedProvider = lsProvider
this._setupEvents()
await this._cacheProviderData()
this._isConnected = true
this._inited = true
return
}
}
}
this.clearCache()
this._inited = true
})
} else {
this.clearCache()
this._inited = true
})
} else {
this.clearCache()
this._inited = true
}
}
})
} else {
this._inited = true
}
Expand Down Expand Up @@ -223,10 +225,11 @@ export default class Web3Connect extends EventEmitter {
this._walletLocked = false

if (SUPPORTED_PROVIDERS[provider]) {
const _connector = getProviderByName(this, provider, true)
const _connector = await getProviderByName(this, provider, true)

if (_connector) {
if (await _connector.Connect()) {
const connected = await _connector.Connect()
if (connected) {
localStorage.setItem(`WEB3CONNECT:PROVIDER`, provider)
this._cachedProviderName = provider
this._cachedProvider = _connector
Expand Down
103 changes: 103 additions & 0 deletions src/common/web3connect/providers/WalletConnectProviderV2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// @ts-nocheck
import { initializeConnector, Web3ReactHooks } from '@web3-react/core'
import { WalletConnect as WalletConnectV2 } from '@web3-react/walletconnect-v2'
import config from 'helpers/externalConfig'


const initConnector = (chainId) => {
const [walletConnectV2, hooks] = initializeConnector<WalletConnectV2>(
(actions) => {
return new WalletConnectV2({
actions,
options: {
projectId: config.api.WalletConnectProjectId,
chains: [chainId],
optionalChains: [],
showQrModal: true,
},
})
}
)
return [walletConnectV2, hooks]
}

export default class WalletConnectProviderV2 {
_web3Connect = null

private _hooks: Web3ReactHooks | WalletConnectV2
private _walletConnectV2: Web3ReactHooks | WalletConnectV2
private _options = {}
private _inited = false

constructor(web3Connect, options) {
this._web3Connect = web3Connect
this._options = options
const [walletConnectV2, hooks] = initConnector(this._options.chainId)
this._hooks = hooks
this._walletConnectV2 = walletConnectV2

// debug class
window.testWC = this
}

async initProvider() {
try {
await this._walletConnectV2.activate(this._options.chainId)
this._inited = true
} catch (err) {
console.log('>>> fail init - reset')
}
}

getAccount() {
if (this._walletConnectV2 && this._walletConnectV2.provider) {
return this._walletConnectV2.provider.accounts[0]
} else {
return `Not connected`
}
}
getChainId() {
if (this._walletConnectV2 && this._walletConnectV2.provider) {
return this._walletConnectV2.provider.chainId
} else {
return 0
}
}
getProvider() {
return this._walletConnectV2.provider
}

async isConnected() {
return (
this._walletConnectV2
&& this._walletConnectV2.provider
&& this._walletConnectV2.provider.connected
)
}

isLocked() {
return false
}

async Disconnect() {
if (this._walletConnectV2 && this._walletConnectV2.provider) {
await this._walletConnectV2.provider.disconnect()
}
}

on(event, callback) {}

async Connect() {
if (!this._inited) return false
try {
await this._walletConnectV2.activate(this._options.chainId)
const connection = await this.isConnected()
return !!connection
} catch (err) {
/* Cancel connect */
console.log('>>> WC - Fail connect')
console.error(err)
return false
}
}
}
54 changes: 31 additions & 23 deletions src/common/web3connect/providers/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import InjectedProvider from './InjectedProvider'
import WalletConnectProvider from './WalletConnectProvider'
import WalletConnectProviderV2 from './WalletConnectProviderV2'
import SUPPORTED_PROVIDERS from './supported'

export const isInjectedEnabled = () => {
Expand All @@ -9,30 +10,37 @@ export const isInjectedEnabled = () => {
const _cachedProviders = {}

const getProviderByName = (web3connect, providerName, newInstance = false) => {
if (!_cachedProviders[providerName] || newInstance) {
switch (providerName) {
case SUPPORTED_PROVIDERS.INJECTED:
_cachedProviders[providerName] = new InjectedProvider(web3connect, {
supportedChainIds: [
web3connect._web3ChainId,
],
})
return _cachedProviders[providerName]
case SUPPORTED_PROVIDERS.WALLETCONNECT:
_cachedProviders[providerName] = new WalletConnectProvider(web3connect, {
rpc: web3connect._web3RPC,
chainId: Number(web3connect._web3ChainId),
bridge: `https://bridge.walletconnect.org`,
qrcode: true,
pollingInterval: 12000,
})
return _cachedProviders[providerName]
default:
console.error('web3connect - not supported provider', providerName)
return new Promise(async (resolve) => {
if (!_cachedProviders[providerName] || newInstance) {
switch (providerName) {
case SUPPORTED_PROVIDERS.INJECTED:
_cachedProviders[providerName] = new InjectedProvider(web3connect, {
supportedChainIds: [
web3connect._web3ChainId,
],
})
resolve(_cachedProviders[providerName])
break
case SUPPORTED_PROVIDERS.WALLETCONNECT:
_cachedProviders[providerName] = new WalletConnectProviderV2(web3connect, {
rpc: web3connect._web3RPC,
chainId: Number(web3connect._web3ChainId),
bridge: `https://bridge.walletconnect.org`,
qrcode: true,
pollingInterval: 12000,
})
await _cachedProviders[providerName].initProvider()
resolve(_cachedProviders[providerName])
break
default:
console.error('web3connect - not supported provider', providerName)
resolve(false)
break
}
} else {
resolve(_cachedProviders[providerName])
}
} else {
return _cachedProviders[providerName]
}
})
}

export default getProviderByName
3 changes: 2 additions & 1 deletion src/front/config/mainnet/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ export default {
txinfo: 'https://txinfo.swaponline.site',
noxon: 'https://noxon.wpmix.net',
phiscan: 'https://phiscan.com/api',
fkwscan: 'https://explorer.fokawa.com/api'
fkwscan: 'https://explorer.fokawa.com/api',
WalletConnectProjectId: 'a23677c4af3139b4eccb52981f76ad94',
}
1 change: 1 addition & 0 deletions src/front/config/testnet/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ export default {
multisig: 'https://multisig.swaponline.site',
txinfo: 'https://txinfo.swaponline.site',
noxon: 'https://noxon.wpmix.net',
WalletConnectProjectId: 'a23677c4af3139b4eccb52981f76ad94',
}
7 changes: 7 additions & 0 deletions src/front/shared/helpers/externalConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ const externalConfig = () => {
},
}

// WalletConnect custom ProjectID
if (window
&& window.SO_WalletConnectProjectId
) {
config.api.WalletConnectProjectId = window.SO_WalletConnectProjectId
}

if (window
&& window.SO_AllowMultiTab
) {
Expand Down

0 comments on commit 64a16a1

Please sign in to comment.