Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
Use address overrides for mapping over hardcoded network contracts (#213
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Padraic-O-Mhuiris authored Mar 2, 2020
1 parent c8823be commit 4506f9c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
18 changes: 15 additions & 3 deletions packages/dai/contracts/networks.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ const addressMapping = {
testnet: testnetAddresses
};

export function contractInfo(network) {
const addresses = addressMapping[network];
export function contractAddressesInfo(addresses) {
return {
// Tokens
[tokens.DAI]: [
Expand Down Expand Up @@ -143,13 +142,26 @@ export function contractInfo(network) {
};
}

export function contractInfo(network) {
const addresses = addressMapping[network];
return contractAddressesInfo(addresses);
}

export const TESTNET_ID = 999;

export default [
{ name: 'mainnet', networkId: 1, contracts: contractInfo('mainnet') },
{ name: 'morden', networkId: 2 },
{ name: 'ropsten', networkId: 3 },
{ name: 'rinkeby', networkId: 4 },
{
name: 'rinkeby',
networkId: 4
},
{
name: 'goerli',
networkId: 5
},

{ name: 'kovan', networkId: 42, contracts: contractInfo('kovan') },
{ name: 'test', networkId: 1337, contracts: contractInfo('testnet') },
{ name: 'test', networkId: TESTNET_ID, contracts: contractInfo('testnet') }
Expand Down
12 changes: 8 additions & 4 deletions packages/dai/src/eth/SmartContractService.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PrivateService } from '@makerdao/services-core';
import contracts from '../../contracts/contracts';
import tokens from '../../contracts/tokens';
import networks from '../../contracts/networks';
import networks, { contractAddressesInfo } from '../../contracts/networks';
import { Contract } from 'ethers';
import { wrapContract } from './smartContract/wrapContract';
import mapValues from 'lodash/mapValues';
Expand Down Expand Up @@ -116,21 +116,25 @@ export default class SmartContractService extends PrivateService {
const mapping = networks.find(m => m.name === networkName);
assert(mapping, `Network "${networkName}" not found in mapping.`);

if (!mapping.contracts)
mapping.contracts = contractAddressesInfo(this._addressOverrides);

if (!this._contractInfoCache) this._contractInfoCache = {};
if (!this._contractInfoCache[networkName]) {
const allContractInfo = this._addedContracts
? { ...mapping.contracts, ...this._addedContracts }
? {
...mapping.contracts,
...this._addedContracts
}
: mapping.contracts;

this._contractInfoCache[networkName] = mapValues(
allContractInfo,
(versions, name) => {
const latest = findLatestContractInfo(versions);

const address =
getSingleAddress(this._addressOverrides[name], networkName) ||
getSingleAddress(latest.address, networkName);

return address !== latest.address
? versions.map(v => (v === latest ? { ...latest, address } : v))
: versions;
Expand Down
18 changes: 18 additions & 0 deletions packages/dai/test/eth/SmartContractService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,21 @@ test('fallback if addressOverrides does not specify current network', async () =
const addresses = service.getContractAddresses();
expect(addresses.PROXY_REGISTRY).toEqual(originalAddresses.PROXY_REGISTRY);
});

test('can use address overrides for contract info', async () => {
const service = buildTestService('smartContract', {
smartContract: {
addressOverrides: {
PROXY_REGISTRY: {
rinkeby: '0xmock1'
}
}
}
});

service.get('web3').networkId = () => 4;
service.get('web3');
await service.manager().authenticate();
const addresses = service.getContractAddresses();
expect(addresses.PROXY_REGISTRY).toEqual('0xmock1');
});

0 comments on commit 4506f9c

Please sign in to comment.