Skip to content

Commit

Permalink
withdrawal test not emitting event
Browse files Browse the repository at this point in the history
  • Loading branch information
Uacias committed Dec 14, 2023
1 parent c492be0 commit f4014ab
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 81 deletions.
6 changes: 6 additions & 0 deletions contracts/L2EthToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ contract L2EthToken is IEthToken, ISystemContract {
emit Withdrawal(msg.sender, _l1Receiver, amount);
}

/// @notice Testing purposes withdrawal method, should only emit the withdrawal event.
function withdrawShouldOnlyEmitWithdrawal() external payable {

emit Withdrawal(msg.sender, address(0), 0);
}

/// @notice Initiate the ETH withdrawal, with the sent message. The funds will be available to claim on L1 `finalizeEthWithdrawal` method.
/// @param _l1Receiver The address on L1 to receive the funds.
/// @param _additionalData Additional data to be sent to L1 with the withdrawal.
Expand Down
108 changes: 27 additions & 81 deletions test/L2EthToken.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { prepareEnvironment, setResult } from "./shared/mocks";
describe("L2EthToken tests", () => {
let walletFrom: Wallet;
let walletTo: Wallet;
let l2EthToken: L2EthToken;

Check failure on line 15 in test/L2EthToken.spec.ts

View workflow job for this annotation

GitHub Actions / lint

'l2EthToken' is assigned a value but never used
let anotherL2EthToken: L2EthToken;
let bootloaderAccount: ethers.Signer;
let l1Receiver: Wallet;
Expand All @@ -21,47 +22,32 @@ describe("L2EthToken tests", () => {
walletFrom = getWallets()[0];
walletTo = getWallets()[1];
l1Receiver = getWallets()[2];
l2EthToken = L2EthToken__factory.connect(TEST_ETH_TOKEN_SYSTEM_CONTRACT_ADDRESS, walletFrom);
anotherL2EthToken = (await deployContract("L2EthToken")) as L2EthToken;
bootloaderAccount = await ethers.getImpersonatedSigner(TEST_BOOTLOADER_FORMAL_ADDRESS);
});

it("withdraw", async () => {
const message: string = ethers.utils.defaultAbiCoder.encode(["address"], [l1Receiver.address]);
await setResult("L1Messenger", "sendToL1", [message], {
failure: false,
returnData: ethers.utils.defaultAbiCoder.encode(["bytes32"], [ethers.utils.keccak256(message)]),
});

const amountToWidthdraw: BigNumber = ethers.utils.parseEther("1.0");
const gasPrice: BigNumber = await ethers.provider.getGasPrice();

const tx = await anotherL2EthToken
.connect(walletFrom)
.withdraw(l1Receiver.address, { value: amountToWidthdraw, gasLimit: 5000000, gasPrice })
.then((tx) => tx.wait());

await expect(tx)
.to.emit(anotherL2EthToken, "Withdrawal")
.withArgs(walletFrom.address, l1Receiver.address, amountToWidthdraw);
it("withdraw should only emit Withdrawal", async () => {
await expect(anotherL2EthToken.connect(walletFrom).withdrawShouldOnlyEmitWithdrawal()).to.emit(
anotherL2EthToken,
"Withdrawal"
);
});

it("withdraw2", async () => {
it("withdraw", async () => {
const message: string = ethers.utils.defaultAbiCoder.encode(["address"], [l1Receiver.address]);
await setResult("L1Messenger", "sendToL1", [message], {
failure: false,
returnData: ethers.utils.defaultAbiCoder.encode(["bytes32"], [ethers.utils.keccak256(message)]),
});

const amountToWidthdraw: BigNumber = ethers.utils.parseEther("1.0");
const amountToWithdraw: BigNumber = ethers.utils.parseEther("1.0");
const gasPrice: BigNumber = await ethers.provider.getGasPrice();
await expect(
await anotherL2EthToken
anotherL2EthToken
.connect(walletFrom)
.withdraw(l1Receiver.address, { value: amountToWidthdraw, gasLimit: 5000000, gasPrice })
.then((tx) => tx.wait())
)
.to.emit(anotherL2EthToken, "XD")
.withArgs(walletFrom.address, l1Receiver.address, amountToWidthdraw);
.withdraw(l1Receiver.address, { value: amountToWithdraw, gasLimit: 5000000, gasPrice })
).to.emit(anotherL2EthToken, "Withdrawal");
});

it("withdrawWithMessage", async () => {
Expand All @@ -78,30 +64,20 @@ describe("L2EthToken tests", () => {
const amountToWithdraw: BigNumber = ethers.utils.parseEther("1.0");
const gasPrice: BigNumber = await ethers.provider.getGasPrice();
await expect(
await anotherL2EthToken
.connect(walletFrom)
.withdrawWithMessage(l1Receiver.address, additionalData, {
value: amountToWithdraw,
gasLimit: 5000000,
gasPrice,
})
.then((tx) => tx.wait())
)
.to.emit(anotherL2EthToken, "WithdrawalWithMessage")
.withArgs(walletFrom.address, l1Receiver.address, amountToWithdraw, additionalData);
anotherL2EthToken.connect(walletFrom).withdrawWithMessage(l1Receiver.address, additionalData, {
value: amountToWithdraw,
gasLimit: 5000000,
gasPrice,
})
).to.emit(anotherL2EthToken, "WithdrawalWithMessage");
});

it("mint", async () => {
const initialSupply: BigNumber = await anotherL2EthToken.totalSupply();
const initialBalanceOfWallet: BigNumber = await anotherL2EthToken.balanceOf(walletFrom.address);
const amountToMint: BigNumber = ethers.utils.parseEther("10.0");

await expect(
await anotherL2EthToken
.connect(bootloaderAccount)
.mint(walletFrom.address, amountToMint)
.then((tx) => tx.wait())
)
await expect(anotherL2EthToken.connect(bootloaderAccount).mint(walletFrom.address, amountToMint))
.to.emit(anotherL2EthToken, "Mint")
.withArgs(walletFrom.address, amountToMint);

Expand All @@ -120,14 +96,11 @@ describe("L2EthToken tests", () => {

const amountToTransfer = ethers.utils.parseEther("10.0");

const tx = await anotherL2EthToken
.connect(bootloaderAccount)
.transferFromTo(walletFrom.address, walletTo.address, amountToTransfer)
.then((tx) => tx.wait());



await expect(tx)
await expect(
anotherL2EthToken
.connect(bootloaderAccount)
.transferFromTo(walletFrom.address, walletTo.address, amountToTransfer)
)
.to.emit(anotherL2EthToken, "Transfer")
.withArgs(walletFrom.address, walletTo.address, amountToTransfer);

Expand All @@ -137,37 +110,14 @@ describe("L2EthToken tests", () => {
expect(recipientBalanceAfterTransfer).to.be.eq(recipientBalanceBeforeTransfer.add(amountToTransfer));
});

// it("transfer successfully", async () => {
// await anotherL2EthToken.connect(bootloaderAccount).mint(walletFrom.address, ethers.utils.parseEther("100.0"));

// const senderBalandeBeforeTransfer: BigNumber = await anotherL2EthToken.balanceOf(walletFrom.address);
// const recipientBalanceBeforeTransfer: BigNumber = await anotherL2EthToken.balanceOf(walletTo.address);

// const amountToTransfer = ethers.utils.parseEther("10.0");

// await expect(
// anotherL2EthToken
// .connect(bootloaderAccount)
// .transferFromTo(walletFrom.address, walletTo.address, amountToTransfer).then((tx) => tx.wait())
// )
// .to.emit(anotherL2EthToken, "Transfer")
// .withArgs(walletFrom.address, walletTo.address, amountToTransfer);

// const senderBalanceAfterTransfer: BigNumber = await anotherL2EthToken.balanceOf(walletFrom.address);
// const recipientBalanceAfterTransfer: BigNumber = await anotherL2EthToken.balanceOf(walletTo.address);
// expect(senderBalanceAfterTransfer).to.be.eq(senderBalandeBeforeTransfer.sub(amountToTransfer));
// expect(recipientBalanceAfterTransfer).to.be.eq(recipientBalanceBeforeTransfer.add(amountToTransfer));
// });

it("no tranfser due to insufficient balance", async () => {
await anotherL2EthToken.connect(bootloaderAccount).mint(walletFrom.address, ethers.utils.parseEther("5.0"));
const amountToTransfer: BigNumber = ethers.utils.parseEther("100000000000000000.0");

await expect(
await anotherL2EthToken
anotherL2EthToken
.connect(bootloaderAccount)
.transferFromTo(walletFrom.address, walletTo.address, amountToTransfer)
.then((tx) => tx.wait())
).to.be.rejectedWith("Transfer amount exceeds balance");
});

Expand All @@ -178,18 +128,14 @@ describe("L2EthToken tests", () => {
privateKey: "0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
};
const maliciousWallet: Wallet = new Wallet(maliciousData.privateKey, provider);
await anotherL2EthToken
.connect(bootloaderAccount)
.mint(maliciousWallet.address, ethers.utils.parseEther("20.0"))
.then((tx) => tx.wait());
await anotherL2EthToken.connect(bootloaderAccount).mint(maliciousWallet.address, ethers.utils.parseEther("20.0"));

const amountToTransfer: BigNumber = ethers.utils.parseEther("20.0");

await expect(
await anotherL2EthToken
anotherL2EthToken
.connect(maliciousWallet)
.transferFromTo(maliciousWallet.address, walletTo.address, amountToTransfer)
.then((tx) => tx.wait())
).to.be.rejectedWith("Only system contracts with special access can call this method");
});
});

0 comments on commit f4014ab

Please sign in to comment.