Skip to content

Commit

Permalink
more revise
Browse files Browse the repository at this point in the history
Signed-off-by: MASDXI <[email protected]>
  • Loading branch information
MASDXI committed Jan 11, 2025
1 parent f761416 commit ca169d0
Show file tree
Hide file tree
Showing 7 changed files with 906 additions and 1,109 deletions.
5 changes: 1 addition & 4 deletions contracts/abstracts/extensions/FreezeBalance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@ abstract contract FreezeBalance {
* @param value The amount of balance to be spent.
*/
modifier checkFrozenBalance(address account, uint256 balance, uint256 value) {
if (balance < value) {
revert BalanceOverflow();
}
uint256 frozenBalance = _frozenBalance[account];
if (frozenBalance > balance - value) {
if (frozenBalance >= balance) {
revert BalanceFrozen(balance, frozenBalance);
}
_;
Expand Down
1 change: 1 addition & 0 deletions hardhat.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require("hardhat-gas-reporter");
require("hardhat-ignore-warnings");
require("@nomicfoundation/hardhat-chai-matchers");
require("@nomicfoundation/hardhat-toolbox");

/** @type import('hardhat/config').HardhatUserConfig */
Expand Down
65 changes: 30 additions & 35 deletions test/contracts/ERC20/ERC20.test.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,48 @@
const {time, loadFixture} = require("@nomicfoundation/hardhat-toolbox/network-helpers");
const {loadFixture} = require("@nomicfoundation/hardhat-toolbox/network-helpers");
const {anyValue} = require("@nomicfoundation/hardhat-chai-matchers/withArgs");
const {expect} = require("chai");
const {ZeroAddress} = require("ethers");

describe("ERC20", function () {
// We define a fixture to reuse the same setup in every test.
// We use loadFixture to run this setup once, snapshot that state,
// and reset Hardhat Network to that snapshot in every test.
async function deployTokenFixture() {
// Contracts are deployed using the first signer/account by default
const [owner, otherAccount] = await ethers.getSigners();
const amount = 1000n;
const frozenAmount = 100n;

async function deployTokenFixture() {
const [owner, alice, bob, charlie, dave, otherAccount] = await ethers.getSigners();
const contract = await ethers.getContractFactory("MockERC20");
const token = await contract.deploy("United States dollar", "USD");

return {token, owner, otherAccount};
return {token, owner, alice, bob, charlie, dave, otherAccount};
}

// Avoid repeating test
describe("Transfers", function () {
it("Should mint the funds to the owner", async function () {
const {token, owner} = await loadFixture(deployTokenFixture);
const address = await owner.getAddress();
await token.mint(address, 1000n);
expect(await token.balanceOf(address)).to.equal(1000n);
it("Freeze Alice Account and transfer", async function () {
const {token, alice, bob} = await loadFixture(deployTokenFixture);
const aliceAddress = alice.address;
const bobAddress = bob.address;
await token.mint(alice, amount);
await token.freezeAddress(aliceAddress);
expect(await token.isFrozen(aliceAddress)).to.equal(true);
await expect(token.connect(alice).transfer(bobAddress, amount)).to.be.reverted;
});

it("Should transfer the funds from the account to other account", async function () {
const {token, owner, otherAccount} = await loadFixture(deployTokenFixture);
const address = await owner.getAddress();
const otherAddress = await otherAccount.getAddress();
let tx = await token.mint(address, 1000n);
tx = await tx.wait();
await token.transfer(otherAddress, 1000n);
expect(await token.balanceOf(otherAddress)).to.equal(1000n);
it("Freeze Alice Account and transferFrom", async function () {
// TODO
});

it("Should transferFrom the funds from the account to other account", async function () {
const {token, owner, otherAccount} = await loadFixture(deployTokenFixture);
const address = await owner.getAddress();
const otherAddress = await otherAccount.getAddress();
await token.mint(address, 1000n);
await token.connect(owner).approve(otherAddress, 1000n);
expect(await token.allowance(address, otherAddress)).to.equal(1000n);
await token.connect(otherAccount).transferFrom(address, otherAddress, 1000n);
expect(await token.balanceOf(otherAddress)).to.equal(1000n);
it("Freeze Alice Balance and transfer", async function () {
const {token, alice, bob} = await loadFixture(deployTokenFixture);
const aliceAddress = alice.address;
const bobAddress = bob.address;
await token.mint(alice, amount);
await token.setFreezeBalance(aliceAddress, frozenAmount);
expect(await token.getFrozenBalance(aliceAddress)).to.equal(frozenAmount);
await expect(token.connect(alice).transfer(bobAddress, amount - frozenAmount)).not.to.be.reverted;
await expect(token.connect(alice).transfer(bobAddress, frozenAmount)).to.be.reverted;
});
});

// extensions
// transfer
// transferFrom
it("Freeze Alice Balance and transferFrom", async function () {
// TODO
});
});
});
140 changes: 23 additions & 117 deletions test/contracts/Forest/Forest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,146 +6,52 @@ const {encodeBytes32String, ZeroAddress, solidityPackedKeccak256, getBytes} = re
describe("Forest", function () {
const transferMethod = "transfer(address,bytes32,uint256)";

// We define a fixture to reuse the same setup in every test.
// We use loadFixture to run this setup once, snapshot that state,
// and reset Hardhat Network to that snapshot in every test.
async function deployTokenFixture() {
// Contracts are deployed using the first signer/account by default
const [owner, otherAccount] = await ethers.getSigners();

const contract = await ethers.getContractFactory("MockForest");
const token = await contract.deploy("United States dollar", "USD");

return {token, owner, otherAccount};
}

describe("Transfers", function () {
it("Should transfer the funds to the owner", async function () {
const {token, owner} = await loadFixture(deployTokenFixture);
const address = await owner.getAddress();
await token.mint(address, 1000n);
expect(await token.balanceOf(address)).to.equal(1000n);
it("Freeze Alice Account and transfer", async function () {
// TODO
});

it("Freeze Alice Account and transferFrom", async function () {
// TODO
});

it("Should transfer the funds to the owner", async function () {
const {token, owner, otherAccount} = await loadFixture(deployTokenFixture);
const address = await owner.getAddress();
const otherAddress = await otherAccount.getAddress();
let tx = await token.mint(address, 1000n);
tx = await tx.wait();
const tokenId = tx.logs[0].args[0];
await token[transferMethod](otherAddress, tokenId, 1000n);
expect(await token.balanceOf(otherAddress)).to.equal(1000n);
it("Freeze Alice Balance and transfer", async function () {
// TODO
});

it("Should not transfer with IERC20 transfer", async function () {
const {token, owner, otherAccount} = await loadFixture(deployTokenFixture);
const amount = 1000n;
const address = await owner.getAddress();
const otherAddress = await otherAccount.getAddress();
let tx = await token.mint(address, amount);
tx = await tx.wait();
const tokenId = tx.logs[0].args[0];
await token[transferMethod](otherAddress, tokenId, amount);
expect(await token.balanceOf(otherAddress)).to.equal(1000n);
// await expect(token.connect(otherAccount).transfer(address, amount)).to.be.revertedWithCustomError(token,"ERC20TransferNotSupported");
it("Freeze Alice Balance and transferFrom", async function () {
// TODO
});

// TODO: transferFrom(address,address,tokenId,amount)
// TODO: burn(address,tokenId,amount)
it("Freeze Alice Token and transfer", async function () {
// TODO
});

it("Should not transferFrom with IERC20 transferFrom", async function () {
const {token, owner, otherAccount} = await loadFixture(deployTokenFixture);
const amount = 1000n;
const address = await owner.getAddress();
const otherAddress = await otherAccount.getAddress();
let tx = await token.mint(address, amount);
tx = await tx.wait();
const tokenId = tx.logs[0].args[0];
await token[transferMethod](otherAddress, tokenId, amount);
await token.connect(otherAccount).approve(address, amount);
await expect(token.connect(owner).transferFrom(otherAddress, address, amount)).to.be.revertedWithCustomError(
token,
"ERC20TransferFromNotSupported",
);
it("Freeze Alice Token and transferFrom", async function () {
// TODO
});

it("Should burn transfer with to address zero", async function () {
const {token, owner, otherAccount} = await loadFixture(deployTokenFixture);
const amount = 1000n;
const address = await owner.getAddress();
// const otherAddress = await otherAccount.getAddress();
let tx = await token.mint(address, amount);
tx = await tx.wait();
const tokenId = tx.logs[0].args[0];
// await expect(token.burn(address, tokenId ,amount)).to.be.emit(token,"Transfer").withArgs(address, ZeroAddress, amount);
await token.burn(address, tokenId, amount);
expect(await token.balanceOf(address)).to.equal(0);
// TODO: transferFrom(address,address,tokenId,amount)
// TODO: burn(address,tokenId,amount)
it("Freeze at root and transfer", async function () {
// TODO
});
});

// extensions
describe("Restrict", function () {
it("Should restrict transfer the funds to the other account by frozen tokenId", async function () {
const {token, owner, otherAccount} = await loadFixture(deployTokenFixture);
const address = await owner.getAddress();
const otherAddress = await otherAccount.getAddress();
let tx = await token.mint(address, 1000n);
tx = await tx.wait();
const tokenId = tx.logs[0].args[0];
tx = await token[transferMethod](otherAddress, tokenId, 100n);
tx = await tx.wait();
const tokenId2 = tx.logs[1].args[0];
expect(await token.balanceOf(otherAddress)).to.equal(100n);
await token.freezeToken(tokenId2);
await expect(token.connect(otherAccount)[transferMethod](address, tokenId2, 10n)).to.be.revertedWithCustomError(
token,
"TokenFrozen",
);
it("Freeze at root and transferFrom", async function () {
// TODO
});

it("Should restrict all transfer the funds to the other account by frozen root tokenId", async function () {
const {token, owner, otherAccount} = await loadFixture(deployTokenFixture);
const address = await owner.getAddress();
const otherAddress = await otherAccount.getAddress();
let tx = await token.mint(address, 1000n);
tx = await tx.wait();
let tokenId = tx.logs[0].args[0];
let root = tx.logs[0].args[1];
tx = await token[transferMethod](otherAddress, tokenId, 10n);
expect(await token.balanceOf(otherAddress)).to.equal(10n);
await token.freezeToken(root);
await expect(token[transferMethod](otherAddress, tokenId, 10n)).to.be.revertedWithCustomError(
token,
"TokenFrozen",
);
it("Freeze at level and transfer", async function () {
// TODO
});

it("Should restrict all transfer the funds to the other account by frozen parent tokenId", async function () {
const {token, owner, otherAccount} = await loadFixture(deployTokenFixture);
// const address = await owner.getAddress();
// const otherAddress = await otherAccount.getAddress();
// let tx = await token.mint(address, 1000n);
// tx = await tx.wait();
// const tokenId = tx.logs[0].args[0];
// tx = await token[transferMethod](
// otherAddress,
// tokenId,
// 100n
// );
// tx = await tx.wait();
// const tokenId2 = tx.logs[1].args[0];
// expect(await token.balanceOf(otherAddress)).to.equal(100n);
// await token.freezeToken(parent);
// await expect(
// await token[transferMethod](
// otherAddress,
// tokenId2,
// 10n
// )
// ).to.equal("");
it("Freeze at level and transferFrom", async function () {
// TODO
});
});
});
6 changes: 0 additions & 6 deletions test/contracts/UTXO/UTXO.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@ const {expect} = require("chai");
const {encodeBytes32String, ZeroAddress, solidityPackedKeccak256, getBytes, isBytesLike, ZeroHash} = require("ethers");

describe("UTXO", function () {
// We define a fixture to reuse the same setup in every test.
// We use loadFixture to run this setup once, snapshot that state,
// and reset Hardhat Network to that snapshot in every test.
async function deployTokenFixture() {
// Contracts are deployed using the first signer/account by default
const [owner, otherAccount] = await ethers.getSigners();

const contract = await ethers.getContractFactory("MockUtxo");
const token = await contract.deploy("United States dollar", "USD");

return {token, owner, otherAccount};
}

Expand Down
6 changes: 0 additions & 6 deletions test/contracts/UTXO/eUTXO.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@ const {expect} = require("chai");
const {encodeBytes32String, solidityPackedKeccak256, getBytes, ZeroHash, decodeBytes32String} = require("ethers");

describe("eUTXO", function () {
// We define a fixture to reuse the same setup in every test.
// We use loadFixture to run this setup once, snapshot that state,
// and reset Hardhat Network to that snapshot in every test.
async function deployTokenFixture() {
// Contracts are deployed using the first signer/account by default
const [owner, otherAccount] = await ethers.getSigners();

const contract = await ethers.getContractFactory("MockeUtxo");
const token = await contract.deploy("United States dollar", "USD");

return {token, owner, otherAccount};
}

Expand Down
Loading

0 comments on commit ca169d0

Please sign in to comment.