Skip to content

Commit

Permalink
deprecate setCallCoin and add mockTransferredCoin
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjah committed Feb 28, 2024
1 parent edb96ff commit daf698f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
9 changes: 4 additions & 5 deletions assembly/__tests__/env-coins.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// This file is aim to test the env coins related functions which are external functions

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

const testAddress = new Address(
'AU12E6N5BFAdC2wyiBV6VJjqkWhpz1kLVp2XpbRdSnL1mKjCWT6oR',
Expand Down Expand Up @@ -69,8 +68,8 @@ describe('Testing env coins related functions', () => {
});

it('callCoins', () => {
setCallCoins(100);
mockTransferredCoins(100);
expect(env.callCoins()).toBe(100);
setCallCoins(0);
mockTransferredCoins(0);
});
});
6 changes: 3 additions & 3 deletions assembly/std/__tests__/context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
timestamp,
} from '../context';
import { validateAddress, json2Address } from '../utils';
import { setCallCoins } from '../../vm-mock';
import { mockTransferredCoins } from '../../vm-mock';

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

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

test('isDeployingContract', () => {
Expand Down
24 changes: 24 additions & 0 deletions assembly/vm-mock/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export declare function mockScCall(value: StaticArray<u8>): void;
/**
* Set mock for call coins.
*
* @deprecated
* Use `mockTransferredCoins` instead.
* @remarks
* This function is used to mock the call coins value for test purpose.
* Don't forget to reset the mock after the test.
Expand All @@ -60,6 +62,28 @@ export declare function mockScCall(value: StaticArray<u8>): void;
@external("massa", "assembly_script_set_call_coins")
export declare function setCallCoins(value: u64): void;

/**
* Set mock for call coins.
*
* @remarks
* This function is used to mock the call coins value for test purpose.
* Don't forget to reset the mock after the test.
*
* @example
* ```typescript
* test('mocked SC call', () => {
* const coins: u64 = 123;
* mockTransferredCoins(coins);
* const res = transferredCoins();
* expect(res).toBe(coins);
* mockTransferredCoins(0); // Don't forget to reset the mock
* });
* ```
*/
@external("massa", "assembly_script_set_call_coins")
export declare function mockTransferredCoins(value: u64): void;

/**
* Add a new smart contract address to the ledger
*
Expand Down

0 comments on commit daf698f

Please sign in to comment.