Skip to content

Commit

Permalink
feat: working distribution example
Browse files Browse the repository at this point in the history
  • Loading branch information
idea404 committed Nov 10, 2023
1 parent c79eb49 commit 1a66eeb
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
21 changes: 21 additions & 0 deletions contracts/PensionAccount.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ contract PensionAccount is IAccount {
// Expiry timestamp
uint256 public expiryTimestamp;

// State variables to track investments
uint256 public totalEthReceived;
mapping(address => uint256) public investmentPerToken;

// Event for swap action
event Swap(address indexed token, uint256 amountToSwap);

Expand All @@ -48,12 +52,29 @@ contract PensionAccount is IAccount {

receive() external payable {
uint256 amountToSwap = msg.value / 4;
totalEthReceived += msg.value; // Track total ETH received

// Update investment for each token
investmentPerToken[DOGE] += amountToSwap;
investmentPerToken[PEPE] += amountToSwap;
investmentPerToken[SHIB] += amountToSwap;
investmentPerToken[BTC] += amountToSwap;

// Emit Swap events (optional, can be removed if events are not supported)
emit Swap(DOGE, amountToSwap);
emit Swap(PEPE, amountToSwap);
emit Swap(SHIB, amountToSwap);
emit Swap(BTC, amountToSwap);
}

// View function to get investment details
function getInvestmentDetails() external view returns (uint256 ethReceived, uint256 dogeInvestment, uint256 pepeInvestment, uint256 shibInvestment, uint256 btcInvestment) {
ethReceived = totalEthReceived;
dogeInvestment = investmentPerToken[DOGE];
pepeInvestment = investmentPerToken[PEPE];
shibInvestment = investmentPerToken[SHIB];
btcInvestment = investmentPerToken[BTC];
}

// Override the executeTransaction function to include the time lock
function executeTransaction(
Expand Down
30 changes: 29 additions & 1 deletion test/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,35 @@ describe("Account Abstraction Tests", function () {
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);
expect(balance).to.be.greaterThan(9.99);
});

it("Should distribute investments correctly", async function () {
// Send 10 ETH to the pension account
const sendAmount = ethers.utils.parseUnits("20", 18);
await firstRichWallet.transfer({
to: pensionAccountContract.address,
amount: sendAmount,
overrides: { type: 113 },
});

// Call getInvestmentDetails to get investment distribution
const investmentDetails = await pensionAccountContract.getInvestmentDetails();

// Check total ETH received
const ethReceived = parseFloat(ethers.utils.formatEther(investmentDetails.ethReceived));
expect(ethReceived).to.equal(120);

// Check distribution to each token (30 ETH each)
const expectedInvestmentPerToken = 30;
const dogeInvestment = parseFloat(ethers.utils.formatEther(investmentDetails.dogeInvestment));
expect(dogeInvestment).to.equal(expectedInvestmentPerToken);
const pepeInvestment = parseFloat(ethers.utils.formatEther(investmentDetails.pepeInvestment));
expect(pepeInvestment).to.equal(expectedInvestmentPerToken);
const shibInvestment = parseFloat(ethers.utils.formatEther(investmentDetails.shibInvestment));
expect(shibInvestment).to.equal(expectedInvestmentPerToken);
const btcInvestment = parseFloat(ethers.utils.formatEther(investmentDetails.btcInvestment));
expect(btcInvestment).to.equal(expectedInvestmentPerToken);
});
});
});
Expand Down
6 changes: 4 additions & 2 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export async function deployMultisig(wallet: Wallet, factoryAddress: string, own

export async function deployPension(wallet: Wallet, factoryAddress: string, walletOwner: Wallet) {
const paFactoryArtifact = await hre.artifacts.readArtifact("PensionAccountFactory");
const accountArtifact = await hre.artifacts.readArtifact("PensionAccount");

const paFactory = new ethers.Contract(factoryAddress, paFactoryArtifact.abi, wallet);

// Contract constructor args
Expand All @@ -61,14 +63,14 @@ export async function deployPension(wallet: Wallet, factoryAddress: string, wall

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

const pensionAccountContract = new ethers.Contract(multisigAddress, paFactoryArtifact.abi, wallet);
const pensionAccountContract = new ethers.Contract(contractAddress, accountArtifact.abi, wallet);
return pensionAccountContract;
}

Expand Down

0 comments on commit 1a66eeb

Please sign in to comment.