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

Add support erc677 on Foreign network for erc-to-erc mode #215

Merged
merged 5 commits into from May 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion e2e-script/parity/Dockerfile
@@ -1,4 +1,4 @@
FROM parity/parity:v2.2.11
FROM parity/parity:v2.3.3

WORKDIR /stuff

Expand Down
2 changes: 1 addition & 1 deletion e2e-script/parity/Dockerfile-foreign
@@ -1,4 +1,4 @@
FROM parity/parity:v2.2.11
FROM parity/parity:v2.3.3

WORKDIR /stuff

Expand Down
10 changes: 5 additions & 5 deletions src/components/Bridge.js
Expand Up @@ -4,7 +4,7 @@ import { toHex } from 'web3-utils'
import foreignLogoPurple from '../assets/images/logos/logo-poa-20-purple@2x.png'
import homeLogoPurple from '../assets/images/logos/logo-poa-sokol-purple@2x.png'
import swal from 'sweetalert'
import { BRIDGE_MODES } from '../stores/utils/bridgeMode'
import { BRIDGE_MODES, ERC_TYPES } from '../stores/utils/bridgeMode'
import { BridgeAddress } from './index'
import { BridgeForm } from './index'
import { BridgeNetwork } from './index'
Expand Down Expand Up @@ -111,8 +111,8 @@ export class Bridge extends React.Component {
}

async _sendToForeign(amount){
const { web3Store, foreignStore, alertStore, txStore, bridgeMode } = this.props.RootStore
const isExternalErc20 = bridgeMode === BRIDGE_MODES.ERC_TO_ERC || bridgeMode === BRIDGE_MODES.ERC_TO_NATIVE
const { web3Store, foreignStore, alertStore, txStore } = this.props.RootStore
const isExternalErc20 = foreignStore.tokenType === ERC_TYPES.ERC20
const { isLessThan, isGreaterThan } = this
if(web3Store.metamaskNet.id.toString() !== web3Store.foreignNet.id.toString()){
swal("Error", `Please switch wallet to ${web3Store.foreignNet.name} network`, "error")
Expand Down Expand Up @@ -257,8 +257,8 @@ export class Bridge extends React.Component {
}

loadForeignDetails = () => {
const { web3Store, foreignStore, bridgeMode } = this.props.RootStore
const isExternalErc20 = bridgeMode === BRIDGE_MODES.ERC_TO_ERC || bridgeMode === BRIDGE_MODES.ERC_TO_NATIVE
const { web3Store, foreignStore } = this.props.RootStore
const isExternalErc20 = foreignStore.tokenType === ERC_TYPES.ERC20
const foreignURL = new URL(web3Store.FOREIGN_HTTP_PARITY_URL)
const foreignDisplayUrl = `${foreignURL.protocol}//${foreignURL.hostname}`

Expand Down
2 changes: 1 addition & 1 deletion src/components/NetworkDetails.js
Expand Up @@ -34,7 +34,7 @@ export const NetworkDetails = ({
: numeral(balance).format('0,0.000', Math.floor)

return (
<div className="network-details">
<div className="network-details" data-testid="network-details">
<div className="details-logo-container">
<div className={logoClass} />
</div>
Expand Down
57 changes: 57 additions & 0 deletions src/components/__tests__/NetworkDetails.test.js
@@ -0,0 +1,57 @@
import React from 'react'
import { render, cleanup } from 'react-testing-library'
import { NetworkDetails } from '../NetworkDetails'
import 'jest-dom/extend-expect'

afterEach(cleanup)

const baseData = {
address: "0xCecBE80Ed3548dE11D7d2D922a36576eA40C4c26",
balance: "99.99",
currency: "TEST",
displayTokenAddress: true,
getExplorerAddressUrl: () => {},
isHome: false,
maxCurrentLimit: "20000",
maxPerTx: "2000",
minPerTx: "0.01",
tokenAddress: "0xb69d9C58C258080eABF499270c778bBDE38dd6Ac",
tokenName: "TEST",
totalSupply: "100",
url: "https://ropsten.infura.io"
}

describe('NetworkDetails', () => {
it('should display bridge limits information ', () => {
// Given
const data = {
...baseData,
displayBridgeLimits: true
}

// When
const { queryByTestId } = render(<NetworkDetails {...data}/>)

// Then
const container = queryByTestId('network-details')
expect(container).toHaveTextContent('Minimum Amount Per Transaction')
expect(container).toHaveTextContent('Maximum Amount Per Transaction')
expect(container).toHaveTextContent('Remaining Daily')
})
it('should not display bridge limits information ', () => {
// Given
const data = {
...baseData,
displayBridgeLimits: false
}

// When
const { queryByTestId } = render(<NetworkDetails {...data}/>)

// Then
const container = queryByTestId('network-details')
expect(container).not.toHaveTextContent('Minimum Amount Per Transaction')
expect(container).not.toHaveTextContent('Maximum Amount Per Transaction')
expect(container).not.toHaveTextContent('Remaining Daily')
})
})
5 changes: 4 additions & 1 deletion src/stores/ForeignStore.js
Expand Up @@ -18,7 +18,8 @@ import {
getFeeManagerMode,
ZERO_ADDRESS,
getDeployedAtBlock,
getValidatorList
getValidatorList,
getTokenType
} from './utils/contract'
import { balanceLoaded, removePendingTransaction } from './utils/testUtils'
import sleep from './utils/sleep'
Expand Down Expand Up @@ -50,6 +51,7 @@ class ForeignStore {
@observable totalSpentPerDay = 0
@observable tokenAddress = '';
@observable feeEventsFinished = false
@observable tokenType = ''
feeManager = {
totalFeeDistributedFromSignatures: BN(0),
totalFeeDistributedFromAffirmation: BN(0)
Expand Down Expand Up @@ -132,6 +134,7 @@ class ForeignStore {
? await getErc20TokenAddress(this.foreignBridge)
: await getErc677TokenAddress(this.foreignBridge)
this.tokenContract = new this.foreignWeb3.eth.Contract(ERC677_ABI, this.tokenAddress);
this.tokenType = await getTokenType(this.tokenContract, this.FOREIGN_BRIDGE_ADDRESS)
const alternativeContract = new this.foreignWeb3.eth.Contract(ERC20Bytes32Abi, this.tokenAddress);
try {
this.symbol =await getSymbol(this.tokenContract)
Expand Down
63 changes: 63 additions & 0 deletions src/stores/utils/__tests__/contract.test.js
@@ -0,0 +1,63 @@
import { getTokenType } from '../contract'
import { ERC_TYPES } from '../bridgeMode'

describe('getTokenType', () => {
it('should return ERC677 if bridgeContract is equal to bridgeAddress', async () => {
// Given
const bridgeAddress = '0xCecBE80Ed3548dE11D7d2D922a36576eA40C4c26'
const contract = {
methods: {
bridgeContract: () => {
return {
call: () => Promise.resolve(bridgeAddress)
}
}
}
}

// When
const type = await getTokenType(contract, bridgeAddress)

// Then
expect(type).toEqual(ERC_TYPES.ERC677)
})
it('should return ERC20 if bridgeContract is not equal to bridgeAddress', async () => {
// Given
const bridgeAddress = '0xCecBE80Ed3548dE11D7d2D922a36576eA40C4c26'
const contract = {
methods: {
bridgeContract: () => {
return {
call: () => Promise.resolve('0xBFCb120F7B1de491262CA4D9D8Eba70438b6896E')
}
}
}
}

// When
const type = await getTokenType(contract, bridgeAddress)

// Then
expect(type).toEqual(ERC_TYPES.ERC20)
})
it('should return ERC20 if bridgeContract is not present', async () => {
// Given
const bridgeAddress = '0xCecBE80Ed3548dE11D7d2D922a36576eA40C4c26'
const contract = {
methods: {
bridgeContract: () => {
return {
call: () => Promise.reject()
}
}
}
}

// When
const type = await getTokenType(contract, bridgeAddress)

// Then
expect(type).toEqual(ERC_TYPES.ERC20)
})
})

5 changes: 5 additions & 0 deletions src/stores/utils/bridgeMode.js
Expand Up @@ -17,6 +17,11 @@ export const FEE_MANAGER_MODE = {
UNDEFINED: 'UNDEFINED'
}

export const ERC_TYPES = {
ERC677: 'ERC677',
ERC20: 'ERC20'
}

export const getBridgeABIs = (bridgeMode) => {
let HOME_ABI = null
let FOREIGN_ABI = null
Expand Down
14 changes: 14 additions & 0 deletions src/stores/utils/contract.js
Expand Up @@ -2,6 +2,7 @@ import BN from 'bignumber.js'
import { fromDecimals } from './decimals'
import { fromWei } from 'web3-utils'
import { abi as rewardableValidatorsAbi } from '../../contracts/RewardableValidators'
import { ERC_TYPES } from "./bridgeMode"

export const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'

Expand Down Expand Up @@ -149,3 +150,16 @@ export const getDeployedAtBlock = async (contract) => {
return 0
}
}

export const getTokenType = async (contract, bridgeAddress) => {
try {
const bridgeContract = await contract.methods.bridgeContract().call()
if (bridgeContract === bridgeAddress) {
return ERC_TYPES.ERC677
} else {
return ERC_TYPES.ERC20
}
} catch (e) {
return ERC_TYPES.ERC20
}
}
2 changes: 1 addition & 1 deletion submodules/poa-bridge-contracts