Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

213 mock coins related functions #214

Merged
merged 6 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Massa-as-sdk
![check-code-coverage](https://img.shields.io/badge/coverage-75%25-orange)
![check-code-coverage](https://img.shields.io/badge/coverage-76%25-orange)


Massa-as-sdk is a collection of tools, objects, and functions specifically designed for Massa smart contracts in AssemblyScript. This SDK enables you to import object classes, such as address and storage objects, and use them without having to write them from scratch every time. Additionally, it allows you to use Massa's ABI functions.

Expand Down
75 changes: 75 additions & 0 deletions assembly/__tests__/env-coins.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// 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 { Address, Storage } from '../std';
import { stringToBytes } from '@massalabs/as-types';

const testAddress = new Address(
'AU12E6N5BFAdC2wyiBV6VJjqkWhpz1kLVp2XpbRdSnL1mKjCWT6oR',
);

const testAddress2 = new Address(
'AU12E6N5BFAdC2wyiBV6VJjqkWhpz1kLVp2XpbRdSnL1mKjCWT6oT',
);

beforeEach(() => {
// We set the balance of the current contract to 100000 in the resetStorage
resetStorage();
});

describe('Testing env coins related functions', () => {
it('transferCoins', () => {
const amount: u64 = 100;
// The sender is the current contract executing the transfer
const senderBalance = env.balance();
const receiverCurrentBalance = env.balanceOf(testAddress.toString());
// given
expect(senderBalance).toBe(100000);
expect(receiverCurrentBalance).toBe(0);
// when
env.transferCoins(testAddress.toString(), amount);
// then
expect(env.balanceOf(testAddress.toString())).toBe(amount);
});

it('transferCoins of another address', () => {
const amount: u64 = 100;
// we first transfer coins to the emitter address
env.transferCoins(testAddress.toString(), amount);

const emitterCurrentBalance = env.balanceOf(testAddress.toString());
const receiverCurrentBalance = env.balanceOf(testAddress2.toString());
// given
expect(emitterCurrentBalance).toBeGreaterThanOrEqual(amount);
expect(receiverCurrentBalance).toBe(0);
// when
env.transferCoinsOf(
testAddress.toString(),
testAddress2.toString(),
amount,
);
// then
expect(env.balanceOf(testAddress2.toString())).toBe(amount);
});

it('get the balance of an address', () => {
const amount: u64 = 100;
// given
expect(env.balanceOf(testAddress.toString())).toBe(0);
// when
env.transferCoins(testAddress.toString(), amount);
// then
expect(env.balanceOf(testAddress.toString())).toBe(amount);
});

it('get the balance of the current address', () => {
// The balance of the current contract is 100000
expect(env.balance()).toBe(100000);
});

it('callCoins', () => {
// We don't have a way to set the call coins yet in the mock
expect(env.callCoins()).toBe(0);
});
});
67 changes: 67 additions & 0 deletions vm-mock/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ let callStack = callerAddress + ' , ' + contractAddress;
"key2" : "value2"
},
"Contract" : "./pathOfTheAssemblyScriptContract",
"Balance" : "100000
}
}
*/
Expand All @@ -62,10 +63,12 @@ function resetLedger() {
ledger.set(callerAddress, {
storage: new Map(),
contract: '',
balance: BigInt(100000),
});
ledger.set(contractAddress, {
storage: new Map(),
contract: '',
balance: BigInt(100000),
});
}

Expand Down Expand Up @@ -489,6 +492,70 @@ export default function createMockedABI(
const addressLedger = ledger.get(a);
return newArrayBuffer(addressLedger.contract);
},

assembly_script_transfer_coins(_addressPtr, _coinsAmount) {
const address = ptrToString(_addressPtr);
if (!ledger.has(address)) {
ledger.set(address, {
storage: new Map(),
contract: '',
balance: BigInt(0),
});
}
const callerBalance = ledger.get(callerAddress).balance;
if (callerBalance < BigInt(_coinsAmount)) {
throw new Error(
`Not enough balance to transfer ${_coinsAmount} coins.`,
);
}
ledger.get(callerAddress).balance -= BigInt(_coinsAmount);
ledger.get(address).balance += BigInt(_coinsAmount);
},

assembly_script_transfer_coins_for(
_addressFromPtr,
_addressToPtr,
_coinsAmount,
) {
const addressFrom = ptrToString(_addressFromPtr);
const addressTo = ptrToString(_addressToPtr);

if (!ledger.has(addressFrom)) {
throw new Error(
`Address ${addressFrom} does not exist in the ledger.`,
);
}

if (!ledger.has(addressTo)) {
ledger.set(addressTo, {
storage: new Map(),
contract: '',
balance: BigInt(0),
});
}

const addressFromBalance = ledger.get(addressFrom).balance;

if (addressFromBalance < BigInt(_coinsAmount)) {
throw new Error(
`Not enough balance to transfer ${_coinsAmount} coins.`,
);
}

ledger.get(addressFrom).balance -= BigInt(_coinsAmount);
ledger.get(addressTo).balance += BigInt(_coinsAmount);
},

assembly_script_get_balance() {
return BigInt(ledger.get(contractAddress).balance);
},

assembly_script_get_balance_for(aPtr) {
const a = ptrToString(aPtr);
if (!ledger.has(a)) return BigInt(0);

return BigInt(ledger.get(a).balance);
},
},
};

Expand Down