Skip to content

Commit

Permalink
Added contracts to DaoArtifacts after deployed
Browse files Browse the repository at this point in the history
  • Loading branch information
fforbeck committed Jul 12, 2021
1 parent a15826c commit 8a1e583
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 26 deletions.
17 changes: 9 additions & 8 deletions migrations/2_deploy_contracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,24 @@ require("dotenv").config();
module.exports = async (deployer, network, accounts) => {
let res;

console.log(`Starting deployment to ${network}`);
console.log(`Deploying tribute-contracts to ${network} network`);

const { contracts } = require(`../deployment/${network}.config`);
const truffleImports = require("../utils/TruffleUtil.js")(contracts);
const DaoArtifacts = truffleImports.DaoArtifacts;

var daoArtifacts = truffleImports.DaoArtifacts;
let daoArtifacts;
if (process.env.DAO_ARTIFACTS_CONTRACT_ADDR) {
console.log(
`Attach to existing DaoArtifacts contract: ${process.env.DAO_ARTIFACTS_CONTRACT_ADDR}`
);
daoArtifacts = await truffleImports.DaoArtifacts.at(
console.log(`Attach to existing DaoArtifacts contract`);
daoArtifacts = await DaoArtifacts.at(
process.env.DAO_ARTIFACTS_CONTRACT_ADDR
);
} else {
daoArtifacts = await deployer.deploy(truffleImports.DaoArtifacts);
console.log(`Deployed new DaoArtifacts contract: ${daoArtifacts.address}`);
console.log(`Creating new DaoArtifacts contract`);
await deployer.deploy(DaoArtifacts);
daoArtifacts = await DaoArtifacts.deployed();
}
console.log(`DaoArtifacts: ${daoArtifacts.address}`);

const deployFunction = truffleImports.deployFunctionFactory(
deployer,
Expand Down
11 changes: 9 additions & 2 deletions truffle-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,17 @@ module.exports = {
rinkeby: {
provider: function () {
let infuraKey = process.env.INFURA_KEY;
let alchemyKey = process.env.ALCHEMY_KEY;

let HDWalletProvider = require("@truffle/hdwallet-provider");
let mnemonic = process.env.TRUFFLE_MNEMONIC;
let infuraUrl = "wss://rinkeby.infura.io/ws/v3/" + infuraKey;
return new HDWalletProvider(mnemonic, infuraUrl);
let url;
if (alchemyKey) {
url = `wss://eth-rinkeby.ws.alchemyapi.io/v2/${alchemyKey}`;
} else {
url = `wss://rinkeby.infura.io/ws/v3/${infuraKey}`;
}
return new HDWalletProvider(mnemonic, url);
},
network_id: 4,
gasPrice: 10000000000,
Expand Down
4 changes: 4 additions & 0 deletions utils/ContractUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const hexToBytes = Web3Utils.hexToBytes;
const toAscii = Web3Utils.toAscii;
const fromAscii = Web3Utils.fromAscii;
const toUtf8 = Web3Utils.toUtf8;
const toHex = Web3Utils.toHex;

const GUILD = "0x000000000000000000000000000000000000dead";
const TOTAL = "0x000000000000000000000000000000000000babe";
Expand All @@ -42,6 +43,7 @@ const UNITS = "0x00000000000000000000000000000000000FF1CE";
const LOOT = "0x00000000000000000000000000000000B105F00D";
const ETH_TOKEN = "0x0000000000000000000000000000000000000000";
const DAI_TOKEN = "0x95b58a6bff3d14b7db2f5cb5f0ad413dc2940658";
const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";

const numberOfUnits = toBN("1000000000000000");
const unitPrice = toBN(toWei("120", "finney"));
Expand All @@ -57,6 +59,7 @@ module.exports = {
toAscii,
fromAscii,
toUtf8,
toHex,
maximumChunks,
numberOfUnits,
unitPrice,
Expand All @@ -69,4 +72,5 @@ module.exports = {
MEMBER_COUNT,
LOOT,
ETH_TOKEN,
ZERO_ADDRESS,
};
70 changes: 54 additions & 16 deletions utils/TruffleUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

const { sha3 } = require("./ContractUtil");
const { sha3, toHex, ZERO_ADDRESS } = require("./ContractUtil");

const { ContractType } = require("../deployment/contracts.config");

Expand All @@ -42,10 +42,8 @@ const getTruffleContracts = (contracts) => {
});
};

const deployFunctionFactory = (deployer, daoArtifacts, allContracts) => {
const deployFunction = (deployer, daoArtifacts, allContracts) => {
const deploy = async (contractInterface, args) => {
console.log("Deploy new contract");
if (!contractInterface) return null;
if (args) {
await deployer.deploy(contractInterface, ...args);
} else {
Expand All @@ -55,26 +53,64 @@ const deployFunctionFactory = (deployer, daoArtifacts, allContracts) => {
};

const loadOrDeploy = async (contractInterface, args) => {
if (!contractInterface) throw Error("Invalid contract interface");

const contractConfig = allContracts.find(
(c) => c.interface === contractInterface
(c) => c.name === contractInterface.contractName
);
console.log(`Contract found: ${contractConfig.name}`);
if (contractConfig.type === ContractType.Test) {
return deploy(contractConfig, contractInterface, args);
if (!contractConfig)
throw Error(
`${contractInterface.contractName} contract not found in deployment/contracts.config`
);

if (
// Always deploy core and test contracts
contractConfig.type === ContractType.Core ||
contractConfig.type === ContractType.Test
) {
return deploy(contractInterface, args);
}

const artifactsOwner = process.env.DAO_ARTIFACTS_OWNER_ADDR
? process.env.DAO_ARTIFACTS_OWNER_ADDR
: process.env.DAO_OWNER_ADDR;

// Attempt to load the contract from the DaoArtifacts to save deploy gas
const address = await daoArtifacts.getArtifactAddress(
sha3(contractConfig.name),
process.env.ARTIFACT_OWNER_ADDR,
contractConfig.version,
artifactsOwner,
toHex(contractConfig.version),
contractConfig.type
);
let contract;
if (address) {
contract = await contractInterface.at(address);
if (address && address !== ZERO_ADDRESS) {
console.log(
`Attached to existing contract ${contractConfig.name}: ${address}`
);
return await contractInterface.at(address);
}
if (contract) return contract;
return deploy(contractConfig, contractInterface, args);

// When the contract is not found in the DaoArtifacts, deploy a new one
const deployedContract = await deploy(contractInterface, args);

if (
// Add the new contract to DaoArtifacts, should not store Core & Test contracts
contractConfig.type === ContractType.Factory ||
contractConfig.type === ContractType.Extension ||
contractConfig.type === ContractType.Adapter ||
contractConfig.type === ContractType.Util
) {
await daoArtifacts.addArtifact(
sha3(contractConfig.name),
toHex(contractConfig.version),
deployedContract.address,
contractConfig.type
);
console.log(
`${contractConfig.type}:${contractConfig.name}:${contractConfig.version}:${deployedContract.address} added to DaoArtifacts`
);
}

return deployedContract;
};

return loadOrDeploy;
Expand All @@ -90,7 +126,9 @@ module.exports = (contracts) => {
return {
...truffleInterfaces,
deployFunctionFactory: (deployer, daoArtifacts) => {
deployFunctionFactory(deployer, daoArtifacts, allContracts);
if (!deployer || !daoArtifacts)
throw Error("Missing deployer or DaoArtifacts contract");
return deployFunction(deployer, daoArtifacts, allContracts);
},
};
};

0 comments on commit 8a1e583

Please sign in to comment.