Skip to content

Commit

Permalink
(fix) Hopefully fix PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rrw-zilliqa committed Sep 6, 2024
1 parent f3d715a commit a0099a8
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 40 deletions.
9 changes: 6 additions & 3 deletions contracts/experimental/ERC20ProxyForZRC2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@ pnpm exec hardhat test --network zq-testnet

Each test has a number prefix so you can select them individually.

If you set the `CACHED` environment variable, we will use a built-in
cached contract deployment whose addresses appear in the source -
please update it if you change the contracts.
If you set the `CACHED` environment variable, we will use:

- `CACHED_ZRC2` - address of a ZRC-2
- `CACHED_ERC20` - address of an ERC-20

To run the tests; this saves you having to redeploy each time.

This allows you to run tests quickly, without waiting for contract
deployment.
84 changes: 54 additions & 30 deletions contracts/experimental/ERC20ProxyForZRC2/test/basicProxyTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ import { ScillaContract } from "hardhat-scilla-plugin";
import { Account } from "@zilliqa-js/zilliqa";
import { initZilliqa } from "hardhat-scilla-plugin";

async function assertedTransfer(
proxy: ZRC2ERC20Proxy,
from: Wallet,
to: Wallet,
amt: number,
) {
const receipt = await (await proxy.connect(from).transfer(to, amt)).wait();

expect(
receipt?.status === 1,
`Transfer of ${amt} from ${from.address} to ${to.address} failed. Transaction status ${receipt?.status || "unknown"}`,
);
}

/// not.to.be.reverted seems to not work here (it complains about null receipts), so ..
describe("basicTest", function () {
let zrc2Contract: ScillaContract;
Expand Down Expand Up @@ -40,6 +54,7 @@ describe("basicTest", function () {
//zrc2OwnerAddress = zrc2Owner.address.toLowerCase();

if (!process.env.CACHED) {
console.log(`Deploying contracts ..`);
zrc2Contract = await hre.deployScillaContract(
"FungibleToken",
zrc2OwnerEVM.address,
Expand All @@ -59,12 +74,14 @@ describe("basicTest", function () {
);
await erc20Proxy.waitForDeployment();
} else {
console.log(`Using predeployed contracts ..`);
zrc2Contract = await hre.interactWithScillaContract(
"0x178ABcED2552522F131E7C89E80E622862E6c00E",
process.env.CACHED_ZRC2,
);
erc20Proxy = (await ethers.getContractFactory("ZRC2ERC20Proxy"))
.connect(proxyDeployer)
.attach("0x0C9fb168f7155Ea54aAaCafdbD9A652bd895b4a4");
.attach(process.env.CACHED_ERC20);
console.log(`proxy ${JSON.stringify(erc20Proxy)}`);
}
console.log(`ERC20 proxy deployed at ${erc20Proxy.target}`);
console.log(` .... proxying to ZRC2 at ${await erc20Proxy.zrc2_proxy()}`);
Expand All @@ -73,12 +90,12 @@ describe("basicTest", function () {
);
});

it("0000 Should deploy successfully", async function () {
it("T0000 Should deploy successfully", async function () {
expect(zrc2Contract.address).to.be.properAddress;
expect(erc20Proxy.target).to.be.properAddress;
});

it("0001 Should report parameters correctly", async function () {
it("T0001 Should report parameters correctly", async function () {
expect(await erc20Proxy.decimals()).to.equal(ZRC2_DECIMALS);
expect(await erc20Proxy.symbol()).to.equal(ZRC2_SYMBOL);
expect(await erc20Proxy.name()).to.equal(ZRC2_NAME);
Expand All @@ -88,37 +105,33 @@ describe("basicTest", function () {
);
});

it("0002 Should deal with transfers correctly", async function () {
it("T0002 Should deal with transfers correctly", async function () {
const AMT = 540;
expect(await erc20Proxy.balanceOf(zrc2OwnerEVM.address)).to.equal(
ZRC2_SUPPLY,
);
expect(await erc20Proxy.balanceOf(tokenHolder.address)).to.equal(0);
await (
await erc20Proxy.connect(zrc2OwnerEVM).transfer(tokenHolder, AMT)
).wait();
await assertedTransfer(erc20Proxy, zrc2OwnerEVM, tokenHolder, AMT);
expect(await erc20Proxy.balanceOf(tokenHolder.address)).to.equal(AMT);
expect(await erc20Proxy.balanceOf(zrc2OwnerEVM.address)).to.equal(
ZRC2_SUPPLY - AMT,
);
await (
await erc20Proxy.connect(tokenHolder).transfer(zrc2OwnerEVM, AMT)
).wait();
await assertedTransfer(erc20Proxy, tokenHolder, zrc2OwnerEVM, AMT);
});

it("MOVE", async function () {
// Use this test to "manually" transfer tokens back from the tokenHolder after a test fails (if it does).
xit("MOVE", async function () {
const AMT = 4;
await expect(erc20Proxy.connect(tokenHolder).transfer(zrc2OwnerEVM, AMT))
.not.to.be.reverted;
await assertedTransfer(erc20Proxy, tokenHolder, zrc2OwnerEVM, AMT);
});

it("0003 should fail transfers if the user doesn't have balance", async function () {
it("T0003 should fail transfers if the user doesn't have balance", async function () {
const AMT = 1;
await expect(erc20Proxy.connect(tokenHolder).transfer(tokenHolder, AMT)).to
.be.reverted;
});

it("0004 should fail transferFrom if the user doesn't have allowance", async function () {
it("T0004 should fail transferFrom if the user doesn't have allowance", async function () {
expect(
await erc20Proxy.allowance(zrc2OwnerEVM.address, tokenHolder.address),
).to.equal(0);
Expand All @@ -129,44 +142,55 @@ describe("basicTest", function () {
).to.be.reverted;
});

it("0005 should succeed transferFrom if there is enough allowance", async function () {
it("T0005 should succeed transferFrom if there is enough allowance", async function () {
const AMT = 4;
expect(await erc20Proxy.balanceOf(zrc2OwnerEVM.address)).to.equal(
ZRC2_SUPPLY,
);
expect(await erc20Proxy.balanceOf(tokenHolder.address)).to.equal(0);
expect(await erc20Proxy.balanceOf(proxyDeployer.address)).to.equal(0);
await (
await erc20Proxy.connect(zrc2OwnerEVM).approve(proxyDeployer, AMT)
).wait();
{
const receipt = await (
await erc20Proxy.connect(zrc2OwnerEVM).approve(proxyDeployer, AMT)
).wait();
expect(
receipt?.status === 1,
`Approval of ${AMT} from ${zrc2OwnerEVM.address} to ${proxyDeployer.address} failed`,
);
}
expect(
await erc20Proxy.allowance(zrc2OwnerEVM.address, proxyDeployer.address),
).to.equal(AMT);
await (
await erc20Proxy
.connect(proxyDeployer)
.transferFrom(zrc2OwnerEVM, tokenHolder, AMT)
).wait();
{
const receipt = await (
await erc20Proxy
.connect(proxyDeployer)
.transferFrom(zrc2OwnerEVM, tokenHolder, AMT)
).wait();
expect(
receipt?.status === 1,
`transferFrom called by ${proxyDeployer.address} for ${zrc2OwnerEVM.address} -> ${tokenHolder.address} for ${AMT} failed`,
);
}
expect(await erc20Proxy.balanceOf(zrc2OwnerEVM.address)).to.equal(
ZRC2_SUPPLY - AMT,
);
expect(await erc20Proxy.balanceOf(tokenHolder.address)).to.equal(AMT);
expect(await erc20Proxy.balanceOf(proxyDeployer.address)).to.equal(0);
});

it("0006 transfer 0005's tokens back", async function () {
it("T0006 transfer 0005's tokens back", async function () {
// Now transfer it all back.
await (
await erc20Proxy.connect(tokenHolder).transfer(zrc2OwnerEVM, AMT)
).wait();
const AMT = 4;
await assertedTransfer(erc20Proxy, tokenHolder, zrc2OwnerEVM, AMT);
expect(await erc20Proxy.balanceOf(zrc2OwnerEVM.address)).to.equal(
ZRC2_SUPPLY,
);
expect(await erc20Proxy.balanceOf(tokenHolder.address)).to.equal(0);
expect(await erc20Proxy.balanceOf(proxyDeployer.address)).to.equal(0);
});

it("0007 correctly limits allowances to 128 bits", async function () {
it("T0007 correctly limits allowances to 128 bits", async function () {
expect(
erc20Proxy
.connect(zrc2OwnerEVM)
Expand Down
34 changes: 27 additions & 7 deletions contracts/experimental/ERC20ProxyForZRC2/test/burnTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,13 @@ describe("burnTest", function () {
const supply = await erc20Proxy.totalSupply();
const AMT = 1;
expect(balance).to.be.at.least(AMT);
await (await erc20Proxy.connect(zrc2OwnerEVM).burn(AMT)).wait();
const receipt = await (
await erc20Proxy.connect(zrc2OwnerEVM).burn(AMT)
).wait();
expect(
receipt?.status === 1,
`Burn of ${AMT} from ${zrc2OwnerEVM.address} failed`,
);
expect(await erc20Proxy.balanceOf(zrc2OwnerEVM.address)).to.equal(
balance - BigInt(AMT),
);
Expand All @@ -96,12 +102,26 @@ describe("burnTest", function () {
const supply = await erc20Proxy.totalSupply();
const AMT = 1;
expect(balance).to.equal(0);
await (
await erc20Proxy.connect(zrc2OwnerEVM).approve(tokenHolder.address, AMT)
).wait();
await (
await erc20Proxy.connect(tokenHolder).burnFrom(zrc2OwnerEVM.address, AMT)
).wait();
{
const receipt = await (
await erc20Proxy.connect(zrc2OwnerEVM).approve(tokenHolder.address, AMT)
).wait();
expect(
receipt?.status === 1,
`Approval of ${AMT} from ${zrc2OwnerEVM.address} for ${tokenHolder.address} failed`,
);
}
{
const receipt = await (
await erc20Proxy
.connect(tokenHolder)
.burnFrom(zrc2OwnerEVM.address, AMT)
).wait();
expect(
receipt?.status === 1,
`BurnFrom for ${AMT} from ${zrc2OwnerEVM.address} issued by ${tokenHolder.address} failed`,
);
}
const holderBalanceAfter = await erc20Proxy.balanceOf(zrc2OwnerEVM.address);
expect(holderBalance - holderBalanceAfter).to.equal(BigInt(AMT));
const supplyAfter = await erc20Proxy.totalSupply();
Expand Down

0 comments on commit a0099a8

Please sign in to comment.