Skip to content

Commit

Permalink
fix: enable contract parallelism
Browse files Browse the repository at this point in the history
  • Loading branch information
cawfree committed Nov 5, 2022
1 parent 553821b commit f2817b5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 31 deletions.
4 changes: 2 additions & 2 deletions apps/web/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {Main as Environment} from 'foundry';
const {rpcUrl} = Environment;

const { chains, provider } = configureChains(
[chain.mainnet, chain.polygon, chain.optimism, chain.arbitrum, chain.localhost],
[chain.localhost],
[
jsonRpcProvider({
rpc: () => ({http: rpcUrl}),
Expand All @@ -34,7 +34,7 @@ const {connectors} = getDefaultWallets({
});

const wagmiClient = createClient({
autoConnect: true,
autoConnect: false,
connectors,
provider
})
Expand Down
1 change: 0 additions & 1 deletion apps/web/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,5 @@ export default function Main(): JSX.Element {
</article>
</div>
</div>

);
}
65 changes: 37 additions & 28 deletions packages/foundry/scripts/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,38 @@ import {ethers} from 'ethers';

const ANVIL_DEFAULT_WALLET_PRIVATE_KEY_DO_NOT_USE_YOU_WILL_GET_REKT = '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80';

const deploy = async ({
contractName,
url,
}: {
readonly contractName: string;
const deployContractsByName = async ({contractNames, url}: {
readonly contractNames: readonly string[];
readonly url: string;
}) => {

const provider = new ethers.providers.JsonRpcProvider(url);
const wallet = new ethers.Wallet(ANVIL_DEFAULT_WALLET_PRIVATE_KEY_DO_NOT_USE_YOU_WILL_GET_REKT, provider);

const {abi, bytecode} = JSON.parse(fs.readFileSync(
path.resolve('..', '..', 'packages', 'foundry', 'out', `${contractName}.sol`, `${contractName}.json`),
'utf-8'
));

const factory = new ethers.ContractFactory(abi, bytecode, wallet);
const contractNameToDeploymentAddress: Record<string, string> = Object.fromEntries(
await Promise.all(
contractNames.map(async contractName => {
const {abi, bytecode} = JSON.parse(fs.readFileSync(
path.resolve('..', '..', 'packages', 'foundry', 'out', `${contractName}.sol`, `${contractName}.json`),
'utf-8'
));
const {address} = await new ethers.ContractFactory(abi, bytecode, wallet).deploy();
return [contractName, address];
})
),
);
return {contractNameToDeploymentAddress};
};

const contract = await factory.deploy();
const {address: deploymentAddress} = contract;
const deployContractsAndWriteModule = async ({
contractNames,
url,
}: {
readonly contractNames: readonly string[];
readonly url: string;
}) => {
const {
contractNameToDeploymentAddress,
} = await deployContractsByName({contractNames, url});

if (!fs.existsSync(path.resolve('dist'))) fs.mkdirSync('dist');

Expand All @@ -35,12 +47,6 @@ const deploy = async ({
`
import {ethers, Wallet} from 'ethers';
// @ts-expect-error missing declaration
const {abi, bytecode} = ${JSON.stringify(JSON.parse(fs.readFileSync(
path.resolve('..', 'foundry', 'out', `${contractName}.sol`, `${contractName}.json`),
'utf-8',
)))};
const ANVIL_DEFAULT_WALLET_PRIVATE_KEY_DO_NOT_USE_YOU_WILL_GET_REKT = "${ANVIL_DEFAULT_WALLET_PRIVATE_KEY_DO_NOT_USE_YOU_WILL_GET_REKT}";
const deployEtherFromFaucet = async ({
Expand All @@ -65,18 +71,21 @@ const deployEtherFromFaucet = async ({
);
return provider.sendTransaction(signedTransaction);
};
${contractNames.map((contractName: string) => `
export const ${contractName} = Object.freeze({
abi,
bytecode,
...${JSON.stringify(JSON.parse(fs.readFileSync(
path.resolve('..', 'foundry', 'out', `${contractName}.sol`, `${contractName}.json`),
'utf-8',
)))},
rpcUrl: "${url}",
contractAddress: "${deploymentAddress}",
contractAddress: "${contractNameToDeploymentAddress[contractName]}",
deployEtherFromFaucet,
});
`.trim(),
)}
`.trim(),
);

console.log(`Deployed ${contractName}.sol to: ${deploymentAddress}!`);
};

const pipe = (
Expand All @@ -94,8 +103,8 @@ void (async () => Promise.all([

// Wait a little time to spin up the deployment.
new Promise(resolve => setTimeout(resolve, 1000))
.then(() => deploy({
contractName: 'Main',
.then(() => deployContractsAndWriteModule({
contractNames: ['Main'],
url: 'http://localhost:8545',
})),
]))();

0 comments on commit f2817b5

Please sign in to comment.