Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xDai chain support by default #7492

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions app/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@
"buyWithWyreDescription": {
"message": "Wyre lets you use a credit card to deposit ETH right in to your MetaMask account."
},
"buyWithBridge": {
"message": "Buy xDAI with DAI"
},
"buyWithBridgeDescription": {
"message": "xDAI bridge lets you exchange your DAI to xDAI in your MetaMask account."
},
"buyCoinSwitch": {
"message": "Buy on CoinSwitch"
},
Expand Down Expand Up @@ -333,6 +339,9 @@
"connectingToGoerli": {
"message": "Connecting to Goerli Test Network"
},
"connectingToXDai": {
"message": "Connecting to xDAI chain"
},
"contacts": {
"message": "Contacts"
},
Expand All @@ -342,6 +351,9 @@
"continueToWyre": {
"message": "Continue to Wyre"
},
"continueToBridge": {
"message": "Continue to Bridge"
},
"continueToCoinSwitch": {
"message": "Continue to CoinSwitch"
},
Expand Down Expand Up @@ -1101,6 +1113,9 @@
"goerli": {
"message": "Goerli Test Network"
},
"xdai": {
"message": "xDAI chain"
},
"save": {
"message": "Save"
},
Expand Down
29 changes: 21 additions & 8 deletions app/scripts/controllers/incoming-transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@ const {
RINKEBY_CODE,
KOVAN_CODE,
GOERLI_CODE,
XDAI_CODE,
ROPSTEN,
RINKEBY,
KOVAN,
GOERLI,
MAINNET,
XDAI,
} = require('./network/enums')
const networkTypeToIdMap = {
[ROPSTEN]: String(ROPSTEN_CODE),
[RINKEBY]: String(RINKEBY_CODE),
[KOVAN]: String(KOVAN_CODE),
[GOERLI]: String(GOERLI_CODE),
[MAINNET]: String(MAINNET_CODE),
[XDAI]: String(XDAI_CODE),
}
const fetch = fetchWithTimeout({
timeout: 30000,
Expand Down Expand Up @@ -57,6 +60,7 @@ class IncomingTransactionsController {
[KOVAN]: null,
[GOERLI]: null,
[MAINNET]: null,
[XDAI]: null,
},
}, opts.initState)
this.store = new ObservableStore(initState)
Expand Down Expand Up @@ -184,20 +188,29 @@ class IncomingTransactionsController {
async _fetchTxs (address, fromBlock, networkType) {
let etherscanSubdomain = 'api'
const currentNetworkID = networkTypeToIdMap[networkType]
const supportedNetworkTypes = [ROPSTEN, RINKEBY, KOVAN, GOERLI, MAINNET]
const supportedNetworkTypes = [ROPSTEN, RINKEBY, KOVAN, GOERLI, MAINNET, XDAI]

if (supportedNetworkTypes.indexOf(networkType) === -1) {
return {}
}

if (networkType !== MAINNET) {
etherscanSubdomain = `api-${networkType}`
}
const apiUrl = `https://${etherscanSubdomain}.etherscan.io`
let url = `${apiUrl}/api?module=account&action=txlist&address=${address}&tag=latest&page=1`
let url
if (networkType === XDAI) {
url = `https://blockscout.com/poa/xdai/api?module=account&action=txlist&address=${address}&page=1`

if (fromBlock) {
url += `&startBlock=${parseInt(fromBlock, 10)}`
if (fromBlock) {
url += `&startblock=${parseInt(fromBlock, 10)}`
}
} else {
if (networkType !== MAINNET) {
etherscanSubdomain = `api-${networkType}`
}
const apiUrl = `https://${etherscanSubdomain}.etherscan.io`
url = `${apiUrl}/api?module=account&action=txlist&address=${address}&tag=latest&page=1`

if (fromBlock) {
url += `&startBlock=${parseInt(fromBlock, 10)}`
}
}
const response = await fetch(url)
const parsedResponse = await response.json()
Expand Down
2 changes: 2 additions & 0 deletions app/scripts/controllers/network/contract-addresses.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ const SINGLE_CALL_BALANCES_ADDRESS = '0xb1f8e55c7f64d203c1400b9d8555d050f94adf39
const SINGLE_CALL_BALANCES_ADDRESS_RINKEBY = '0x9f510b19f1ad66f0dcf6e45559fab0d6752c1db7'
const SINGLE_CALL_BALANCES_ADDRESS_ROPSTEN = '0xb8e671734ce5c8d7dfbbea5574fa4cf39f7a54a4'
const SINGLE_CALL_BALANCES_ADDRESS_KOVAN = '0xb1d3fbb2f83aecd196f474c16ca5d9cffa0d0ffc'
const SINGLE_CALL_BALANCES_ADDRESS_XDAI = '0x0a897e2bdc68ce63563233e2ae6a4513497f979a'

module.exports = {
SINGLE_CALL_BALANCES_ADDRESS,
SINGLE_CALL_BALANCES_ADDRESS_RINKEBY,
SINGLE_CALL_BALANCES_ADDRESS_ROPSTEN,
SINGLE_CALL_BALANCES_ADDRESS_KOVAN,
SINGLE_CALL_BALANCES_ADDRESS_XDAI,
}
6 changes: 6 additions & 0 deletions app/scripts/controllers/network/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ const KOVAN = 'kovan'
const MAINNET = 'mainnet'
const LOCALHOST = 'localhost'
const GOERLI = 'goerli'
const XDAI = 'xdai'

const MAINNET_CODE = 1
const ROPSTEN_CODE = 3
const RINKEBY_CODE = 4
const KOVAN_CODE = 42
const GOERLI_CODE = 5
const XDAI_CODE = 100

const ROPSTEN_DISPLAY_NAME = 'Ropsten'
const RINKEBY_DISPLAY_NAME = 'Rinkeby'
const KOVAN_DISPLAY_NAME = 'Kovan'
const MAINNET_DISPLAY_NAME = 'Main Ethereum Network'
const GOERLI_DISPLAY_NAME = 'Goerli'
const XDAI_DISPLAY_NAME = 'xDAI'

module.exports = {
ROPSTEN,
Expand All @@ -24,14 +27,17 @@ module.exports = {
MAINNET,
LOCALHOST,
GOERLI,
XDAI,
MAINNET_CODE,
ROPSTEN_CODE,
RINKEBY_CODE,
KOVAN_CODE,
GOERLI_CODE,
XDAI_CODE,
ROPSTEN_DISPLAY_NAME,
RINKEBY_DISPLAY_NAME,
KOVAN_DISPLAY_NAME,
MAINNET_DISPLAY_NAME,
GOERLI_DISPLAY_NAME,
XDAI_DISPLAY_NAME,
}
10 changes: 9 additions & 1 deletion app/scripts/controllers/network/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const {
MAINNET,
LOCALHOST,
GOERLI,
XDAI,
XDAI_CODE,
} = require('./enums')
const INFURA_PROVIDER_TYPES = [ROPSTEN, RINKEBY, KOVAN, MAINNET, GOERLI]

Expand Down Expand Up @@ -54,6 +56,9 @@ module.exports = class NetworkController extends EventEmitter {
// create stores
this.providerStore = new ObservableStore(providerConfig)
this.networkStore = new ObservableStore('loading')
if (parseInt(opts.network) === XDAI_CODE) {
defaultNetworkConfig.ticker = 'XDAI'
}
this.networkConfig = new ObservableStore(defaultNetworkConfig)
this.store = new ComposedStore({ provider: this.providerStore, network: this.networkStore, settings: this.networkConfig })
this.on('networkDidChange', this.lookupNetwork)
Expand Down Expand Up @@ -145,7 +150,7 @@ module.exports = class NetworkController extends EventEmitter {

async setProviderType (type, rpcTarget = '', ticker = 'ETH', nickname = '') {
assert.notEqual(type, 'rpc', `NetworkController - cannot call "setProviderType" with type 'rpc'. use "setRpcTarget"`)
assert(INFURA_PROVIDER_TYPES.includes(type) || type === LOCALHOST, `NetworkController - Unknown rpc type "${type}"`)
assert(INFURA_PROVIDER_TYPES.includes(type) || type === XDAI || type === LOCALHOST, `NetworkController - Unknown rpc type "${type}"`)
const providerConfig = { type, rpcTarget, ticker, nickname }
this.providerConfig = providerConfig
}
Expand Down Expand Up @@ -180,6 +185,8 @@ module.exports = class NetworkController extends EventEmitter {
if (isInfura) {
this._configureInfuraProvider(opts)
// other type-based rpc endpoints
} else if (type === XDAI) {
this._configureStandardProvider({ rpcUrl: 'https://dai.poa.network', chainId: '100', ticker: 'XDAI', nickname })
} else if (type === LOCALHOST) {
this._configureLocalhostProvider()
// url-based rpc endpoints
Expand Down Expand Up @@ -223,6 +230,7 @@ module.exports = class NetworkController extends EventEmitter {
// setup networkConfig
var settings = {
network: chainId,
ticker,
}
settings = extend(settings, networks.networkList['rpc'])
this.networkConfig.putState(settings)
Expand Down
5 changes: 5 additions & 0 deletions app/scripts/controllers/network/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ const {
KOVAN,
MAINNET,
GOERLI,
XDAI,
ROPSTEN_CODE,
RINKEBY_CODE,
KOVAN_CODE,
GOERLI_CODE,
XDAI_CODE,
ROPSTEN_DISPLAY_NAME,
RINKEBY_DISPLAY_NAME,
KOVAN_DISPLAY_NAME,
MAINNET_DISPLAY_NAME,
GOERLI_DISPLAY_NAME,
XDAI_DISPLAY_NAME,
} = require('./enums')

const networkToNameMap = {
Expand All @@ -21,10 +24,12 @@ const networkToNameMap = {
[KOVAN]: KOVAN_DISPLAY_NAME,
[MAINNET]: MAINNET_DISPLAY_NAME,
[GOERLI]: GOERLI_DISPLAY_NAME,
[XDAI]: XDAI_DISPLAY_NAME,
[ROPSTEN_CODE]: ROPSTEN_DISPLAY_NAME,
[RINKEBY_CODE]: RINKEBY_DISPLAY_NAME,
[KOVAN_CODE]: KOVAN_DISPLAY_NAME,
[GOERLI_CODE]: GOERLI_DISPLAY_NAME,
[XDAI_CODE]: XDAI_DISPLAY_NAME,
}

const getNetworkDisplayName = key => networkToNameMap[key]
Expand Down
10 changes: 6 additions & 4 deletions app/scripts/controllers/recent-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ const {
KOVAN,
MAINNET,
GOERLI,
XDAI,
} = require('./network/enums')
const INFURA_PROVIDER_TYPES = [ROPSTEN, RINKEBY, KOVAN, MAINNET, GOERLI]

const DEFAULT_PROVIDER_TYPES = INFURA_PROVIDER_TYPES
DEFAULT_PROVIDER_TYPES.push(XDAI)

class RecentBlocksController {

Expand Down Expand Up @@ -51,15 +53,15 @@ class RecentBlocksController {
}
let isListening = false
const { type } = networkController.getProviderConfig()
if (!INFURA_PROVIDER_TYPES.includes(type) && type !== 'loading') {
if (!DEFAULT_PROVIDER_TYPES.includes(type) && type !== 'loading') {
this.blockTracker.on('latest', blockListner)
isListening = true
}
networkController.on('networkDidChange', (newType) => {
if (INFURA_PROVIDER_TYPES.includes(newType) && isListening) {
if (DEFAULT_PROVIDER_TYPES.includes(newType) && isListening) {
this.blockTracker.removeListener('latest', blockListner)
} else if (
!INFURA_PROVIDER_TYPES.includes(type) &&
!DEFAULT_PROVIDER_TYPES.includes(type) &&
type !== 'loading' &&
!isListening
) {
Expand Down
8 changes: 6 additions & 2 deletions app/scripts/lib/account-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const Web3 = require('web3')
const SINGLE_CALL_BALANCES_ABI = require('single-call-balance-checker-abi')

const { bnToHex } = require('./util')
const { MAINNET_CODE, RINKEBY_CODE, ROPSTEN_CODE, KOVAN_CODE } = require('../controllers/network/enums')
const { SINGLE_CALL_BALANCES_ADDRESS, SINGLE_CALL_BALANCES_ADDRESS_RINKEBY, SINGLE_CALL_BALANCES_ADDRESS_ROPSTEN, SINGLE_CALL_BALANCES_ADDRESS_KOVAN } = require('../controllers/network/contract-addresses')
const { MAINNET_CODE, RINKEBY_CODE, ROPSTEN_CODE, KOVAN_CODE, XDAI_CODE } = require('../controllers/network/enums')
const { SINGLE_CALL_BALANCES_ADDRESS, SINGLE_CALL_BALANCES_ADDRESS_RINKEBY, SINGLE_CALL_BALANCES_ADDRESS_ROPSTEN, SINGLE_CALL_BALANCES_ADDRESS_KOVAN, SINGLE_CALL_BALANCES_ADDRESS_XDAI } = require('../controllers/network/contract-addresses')


class AccountTracker {
Expand Down Expand Up @@ -202,6 +202,10 @@ class AccountTracker {
await this._updateAccountsViaBalanceChecker(addresses, SINGLE_CALL_BALANCES_ADDRESS_KOVAN)
break

case XDAI_CODE:
await this._updateAccountsViaBalanceChecker(addresses, SINGLE_CALL_BALANCES_ADDRESS_XDAI)
break

default:
await Promise.all(addresses.map(this._updateAccount.bind(this)))
}
Expand Down
4 changes: 4 additions & 0 deletions app/scripts/lib/buy-eth-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ function getBuyEthUrl ({ network, amount, address, service }) {
return 'https://github.com/kovan-testnet/faucet'
case 'goerli-faucet':
return 'https://goerli-faucet.slock.it/'
case 'bridge':
return 'https://dai-bridge.poa.network/'
}
throw new Error(`Unknown cryptocurrency exchange or faucet: "${service}"`)
}
Expand All @@ -48,6 +50,8 @@ function getDefaultServiceForNetwork (network) {
return 'kovan-faucet'
case '5':
return 'goerli-faucet'
case '100':
return 'bridge'
}
throw new Error(`No default cryptocurrency exchange or faucet for networkId: "${network}"`)
}
2 changes: 2 additions & 0 deletions app/scripts/lib/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const ROPSTEN_CHAIN_ID = '0x3'
const RINKEBY_CHAIN_ID = '0x4'
const KOVAN_CHAIN_ID = '0x2a'
const GOERLI_CHAIN_ID = '0x5'
const XDAI_CHAIN_ID = '0x64'

module.exports = {
ENVIRONMENT_TYPE_POPUP,
Expand All @@ -30,4 +31,5 @@ module.exports = {
RINKEBY_CHAIN_ID,
KOVAN_CHAIN_ID,
GOERLI_CHAIN_ID,
XDAI_CHAIN_ID,
}
2 changes: 2 additions & 0 deletions app/scripts/lib/select-chain-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
RINKEBY_CHAIN_ID,
KOVAN_CHAIN_ID,
GOERLI_CHAIN_ID,
XDAI_CHAIN_ID,
} = require('./enums')

const standardNetworkId = {
Expand All @@ -12,6 +13,7 @@ const standardNetworkId = {
'4': RINKEBY_CHAIN_ID,
'42': KOVAN_CHAIN_ID,
'5': GOERLI_CHAIN_ID,
'100': XDAI_CHAIN_ID,
}

function selectChainId (metamaskState) {
Expand Down
8 changes: 7 additions & 1 deletion app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const {
PhishingController,
} = require('gaba')
const backEndMetaMetricsEvent = require('./lib/backend-metametrics')
const { XDAI } = require('../../ui/app/helpers/constants/common')

module.exports = class MetamaskController extends EventEmitter {

Expand Down Expand Up @@ -1667,9 +1668,14 @@ module.exports = class MetamaskController extends EventEmitter {
setCurrentCurrency (currencyCode, cb) {
const { ticker } = this.networkController.getNetworkConfig()
try {
let nativeCurrency = ticker
if (ticker === XDAI) {
nativeCurrency = 'usd'
}
const currencyState = {
nativeCurrency: ticker,
nativeCurrency: nativeCurrency,
currentCurrency: currencyCode,
ticker: ticker,
}
this.currencyRateController.update(currencyState)
this.currencyRateController.configure(currencyState)
Expand Down
8 changes: 8 additions & 0 deletions test/unit/app/buy-eth-url.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ describe('buy-eth-url', function () {
const kovan = {
network: '42',
}
const xdai = {
network: '100',
}

it('returns coinbase url with amount and address for network 1', function () {
const wyreUrl = getBuyEthUrl(mainnet)
Expand All @@ -39,5 +42,10 @@ describe('buy-eth-url', function () {
assert.equal(kovanUrl, 'https://github.com/kovan-testnet/faucet')
})

it('returns burner wallet for network 100', function () {
const xdaiUrl = getBuyEthUrl(xdai)
assert.equal(xdaiUrl, 'https://dai-bridge.poa.network/')
})

})

Loading