Skip to content

Commit

Permalink
Fix test throw error if from balance is 0
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben-Rey committed Mar 18, 2024
1 parent aa300c7 commit 308ee94
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 18 deletions.
12 changes: 0 additions & 12 deletions assembly/__tests__/env-coins.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,6 @@ describe('Testing env coins related functions', () => {
expect(env.balanceOf(testAddress2.toString())).toBe(amount);
});

it('transferCoins to mock address created with addAddressToLedger', () => {
addAddressToLedger(testAddress3.toString());
const amount: u64 = 100;
const receiverCurrentBalance = env.balanceOf(testAddress3.toString());
// given
expect(receiverCurrentBalance).toBe(0);
// when
env.transferCoins(testAddress3.toString(), amount);
// then
expect(env.balanceOf(testAddress3.toString())).toBe(amount);
});

it('get the balance of an address', () => {
const amount: u64 = 100;
// given
Expand Down
13 changes: 7 additions & 6 deletions vm-mock/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -699,20 +699,21 @@ export default function createMockedABI(
if (!ledger.has(fromAddress)) {
ERROR(`Sending address ${fromAddress} does not exist in ledger.`);
}
const senderBalance = ledger.get(fromAddress).balance;

// We should not have this case, but someone could add a ledger.set without a balance
// Typescript could be used to avoid this kind of error
if (ledger.get(fromAddress).balance === undefined) {
ERROR('not enough balance to transfer ' + amount + ' coins.');
}

const senderBalance = ledger.get(fromAddress).balance;
if (senderBalance < BigInt(amount)) {
ERROR('not enough balance to transfer ' + amount + ' coins.');
}

// We should not have this case, but someone could add a ledger.set without a balance
// Typescript could be used to avoid this kind of error
if (ledger.get(toAddress).balance === undefined) {
ledger.get(toAddress).balance = BigInt(0);
}
if (ledger.get(fromAddress).balance === undefined) {
ledger.get(fromAddress).balance = BigInt(0);
}

ledger.get(toAddress).balance += BigInt(amount);
ledger.get(fromAddress).balance -= BigInt(amount);
Expand Down

0 comments on commit 308ee94

Please sign in to comment.