Skip to content

Commit

Permalink
Migrate TruffleLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
kronosapiens committed Dec 8, 2023
1 parent 65ec64b commit f6b92ba
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 20 deletions.
15 changes: 8 additions & 7 deletions packages/package-utils/TruffleLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,18 @@ class TruffleContractLoader {
return true;
}

constructor({ contractDir } = {}) {
assert(typeof contractDir === "string" && contractDir, "A `contractDir` option must be provided");
this._contractDir = contractDir;
constructor({ contractRoot } = {}) {
assert(typeof contractRoot === "string" && contractRoot, "A `contractRoot` option must be provided");
this._contractRoot = contractRoot;
}

async _load(query = {}) {
const { contractName = "" } = query;
const { contractDir = "", contractName = "" } = query;

assert(!!contractDir, "A `contractDir` option must be provided");
assert(!!contractName, "A `contractName` option must be provided");

const file = path.resolve(this._contractDir, `${contractName}.json`);
const file = path.resolve(this._contractRoot, contractDir, `${contractName}.sol`, `${contractName}.json`);
return new Promise((resolve, reject) => {
jsonfile.readFile(file, (error, contents) => {
let transformed;
Expand All @@ -72,13 +73,13 @@ class TruffleContractLoader {
}

async load(query, requiredProps = DEFAULT_REQUIRED_CONTRACT_PROPS) {
const { contractName, contractAddress, routerName, routerAddress, ...otherQuery } = query;
const { contractDir, contractName, contractAddress, routerName, routerAddress, ...otherQuery } = query;

if (!(contractName || contractAddress)) throw new TypeError("The field `contractName` or `contractAddress` must be supplied");

// Load the contract definition by either the contract name or address
const firstQuery = {
...(contractName ? { contractName } : { contractAddress }),
...(contractName ? { contractDir, contractName } : { contractAddress }),
...otherQuery,
};
const result = await this._load(firstQuery);
Expand Down
20 changes: 12 additions & 8 deletions packages/reputation-miner/ReputationMiner.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ class ReputationMiner {
* @return {Promise}
*/
async initialise(colonyNetworkAddress) {
this.colonyNetworkContractDef = await this.loader.load({ contractName: "IColonyNetwork" }, { abi: true, address: false });
this.repCycleContractDef = await this.loader.load({ contractName: "IReputationMiningCycle" }, { abi: true, address: false });
this.tokenLockingContractDef = await this.loader.load({ contractName: "ITokenLocking" }, { abi: true, address: false });
this.colonyContractDef = await this.loader.load({ contractName: "IColony" }, { abi: true, address: false });
const flags = { abi: true, address: false };

this.colonyNetworkContractDef = await this.loader.load({ contractDir: "colonyNetwork", contractName: "IColonyNetwork" }, flags);
this.repCycleContractDef = await this.loader.load({ contractDir: "reputationMiningCycle", contractName: "IReputationMiningCycle" }, flags);
this.tokenLockingContractDef = await this.loader.load({ contractDir: "tokenLocking", contractName: "ITokenLocking" }, flags);
this.colonyContractDef = await this.loader.load({ contractDir: "colony", contractName: "IColony" }, flags);

this.colonyNetwork = new ethers.Contract(colonyNetworkAddress, this.colonyNetworkContractDef.abi, this.realWallet);
const tokenLockingAddress = await this.colonyNetwork.getTokenLocking();
Expand All @@ -85,12 +87,14 @@ class ReputationMiner {
const metaColony = new ethers.Contract(metaColonyAddress, this.colonyContractDef.abi, this.realWallet);
this.clnyAddress = await metaColony.getToken();


if (!this.useJsTree) {
this.patriciaTreeContractDef = await this.loader.load({ contractName: "PatriciaTree" }, { abi: true, address: false, bytecode: true });
this.patriciaTreeContractDef = await this.loader.load(
{ contractDir: "patriciaTree", contractName: "PatriciaTree" },
{ ...flags, bytecode: true }
);
this.patriciaTreeNoHashContractDef = await this.loader.load(
{ contractName: "PatriciaTreeNoHash" },
{ abi: true, address: false, bytecode: true }
{ contractDir: "patriciaTree", contractName: "PatriciaTreeNoHash" },
{ ...flags, bytecode: true }
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/reputation-miner/patricia-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const IColonyNetwork = artifacts.require("IColonyNetwork");
const PatriciaTree = artifacts.require("PatriciaTree");

const contractLoader = new TruffleLoader({
contractDir: path.resolve(__dirname, "..", "..", "build", "contracts")
contractRoot: path.resolve(__dirname, "../..", "artifacts", "contracts"),
});

contract("Javascript Patricia Tree", accounts => {
Expand Down
2 changes: 1 addition & 1 deletion test-gas-costs/gasCosts.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const IVotingReputation = artifacts.require("IVotingReputation");
const REAL_PROVIDER_PORT = process.env.SOLIDITY_COVERAGE ? 8555 : 8545;

const contractLoader = new TruffleLoader({
contractDir: path.resolve(__dirname, "..", "build", "contracts"),
contractRoot: path.resolve(__dirname, "../..", "artifacts", "contracts"),
});

contract("All", function (accounts) {
Expand Down
2 changes: 1 addition & 1 deletion test/contracts-network/colony-network-recovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const Resolver = artifacts.require("Resolver");
const ContractEditing = artifacts.require("ContractEditing");

const contractLoader = new TruffleLoader({
contractDir: path.resolve(__dirname, "../..", "build", "contracts"),
contractRoot: path.resolve(__dirname, "../..", "artifacts", "contracts"),
});

const REAL_PROVIDER_PORT = process.env.SOLIDITY_COVERAGE ? 8555 : 8545;
Expand Down
2 changes: 1 addition & 1 deletion test/contracts-network/colony-reward-payouts.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const Token = artifacts.require("Token");
const DSRoles = artifacts.require("DSRoles");

const contractLoader = new TruffleLoader({
contractDir: path.resolve(__dirname, "../..", "build", "contracts"),
contractRoot: path.resolve(__dirname, "../..", "artifacts", "contracts"),
});

const REAL_PROVIDER_PORT = process.env.SOLIDITY_COVERAGE ? 8555 : 8545;
Expand Down
2 changes: 1 addition & 1 deletion test/contracts-network/token-locking.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const TestVotingToken = artifacts.require("TestVotingToken");
const Resolver = artifacts.require("Resolver");

const contractLoader = new TruffleLoader({
contractDir: path.resolve(__dirname, "../..", "build", "contracts"),
contractRoot: path.resolve(__dirname, "../..", "artifacts", "contracts"),
});

const REAL_PROVIDER_PORT = process.env.SOLIDITY_COVERAGE ? 8555 : 8545;
Expand Down

0 comments on commit f6b92ba

Please sign in to comment.