Skip to content

Commit

Permalink
Merge branch 'main' of github.com:bobanetwork/aa-hc-example
Browse files Browse the repository at this point in the history
  • Loading branch information
wsdt committed Aug 15, 2024
2 parents 1ad7246 + 2a89677 commit 9986339
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 171 deletions.
217 changes: 48 additions & 169 deletions contracts/script/deploy-local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
DEFAULT_SNAP_VERSION, getContractFromDeployAddresses,
getLocalIpAddress,
isPortInUse,
parseDeployAddresses
parseDeployAddresses, updateEnvVariable
} from "./utils";
import {execPromise} from './utils'
import {readHybridAccountAddress} from "./utils";
Expand All @@ -17,65 +17,29 @@ const deployAddr = ethers.getAddress("0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
const deployKey = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80";
const bundlerAddr = ethers.getAddress("0xB834a876b7234eb5A45C0D5e693566e8842400bB");
const builderPrivkey = "0xf91be07ef5a01328015cae4f2e5aefe3c4577a90abb8e2e913fe071b0e3732ed";
const clientOwner = ethers.getAddress("0x77Fe14A710E33De68855b0eA93Ed8128025328a9");
const clientPrivkey = "0x541b3e3b20b8bb0e5bae310b2d4db4c8b7912ba09750e6ff161b7e67a26a9bf7";
const ha0Owner = ethers.getAddress("0x2A9099A58E0830A4Ab418c2a19710022466F1ce7");
const ha0Privkey = "0x75cd983f0f4714969b152baa258d849473732905e2301467303dacf5a09fdd57";
const ha1Owner = ethers.getAddress("0xE073fC0ff8122389F6e693DD94CcDc5AF637448e");
const ha1Privkey = "0x7c0c629efc797f8c5f658919b7efbae01275470d59d03fdeb0fca1e6bd11d7fa";


/** @DEV Configurations */
const snapEnv = '../snap-account-abstraction-keyring/packages/snap/.env-local'
const l2Provider = new ethers.JsonRpcProvider('http://localhost:9545');
const l2Wallet = new ethers.Wallet(deployKey, l2Provider);
let aaConfigFile = fs.readFileSync('../snap-account-abstraction-keyring/packages/snap/src/constants/aa-config.ts', 'utf8');


const updateEnvVariable = (key: string, value: string, envPath: string) => {
console.log(`Updating ${key} = ${value}`)
let envFile = fs.readFileSync(envPath, "utf8");
const regex = new RegExp(`^${key}=.*`, "m");
if (regex.test(envFile)) {
envFile = envFile.replace(regex, `${key}=${value}`);
} else {
envFile += `\n${key}=${value}`;
}
fs.writeFileSync(envPath, envFile);
dotenv.config();
};

// TODO: fix .env file loading. Currently .env needs to be in /script directory
async function main() {
try {
if (!isPortInUse(8545) && !isPortInUse(9545)) {
await execPromise(
"pnpm install",
[],
path.resolve(__dirname, "../../boba")
);

await execPromise(
"make devnet-hardhat-up",
[],
path.resolve(__dirname, "../../boba")
);
await execPromise("pnpm install", [], path.resolve(__dirname, "../../boba"));
await execPromise("make devnet-hardhat-up", [], path.resolve(__dirname, "../../boba"));
} else {
console.log("Boba Chain already running, skipping")
}
const fundL2Vars = {
...process.env,
PRIVATE_KEY: deployKey,
};
const fundL2Vars = {...process.env, PRIVATE_KEY: deployKey,};

await execPromise(
"node fundL2.js",
undefined,
path.resolve(__dirname, "../script/"),
fundL2Vars
);

console.log("Funding L2 done...");
await execPromise("node fundL2.js", undefined, path.resolve(__dirname, "../script/"), fundL2Vars);

const BACKEND_URL = process.env.BACKEND_URL ?? `http://${getLocalIpAddress()}:1234/rpc`
if (!process.env.BACKEND_URL) {
Expand Down Expand Up @@ -105,153 +69,68 @@ async function main() {
const tokenPriceAddress = getContractFromDeployAddresses(contracts, "TokenPrice");
const tokenPaymasterAddress = getContractFromDeployAddresses(contracts, "TokenPaymaster");
const verifyingPaymasterContract = getContractFromDeployAddresses(contracts, "VerifyingPaymaster");
const hybridAccountAddr = readHybridAccountAddress(latestBroadcast);

if (!hcHelperAddr || !hybridAccountAddr || !haFactory || !tokenPriceAddress || !tokenPaymasterAddress || !verifyingPaymasterContract || !saFactory) {
throw Error("Some contracts are not defined!");
}

const entrypoint = contracts?.find((c) => c.contractName === "EntryPoint")?.address
const envVars = {
HC_HELPER_ADDR: hcHelperAddr,
HC_SYS_ACCOUNT: hybridAccountAddr,
HC_SYS_OWNER: ha0Owner,
HC_SYS_PRIVKEY: ha0Privkey,
ENTRY_POINTS: entrypoint,
BUILDER_PRIVKEY: builderPrivkey,
NODE_HTTP: `http://${getLocalIpAddress()}:9545`,
CHAIN_ID: "901",
};

await execPromise(
"docker compose up -d --build rundler-hc",
[],
path.resolve(__dirname, "../../rundler-hc/hybrid-compute/"),
{...process.env, ...envVars}
);
const hybridAccountAddr = readHybridAccountAddress(latestBroadcast);

// Frontend env vars
// Paths
const frontendEnvPath = path.resolve(__dirname, "../../frontend/.env-local");
updateEnvVariable(
"VITE_SMART_CONTRACT",
tokenPriceAddress,
frontendEnvPath
);
updateEnvVariable(
"VITE_RPC_PROVIDER",
"http://localhost:9545",
frontendEnvPath
);
updateEnvVariable(
"VITE_SNAP_ORIGIN",
"local:http://localhost:8080",
frontendEnvPath
);
updateEnvVariable(
"VITE_SNAP_VERSION",
DEFAULT_SNAP_VERSION,
frontendEnvPath
);

console.log("Frontend ENV vars set...");

// Backend env vars
const backendEnvPath = path.resolve(__dirname, "../../backend/.env");
updateEnvVariable(
"OC_HYBRID_ACCOUNT",
hybridAccountAddr,
backendEnvPath
);
const contractsEnvPath = path.resolve(__dirname, "../.env");

if (!entrypoint) {
throw Error("Entrypoint not defined!")
if (!hcHelperAddr || !hybridAccountAddr || !haFactory || !tokenPriceAddress || !tokenPaymasterAddress || !verifyingPaymasterContract || !saFactory || !entrypoint) {
throw Error("Some contracts are not defined!");
}

updateEnvVariable(
"ENTRY_POINTS",
entrypoint,
backendEnvPath
);

/** @DEV Build Rundler with passed envs */
await execPromise("docker compose up -d --build rundler-hc", [],
path.resolve(__dirname, "../../rundler-hc/hybrid-compute/"), {...process.env, ...{
HC_HELPER_ADDR: hcHelperAddr,
HC_SYS_ACCOUNT: hybridAccountAddr,
HC_SYS_OWNER: ha0Owner,
HC_SYS_PRIVKEY: ha0Privkey,
ENTRY_POINTS: entrypoint,
BUILDER_PRIVKEY: builderPrivkey,
NODE_HTTP: `http://${getLocalIpAddress()}:9545`,
CHAIN_ID: "901",
}}
);

/** @DEV Frontend Environment */
updateEnvVariable("VITE_SMART_CONTRACT", tokenPriceAddress, frontendEnvPath);
updateEnvVariable("VITE_RPC_PROVIDER", "http://localhost:9545", frontendEnvPath);
updateEnvVariable("VITE_SNAP_ORIGIN", "local:http://localhost:8080", frontendEnvPath);
updateEnvVariable("VITE_SNAP_VERSION", DEFAULT_SNAP_VERSION, frontendEnvPath);

/** @DEV Backend Environment */
updateEnvVariable("OC_HYBRID_ACCOUNT", hybridAccountAddr, backendEnvPath);
updateEnvVariable("ENTRY_POINTS", entrypoint, backendEnvPath);
updateEnvVariable("CHAIN_ID", "901", backendEnvPath);
updateEnvVariable("OC_PRIVKEY", deployKey, backendEnvPath);
updateEnvVariable(
"HC_HELPER_ADDR",
hcHelperAddr,
backendEnvPath
);

console.log("Backend ENV vars set...");
updateEnvVariable("HC_HELPER_ADDR", hcHelperAddr, backendEnvPath);

// Contract env vars
const ENTRYPOINT = contracts?.find((c) => c.contractName === "EntryPoint")?.address ?? ""
const contractsEnvPath = path.resolve(__dirname, "../.env");
updateEnvVariable(
"HYBRID_ACCOUNT",
hybridAccountAddr,
contractsEnvPath
);
updateEnvVariable(
"ENTRY_POINT",
ENTRYPOINT,
contractsEnvPath
);
updateEnvVariable(
"TOKEN_PRICE_CONTRACT",
tokenPriceAddress,
contractsEnvPath
);
updateEnvVariable(
"HC_HELPER_ADDR",
hcHelperAddr,
contractsEnvPath
);
/** @DEV Contracts Environment */
updateEnvVariable("HYBRID_ACCOUNT", hybridAccountAddr, contractsEnvPath);
updateEnvVariable("ENTRY_POINT", entrypoint, contractsEnvPath);
updateEnvVariable("TOKEN_PRICE_CONTRACT", tokenPriceAddress, contractsEnvPath);
updateEnvVariable("HC_HELPER_ADDR", hcHelperAddr, contractsEnvPath);
updateEnvVariable("PRIVATE_KEY", deployKey, contractsEnvPath);
updateEnvVariable(
"CLIENT_ADDR",
contracts?.find((c) => c.contractName === "SimpleAccount")?.address ?? "",
contractsEnvPath
);
updateEnvVariable("CLIENT_ADDR", contracts?.find((c) => c.contractName === "SimpleAccount")?.address ?? "", contractsEnvPath);

/** @DEV SNAP Environment */
const localConfigRegex = /(\[CHAIN_IDS\.LOCAL\]:\s*{[\s\S]*?entryPoint:\s*')([^']*)(\'[\s\S]*?simpleAccountFactory:\s*')([^']*)(\'[\s\S]*?bobaPaymaster:\s*')([^']*)(\'[\s\S]*?})/;

aaConfigFile = aaConfigFile.replace(localConfigRegex, (match, before1, oldEntryPoint, middle1, oldSimpleAccountFactory, middle2, oldBobaPaymaster, after) => {
return `${before1}${ENTRYPOINT}${middle1}${haFactory}${middle2}${tokenPaymasterAddress}${after}`;
return `${before1}${entrypoint}${middle1}${haFactory}${middle2}${tokenPaymasterAddress}${after}`;
});

// Update the AA-config
fs.writeFileSync('../snap-account-abstraction-keyring/packages/snap/src/constants/aa-config.ts', aaConfigFile, 'utf8');

// Update LOCAL_ENTRYPOINT
updateEnvVariable(
"LOCAL_ENTRYPOINT",
ENTRYPOINT,
snapEnv
);

// Update Account Factory
updateEnvVariable(
"LOCAL_SIMPLE_ACCOUNT_FACTORY",
saFactory, // here we actually need the sa factory
snapEnv
);

// Update Account Factory
updateEnvVariable(
"VERIFYING_PAYMASTER_ADDRESS",
verifyingPaymasterContract,
snapEnv
);

// Update Account Factory
updateEnvVariable(
"LOCAL_BOBAPAYMASTER",
tokenPaymasterAddress,
snapEnv
);
updateEnvVariable("LOCAL_ENTRYPOINT", entrypoint, snapEnv);
updateEnvVariable("LOCAL_SIMPLE_ACCOUNT_FACTORY", saFactory, snapEnv);
updateEnvVariable("VERIFYING_PAYMASTER_ADDRESS", verifyingPaymasterContract, snapEnv);
updateEnvVariable("LOCAL_BOBAPAYMASTER", tokenPaymasterAddress, snapEnv);

/** @DEV bootstrap frontend, backend and snap */
await execPromise(
"docker-compose -f docker-compose.local.yml up --build",
"docker-compose -f docker-compose.local.yml up -d",
[],
path.resolve(__dirname, "../../")
);
Expand Down
4 changes: 2 additions & 2 deletions contracts/script/deploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ contract DeployExample is Script {
// use block number to always deploy fresh HA & SA
hybridAccount = haf.createAccount(deployerAddress, block.number);

tokenPrice = new TokenPrice(hybridAccount);
tokenPrice = new TokenPrice(payable(hybridAccount));
hybridAccount.PermitCaller(address(tokenPrice), true);

verifyingPaymaster = new VerifyingPaymaster(entrypoint, address(deployerAddress));
tokenPaymaster = new TokenPaymaster(haf, entrypoint, deployerAddress);
tokenPaymaster = new TokenPaymaster(address(haf), "sym", IEntryPoint(entrypoint));

entrypoint.depositTo{value: 0.1 ether}(address(hybridAccount)); // only needed for HA
verifyingPaymaster.deposit{value: 0.1 ether}();
Expand Down
1 change: 1 addition & 0 deletions contracts/script/fundL2.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ async function main() {
};
const response = await walletL1.sendTransaction(tx);
await response.wait();
console.log("Funding L2 done...");
} catch (e) {
console.error("Error: ", e);
}
Expand Down

0 comments on commit 9986339

Please sign in to comment.