Skip to content

Commit

Permalink
test: ✅ Added more basic erc20 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
timbrinded committed Apr 25, 2024
1 parent 4573067 commit 72e8383
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 14 deletions.
10 changes: 4 additions & 6 deletions test/contracts/src/ERC20Sample.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract ERC20Sample is ERC20, ERC20Burnable, Ownable {
constructor()
ERC20("SampleToken", "SAM")
Ownable()
{}
constructor() ERC20("SampleToken", "SAM") Ownable() {
_mint(msg.sender, 1000 * 10 ** decimals());
}

function greeter() public view returns (string memory) {
return "Hello, ERC20!";
Expand All @@ -18,5 +17,4 @@ contract ERC20Sample is ERC20, ERC20Burnable, Ownable {
function mint(address to, uint256 amount) public onlyOwner {
_mint(to, amount);
}

}
}
156 changes: 149 additions & 7 deletions test/suites/dev/moonbase/test-contract/test-erc20-interactions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { beforeAll, describeSuite, expect } from "@moonwall/cli";
import { beforeEach, describeSuite, expect } from "@moonwall/cli";
import {
ALITH_ADDRESS,
BALTATHAR_ADDRESS,
CHARLETH_ADDRESS,
CHARLETH_PRIVATE_KEY,
GLMR,
} from "@moonwall/util";

describeSuite({
id: "D010611",
Expand All @@ -7,7 +14,7 @@ describeSuite({
testCases: ({ context, it, log }) => {
let contract: `0x${string}`;

beforeAll(async function () {
beforeEach(async function () {
const { contractAddress } = await context.deployContract!("ERC20Sample");
contract = contractAddress;
});
Expand All @@ -26,15 +33,150 @@ describeSuite({
},
});

// TODO add test for minting
it({
id: "T02",
title: "Should mint as expected",
test: async function () {
const tx = await context.writeContract!({
contractName: "ERC20Sample",
contractAddress: contract,
functionName: "mint",
args: [BALTATHAR_ADDRESS, 10n * GLMR],
rawTxOnly: true,
});

const { result } = await context.createBlock(tx);

const bal = await context.readContract!({
contractName: "ERC20Sample",
contractAddress: contract,
functionName: "balanceOf",
args: [BALTATHAR_ADDRESS],
});

expect(result?.successful).toBe(true);
expect(bal).toEqual(10n * GLMR);
},
});

it({
id: "T03",
title: "Should burn as expected",
test: async function () {
const amount = 10n * GLMR;

const balBefore = (await context.readContract!({
contractName: "ERC20Sample",
contractAddress: contract,
functionName: "balanceOf",
args: [ALITH_ADDRESS],
})) as bigint;

const tx = await context.writeContract!({
contractName: "ERC20Sample",
contractAddress: contract,
functionName: "burn",
args: [amount],
rawTxOnly: true,
});

const { result } = await context.createBlock(tx);

const balAfter = (await context.readContract!({
contractName: "ERC20Sample",
contractAddress: contract,
functionName: "balanceOf",
args: [ALITH_ADDRESS],
})) as bigint;

expect(result?.successful).toBe(true);
expect(balBefore - balAfter).toEqual(amount);
},
});

it({
id: "T04",
title: "Should approve as expected",
test: async function () {
const tx = await context.writeContract!({
contractName: "ERC20Sample",
contractAddress: contract,
functionName: "approve",
args: [BALTATHAR_ADDRESS, 7n * GLMR],
rawTxOnly: true,
});

const { result } = await context.createBlock(tx);

const approval = (await context.readContract!({
contractName: "ERC20Sample",
contractAddress: contract,
functionName: "allowance",
args: [ALITH_ADDRESS, BALTATHAR_ADDRESS],
})) as bigint;

// TODO add test for burning
expect(result?.successful).toBe(true);
expect(approval).toEqual(7n * GLMR);
},
});

it({
id: "T05",
title: "Should transfer as expected",
test: async function () {
const tx = await context.writeContract!({
contractName: "ERC20Sample",
contractAddress: contract,
functionName: "transfer",
args: [BALTATHAR_ADDRESS, 10n * GLMR],
rawTxOnly: true,
});

// TODO add test for approval
const { result } = await context.createBlock(tx);

// TODO add test for transfer
const bal = await context.readContract!({
contractName: "ERC20Sample",
contractAddress: contract,
functionName: "balanceOf",
args: [BALTATHAR_ADDRESS],
});

// TODO add test for transferFrom
expect(result?.successful).toBe(true);
expect(bal).toEqual(10n * GLMR);
},
});

it({
id: "T06",
title: "Should transferFrom as expected",
test: async function () {
await context.writeContract!({
contractName: "ERC20Sample",
contractAddress: contract,
functionName: "approve",
args: [CHARLETH_ADDRESS, 3n * GLMR],
});
await context.createBlock();

await context.writeContract!({
contractName: "ERC20Sample",
contractAddress: contract,
functionName: "transferFrom",
args: [ALITH_ADDRESS, BALTATHAR_ADDRESS, 3n * GLMR],
privateKey: CHARLETH_PRIVATE_KEY,
});
await context.createBlock();

const bal = await context.readContract!({
contractName: "ERC20Sample",
contractAddress: contract,
functionName: "balanceOf",
args: [BALTATHAR_ADDRESS],
});

expect(bal).toEqual(3n * GLMR);
},
});

// TODO mint via XCM

Expand Down
2 changes: 1 addition & 1 deletion test/suites/dev/moonbase/test-txpool/test-txpool-limits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describeSuite({
id: "T01",
title: "should be able to fill a block with 2650 tx",
test: async function () {
for (let i = 0; i < 2650; i++) {
for (let i = 0; i < 3000; i++) {
const rawTxn = await createRawTransfer(context, BALTATHAR_ADDRESS, 1n, {
nonce: i,
});
Expand Down

0 comments on commit 72e8383

Please sign in to comment.