Skip to content

Commit

Permalink
feat(FindContractVersion): update FindContractVersion to build contra…
Browse files Browse the repository at this point in the history
…ct hashes during core build process (UMAprotocol#2873)
  • Loading branch information
chrismaree authored Apr 21, 2021
1 parent f05b0ea commit 4e5a3bd
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 28 deletions.
1 change: 0 additions & 1 deletion packages/common/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ module.exports = {
...require("./src/TimeUtils"),
...require("./src/VotingUtils"),
...require("./src/PriceIdentifierUtils"),
...require("./src/FindContractVersion"),
...require("./src/MultiVersionTestHelpers.js")
};
11 changes: 5 additions & 6 deletions packages/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const fs = require("fs");
const path = require("path");
const { getWeb3 } = require("@uma/common");

const { findContractVersion } = require("./src/FindContractVersion");

/**
* @notice Gets the directory for version of core specified by an input version string.
* @param {String} version Version string in the form of x.y.z.
Expand Down Expand Up @@ -133,12 +135,9 @@ if (global.artifacts) {
module.exports = {
getAbi: getAbiTest,
getAddress: getAddressTest,
getTruffleContract: getTruffleContractTest
getTruffleContract: getTruffleContractTest,
findContractVersion
};
} else {
module.exports = {
getAbi,
getAddress,
getTruffleContract
};
module.exports = { getAbi, getAddress, getTruffleContract, findContractVersion };
}
3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
"mocha-test": "mocha ./mocha-test --recursive",
"load-addresses": "yarn run apply-registry",
"clean": "rm -rf build",
"build": "yarn truffle compile && yarn load-addresses && tsc",
"buildLatestHardhatVersionHashes": "yarn hardhat run ./src/BuildContractVersionHashes.js",
"build": "yarn truffle compile && yarn load-addresses && tsc && yarn buildLatestHardhatVersionHashes",
"prepublish": "yarn build"
},
"bugs": {
Expand Down
127 changes: 127 additions & 0 deletions packages/core/src/BuildContractVersionHashes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
const assert = require("assert");
const path = require("path");
const fs = require("fs");

const { toWei, utf8ToHex, padRight, soliditySha3 } = web3.utils;

const { createConstructorParamsForContractVersion, interfaceName } = require("@uma/common");
const { getTruffleContract } = require("../index");

async function buildHashes(contractType) {
assert(contractType == "Perpetual" || contractType == "ExpiringMultiParty", "Invalid contract type defined!");

const contractCreator = (await web3.eth.getAccounts())[0];

const FinancialContract = getTruffleContract(contractType, web3);
const Finder = getTruffleContract("Finder", web3);
const IdentifierWhitelist = getTruffleContract("IdentifierWhitelist", web3);
const AddressWhitelist = getTruffleContract("AddressWhitelist", web3);
const MockOracle = getTruffleContract("MockOracle", web3);
const Token = getTruffleContract("ExpandedERC20", web3);
const SyntheticToken = getTruffleContract("SyntheticToken", web3);
const Timer = getTruffleContract("Timer", web3);
const Store = getTruffleContract("Store", web3);
const ConfigStore = getTruffleContract("ConfigStore", web3);
const OptimisticOracle = getTruffleContract("OptimisticOracle", web3);

const identifier = "TEST_IDENTIFIER";
const fundingRateIdentifier = "TEST_FUNDING_IDENTIFIER";

const finder = await Finder.new({ from: contractCreator });

const identifierWhitelist = await IdentifierWhitelist.new({ from: contractCreator });
await identifierWhitelist.addSupportedIdentifier(utf8ToHex(identifier), { from: contractCreator });

await finder.changeImplementationAddress(utf8ToHex(interfaceName.IdentifierWhitelist), identifierWhitelist.address, {
from: contractCreator
});

const timer = await Timer.new({ from: contractCreator });

const mockOracle = await MockOracle.new(finder.address, timer.address, { from: contractCreator });
await finder.changeImplementationAddress(utf8ToHex(interfaceName.Oracle), mockOracle.address, {
from: contractCreator
});

const store = await Store.new({ rawValue: "0" }, { rawValue: "0" }, timer.address, { from: contractCreator });
await finder.changeImplementationAddress(utf8ToHex(interfaceName.Store), store.address, { from: contractCreator });

await finder.changeImplementationAddress(utf8ToHex(interfaceName.FinancialContractsAdmin), contractCreator, {
from: contractCreator
});

const syntheticToken = await SyntheticToken.new("Test Synthetic Token", "SYNTH", 18, { from: contractCreator });
const collateralToken = await Token.new("Wrapped Ether", "WETH", 18, { from: contractCreator });

const collateralWhitelist = await AddressWhitelist.new({ from: contractCreator });
await finder.changeImplementationAddress(utf8ToHex(interfaceName.CollateralWhitelist), collateralWhitelist.address, {
from: contractCreator
});
await collateralWhitelist.addToWhitelist(collateralToken.address, { from: contractCreator });

let configStore, optimisticOracle;
if (contractType == "Perpetual") {
configStore = await ConfigStore.new(
{
timelockLiveness: 86400, // 1 day
rewardRatePerSecond: { rawValue: "0" },
proposerBondPercentage: { rawValue: "0" },
maxFundingRate: { rawValue: toWei("0.00001") },
minFundingRate: { rawValue: toWei("-0.00001") },
proposalTimePastLimit: 0
},
timer.address,
{ from: contractCreator }
);

await identifierWhitelist.addSupportedIdentifier(padRight(utf8ToHex(fundingRateIdentifier)), {
from: contractCreator
});
optimisticOracle = await OptimisticOracle.new(7200, finder.address, timer.address, { from: contractCreator });
await finder.changeImplementationAddress(utf8ToHex(interfaceName.OptimisticOracle), optimisticOracle.address, {
from: contractCreator
});
}

const constructorParams = await createConstructorParamsForContractVersion(
{ contractVersion: "latest", contractType },
{
convertSynthetic: toWei,
finder,
collateralToken,
syntheticToken,
identifier,
fundingRateIdentifier,
timer,
store,
configStore: configStore || {}
},
{ expirationTimestamp: (await timer.getCurrentTime()).toNumber() + 100 }, // config override expiration time.
{ from: contractCreator }
);

const financialContract = await FinancialContract.new(constructorParams, { from: contractCreator });
const contractCode = await web3.eth.getCode(financialContract.address);

return soliditySha3(contractCode);
}

function saveContractHashArtifacts(contractHashes) {
const savePath = `${path.resolve(__dirname)}/../build/contract-type-hash-map.json`;
fs.writeFileSync(savePath, JSON.stringify(contractHashes));
}

async function main() {
const contractHashesToGenerate = ["Perpetual", "ExpiringMultiParty"];
let versionMap = {};
for (const contractType of contractHashesToGenerate) {
const contractHash = await buildHashes(contractType);
versionMap[contractHash] = { contractType, contractVersion: "latest" };
}
console.log("versionMap", versionMap);
saveContractHashArtifacts(versionMap);
}

main().then(() => {
process.exit(0);
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
const assert = require("assert");
const path = require("path");
const fs = require("fs");

let latestVersionMap = {};
try {
latestVersionMap = JSON.parse(fs.readFileSync(`${path.resolve(__dirname)}/../build/contract-type-hash-map.json`));
} catch (error) {
console.log("WARNING: latest version map was not found in the build directory! Run `yarn build` from core first!");
}

/**
* Get the version and type of a financial contract deployed using the official UMA contract factories.
Expand Down Expand Up @@ -50,11 +59,6 @@ const versionMap = {
contractType: "ExpiringMultiParty",
contractVersion: "1.2.2"
},
"0x87b2de625ae5a42d5ac38269993143805cea5ec7297c17c71007d148a4627cae": {
// latest ExpiringMultiParty deployed from hardhat tests.
contractType: "ExpiringMultiParty",
contractVersion: "latest"
},
"0x1f75b3ae77a4a3b91fefd81264ec94751dcceafb02d42d2250a209385cdee39a": {
// Latest Mainnet ExpiringMultiParty.
contractType: "ExpiringMultiParty",
Expand All @@ -66,17 +70,11 @@ const versionMap = {
contractVersion: "latest"
},
"0x7202352fa756f41d3b4646441b82271ab44909e6e24c12326fb73f34e6ca2aa9": {
// latest Perpetual deployed from hardhat tests.
contractType: "Perpetual",
contractVersion: "latest"
},
"0x238569485842107d2e938ff59c78841860b4dcd00d37be9859699f2c4ddbb3a0": {
// Latest Mainnet Perpetual contract.
contractType: "Perpetual",
contractVersion: "latest"
}
},
...latestVersionMap // latest versions built from hard hat. This makes this utility work out of the box with "latest".
};

module.exports = {
findContractVersion
};
module.exports = { findContractVersion };
4 changes: 2 additions & 2 deletions packages/disputer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require("dotenv").config();
const retry = require("async-retry");

// Helpers
const { findContractVersion, SUPPORTED_CONTRACT_VERSIONS } = require("@uma/common");
const { SUPPORTED_CONTRACT_VERSIONS } = require("@uma/common");

// JS libs
const { Disputer } = require("./src/disputer");
Expand All @@ -21,7 +21,7 @@ const {
} = require("@uma/financial-templates-lib");

// Truffle contracts.
const { getAbi } = require("@uma/core");
const { getAbi, findContractVersion } = require("@uma/core");
const { getWeb3, PublicNetworks } = require("@uma/common");

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/liquidator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require("dotenv").config();
const retry = require("async-retry");

// Helpers
const { getWeb3, findContractVersion, SUPPORTED_CONTRACT_VERSIONS, PublicNetworks } = require("@uma/common");
const { getWeb3, SUPPORTED_CONTRACT_VERSIONS, PublicNetworks } = require("@uma/common");
// JS libs
const { Liquidator } = require("./src/liquidator");
const { ProxyTransactionWrapper } = require("./src/proxyTransactionWrapper");
Expand All @@ -22,7 +22,7 @@ const {
} = require("@uma/financial-templates-lib");

// Contract ABIs and network Addresses.
const { getAbi, getAddress } = require("@uma/core");
const { getAbi, getAddress, findContractVersion } = require("@uma/core");

/**
* @notice Continuously attempts to liquidate positions in the Financial Contract contract.
Expand Down
4 changes: 2 additions & 2 deletions packages/monitors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ const { CRMonitor } = require("./src/CRMonitor");
const { SyntheticPegMonitor } = require("./src/SyntheticPegMonitor");

// Contract ABIs and network Addresses.
const { getAbi, getAddress } = require("@uma/core");
const { getWeb3, findContractVersion, SUPPORTED_CONTRACT_VERSIONS, PublicNetworks } = require("@uma/common");
const { getAbi, getAddress, findContractVersion } = require("@uma/core");
const { getWeb3, SUPPORTED_CONTRACT_VERSIONS, PublicNetworks } = require("@uma/common");

/**
* @notice Continuously attempts to monitor contract positions and reports based on monitor modules.
Expand Down

0 comments on commit 4e5a3bd

Please sign in to comment.