Skip to content

Commit

Permalink
feat: simple pension account tests
Browse files Browse the repository at this point in the history
  • Loading branch information
idea404 committed Nov 10, 2023
1 parent 9d8ca6c commit c79eb49
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
22 changes: 21 additions & 1 deletion test/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expect } from "chai";
import * as hre from "hardhat";
import { ethers } from "ethers";
import * as zks from "zksync-web3";
import { deployFactory, deployMultisig, fundAccount, MultiSigWallet } from "./utils";
import { deployFactory, deployMultisig, fundAccount, MultiSigWallet, deployPension } from "./utils";

const config = {
firstWalletPrivateKey: "0x7726827caac94a7f9e1b160f7ea819f172f7b6f9d2a97f992c38edeab82d4110",
Expand Down Expand Up @@ -96,5 +96,25 @@ describe("Account Abstraction Tests", function () {
expect(result).to.contains("0x");
});
});

describe("Pension Account", function () {
const ownerWallet = zks.Wallet.createRandom();
let pensionAccountContract: ethers.Contract;
before(async function () {
pensionAccountContract = await deployPension(firstRichWallet, factoryContract.address, ownerWallet);
await fundAccount(firstRichWallet, pensionAccountContract.address);
});

it("Should have a tx hash that starts from 0x", async function () {
result = factoryContract.deployTransaction.hash;
expect(result).to.contains("0x");
});

it("Should have a balance", async function () {
const result = await pensionAccountContract.provider.getBalance(pensionAccountContract.address);
const balance = parseFloat(ethers.utils.formatEther(result));
expect(balance).to.be.greaterThan(99.99);
});
});
});
});
29 changes: 16 additions & 13 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,31 @@ export async function deployMultisig(wallet: Wallet, factoryAddress: string, own
return accountContract;
}

export async function deployAccount(wallet: Wallet, factoryAddress: string, accountOwnerPublicKey: string) {
export async function deployPension(wallet: Wallet, factoryAddress: string, walletOwner: Wallet) {
const paFactoryArtifact = await hre.artifacts.readArtifact("PensionAccountFactory");
const paFactory = new ethers.Contract(factoryAddress, paFactoryArtifact.abi, wallet);

// Account owner address
const owner = ethers.utils.getAddress(accountOwnerPublicKey);

// Contract constructor args
const dex = "0x123dex";
const doge = "0x123doge";
const pepe = "0x123pepe";
const shib = "0x123shib";
const btc = "0x123btc";
const dex = createMockAddress("decentralizedex"); // "0xdex0000000000000000000000000000000000000"
const doge = createMockAddress("dogecoin"); // "0xdoge000000000000000000000000000000000000"
const pepe = createMockAddress("pepecoin"); // "0xpepe000000000000000000000000000000000000"
const shib = createMockAddress("shibainucoin"); // "0xshib0000000000000000000000000000000000000"
const btc = createMockAddress("bitcoin"); // "0xbtc00000000000000000000000000000000000000"

// For the simplicity of the tutorial, we will use zero hash as salt
const salt = ethers.constants.HashZero;

// deploy account with dex and token addresses
const tx = await paFactory.deployAccount(salt, owner, dex, doge, pepe, shib, btc, { gasLimit: 10000000 });
const tx = await paFactory.deployPensionAccount(salt, walletOwner.address, dex, doge, pepe, shib, btc, { gasLimit: 10000000 });
await tx.wait();

// Getting the address of the deployed contract account
const abiCoder = new ethers.utils.AbiCoder();
let multisigAddress = utils.create2Address(
factoryAddress,
await paFactory.aaBytecodeHash(),
await paFactory.pensionAccountBytecodeHash(),
salt,
abiCoder.encode(["owner", "dex", "doge", "pepe", "shib", "btc"], [owner, dex, doge, pepe, shib, btc])
abiCoder.encode(["address", "address", "address", "address", "address", "address"], [walletOwner.address, dex, doge, pepe, shib, btc])
);

const pensionAccountContract = new ethers.Contract(multisigAddress, paFactoryArtifact.abi, wallet);
Expand Down Expand Up @@ -117,4 +114,10 @@ export class MultiSigWallet extends Wallet {
transaction.customData.customSignature = sig1 + sig2.substring(2);
return (0, utils.serialize)(transaction);
}
}
}

function createMockAddress(base: string) {
const baseHex = base.replace(/[^0-9A-Fa-f]/g, ''); // Remove non-hex characters
const paddingLength = 40 - baseHex.length; // Calculate padding length
return '0x' + baseHex + '0'.repeat(paddingLength);
}

0 comments on commit c79eb49

Please sign in to comment.