Skip to content

Commit

Permalink
Add setCallCoins to mock SC call coins
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben-Rey committed Jan 8, 2024
1 parent 4cc03c2 commit 2c50135
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
6 changes: 3 additions & 3 deletions assembly/__tests__/env-coins.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This file is aim to test the env coins related functions which are external functions

import { env } from '../env';
import { resetStorage } from '../vm-mock';
import { resetStorage, setCallCoins } from '../vm-mock';
import { Address, Storage } from '../std';
import { stringToBytes } from '@massalabs/as-types';

Expand Down Expand Up @@ -69,7 +69,7 @@ describe('Testing env coins related functions', () => {
});

it('callCoins', () => {
// We don't have a way to set the call coins yet in the mock
expect(env.callCoins()).toBe(0);
setCallCoins(100);
expect(env.callCoins()).toBe(100);
});
});
5 changes: 3 additions & 2 deletions assembly/std/__tests__/context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
timestamp,
} from '../context';
import { validateAddress } from '../utils';
import { setCallCoins } from '../../vm-mock';

describe('Context', () => {
test('ownedAddresses', () => {
Expand All @@ -33,8 +34,8 @@ describe('Context', () => {
});

test('transferredCoins', () => {
const coins = transferredCoins();
expect(coins).toBe(0);
setCallCoins(100);
expect(transferredCoins()).toBe(100);
});

test('isDeployingContract', () => {
Expand Down
16 changes: 16 additions & 0 deletions assembly/vm-mock/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@
@external("massa", "assembly_script_mock_call")
export declare function mockScCall(value: StaticArray<u8>): void;

/**
* Set mock for call coins.
*
* @example
* ```typescript
* test('mocked SC call', () => {
* const mockValue: u64 = 123;
* mockCallCoins(mockValue);
* const res = transferredCoins();
* expect(res).toBe(mockValue);
* });
* ```
*/
@external("massa", "assembly_script_set_call_coins")
export declare function setCallCoins(value: u64): void;

/**
* Add a new smart contract address to the ledger
*
Expand Down
9 changes: 8 additions & 1 deletion vm-mock/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ function resetLedger() {
let webModule;

const scCallMockStack = [];
let callCoins = BigInt(0); // Default value, coins for a call
let chainIdMock = BigInt(77658366); // Default value, chain id for Buildnet

/**
Expand Down Expand Up @@ -582,8 +583,14 @@ export default function createMockedABI(
return newArrayBuffer(addressStorage.get(k));
},

assembly_script_set_call_coins(coinAmount) {
callCoins = BigInt(coinAmount);
},

assembly_script_get_call_coins() {
return BigInt(0);
const coins = callCoins;
callCoins = BigInt(0);
return coins;
},

assembly_script_local_execution() {
Expand Down

0 comments on commit 2c50135

Please sign in to comment.