Skip to content

Commit

Permalink
vm-mock: get dynamuc caller/callee
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjah committed Jun 3, 2024
1 parent 352cca1 commit 61a8258
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 88 deletions.
40 changes: 32 additions & 8 deletions assembly/__tests__/vm-mock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
mockBalance,
mockOriginOperationId,
mockSetChainId,
mockTransferredCoins,
setDeployContext,
setLocalContext,
} from '../vm-mock/env';
Expand Down Expand Up @@ -220,11 +221,12 @@ describe('Testing mocked Context', () => {
expect(isDeployingContract()).toStrictEqual(true);
});

it('should set the same address for caller and callee when deploy context is set', () => {
setDeployContext('AS12BqZEQ6sByNCALLER');
it('should update the caller address', () => {
const deployerAddr = 'AS12BqZEQ6sByNCALLER';
setDeployContext(deployerAddr);

expect(caller()).toStrictEqual(new Address('AS12BqZEQ6sByNCALLER'));
expect(callee()).not.toStrictEqual(new Address('AS12BqZEQ6sByNCALLER'));
expect(caller()).toStrictEqual(new Address(deployerAddr));
expect(callee()).toStrictEqual(contractAddress);
});

it('should give a random distinct value to callerAddress when deploy context is set', () => {
Expand All @@ -239,9 +241,7 @@ describe('Testing mocked Context', () => {

setDeployContext();

expect(caller()).not.toStrictEqual(callerAddress);
expect(caller()).not.toStrictEqual(callee());

expect(caller()).toStrictEqual(callerAddress);
expect(callee()).toStrictEqual(contractAddress);
});

Expand Down Expand Up @@ -332,6 +332,10 @@ describe('Testing mocked origin operation id', () => {
});

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

it('mock balance', () => {
mockBalance(testAddress.toString(), 9999);
expect(balanceOf(testAddress.toString())).toBe(9999);
Expand All @@ -342,6 +346,12 @@ describe('balance mock', () => {
expect(balance()).toBe(9999);
});

it('mock contract balance with transferred coins', () => {
mockTransferredCoins(1000);
mockBalance(contractAddress.toString(), 9999);
expect(balance()).toBe(1000 + 9999);
});

it('mock contract balance and preserve storage', () => {
Storage.set('thekey', 'thevalue');
mockBalance(contractAddress.toString(), 9999);
Expand All @@ -351,9 +361,14 @@ describe('balance mock', () => {
});

describe('transfer coins', () => {
afterEach(() => {
resetStorage();
});

const amount = 666;
it('contract transfer amount to user', () => {
const initialContractBalance = balance();
const initialContractBalance = 987654321;
mockBalance(contractAddress.toString(), initialContractBalance);

transferCoins(testAddress, amount);
expect(balanceOf(testAddress.toString())).toBe(amount);
Expand Down Expand Up @@ -382,4 +397,13 @@ describe('transfer coins', () => {
expect(balance()).toBe(0);
transferCoins(testAddress, amount);
});

it('Use the caller transfered coins to process the transfer', () => {
mockBalance(contractAddress.toString(), 0);
expect(balance()).toBe(0);
mockTransferredCoins(amount);
expect(balance()).toBe(amount);
transferCoins(testAddress, amount);
expect(balance()).toBe(0);
});
});
13 changes: 3 additions & 10 deletions assembly/vm-mock/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,10 @@ export declare function addAddressToLedger(address: string): void;
export declare function mockAdminContext(isAdmin: bool): void;

/**
* Emulate a deployment context by giving the write access to all contracts
* as well a emulating a deployment for all of them.
* Emulate a deployment context by giving the write access to called contract.
* If callerAddress is not passed, uses the current call stack caller address as the caller address.
* If callerAddress is passed, the call stack will be updated to have the given address as the caller address.
*
* @remarks
* By default, the context as already callStack that emulates a deployment but has not write access.
* This function ensure that both are set.
*
* The deployment emulation is done by modifying the call stack to have different caller and callee addresses.
*
* If the given callerAddress is the same as the current contract address in the call stack,
* it is ignored to generate a different one.
*
* @param callerAddress - the optional caller address to use for the deployment emulation
*
Expand Down
Loading

0 comments on commit 61a8258

Please sign in to comment.