Skip to content

Commit

Permalink
fix: scripts on custom chains (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
bl0up authored Dec 5, 2024
1 parent 9510c51 commit b68c1a5
Show file tree
Hide file tree
Showing 11 changed files with 346 additions and 115 deletions.
34 changes: 0 additions & 34 deletions ignition/modules/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,4 @@ const MetaDogModule = buildModule('MetaDogModule', (m) => {
return { metadog };
});

const MetaDogReserve = buildModule('MetaDogReserve', (m) => {
const metadogAddress = m.getParameter('address');
const metadog = m.contractAt('MetaDog', metadogAddress);
m.call(metadog, 'collectReserves');

return { metadog };
});

const MetaDogPresale = buildModule('MetaDogPresale', (m) => {
const metadogAddress = m.getParameter('address');
const whitelistRoot = m.getParameter('whitelistRoot');
const metadog = m.contractAt('MetaDog', metadogAddress);
m.call(metadog, 'setWhitelistMerkleRoot', [whitelistRoot]);
return { metadog };
});

const MetaDogPublicSale = buildModule('MetaDogPublicSale', (m) => {
const metadogAddress = m.getParameter('address');
const metadog = m.contractAt('MetaDog', metadogAddress);
m.call(metadog, 'startPublicSale');

return { metadog };
});

const MetaDogReveal = buildModule('MetaDogReveal', (m) => {
const metadogAddress = m.getParameter('address');
const revealTokenURI = m.getParameter('revealTokenURI');
const metadog = m.contractAt('MetaDog', metadogAddress);

m.call(metadog, 'setBaseURI', [`ipfs://${revealTokenURI}/`]);
return { metadog };
});

export default MetaDogModule;
export { MetaDogReserve, MetaDogPresale, MetaDogPublicSale, MetaDogReveal };
11 changes: 11 additions & 0 deletions ignition/modules/metaDogPresale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { buildModule } from '@nomicfoundation/hardhat-ignition/modules';

const MetaDogPresale = buildModule('MetaDogPresale', (m) => {
const metadogAddress = m.getParameter('address');
const whitelistRoot = m.getParameter('whitelistRoot');
const metadog = m.contractAt('MetaDog', metadogAddress);
m.call(metadog, 'setWhitelistMerkleRoot', [whitelistRoot]);
return { metadog };
});

export default MetaDogPresale;
11 changes: 11 additions & 0 deletions ignition/modules/metaDogPublicSale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { buildModule } from '@nomicfoundation/hardhat-ignition/modules';

const MetaDogPublicSale = buildModule('MetaDogPublicSale', (m) => {
const metadogAddress = m.getParameter('address');
const metadog = m.contractAt('MetaDog', metadogAddress);
m.call(metadog, 'startPublicSale');

return { metadog };
});

export default MetaDogPublicSale;
11 changes: 11 additions & 0 deletions ignition/modules/metaDogReserve.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { buildModule } from '@nomicfoundation/hardhat-ignition/modules';

const MetaDogReserve = buildModule('MetaDogReserve', (m) => {
const metadogAddress = m.getParameter('address');
const metadog = m.contractAt('MetaDog', metadogAddress);
m.call(metadog, 'collectReserves');

return { metadog };
});

export default MetaDogReserve;
12 changes: 12 additions & 0 deletions ignition/modules/metaDogReveal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { buildModule } from '@nomicfoundation/hardhat-ignition/modules';

const MetaDogReveal = buildModule('MetaDogReveal', (m) => {
const metadogAddress = m.getParameter('address');
const revealTokenURI = m.getParameter('revealTokenURI');
const metadog = m.contractAt('MetaDog', metadogAddress);

m.call(metadog, 'setBaseURI', [`ipfs://${revealTokenURI}/`]);
return { metadog };
});

export default MetaDogReveal;
78 changes: 61 additions & 17 deletions scripts/collectReserved.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,75 @@
import { readFileSync } from 'fs';
import hre, { network } from 'hardhat';
import { MetaDogReserve } from '../ignition/modules/main';
import { execSync } from 'child_process';
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
import { network, run } from 'hardhat';
import path from 'path';

async function main() {
// Check if the image collection exists
const collectionExists = await run('check-images');

if (!collectionExists) {
throw new Error('You have not created any assets.');
}

// Get the chain ID
const chainIdHex = await network.provider.send('eth_chainId');
const chainId = String(parseInt(chainIdHex, 16));

// Read the deployed addresses JSON to get the MetaDog contract address
const deployedAddressesPath = `./ignition/deployments/chain-${chainId}/deployed_addresses.json`;
let address: string;
try {
const jsonData = JSON.parse(readFileSync(deployedAddressesPath, 'utf8'));
address = jsonData['MetaDogModule#MetaDog'];
if (!address) {
throw new Error(
`MetaDogModule address not found in ${deployedAddressesPath}`
);
}
} catch (err) {
console.error('Error reading deployed addresses:', err);
throw err;
}

console.log(`MetaDog contract address: ${address}`);

// Prepare the parameters
const parameters = {
MetaDogReserve: {
address,
},
};

// Define the parameter file path
const dirPath = path.resolve(__dirname, '../ignition/parameters');
const filePath = path.resolve(dirPath, 'metaDogReserve.json');

// Ensure the parameters directory exists
if (!existsSync(dirPath)) {
mkdirSync(dirPath, { recursive: true });
console.log(`Directory created: ${dirPath}`);
}

// Write the parameters to the file
writeFileSync(filePath, JSON.stringify(parameters, null, 2));
console.log(`Parameters written to ${filePath}:`);
console.log(JSON.stringify(parameters, null, 2));

// Construct the deployment command
const modulePath = path.resolve(
__dirname,
'../ignition/modules/metaDogReserve.ts'
);
const command = `npx hardhat ignition deploy ${modulePath} --parameters ${filePath} --network btp`;

console.log(`Executing deployment: ${command}`);

// Execute the deployment command
try {
const jsonData = JSON.parse(
readFileSync(
`./ignition/deployments/chain-${chainId}/deployed_addresses.json`,
'utf8'
)
);
const address = jsonData['MetaDogModule#MetaDog'];
const { metadog } = await hre.ignition.deploy(MetaDogReserve, {
parameters: {
MetaDogReserve: { address: address },
},
});
execSync(command, { stdio: 'inherit' });
console.log('MetaDogReserve deployed successfully.');
} catch (err) {
console.error('Error:', err);
console.error('Error during deployment:', err);
throw err;
}
}

Expand Down
44 changes: 36 additions & 8 deletions scripts/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import hre from 'hardhat';
import { default as MetaDogModule } from '../ignition/modules/main';

import { execSync } from 'child_process';
import fs from 'fs';
import { network } from 'hardhat';
import path from 'path';

async function main() {
const chainIdHex = await network.provider.send('eth_chainId');
Expand All @@ -16,12 +16,40 @@ async function main() {
const placeholder: string = await run('placeholder', {
amount: 1,
});
const { metadog } = await hre.ignition.deploy(MetaDogModule, {
parameters: {
MetaDogModule: { placeholder: placeholder, proxyaddress: proxyaddress },

// Prepare the parameters
const parameters = {
MetaDogModule: {
placeholder,
proxyaddress,
},
});
console.log('Contract deployed');
};

// Define the parameter file path
const dirPath = path.resolve(__dirname, '../ignition/parameters');
const filePath = path.resolve(dirPath, 'metaDogDeploy.json');

// Ensure the directory exists
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
console.log(`Directory created: ${dirPath}`);
}

// Write the parameters to the file
fs.writeFileSync(filePath, JSON.stringify(parameters, null, 2));
console.log(`Parameters written to ${filePath}:`);
console.log(JSON.stringify(parameters, null, 2));

// Construct the deployment command
const modulePath = path.resolve(__dirname, '../ignition/modules/main.ts');
const command = `npx hardhat ignition deploy ${modulePath} --parameters ${filePath} --network btp`;

console.log(`Executing deployment: ${command}`);

// Execute the deployment command
execSync(command, { stdio: 'inherit' });

console.log('MetaDogModule deployed successfully.');
}

main().catch(console.error);
94 changes: 73 additions & 21 deletions scripts/presale.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,88 @@
import { readFileSync } from 'fs';
import hre, { network } from 'hardhat';
import { MetaDogPresale } from '../ignition/modules/main';
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs';
import hre, { network, run } from 'hardhat';
import path from 'path';
import { execSync } from 'child_process';

async function main() {
// Check if the image collection exists
const collectionExists = await run('check-images');

if (!collectionExists) {
throw new Error('You have not created any assets.');
}

// Get the chain ID
const chainIdHex = await network.provider.send('eth_chainId');
const chainId = String(parseInt(chainIdHex, 16));

const whitelist: {
root: string;
proofs: string[];
} = JSON.parse(readFileSync('./assets/generated/whitelist.json', 'utf8'));

// Read the whitelist JSON file
let whitelist: { root: string; proofs: string[] };
try {
const jsonData = JSON.parse(
readFileSync(
`./ignition/deployments/chain-${chainId}/deployed_addresses.json`,
'utf8'
)
whitelist = JSON.parse(
readFileSync('./assets/generated/whitelist.json', 'utf8')
);
const address = jsonData['MetaDogModule#MetaDog'];
const { metadog } = await hre.ignition.deploy(MetaDogPresale, {
parameters: {
MetaDogPresale: { address: address, whitelistRoot: whitelist.root },
},
});
} catch (err) {
console.error('Error:', err);
console.error('Error reading whitelist file:', err);
throw err;
}
console.log(`Whitelist root: ${whitelist.root}`);

// Read the deployed addresses JSON to get the MetaDog contract address
const deployedAddressesPath = `./ignition/deployments/chain-${chainId}/deployed_addresses.json`;
let address: string;
try {
const jsonData = JSON.parse(readFileSync(deployedAddressesPath, 'utf8'));
address = jsonData['MetaDogModule#MetaDog'];
if (!address) {
throw new Error(
`MetaDogModule address not found in ${deployedAddressesPath}`
);
}
} catch (err) {
console.error('Error reading deployed addresses:', err);
throw err;
}

console.log(`MetaDog contract address: ${address}`);

// Prepare the parameters
const parameters = {
MetaDogPresale: {
address,
whitelistRoot: whitelist.root,
},
};

// Define the parameter file path
const dirPath = path.resolve(__dirname, '../ignition/parameters');
const filePath = path.resolve(dirPath, 'metadogPresale.json');

// Ensure the parameters directory exists
if (!existsSync(dirPath)) {
mkdirSync(dirPath, { recursive: true });
console.log(`Directory created: ${dirPath}`);
}

// Write the parameters to the file
writeFileSync(filePath, JSON.stringify(parameters, null, 2));
console.log(`Parameters written to ${filePath}:`);
console.log(JSON.stringify(parameters, null, 2));

// Construct the deployment command
const modulePath = path.resolve(
__dirname,
'../ignition/modules/metaDogPresale.ts'
);
const command = `npx hardhat ignition deploy ${modulePath} --parameters ${filePath} --network btp`;

console.log(`Executing deployment: ${command}`);

// Execute the deployment command
try {
execSync(command, { stdio: 'inherit' });
console.log('MetaDogPresale deployed successfully.');
} catch (err) {
console.error('Error during deployment:', err);
throw err;
}
}

Expand Down
Loading

0 comments on commit b68c1a5

Please sign in to comment.