diff --git a/packages/dai-plugin-mcd/contracts/addresses/kovan.json b/packages/dai-plugin-mcd/contracts/addresses/kovan.json index d5bdefe91..3a36c3d2a 100644 --- a/packages/dai-plugin-mcd/contracts/addresses/kovan.json +++ b/packages/dai-plugin-mcd/contracts/addresses/kovan.json @@ -68,7 +68,8 @@ "MCD_JOIN_KNC_A": "0xE42427325A0e4c8e194692FfbcACD92C2C381598", "MCD_FLIP_KNC_A": "0xf14Ec3538C86A31bBf576979783a8F6dbF16d571", - "MCD_JOIN_PSM_USDC_A": "0xc2FB16712AD538EbcBabB00DfAC5fa511A4cc657", - "MCD_FLIP_PSM_USDC_A": "0x481d4eb969a821B6A8137D9c352aA7a7cBc4C8F2", - "PSM_USDC_A": "0xE13eF5966031A50A8df9D7612366a56e3BB36CB1" + "MCD_FLIP_PSM_USDC_A": "0x481d4eb969a821B6A8137D9c352aA7a7cBc4C8F2" + "MCD_JOIN_PSM_USDC_A": "0xaf774866c4b165b88c558C7891290698cf7Ed6B5", + "PSM_USDC_A": "0xAfB79e262B7A07666161B95992934eB0AE230Fb4", + } diff --git a/packages/dai-plugin-mcd/src/PsmService.js b/packages/dai-plugin-mcd/src/PsmService.js deleted file mode 100644 index 29b1749c6..000000000 --- a/packages/dai-plugin-mcd/src/PsmService.js +++ /dev/null @@ -1,26 +0,0 @@ -import assert from 'assert'; -import { PublicService } from '@makerdao/services-core'; -import { ServiceRoles } from './constants'; -import tracksTransactions from './utils/tracksTransactions'; - -export default class PsmService extends PublicService { - constructor(name = ServiceRoles.PSM) { - super(name, ['smartContract', 'web3', ServiceRoles.CDP_TYPE]); - } - - async getFeeIn() {} - async getFeeOut() {} - - /* - * gemAmount - */ - @tracksTransactions - async join(gemAmount, { promise }) {} - - @tracksTransactions - async exit(amountInDai, { promise }) {} - - get _psm() { - return this.get('smartContract').getContract('PSM'); - } -} diff --git a/packages/dai-plugin-mcd/src/PsmType.js b/packages/dai-plugin-mcd/src/PsmType.js new file mode 100644 index 000000000..41c81265e --- /dev/null +++ b/packages/dai-plugin-mcd/src/PsmType.js @@ -0,0 +1,37 @@ +import assert from 'assert'; +import { stringToBytes } from './utils'; +import tracksTransactions from './utils/tracksTransactions'; + +export default class PsmType { + constructor( + psmTypeService, + { currency, ilk, decimals }, + options = { prefect: true } + ) { + assert(currency && ilk, 'currency and ilk are required'); + + this._psmTypeService = psmTypeService; + this.currency = currency; + this.decimals = decimals; + this.ilk = ilk; + this._ilkBytes = stringToBytes(this.ilk); + this.cache = {}; + if (options.prefetch) this.prefetch(); + } + + get feeIn() {} + + get feeOut() {} + + prefetch() {} + + async reset() { + this._prefetchPromise = null; + this.cache = {}; + } + + _getCached(name) { + assert(this.cache[name], `${name} is not cached`); + return this.cache[name]; + } +} diff --git a/packages/dai-plugin-mcd/src/PsmTypeService.js b/packages/dai-plugin-mcd/src/PsmTypeService.js new file mode 100644 index 000000000..4a7ba6934 --- /dev/null +++ b/packages/dai-plugin-mcd/src/PsmTypeService.js @@ -0,0 +1,27 @@ +import { PublicService } from '@makerdao/services-core'; +import { ServiceRoles } from './constants'; +import PsmType from './PsmType'; + +const { PSM_TYPE } = ServiceRoles; + +export default class PsmService extends PublicService { + constructor(name = PSM_TYPE) { + super(name); + this.reset = this.resetAllPsmTypes; + } + + initialize(settings = {}) { + this.settings = settings; + this.psmTypes = (settings.psmTypes || []).map( + psmType => new PsmType(this, psmType, { prefetch: settings.prefetch }) + ); + } + + async connect() { + if (this.settings.prefetch) await this.prefetchAllCdpTypes(); + } + + async prefetchAllPsmTypes() { + await Promise.all(this.psmTypes.map(type => type.prefetch())); + } +} diff --git a/packages/dai-plugin-mcd/src/constants.js b/packages/dai-plugin-mcd/src/constants.js index 0c6c8c087..d83136af6 100644 --- a/packages/dai-plugin-mcd/src/constants.js +++ b/packages/dai-plugin-mcd/src/constants.js @@ -6,7 +6,7 @@ export const ServiceRoles = { AUCTION: 'mcd:auction', SYSTEM_DATA: 'mcd:systemData', SAVINGS: 'mcd:savings', - PSM: 'mcd:psm' + PSM_TYPE: 'mcd:psmType' }; export const WAD = new BigNumber('1e18'); diff --git a/packages/dai-plugin-mcd/src/index.js b/packages/dai-plugin-mcd/src/index.js index ee2be9896..3006a55c3 100644 --- a/packages/dai-plugin-mcd/src/index.js +++ b/packages/dai-plugin-mcd/src/index.js @@ -12,12 +12,21 @@ import SavingsService from './SavingsService'; import CdpTypeService from './CdpTypeService'; import AuctionService from './AuctionService'; import SystemDataService from './SystemDataService'; +import PsmService from './PsmService'; + import { ServiceRoles as ServiceRoles_ } from './constants'; import BigNumber from 'bignumber.js'; import wethAbi from '../contracts/abis/WETH9.json'; export const ServiceRoles = ServiceRoles_; -const { CDP_MANAGER, CDP_TYPE, SYSTEM_DATA, AUCTION, SAVINGS } = ServiceRoles; +const { + CDP_MANAGER, + CDP_TYPE, + SYSTEM_DATA, + AUCTION, + SAVINGS, + PSM_TYPE +} = ServiceRoles; // look up contract ABIs using abiMap. // if an exact match is not found, prefix-match against keys ending in *, e.g. @@ -83,6 +92,10 @@ export const defaultCdpTypes = [ { currency: ZRX, ilk: 'ZRX-A', decimals: 18 } ]; +export const defaultPsmTypes = [ + { currency: USDC, ilk: 'PSM-USDC-A', decimals: 6 } +]; + export const SAI = createCurrency('SAI'); export const ALLOWANCE_AMOUNT = BigNumber( @@ -102,7 +115,12 @@ export const defaultTokens = [ export const McdPlugin = { addConfig: ( _, - { cdpTypes = defaultCdpTypes, addressOverrides, prefetch = true } = {} + { + cdpTypes = defaultCdpTypes, + psmTypes = defaultPsmTypes, + addressOverrides, + prefetch = true + } = {} ) => { if (addressOverrides) { addContracts = mapValues(addContracts, (contractDetails, name) => ({ @@ -141,13 +159,15 @@ export const McdPlugin = { CDP_TYPE, AUCTION, SYSTEM_DATA, - SAVINGS + SAVINGS, + PSM_TYPE ], [CDP_TYPE]: [CdpTypeService, { cdpTypes, prefetch }], [CDP_MANAGER]: CdpManager, [SAVINGS]: SavingsService, [AUCTION]: AuctionService, - [SYSTEM_DATA]: SystemDataService + [SYSTEM_DATA]: SystemDataService, + [PSM_TYPE]: [PsmService, { psmTypes, prefetch }] }; } };