Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 72 additions & 3 deletions contracts/erc20/contract/src/test/erc20.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ describe('ERC20', () => {
})

describe('balanceOf', () => {
it('returns zero when requested account has no balance', () => {
it('should return zero when requested account has no balance', () => {
expect(token.balanceOf(Z_OWNER)).toEqual(0n);
});

it('returns balance when requested account has tokens', () => {
it('should return balance when requested account has tokens', () => {
token._mint(Z_OWNER, AMOUNT);
expect(token.balanceOf(Z_OWNER)).toEqual(AMOUNT);
});
Expand Down Expand Up @@ -135,6 +135,22 @@ describe('ERC20', () => {
token.transfer(utils.ZERO_ADDRESS, AMOUNT, caller);
}).toThrow('ERC20: invalid receiver');
});

it('should allow transfer of 0 tokens', () => {
const txSuccess = token.transfer(Z_RECIPIENT, 0n, caller);

expect(txSuccess).toBeTruthy();
expect(token.balanceOf(Z_OWNER)).toEqual(AMOUNT);
expect(token.balanceOf(Z_RECIPIENT)).toEqual(0n);
});

it('should handle transfer with empty _balances', () => {
caller = SPENDER;

expect(() => {
token.transfer(Z_RECIPIENT, 1n, caller);
}).toThrow('ERC20: insufficient balance');
});
});

describe('approve', () => {
Expand Down Expand Up @@ -176,6 +192,30 @@ describe('ERC20', () => {
token.approve(utils.ZERO_ADDRESS, AMOUNT, caller);
}).toThrow('ERC20: invalid spender');
});

it('should transfer exact allowance and fail subsequent transfer', () => {
token._mint(Z_OWNER, AMOUNT);
caller = OWNER;
token.approve(Z_SPENDER, AMOUNT, caller);

caller = SPENDER;
token.transferFrom(Z_OWNER, Z_RECIPIENT, AMOUNT, caller);
expect(token.allowance(Z_OWNER, Z_SPENDER)).toEqual(0n);

expect(() => {
token.transferFrom(Z_OWNER, Z_RECIPIENT, 1n, caller);
}).toThrow('ERC20: insufficient allowance');
});

it('should allow approve of 0 tokens', () => {
caller = OWNER;
token.approve(Z_SPENDER, 0n, caller);
expect(token.allowance(Z_OWNER, Z_SPENDER)).toEqual(0n);
});

it('should handle allowance with empty _allowances', () => {
expect(token.allowance(Z_OWNER, Z_SPENDER)).toEqual(0n);
});
});

describe('transferFrom', () => {
Expand Down Expand Up @@ -291,7 +331,7 @@ describe('ERC20', () => {

expect(token.balanceOf(Z_OWNER)).toEqual(1n);
expect(token.balanceOf(Z_RECIPIENT)).toEqual(partialAmt);
})
});
})

describe('_mint', () => {
Expand All @@ -314,6 +354,12 @@ describe('ERC20', () => {
token._mint(utils.ZERO_ADDRESS, AMOUNT);
}).toThrow('ERC20: invalid receiver');
});

it('should allow mint of 0 tokens', () => {
token._mint(Z_OWNER, 0n);
expect(token.totalSupply()).toEqual(0n);
expect(token.balanceOf(Z_OWNER)).toEqual(0n);
});
});

describe('_burn', () => {
Expand All @@ -340,6 +386,12 @@ describe('ERC20', () => {
token._burn(Z_OWNER, AMOUNT + 1n);
}).toThrow('ERC20: insufficient balance');
});

it('should allow burn of 0 tokens', () => {
token._burn(Z_OWNER, 0n);
expect(token.totalSupply()).toEqual(AMOUNT);
expect(token.balanceOf(Z_OWNER)).toEqual(AMOUNT);
});
});

describe('_update', () => {
Expand Down Expand Up @@ -389,4 +441,21 @@ describe('ERC20', () => {
expect(token.isZero(SOME_CONTRACT)).toBeFalsy;
});
});

describe('Multiple Operations', () => {
it('should handle mint → transfer → burn sequence', () => {
token._mint(Z_OWNER, AMOUNT);
expect(token.totalSupply()).toEqual(AMOUNT);
expect(token.balanceOf(Z_OWNER)).toEqual(AMOUNT);

caller = OWNER;
token.transfer(Z_RECIPIENT, AMOUNT - 1n, caller);
expect(token.balanceOf(Z_OWNER)).toEqual(1n);
expect(token.balanceOf(Z_RECIPIENT)).toEqual(AMOUNT - 1n);

token._burn(Z_OWNER, 1n);
expect(token.totalSupply()).toEqual(AMOUNT - 1n);
expect(token.balanceOf(Z_OWNER)).toEqual(0n);
});
});
});