Added Commons sub-repository (#139)

* Added Commons

* Typo.

* Typo.

* Typo.
This commit is contained in:
Przemyslaw Rzad 2019-07-11 15:46:52 +02:00 committed by GitHub
parent cf3aa56929
commit 054225d348
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 344 additions and 0 deletions

@ -26,6 +26,7 @@ Sub-repositories maintained within this monorepo are listed below.
| [Oracle-E2E](oracle-e2e/README.md) | End to end tests for the Oracle |
| [Monitor-E2E](monitor-e2e/README.md) | End to end tests for the Monitor |
| [UI-E2E](ui-e2e/README.md) | End to end tests for the UI |
| [Commons](commons/README.md) | Interfaces, constants and utilities shared between the sub-repositories |
| [E2E-Commons](e2e-commons/README.md) | Common utilities and configuration used in end to end tests |
Additionally there are [Smart Contracts](https://github.com/poanetwork/poa-bridge-contracts) used to manage bridge validators, collect signatures, and confirm asset relay and disposal.

12
commons/.eslintrc Normal file

@ -0,0 +1,12 @@
{
"extends": [
"airbnb-base",
"../.eslintrc"
],
"rules": {
"no-unused-expressions": "off"
},
"env": {
"mocha": true
}
}

2
commons/README.md Normal file

@ -0,0 +1,2 @@
# POA Token Bridge / Commons
Interfaces, constants and utilities shared between the sub-repositories

55
commons/abis.js Normal file

@ -0,0 +1,55 @@
const path = '../contracts/build/contracts/'
const HOME_NATIVE_TO_ERC_ABI = require(`${path}HomeBridgeNativeToErc`).abi
const FOREIGN_NATIVE_TO_ERC_ABI = require(`${path}ForeignBridgeNativeToErc`).abi
const HOME_ERC_TO_ERC_ABI = require(`${path}HomeBridgeErcToErc`).abi
const FOREIGN_ERC_TO_ERC_ABI = require(`${path}ForeignBridgeErc677ToErc677`).abi
const HOME_ERC_TO_NATIVE_ABI = require(`${path}HomeBridgeErcToNative`).abi
const FOREIGN_ERC_TO_NATIVE_ABI = require(`${path}ForeignBridgeErcToNative`).abi
const ERC20_ABI = require(`${path}ERC20`).abi
const ERC677_ABI = require(`${path}ERC677`).abi
const ERC677_BRIDGE_TOKEN_ABI = require(`${path}ERC677BridgeToken`).abi
const BLOCK_REWARD_ABI = require(`${path}IBlockReward`).abi
const BRIDGE_VALIDATORS_ABI = require(`${path}BridgeValidators`).abi
const REWARDABLE_VALIDATORS_ABI = require(`${path}RewardableValidators`).abi
const { homeV1Abi, foreignViAbi } = require('./v1Abis')
const { BRIDGE_MODES } = require('./constants')
function getBridgeABIs(bridgeMode) {
let HOME_ABI = null
let FOREIGN_ABI = null
if (bridgeMode === BRIDGE_MODES.NATIVE_TO_ERC) {
HOME_ABI = HOME_NATIVE_TO_ERC_ABI
FOREIGN_ABI = FOREIGN_NATIVE_TO_ERC_ABI
} else if (bridgeMode === BRIDGE_MODES.ERC_TO_ERC) {
HOME_ABI = HOME_ERC_TO_ERC_ABI
FOREIGN_ABI = FOREIGN_ERC_TO_ERC_ABI
} else if (bridgeMode === BRIDGE_MODES.ERC_TO_NATIVE) {
HOME_ABI = HOME_ERC_TO_NATIVE_ABI
FOREIGN_ABI = FOREIGN_ERC_TO_NATIVE_ABI
} else if (bridgeMode === BRIDGE_MODES.NATIVE_TO_ERC_V1) {
HOME_ABI = homeV1Abi
FOREIGN_ABI = foreignViAbi
} else {
throw new Error(`Unrecognized bridge mode: ${bridgeMode}`)
}
return { HOME_ABI, FOREIGN_ABI }
}
module.exports = {
getBridgeABIs,
HOME_NATIVE_TO_ERC_ABI,
FOREIGN_NATIVE_TO_ERC_ABI,
HOME_ERC_TO_ERC_ABI,
FOREIGN_ERC_TO_ERC_ABI,
HOME_ERC_TO_NATIVE_ABI,
FOREIGN_ERC_TO_NATIVE_ABI,
ERC20_ABI,
ERC677_ABI,
ERC677_BRIDGE_TOKEN_ABI,
BLOCK_REWARD_ABI,
BRIDGE_VALIDATORS_ABI,
REWARDABLE_VALIDATORS_ABI
}

19
commons/constants.js Normal file

@ -0,0 +1,19 @@
const BRIDGE_MODES = {
NATIVE_TO_ERC: 'NATIVE_TO_ERC',
ERC_TO_ERC: 'ERC_TO_ERC',
ERC_TO_NATIVE: 'ERC_TO_NATIVE',
NATIVE_TO_ERC_V1: 'NATIVE_TO_ERC_V1'
}
const ERC_TYPES = {
ERC20: 'ERC20',
ERC677: 'ERC677'
}
const FEE_MANAGER_MODE = {
ONE_DIRECTION: 'ONE_DIRECTION',
BOTH_DIRECTIONS: 'BOTH_DIRECTIONS',
UNDEFINED: 'UNDEFINED'
}
module.exports = { BRIDGE_MODES, ERC_TYPES, FEE_MANAGER_MODE }

9
commons/index.js Normal file

@ -0,0 +1,9 @@
const constants = require('./constants')
const abis = require('./abis')
const utils = require('./utils')
module.exports = {
...constants,
...abis,
...utils
}

10
commons/package.json Normal file

@ -0,0 +1,10 @@
{
"name": "commons",
"version": "0.0.1",
"private": true,
"main": "index.js",
"scripts": {
"lint": "eslint . --ignore-path ../.eslintignore",
"test": "NODE_ENV=test mocha"
}
}

12
commons/test/constants.js Normal file

@ -0,0 +1,12 @@
const { expect } = require('chai')
const { BRIDGE_MODES, ERC_TYPES } = require('../constants')
describe('constants', () => {
it('should contain correct number of bridge types', () => {
expect(Object.keys(BRIDGE_MODES).length).to.be.equal(4)
})
it('should contain correct number of erc types', () => {
expect(Object.keys(ERC_TYPES).length).to.be.equal(2)
})
})

60
commons/utils.js Normal file

@ -0,0 +1,60 @@
const { BRIDGE_MODES, FEE_MANAGER_MODE } = require('./constants')
function decodeBridgeMode(bridgeModeHash) {
switch (bridgeModeHash) {
case '0x92a8d7fe':
return BRIDGE_MODES.NATIVE_TO_ERC
case '0xba4690f5':
return BRIDGE_MODES.ERC_TO_ERC
case '0x18762d46':
return BRIDGE_MODES.ERC_TO_NATIVE
default:
throw new Error(`Unrecognized bridge mode hash: '${bridgeModeHash}'`)
}
}
const decodeFeeManagerMode = managerModeHash => {
switch (managerModeHash) {
case '0xf2aed8f7':
return FEE_MANAGER_MODE.ONE_DIRECTION
case '0xd7de965f':
return FEE_MANAGER_MODE.BOTH_DIRECTIONS
default:
throw new Error(`Unrecognized fee manager mode hash: '${managerModeHash}'`)
}
}
async function getBridgeMode(contract) {
try {
const bridgeModeHash = await contract.methods.getBridgeMode().call()
return decodeBridgeMode(bridgeModeHash)
} catch (e) {
return BRIDGE_MODES.NATIVE_TO_ERC_V1
}
}
const getUnit = bridgeMode => {
let unitHome = null
let unitForeign = null
if (bridgeMode === BRIDGE_MODES.NATIVE_TO_ERC) {
unitHome = 'Native coins'
unitForeign = 'Tokens'
} else if (bridgeMode === BRIDGE_MODES.ERC_TO_ERC) {
unitHome = 'Tokens'
unitForeign = 'Tokens'
} else if (bridgeMode === BRIDGE_MODES.ERC_TO_NATIVE) {
unitHome = 'Native coins'
unitForeign = 'Tokens'
} else {
throw new Error(`Unrecognized bridge mode: ${bridgeMode}`)
}
return { unitHome, unitForeign }
}
module.exports = {
decodeBridgeMode,
decodeFeeManagerMode,
getBridgeMode,
getUnit
}

163
commons/v1Abis.js Normal file

@ -0,0 +1,163 @@
const homeV1Abi = [
{
constant: true,
inputs: [],
name: 'validatorContract',
outputs: [
{
name: '',
type: 'address'
}
],
payable: false,
stateMutability: 'view',
type: 'function'
},
{
anonymous: false,
inputs: [
{
indexed: false,
name: 'recipient',
type: 'address'
},
{
indexed: false,
name: 'value',
type: 'uint256'
}
],
name: 'Deposit',
type: 'event'
},
{
anonymous: false,
inputs: [
{
indexed: false,
name: 'recipient',
type: 'address'
},
{
indexed: false,
name: 'value',
type: 'uint256'
},
{
indexed: false,
name: 'transactionHash',
type: 'bytes32'
}
],
name: 'Withdraw',
type: 'event'
},
{
constant: true,
inputs: [],
name: 'deployedAtBlock',
outputs: [
{
name: '',
type: 'uint256'
}
],
payable: false,
stateMutability: 'view',
type: 'function'
}
]
const foreignViAbi = [
{
constant: true,
inputs: [],
name: 'validatorContract',
outputs: [
{
name: '',
type: 'address'
}
],
payable: false,
stateMutability: 'view',
type: 'function'
},
{
anonymous: false,
inputs: [
{
indexed: false,
name: 'recipient',
type: 'address'
},
{
indexed: false,
name: 'value',
type: 'uint256'
},
{
indexed: false,
name: 'transactionHash',
type: 'bytes32'
}
],
name: 'Deposit',
type: 'event'
},
{
anonymous: false,
inputs: [
{
indexed: false,
name: 'recipient',
type: 'address'
},
{
indexed: false,
name: 'value',
type: 'uint256'
},
{
indexed: false,
name: 'homeGasPrice',
type: 'uint256'
}
],
name: 'Withdraw',
type: 'event'
},
{
constant: true,
inputs: [],
name: 'deployedAtBlock',
outputs: [
{
name: '',
type: 'uint256'
}
],
payable: false,
stateMutability: 'view',
type: 'function'
},
{
constant: true,
inputs: [],
name: 'erc677token',
outputs: [
{
name: '',
type: 'address'
}
],
payable: false,
stateMutability: 'view',
type: 'function'
}
]
module.exports = {
homeV1Abi,
foreignViAbi
}

@ -23,6 +23,7 @@
"mocha": "^5.2.0"
},
"workspaces": [
"commons",
"oracle",
"oracle-e2e",
"ui",