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

support multiple evm chains #329

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 12 additions & 4 deletions adapters/ethereum/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { MerkleTree, Channel, ChannelState } = require('adex-protocol-eth/js')
const { Wallet, Contract, utils, getDefaultProvider } = require('ethers')
const { Wallet, Contract, utils, providers, getDefaultProvider } = require('ethers')
const coreABI = require('adex-protocol-eth/abi/AdExCore')
const formatAddress = require('ethers').utils.getAddress
const fetch = require('node-fetch')
Expand All @@ -23,9 +23,6 @@ let keystoreJson = null
let wallet = null

function Adapter(opts, cfg, ethProvider) {
const provider = ethProvider || getDefaultProvider(cfg.ETHEREUM_NETWORK)
const core = new Contract(cfg.ETHEREUM_CORE_ADDR, coreABI, provider)

this.init = function() {
assert.ok(typeof opts.keystoreFile === 'string', 'keystoreFile required')
return readFile(opts.keystoreFile).then(json => {
Expand Down Expand Up @@ -66,6 +63,7 @@ function Adapter(opts, cfg, ethProvider) {

this.validateChannel = async function(channel, options = {}) {
const ethChannel = toEthereumChannel(channel)
const core = getCoreContractForChannel(cfg, channel, ethProvider)

assert.equal(channel.id, ethChannel.hashHex(core.address), 'channel.id is not valid')
assert.equal(channel.creator, formatAddress(channel.creator), 'channel.creator is checksummed')
Expand Down Expand Up @@ -180,6 +178,16 @@ function toEthereumChannel(channel) {
})
}

function getCoreContractForChannel(cfg, channel, ethProvider) {
const depositChainId =
channel.spec.depositChainId || cfg.depositAssetsToChainId[channel.depositAsset]
const provider =
ethProvider ||
(depositChainId && new providers.JsonRpcProvider(cfg.supportedChainIdsByRPC[depositChainId])) ||
getDefaultProvider(cfg.ETHEREUM_NETWORK)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no ETHEREUM_NETWORK in the cfg - it is NETWORK. In docs/configuration.md it's ETHEREUM_NETWORK - just has to be NETWORK everywhere I guess.

Copy link
Member

@ivopaunov ivopaunov Feb 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should check cfg.supportedChainIdsByRPC[depositChainId] before new providers.JsonRpcProvider... as at the moment cfg.supportedChainIdsByRPC is empty and I'm not sure if it will throw or use default

return new Contract(cfg.chainIdsByCoreAddr[depositChainId], coreABI, provider)
}

/*
const IVO_MM = '0x54122C899013e2c4229e1789CFE5B17446Dae7f9'
const GOERLI_TST = '0x7af963cf6d228e564e2a0aa0ddbf06210b38615d'
Expand Down
8 changes: 5 additions & 3 deletions cfg/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ module.exports = {
CREATORS_WHITELIST: [],
MINIMAL_DEPOSIT: 0,
MINIMAL_FEE: 0,
ETHEREUM_CORE_ADDR: '0x333420fc6a897356e69b62417cd17ff012177d2b',
ETHEREUM_NETWORK: 'goerli',
NETWORK: 'goerli',
ETHEREUM_ADAPTER_RELAYER: 'https://goerli-relayer.adex.network',
VALIDATORS_WHITELIST: [],
CHANNEL_REFRESH_INTERVAL: 1000,
MAX_CHANNEL_SPEC_BYTES_SIZE: 35000,
admins: ['0xce07CbB7e054514D590a0262C93070D838bFBA2e']
admins: ['0xce07CbB7e054514D590a0262C93070D838bFBA2e'],
chainIdsByCoreAddr: {},
supportedChainIdsByRPC: {},
depositAssetsToChainId: {}
}
29 changes: 26 additions & 3 deletions cfg/prod.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
const {
ETHEREUM_MAINNET_CHAIN_ID,
ETHEREUM_GOERLI_CHAIN_ID,
BINANCE_MAINNET_CHAIN_ID,
XDAI_MAINNET_CHAIN_ID
} = require('../services/constants')

module.exports = {
MAX_CHANNELS: 512,
WAIT_TIME: 40000,
Expand All @@ -24,11 +31,27 @@ module.exports = {
'0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359',
'0x6B175474E89094C44Da98b954EedeAC495271d0F'
],
ETHEREUM_CORE_ADDR: '0x333420fc6a897356e69b62417cd17ff012177d2b',
ETHEREUM_NETWORK: 'homestead',
NETWORK: 'homestead',
ETHEREUM_ADAPTER_RELAYER: 'https://relayer.adex.network',
VALIDATORS_WHITELIST: [],
CHANNEL_REFRESH_INTERVAL: 40000,
MAX_CHANNEL_SPEC_BYTES_SIZE: 35000,
admins: []
admins: [],

chainIdsByCoreAddr: {
[ETHEREUM_MAINNET_CHAIN_ID]: '0x333420fc6a897356e69b62417cd17ff012177d2b',
[ETHEREUM_GOERLI_CHAIN_ID]: '',
[BINANCE_MAINNET_CHAIN_ID]: '',
[XDAI_MAINNET_CHAIN_ID]: ''
},
supportedChainIdsByRPC: {
[ETHEREUM_MAINNET_CHAIN_ID]: '',
[ETHEREUM_GOERLI_CHAIN_ID]: '',
[BINANCE_MAINNET_CHAIN_ID]: '',
[XDAI_MAINNET_CHAIN_ID]: ''
},
depositAssetsToChainId: {
'0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359': ETHEREUM_MAINNET_CHAIN_ID,
'0x6B175474E89094C44Da98b954EedeAC495271d0F': ETHEREUM_MAINNET_CHAIN_ID
}
}
6 changes: 4 additions & 2 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
| CREATORS_WHITELIST | List of accepted channel creators; leave empty for all | [] |
| MINIMAL_DEPOSIT | Least amount a channel is required to have to be accepted | 0 |
| TOKEN_ADDRESS_WHITELIST | List of valid payment tokens address; leave empty for all | [] |
| ETHEREUM_CORE_ADDR | On chain contract address | 0x333420fc6a897356e69b62417cd17ff012177d2b |
| ETHEREUM_NETWORK | Ethereum network id | homestead |,
| ETHEREUM_NETWORK | Ethereum network id | homestead |
| VALIDATORS_WHITELIST | List of accepted channel validators; leave empty for all | [] |
| chainIdsByCoreAddr | Supported chain id to the network deployed on chain contract core addr | { '1': '0x333420fc6a897356e69b62417cd17ff012177d2b' } |
| supportedChainIdsByRPC | Map of a network chain id to the network rpc url | { '1': 'https://mainnet.infura.io/*' } |
| depositAssetsToChainId | Default fallback mapping whitelisted deposit asset to its network chain id | {'0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359': '1' } |
2 changes: 2 additions & 0 deletions routes/schemas.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { Joi } = require('celebrate')
const { eventTypes } = require('../services/constants')
const cfg = require('../cfg')

const numericString = Joi.string().regex(/^\d+$/)

Expand Down Expand Up @@ -97,6 +98,7 @@ module.exports = {
.allow(''),
adUnits: Joi.array().items(Joi.object()),
targeting: Joi.array().items(Joi.object()),
depositChainId: Joi.string().valid(Object.keys(cfg.supportedChainIdsByRPC)),
validators: Joi.array()
.items(
Joi.object({
Expand Down
3 changes: 2 additions & 1 deletion scripts/distribute-incentives.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const identityAbi = require('adex-protocol-eth/abi/Identity')
const db = require('../db')
const cfg = require('../cfg')
const adapters = require('../adapters')
const constants = require('../services/constants')

const ADDR_STAKING = '0x4846c6837ec670bbd1f5b485471c8f64ecb9c534'

Expand Down Expand Up @@ -50,7 +51,7 @@ const ADXToken = new Contract(
)
const idInterface = new Interface(identityAbi)

const coreAddr = cfg.ETHEREUM_CORE_ADDR
const coreAddr = cfg.chainIdsByCoreAddr[constants.ETHEREUM_MAINNET_CHAIN_ID]

const ZERO = bigNumberify(0)

Expand Down
3 changes: 2 additions & 1 deletion scripts/distribute-rewards.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const identityAbi = require('adex-protocol-eth/abi/Identity')
const db = require('../db')
const cfg = require('../cfg')
const adapters = require('../adapters')
const constants = require('../services/constants')

const STAKING_START_MONTH = new Date('01-01-2020')
const ADDR_STAKING = '0x4846c6837ec670bbd1f5b485471c8f64ecb9c534'
Expand All @@ -38,7 +39,7 @@ const Token = new Contract(
)
const idInterface = new Interface(identityAbi)

const coreAddr = cfg.ETHEREUM_CORE_ADDR
const coreAddr = cfg.chainIdsByCoreAddr[constants.ETHEREUM_MAINNET_CHAIN_ID]

const keystoreFile = process.argv[2]
const keystorePwd = process.env.KEYSTORE_PWD
Expand Down
6 changes: 5 additions & 1 deletion services/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ module.exports = Object.freeze({
impression: 'IMPRESSION',
click: 'CLICK',
close: 'CLOSE'
}
},
ETHEREUM_MAINNET_CHAIN_ID: 1,
ETHEREUM_GOERLI_CHAIN_ID: 5,
BINANCE_MAINNET_CHAIN_ID: 56,
XDAI_MAINNET_CHAIN_ID: 100
})
38 changes: 31 additions & 7 deletions test/ethereum_adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const opts = {
}

const provider = new providers.JsonRpcProvider('http://localhost:8545')
const testnetChainId = '123456654321'
let validChannel

// ethereum adapter
Expand Down Expand Up @@ -136,14 +137,20 @@ tape('should validate channel properly', async function(t) {

tape('should not validate channel with invalid id', async function(t) {
const { core } = await deployContracts()
const okChannel = await getValidChannel()
const ethereumAdapter = new ethereum.Adapter(
opts,
{ ...cfg, ETHEREUM_CORE_ADDR: core.address },
{
...cfg,
depositAssetsToChainId: {
...cfg.depositAssetsToChainId,
[okChannel.depositAsset]: testnetChainId
},
chainIdsByCoreAddr: { ...cfg.chainIdsByCoreAddr, [testnetChainId]: core.address }
},
provider
)

const okChannel = await getValidChannel()

const invalidChannelId = {
...okChannel,
id: '0xdffsfsfsfs'
Expand All @@ -164,7 +171,15 @@ tape('should not validate invalid channels', async function(t) {
const [channel, config, err] = item
const ethAdapter = new ethereum.Adapter(
opts,
{ ...cfg, ...config, ETHEREUM_CORE_ADDR: core.address },
{
...cfg,
...config,
depositAssetsToChainId: {
...cfg.depositAssetsToChainId,
[channel.depositAsset]: testnetChainId
},
chainIdsByCoreAddr: { ...cfg.chainIdsByCoreAddr, [testnetChainId]: core.address }
},
provider
)
await ethAdapter.init()
Expand Down Expand Up @@ -215,16 +230,25 @@ tape('EWT: should verify message', async function(t) {
async function getValidChannel() {
if (validChannel) return validChannel
const { core } = await deployContracts()

// get a sample valid channel
const channel = await sampleChannel()

const ethereumAdapter = new ethereum.Adapter(
opts,
{ ...cfg, ETHEREUM_CORE_ADDR: core.address },
{
...cfg,
depositAssetsToChainId: {
...cfg.depositAssetsToChainId,
[channel.depositAsset]: testnetChainId
},
chainIdsByCoreAddr: { ...cfg.chainIdsByCoreAddr, [testnetChainId]: core.address }
},
provider
)

await ethereumAdapter.init()

// get a sample valid channel
const channel = await sampleChannel()
const ethChannel = ethereum.toEthereumChannel(channel)
channel.id = ethChannel.hashHex(core.address)

Expand Down