Skip to content

Commit

Permalink
vm-mock: fix callee balance
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjah committed Aug 20, 2024
1 parent 5f9d603 commit 42343ce
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
22 changes: 5 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 23 additions & 12 deletions vm-mock/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import sha3 from 'js-sha3';
*/

// Those both addresses have been randomly generated
let defaultCallerAddress = 'AU12UBnqTHDQALpocVBnkPNy7y5CndUJQTLutaVDDFgMJcq5kQiKq';
let defaultContractAddress = 'AS12BqZEQ6sByhRLyEuf0YbQmcF2PsDdkNNG1akBJu9XcjZA1eT';
let defaultCallerAddress =
'AU12UBnqTHDQALpocVBnkPNy7y5CndUJQTLutaVDDFgMJcq5kQiKq';
let defaultContractAddress =
'AS12BqZEQ6sByhRLyEuf0YbQmcF2PsDdkNNG1akBJu9XcjZA1eT';

let mockedOriginOpId = '';

Expand Down Expand Up @@ -134,7 +136,11 @@ function getCallee() {
*
*/
function getCalleeBalance() {
return BigInt(ledger.get(getCallee()).balance) + callCoins - spentCoins;
let calleeBalance = 0n;
if (ledger.has(getCallee())) {
calleeBalance = BigInt(ledger.get(getCallee()).balance);
}
return calleeBalance + callCoins - spentCoins;
}

/**
Expand Down Expand Up @@ -489,8 +495,10 @@ export default function createMockedABI(

assembly_script_call(_address, method, _param, coins) {
if (scCallMockStack.length) {
if(BigInt(coins) > getCalleeBalance()) {
ERROR('Not enough balance to pay the call to ' + ptrToString(method));
if (BigInt(coins) > getCalleeBalance()) {
ERROR(
'Not enough balance to pay the call to ' + ptrToString(method),
);
}
spentCoins += BigInt(coins);
return newArrayBuffer(scCallMockStack.shift());
Expand All @@ -512,7 +520,7 @@ export default function createMockedABI(
assembly_script_set_deploy_context(addrPtr) {
adminContext = true;
let caller = getCaller();
if(addrPtr) {
if (addrPtr) {
caller = ptrToString(addrPtr);
if (!ledger.has(caller)) {
// add the new address to the ledger
Expand Down Expand Up @@ -623,22 +631,25 @@ export default function createMockedABI(
},

assembly_script_set_call_coins(coinAmount) {
const amount = BigInt(coinAmount);
if(amount == 0n) {
const amount = BigInt(coinAmount);
if (amount == 0n) {
callCoins = 0n;
return;
}
const caller = ledger.get(getCaller());
if (!caller) {
ERROR(`Unable to add coins for contract call. Address ${getCaller()} does not exists in ledger.`);
ERROR(
`Unable to add coins for contract call. Address ${getCaller()} does not exists in ledger.`,
);
}

if (caller.balance < amount) {
ERROR(`${getCaller()} has not enough balance to pay ${amount.toString()} coins.`);
ERROR(
`${getCaller()} has not enough balance to pay ${amount.toString()} coins.`,
);
}
callCoins = amount;
caller.balance -= callCoins;

},

assembly_script_get_call_coins() {
Expand Down Expand Up @@ -807,7 +818,7 @@ export default function createMockedABI(
const addr = ptrToString(aPtr);
if (!ledger.has(addr)) return 0n;

if(addr === getCallee()) {
if (addr === getCallee()) {
return getCalleeBalance();
}
return ledger.get(addr).balance;
Expand Down

0 comments on commit 42343ce

Please sign in to comment.