|
1 | | -import { Blockchain, BlockchainTransaction, SandboxContract, TreasuryContract } from '@ton/sandbox' |
| 1 | +import { Blockchain, printTransactionFees, SandboxContract, TreasuryContract } from '@ton/sandbox' |
2 | 2 | import { |
3 | 3 | toNano, |
4 | 4 | Address, |
@@ -28,7 +28,8 @@ import { newWithdrawableSpec } from '../lib/funding/WithdrawableSpec' |
28 | 28 | import * as ownable2step from '../../wrappers/libraries/access/Ownable2Step' |
29 | 29 | import * as UpgradeableSpec from '../lib/versioning/UpgradeableSpec' |
30 | 30 | import * as TypeAndVersionSpec from '../lib/versioning/TypeAndVersionSpec' |
31 | | -import { dump } from '../utils/prettyPrint' |
| 31 | +import { dump, prettifyAddressesMap } from '../utils/prettyPrint' |
| 32 | +import { mapOpcode } from '../utils/opcodes' |
32 | 33 |
|
33 | 34 | const CHAINSEL_EVM_TEST_90000001 = 909606746561742123n |
34 | 35 | const CHAINSEL_EVM_TEST_90000002 = 5548718428018410741n |
@@ -675,6 +676,85 @@ describe('Router', () => { |
675 | 676 | }) |
676 | 677 | }, |
677 | 678 | }) |
| 679 | + |
| 680 | + printTransactionFees(result.transactions, mapOpcode) |
| 681 | + const addresses = prettifyAddressesMap(result.transactions) |
| 682 | + |
| 683 | + result.transactions.forEach((tx) => { |
| 684 | + if ( |
| 685 | + tx.inMessage && |
| 686 | + tx.inMessage.info.type === 'internal' && |
| 687 | + tx.description.type === 'generic' |
| 688 | + ) { |
| 689 | + const inValue = tx.inMessage.info.value.coins |
| 690 | + const outValue = tx.outMessages |
| 691 | + .values() |
| 692 | + .reduce( |
| 693 | + (acc, msg) => acc + (msg.info.type === 'internal' ? msg.info.value.coins : 0n), |
| 694 | + 0n, |
| 695 | + ) |
| 696 | + |
| 697 | + const fees = { |
| 698 | + inFwdFee: tx.inMessage.info.forwardFee, |
| 699 | + gasFees: |
| 700 | + tx.description.computePhase.type === 'vm' ? tx.description.computePhase.gasFees : 0n, |
| 701 | + actionFees: tx.description.actionPhase?.totalActionFees ?? 0n, |
| 702 | + fwdFees: tx.description.actionPhase?.totalFwdFees ?? 0n, |
| 703 | + storageFees: tx.description.storagePhase?.storageFeesCollected ?? 0n, |
| 704 | + } |
| 705 | + const totalFees = [fees.actionFees, fees.gasFees, fees.storageFees].reduce( |
| 706 | + (a, b) => a + b, |
| 707 | + 0n, |
| 708 | + ) |
| 709 | + |
| 710 | + console.log( |
| 711 | + `Balance check for tx from ${addresses.get(tx.inMessage.info.src.toRawString())} to ${addresses.get(tx.inMessage.info.dest.toRawString())}:\n`, |
| 712 | + ) |
| 713 | + // table format |
| 714 | + console.table({ |
| 715 | + 'In Value': inValue, |
| 716 | + 'Out Value': outValue, |
| 717 | + 'In Fwd Fee': fees.inFwdFee, |
| 718 | + 'Gas Fees': fees.gasFees, |
| 719 | + 'Action Fees': fees.actionFees, |
| 720 | + 'Fwd Fees': fees.fwdFees, |
| 721 | + 'Storage Fees': fees.storageFees, |
| 722 | + 'Total Fees': totalFees, |
| 723 | + 'In Value - Out Value - Fees': inValue - outValue - totalFees, |
| 724 | + }) |
| 725 | + } |
| 726 | + }) |
| 727 | + |
| 728 | + // Verify balance handling: OnRamp doesn't lose balance on messageSent fees |
| 729 | + const finalOnRampBalance = (await blockchain.getContract(onRamp.address)).balance |
| 730 | + const rentFees = result.transactions |
| 731 | + .filter((tx) => { |
| 732 | + return ( |
| 733 | + tx.inMessage != null && |
| 734 | + tx.inMessage != undefined && |
| 735 | + tx.inMessage.info.dest != null && |
| 736 | + tx.inMessage.info.dest != undefined && |
| 737 | + tx.inMessage.info.dest instanceof Address && |
| 738 | + tx.inMessage.info.dest.equals(router.address) |
| 739 | + ) |
| 740 | + }) |
| 741 | + .reduce((acc, tx) => { |
| 742 | + switch (tx.description.type) { |
| 743 | + case 'generic': { |
| 744 | + const rentFee = tx.description.storagePhase?.storageFeesCollected ?? 0n |
| 745 | + return acc + rentFee |
| 746 | + } |
| 747 | + case 'storage': { |
| 748 | + const rentFee = tx.description.storagePhase.storageFeesCollected |
| 749 | + return acc + rentFee |
| 750 | + } |
| 751 | + } |
| 752 | + return acc |
| 753 | + }, 0n) |
| 754 | + |
| 755 | + // The final balance should be initial balance minus rent fees plus the fee that was paid |
| 756 | + // (the fee comes from the validated fee calculation above) |
| 757 | + expect(finalOnRampBalance).toBe(initialOnRampBalance - rentFees + amount.fee) |
678 | 758 | } |
679 | 759 | }) |
680 | 760 |
|
|
0 commit comments