Common Testing Scenarios
This guide highlights key testing scenarios for OP_NET smart contracts, including token transfers, access control, and inter-contract communication. These tests ensure your contracts function correctly in common use cases.
Testing Token Transfers and Balances
Validating Transfers
Ensure tokens are transferred correctly between accounts:
await vm.it("should transfer tokens correctly", async () => {
const sender = Blockchain.generateRandomAddress();
const receiver = Blockchain.generateRandomAddress();
const amount = 100;
await token.mint(sender, amount); // Mint tokens to sender
await token.transfer(sender, receiver, BigInt(amount * 10 ** 18)); // Transfer tokens from sender to receiver
const senderBalance = await token.balanceOf(sender);
const receiverBalance = await token.balanceOf(receiver);
Assert.expect(senderBalance).toEqual(0n);
Assert.expect(receiverBalance).toEqual(BigInt(amount * 10 ** 18));
});
Testing Transfer Restrictions
Validate that invalid transfers are restricted:
await vm.it("should not allow transfer exceeding balance", async () => {
const sender = Blockchain.generateRandomAddress();
const receiver = Blockchain.generateRandomAddress();
await Assert.expect(async () => {
await token.transfer(sender, receiver, 100n); // Exceeds sender's balance
}).toThrow("Insufficient balance");
});
Testing Access Control and Permissions
Ensure that only authorized users can perform certain actions:
await vm.it("should allow only the owner to mint tokens", async () => {
const receiver = Blockchain.generateRandomAddress();
await Assert.expect(async () => {
await token.mint(receiver, 100); // Authorized sender
}).toNotThrow();
const unauthorizedAddress = Blockchain.generateRandomAddress();
Blockchain.msgSender = unauthorizedAddress; // Change the sender to an unauthorized address
Blockchain.txOrigin = unauthorizedAddress; // Change the txOrigin to an unauthorized address
await Assert.expect(async () => {
await token.mint(receiver, 100); // Unauthorized sender
}).toThrow("Caller is not the owner");
});
Testing Permissioned Transfers
Validate access control on specific operations:
await vm.it("should prevent unauthorized transfers", async () => {
const sender = Blockchain.generateRandomAddress();
const receiver = Blockchain.generateRandomAddress();
await Assert.expect(async () => {
await token.mint(sender, 100);
}).toNotThrow();
Blockchain.msgSender = Blockchain.generateRandomAddress(); // Change the sender to an unauthorized address
Blockchain.txOrigin = Blockchain.generateRandomAddress(); // Change the txOrigin to an unauthorized address
await Assert.expect(async () => {
await token.transfer(sender, receiver, 100n);
}).toThrow("Unauthorized transfer");
});
Testing Inter-Contract Communication (In-Progress)
Inter-contract interactions require robust testing. While some features are in progress, here’s how you can prepare for future scenarios:
Mocking External Contract Calls (In-Progress)
Simulate external contract interactions by mocking their responses.
Detailed examples once OP_NET's mocking features are finalized.
Verifying Cross-Contract Interactions (In-Progress)
Ensure that contracts interact correctly with others by verifying calldata and responses.
Additional tools for cross-contract testing.