From 43e5cd8fd2a5023ebb30df8fdb67ffb5eacfb363 Mon Sep 17 00:00:00 2001 From: cckelly21 Date: Tue, 23 Oct 2018 17:44:44 -0400 Subject: [PATCH] refactor(): remove proxyExists --- README.md | 12 ------------ commerce.js | 25 ++++++++++++------------- library.js | 16 +++++++--------- purchase.js | 16 +++++++--------- registry.js | 19 ++++++++----------- rewards.js | 26 +------------------------- 6 files changed, 35 insertions(+), 79 deletions(-) diff --git a/README.md b/README.md index a54d251..0ea33bc 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,6 @@ The contracts in this repository are deployed on [Ara Privatenet](https://github ### Registry -* [registry.proxyExists(contentDid)](#proxyexists) * [registry.getProxyAddress(contentDid)](#getproxy) * [registry.upgradeProxy(opts)](#upgrade) * [registry.deployProxy(opts)](#deploy) @@ -112,17 +111,6 @@ await purchase({ }) ``` - -### `registry.proxyExists(contentDid)` - -Checks if the proxy for a content `DID` exists. - -- `contentDid` - The `DID` of the content to check - -```js -const exists = await registry.proxyExists(contentDid) -``` - ### `registry.getProxyAddress(contentDid)` diff --git a/commerce.js b/commerce.js index a613a39..c51eea0 100644 --- a/commerce.js +++ b/commerce.js @@ -1,4 +1,5 @@ const { abi } = require('./build/contracts/AFS.json') +const { getProxyAddress } = require('./registry') const hasDIDMethod = require('has-did-method') const { AID_PREFIX } = require('./constants') @@ -14,11 +15,6 @@ const { } } = require('ara-util') -const { - getProxyAddress, - proxyExists -} = require('./registry') - /** * Requests ownership of an AFS. * @param {Object} opts @@ -103,11 +99,13 @@ async function approveOwnershipTransfer(opts) { Ensure ${newOwnerDid} is a valid Ara identity.`) } - if (!(await proxyExists(did))) { - throw new Error('Content does not have a valid proxy contract') + let proxy + try { + proxy = await getProxyAddress(did) + } catch (err) { + throw err } - - const proxy = await getProxyAddress(did) + let owner = getDocumentOwner(ddo, true) owner = `${AID_PREFIX}${owner}` @@ -170,12 +168,13 @@ async function _updateOwnershipRequest(opts, functionName = '') { Ensure ${requesterDid} is a valid Ara identity.`) } - if (!(await proxyExists(contentDid))) { - throw new Error('Content does not have a valid proxy contract') + let proxy + try { + proxy = await getProxyAddress(contentDid) + } catch (err) { + throw err } - const proxy = await getProxyAddress(contentDid) - if (!hasDIDMethod(requesterDid)) { requesterDid = `${AID_PREFIX}${requesterDid}` } diff --git a/library.js b/library.js index b7637a0..d8fce6f 100644 --- a/library.js +++ b/library.js @@ -3,11 +3,7 @@ const { abi: libAbi } = require('./build/contracts/Library.json') const { abi: proxyAbi } = require('./build/contracts/AFS.json') const { LIBRARY_ADDRESS } = require('./constants') - -const { - proxyExists, - getProxyAddress -} = require('./registry') +const { getProxyAddress } = require('./registry') const { hashDID, @@ -119,11 +115,13 @@ async function hasPurchased(opts) { } const { purchaserDid, contentDid, keyringOpts } = opts - if (!(await proxyExists(contentDid))) { - throw new Error('This content does not have a valid proxy contract') - } - const proxy = await getProxyAddress(contentDid) + let proxy + try { + proxy = await getProxyAddress(contentDid) + } catch (err) { + throw err + } let purchaser = await getAddressFromDID(purchaserDid, keyringOpts) if (!isAddress(purchaser)) { diff --git a/purchase.js b/purchase.js index 0e34a8a..50c07f0 100644 --- a/purchase.js +++ b/purchase.js @@ -1,14 +1,10 @@ const { abi: afsAbi } = require('./build/contracts/AFS.json') const debug = require('debug')('ara-contracts:purchase') +const { getProxyAddress } = require('./registry') const { randomBytes } = require('ara-crypto') const { AID_PREFIX } = require('./constants') const token = require('./token') -const { - proxyExists, - getProxyAddress -} = require('./registry') - const { hasPurchased, getLibrarySize, @@ -87,11 +83,13 @@ async function purchase(opts) { throw new Error('Identity has already purchased this') } - if (!(await proxyExists(contentDid))) { - throw new Error('This content does not have a valid proxy contract') + let proxy + try { + proxy = await getProxyAddress(contentDid) + } catch (err) { + throw err } - - const proxy = await getProxyAddress(contentDid) + let price = await call({ abi: afsAbi, address: proxy, diff --git a/registry.js b/registry.js index 9b4ae2a..2ac11bb 100644 --- a/registry.js +++ b/registry.js @@ -1,6 +1,7 @@ const { abi } = require('./build/contracts/Registry.json') const debug = require('debug')('ara-contracts:registry') const { parse, resolve } = require('path') +const { deprecate } = require('util') const solc = require('solc') const fs = require('fs') @@ -26,15 +27,6 @@ const { } } = require('ara-util') -async function proxyExists(contentDid = '') { - try { - const address = await getProxyAddress(contentDid) - return !/^0x0+$/.test(address) - } catch (err) { - return false - } -} - /** * Gets the proxy contract address for contentDid * @param {String} contentDid //unhashed @@ -48,8 +40,9 @@ async function getProxyAddress(contentDid = '') { contentDid = normalize(contentDid) + let address try { - return call({ + address = await call({ abi, address: REGISTRY_ADDRESS, functionName: 'getProxyAddress', @@ -57,9 +50,14 @@ async function getProxyAddress(contentDid = '') { ethify(contentDid) ] }) + if (!/^0x0+$/.test(address)) { + throw new Error('Content does not have a valid proxy.') + } } catch (err) { throw err } + + return address } async function getProxyVersion(contentDid = '') { @@ -430,7 +428,6 @@ async function deployNewStandard(opts) { } module.exports = { - proxyExists, deployProxy, getStandard, upgradeProxy, diff --git a/rewards.js b/rewards.js index ace80bd..5ca68cc 100644 --- a/rewards.js +++ b/rewards.js @@ -1,6 +1,7 @@ const { abi: tokenAbi } = require('./build/contracts/AraToken.json') const { abi: afsAbi } = require('./build/contracts/AFS.json') const debug = require('debug')('ara-contracts:rewards') +const { getProxyAddress } = require('./registry') const { info } = require('ara-console') const token = require('./token') @@ -10,11 +11,6 @@ const { AID_PREFIX } = require('./constants') -const { - proxyExists, - getProxyAddress -} = require('./registry') - const { validate, normalize, @@ -105,10 +101,6 @@ async function submit(opts) { let receipt try { - if (!(await proxyExists(contentDid))) { - throw new Error('This content does not have a valid proxy contract') - } - const proxy = await getProxyAddress(contentDid) budget = budget.toString() @@ -253,10 +245,6 @@ async function allocate(opts) { debug(did, 'allocating rewards for job:', jobId) try { - if (!(await proxyExists(contentDid))) { - throw new Error('This content does not have a valid proxy contract') - } - const proxy = await getProxyAddress(contentDid) const allocateTx = await tx.create({ account: acct, @@ -333,10 +321,6 @@ async function redeem(opts) { let balance = 0 try { - if (!(await proxyExists(contentDid))) { - throw new Error('This content does not have a valid proxy contract') - } - const proxy = await getProxyAddress(contentDid) const redeemTx = await tx.create({ @@ -395,10 +379,6 @@ async function getBudget(opts) { contentDid = normalize(contentDid) try { - if (!(await proxyExists(contentDid))) { - throw new Error('This content does not have a valid proxy contract') - } - const proxy = await getProxyAddress(contentDid) const budget = await call({ abi: afsAbi, @@ -451,10 +431,6 @@ async function getRewardsBalance(opts) { const { address } = await account.load({ did, password }) try { - if (!(await proxyExists(contentDid))) { - throw new Error('This content does not have a valid proxy contract') - } - const proxy = await getProxyAddress(contentDid) const balance = await call({ abi: afsAbi,