Skip to content

Commit

Permalink
vm-mock: add spent coins during call
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjah committed Jun 6, 2024
1 parent 2a9a88a commit db8a968
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
18 changes: 18 additions & 0 deletions assembly/__tests__/vm-mock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import {
balance,
balanceOf,
transferCoins,
call,
} from '../std';
import { changeCallStack, resetStorage } from '../vm-mock/storage';
import {
mockAdminContext,
mockBalance,
mockOriginOperationId,
mockScCall,
mockSetChainId,
mockTransferredCoins,
setDeployContext,
Expand Down Expand Up @@ -442,3 +444,19 @@ describe('transfer coins', () => {
expect(balance()).toBe(0);
});
});

describe('Callee balance', () => {
afterEach(() => {
resetStorage();
});

it('Contract balance must be updated after internal SC call', () => {
const amount = 666;
mockBalance(contractAddress.toString(), amount);
expect(balance()).toBe(amount);
mockScCall([]);
call(new Address(), 'targetFn', new Args(), amount);
expect(balance()).toBe(0);
expect(balanceOf(contractAddress.toString())).toBe(0);
});
});
6 changes: 4 additions & 2 deletions vm-mock/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ let webModule;

const scCallMockStack = [];
let callCoins = 0n; // Default value, coins for a call
let spentCoins = 0n; // Coins spent during the call
let chainIdMock = 77658366n; // Default value, chain id for Buildnet

/**
Expand All @@ -102,6 +103,7 @@ function resetLedger() {
balance: 100_000n,
});
callCoins = 0n;
spentCoins = 0n;
}

/**
Expand Down Expand Up @@ -132,7 +134,7 @@ function getCallee() {
*
*/
function getCalleeBalance() {
return BigInt(ledger.get(getCallee()).balance) + callCoins;
return BigInt(ledger.get(getCallee()).balance) + callCoins - spentCoins;
}

/**
Expand Down Expand Up @@ -490,7 +492,7 @@ export default function createMockedABI(
if(BigInt(coins) > getCalleeBalance()) {
ERROR('Not enough balance to pay the call to ' + ptrToString(method));
}
// todo: remove spent coins from the callee balance
spentCoins += BigInt(coins);
return newArrayBuffer(scCallMockStack.shift());
}
ERROR('No mock defined for sc call on ' + ptrToString(method));
Expand Down

0 comments on commit db8a968

Please sign in to comment.