將之前實作的所有功能(如轉移、批准、跨鏈轉移集成)整合到同一個合約中,確保它們能夠協同工作。
const MyToken = artifacts.require("MyToken");
contract("MyToken", accounts => {
let tokenInstance;
const totalSupply = web3.utils.toWei("1000", "ether"); // 總供應量
before(async () => {
tokenInstance = await MyToken.deployed();
});
// 測試合約初始化時的總供應量
it("should allocate the initial supply upon deployment", async () => {
const supply = await tokenInstance.totalSupply();
assert.equal(supply.toString(), totalSupply, "Initial supply is incorrect");
});
// 測試轉移
it("should transfer token ownership", async () => {
const sender = accounts[0];
const receiver = accounts[1];
const amount = web3.utils.toWei("100", "ether");
await tokenInstance.transfer(receiver, amount, { from: sender });
const balanceReceiver = await tokenInstance.balanceOf(receiver);
assert.equal(balanceReceiver.toString(), amount, "Receiver should have received 100 tokens");
const balanceSender = await tokenInstance.balanceOf(sender);
assert.equal(balanceSender.toString(), totalSupply - amount, "Sender's balance should be reduced");
});
// 測試轉移Token時餘額不足的情況
it("should not allow transfer of more tokens than balance", async () => {
const sender = accounts[0];
const receiver = accounts[1];
const amount = web3.utils.toWei("2000", "ether"); // 大於可用餘額
try {
await tokenInstance.transfer(receiver, amount, { from: sender });
assert.fail("Transfer should have thrown an error");
} catch (error) {
assert(error.message.includes("餘額不足"), "Expected '餘額不足' error message");
}
});
// 測試批准功能
it("should approve tokens for delegated transfer", async () => {
const owner = accounts[0];
const spender = accounts[1];
const amount = web3.utils.toWei("50", "ether");
await tokenInstance.approve(spender, amount, { from: owner });
const allowance = await tokenInstance.allowance(owner, spender);
assert.equal(allowance.toString(), amount, "Allowance should be correctly set");
});
// 測試轉移
it("should transfer tokens on behalf of the owner", async () => {
const owner = accounts[0];
const spender = accounts[1];
const receiver = accounts[2];
const amount = web3.utils.toWei("30", "ether");
// 先批准轉移
await tokenInstance.approve(spender, amount, { from: owner });
await tokenInstance.transferFrom(owner, receiver, amount, { from: spender });
const balanceReceiver = await tokenInstance.balanceOf(receiver);
assert.equal(balanceReceiver.toString(), amount, "Receiver should have received 30 tokens");
const balanceOwner = await tokenInstance.balanceOf(owner);
assert.equal(balanceOwner.toString(), totalSupply - amount, "Owner's balance should be reduced");
});
// 測試金額的轉移
it("should not allow transfer from another user beyond allowed amount", async () => {
const owner = accounts[0];
const spender = accounts[1];
const receiver = accounts[2];
const amount = web3.utils.toWei("100", "ether"); // 大於數量
try {
await tokenInstance.transferFrom(owner, receiver, amount, { from: spender });
assert.fail("Transfer should have thrown an error");
} catch (error) {
assert(error.message.includes("餘額不足"), "Expected '餘額不足' error message");
}
});
// 測試銷毀功能
it("should burn tokens", async () => {
const owner = accounts[0];
const burnAmount = web3.utils.toWei("20", "ether");
const initialSupply = await tokenInstance.totalSupply();
await tokenInstance.burn(burnAmount, { from: owner });
const finalSupply = await tokenInstance.totalSupply();
assert.equal(finalSupply.toString(), initialSupply.sub(burnAmount).toString(), "Total supply should decrease after burning tokens");
});
// 測試超過餘額的Token
it("should not allow burning more tokens than balance", async () => {
const owner = accounts[0];
const burnAmount = web3.utils.toWei("1000", "ether"); // 大於可用餘額
try {
await tokenInstance.burn(burnAmount, { from: owner });
assert.fail("Burn should have thrown an error");
} catch (error) {
assert(error.message.includes("餘額不足"), "Expected '餘額不足' error message");
}
});
});
說明