Skip to content

Commit

Permalink
Initialize usernames
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastijankuzner committed Nov 21, 2024
1 parent 84003c4 commit 31da706
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
78 changes: 77 additions & 1 deletion packages/evm-consensus/source/deployer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { inject, injectable, tagged } from "@mainsail/container";
import { Contracts, Identifiers } from "@mainsail/contracts";
import { ConsensusAbi, ERC1967ProxyAbi } from "@mainsail/evm-contracts";
import { ConsensusAbi, ERC1967ProxyAbi, UsernamesAbi } from "@mainsail/evm-contracts";
import { Utils } from "@mainsail/kernel";
import { ethers, sha256 } from "ethers";

Expand Down Expand Up @@ -36,6 +36,9 @@ export class Deployer {
const consensusContractAddress = await this.#deployConsensusContract();
await this.#deployConsensusProxy(consensusContractAddress);

const usernamesContractAddress = await this.#deployUsernamesContract();
await this.#deployUsernamesProxy(usernamesContractAddress);

await this.evm.onCommit({
...this.#getBlockContext().commitKey,
getBlock: () => ({ data: { round: BigInt(0) } }),
Expand Down Expand Up @@ -153,4 +156,77 @@ export class Deployer {
.bind(EvmConsensusIdentifiers.Contracts.Addresses.Consensus)
.toConstantValue(proxyResult.receipt.deployedContractAddress!);
}

async #deployUsernamesContract(): Promise<string> {
const result = await this.evm.process({
blockContext: this.#getBlockContext(),
caller: this.deployerAddress,
data: Buffer.concat([Buffer.from(ethers.getBytes(UsernamesAbi.bytecode.object))]),
gasLimit: BigInt(10_000_000),
nonce: BigInt(2),
specId: this.#getSpecId(),
txHash: this.#generateTxHash(),
value: 0n,
});

if (!result.receipt.success) {
throw new Error("failed to deploy Usernames contract");
}

if (
result.receipt.deployedContractAddress !== ethers.getCreateAddress({ from: this.deployerAddress, nonce: 2 })
) {
throw new Error("Contract address mismatch");
}

this.logger.info(
`Deployed Usernames contract from ${this.deployerAddress} to ${result.receipt.deployedContractAddress}`,
);

return result.receipt.deployedContractAddress!;
}

async #deployUsernamesProxy(usernamesContractAddress: string): Promise<void> {
// Logic contract initializer function ABI
const logicInterface = new ethers.Interface(UsernamesAbi.abi);
// Encode the initializer call
const initializerCalldata = logicInterface.encodeFunctionData("initialize");
// Prepare the constructor arguments for the proxy contract
const proxyConstructorArguments = new ethers.AbiCoder()
.encode(["address", "bytes"], [usernamesContractAddress, initializerCalldata])
.slice(2);

const proxyResult = await this.evm.process({
blockContext: this.#getBlockContext(),
caller: this.deployerAddress,
data: Buffer.concat([
Buffer.from(ethers.getBytes(ERC1967ProxyAbi.bytecode.object)),
Buffer.from(proxyConstructorArguments, "hex"),
]),
gasLimit: BigInt(10_000_000),
nonce: BigInt(3),
specId: this.#getSpecId(),
txHash: this.#generateTxHash(),
value: 0n,
});

if (!proxyResult.receipt.success) {
throw new Error("failed to deploy Usernames PROXY contract");
}

if (
proxyResult.receipt.deployedContractAddress !==
ethers.getCreateAddress({ from: this.deployerAddress, nonce: 3 })
) {
throw new Error("Contract address mismatch");
}

this.logger.info(
`Deployed Usernames PROXY contract from ${this.deployerAddress} to ${proxyResult.receipt.deployedContractAddress}`,
);

this.app
.bind(EvmConsensusIdentifiers.Contracts.Addresses.Usernames)
.toConstantValue(proxyResult.receipt.deployedContractAddress!);
}
}
1 change: 1 addition & 0 deletions packages/evm-consensus/source/identifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export const Identifiers = {
Contracts: {
Addresses: {
Consensus: Symbol.for("Evm.Consensus<Contracts.Addresses.Consensus>"),
Usernames: Symbol.for("Evm.Consensus<Contracts.Addresses.Usernames>"),
},
},
Internal: {
Expand Down

0 comments on commit 31da706

Please sign in to comment.