|
8 | 8 | import HathorWallet from '../../../src/new/wallet'; |
9 | 9 | import { TxTemplateContext } from '../../../src/template/transaction/context'; |
10 | 10 | import { WalletTxTemplateInterpreter } from '../../../src/template/transaction/interpreter'; |
| 11 | +import FeeHeader from '../../../src/headers/fee'; |
| 12 | +import { NATIVE_TOKEN_UID } from '../../../src/constants'; |
| 13 | +import { getDefaultLogger } from '../../../src/types'; |
11 | 14 |
|
12 | 15 | describe('Wallet tx-template interpreter', () => { |
13 | 16 | it('should get an address from the wallet', async () => { |
@@ -69,4 +72,143 @@ describe('Wallet tx-template interpreter', () => { |
69 | 72 | await expect(interpreter.getTokenDetails('fbt-token-uid')).resolves.toStrictEqual(mockValue); |
70 | 73 | expect(wallet.getTokenDetails).toHaveBeenCalledTimes(1); |
71 | 74 | }); |
| 75 | + |
| 76 | + describe('buildFeeHeader', () => { |
| 77 | + const token1 = '0000000110eb9ec96e255a09d6ae7d856bff53453773bae5500cee2905db670e'; |
| 78 | + const token2 = '0000000220eb9ec96e255a09d6ae7d856bff53453773bae5500cee2905db670f'; |
| 79 | + |
| 80 | + it('should build a FeeHeader with HTR fee entry', async () => { |
| 81 | + const wallet = { |
| 82 | + logger: getDefaultLogger(), |
| 83 | + } as unknown as HathorWallet; |
| 84 | + |
| 85 | + const interpreter = new WalletTxTemplateInterpreter(wallet); |
| 86 | + const tx = await interpreter.build([ |
| 87 | + { type: 'action/fee', token: NATIVE_TOKEN_UID, amount: 100n }, |
| 88 | + ]); |
| 89 | + |
| 90 | + expect(tx.headers).toHaveLength(1); |
| 91 | + expect(tx.headers[0]).toBeInstanceOf(FeeHeader); |
| 92 | + |
| 93 | + const feeHeader = tx.getFeeHeader(); |
| 94 | + expect(feeHeader).not.toBeNull(); |
| 95 | + expect(feeHeader!.entries).toHaveLength(1); |
| 96 | + expect(feeHeader!.entries[0].tokenIndex).toBe(0); // HTR is always index 0 |
| 97 | + expect(feeHeader!.entries[0].amount).toBe(100n); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should build a FeeHeader with custom token fee entry', async () => { |
| 101 | + const wallet = { |
| 102 | + logger: getDefaultLogger(), |
| 103 | + getTokenDetails: jest.fn().mockResolvedValue({ |
| 104 | + totalSupply: 1000n, |
| 105 | + totalTransactions: 1, |
| 106 | + tokenInfo: { name: 'Token1', symbol: 'TK1', version: 2 }, |
| 107 | + authorities: { mint: true, melt: true }, |
| 108 | + }), |
| 109 | + } as unknown as HathorWallet; |
| 110 | + |
| 111 | + const interpreter = new WalletTxTemplateInterpreter(wallet); |
| 112 | + const tx = await interpreter.build([ |
| 113 | + // First add the token to the context so it gets a token index |
| 114 | + { type: 'output/raw', script: 'cafe', amount: 10n, token: token1 }, |
| 115 | + // Then add the fee |
| 116 | + { type: 'action/fee', token: token1, amount: 500n }, |
| 117 | + ]); |
| 118 | + |
| 119 | + expect(tx.headers).toHaveLength(1); |
| 120 | + const feeHeader = tx.getFeeHeader(); |
| 121 | + expect(feeHeader).not.toBeNull(); |
| 122 | + expect(feeHeader!.entries).toHaveLength(1); |
| 123 | + expect(feeHeader!.entries[0].tokenIndex).toBe(1); // Custom token is index 1 |
| 124 | + expect(feeHeader!.entries[0].amount).toBe(500n); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should build a FeeHeader with multiple fee entries', async () => { |
| 128 | + const wallet = { |
| 129 | + logger: getDefaultLogger(), |
| 130 | + getTokenDetails: jest.fn().mockImplementation(token => { |
| 131 | + if (token === token1) { |
| 132 | + return Promise.resolve({ |
| 133 | + totalSupply: 1000n, |
| 134 | + totalTransactions: 1, |
| 135 | + tokenInfo: { name: 'Token1', symbol: 'TK1', version: 2 }, |
| 136 | + authorities: { mint: true, melt: true }, |
| 137 | + }); |
| 138 | + } |
| 139 | + if (token === token2) { |
| 140 | + return Promise.resolve({ |
| 141 | + totalSupply: 2000n, |
| 142 | + totalTransactions: 2, |
| 143 | + tokenInfo: { name: 'Token2', symbol: 'TK2', version: 2 }, |
| 144 | + authorities: { mint: true, melt: true }, |
| 145 | + }); |
| 146 | + } |
| 147 | + return Promise.reject(new Error('Unknown token')); |
| 148 | + }), |
| 149 | + } as unknown as HathorWallet; |
| 150 | + |
| 151 | + const interpreter = new WalletTxTemplateInterpreter(wallet); |
| 152 | + const tx = await interpreter.build([ |
| 153 | + // Add tokens to the context |
| 154 | + { type: 'output/raw', script: 'cafe', amount: 10n, token: token1 }, |
| 155 | + { type: 'output/raw', script: 'cafe', amount: 20n, token: token2 }, |
| 156 | + // Add fees for multiple tokens |
| 157 | + { type: 'action/fee', token: NATIVE_TOKEN_UID, amount: 100n }, |
| 158 | + { type: 'action/fee', token: token1, amount: 200n }, |
| 159 | + { type: 'action/fee', token: token2, amount: 300n }, |
| 160 | + ]); |
| 161 | + |
| 162 | + expect(tx.headers).toHaveLength(1); |
| 163 | + const feeHeader = tx.getFeeHeader(); |
| 164 | + expect(feeHeader).not.toBeNull(); |
| 165 | + expect(feeHeader!.entries).toHaveLength(3); |
| 166 | + |
| 167 | + // Find entries by tokenIndex |
| 168 | + const htrEntry = feeHeader!.entries.find(e => e.tokenIndex === 0); |
| 169 | + const token1Entry = feeHeader!.entries.find(e => e.tokenIndex === 1); |
| 170 | + const token2Entry = feeHeader!.entries.find(e => e.tokenIndex === 2); |
| 171 | + |
| 172 | + expect(htrEntry).toBeDefined(); |
| 173 | + expect(htrEntry!.amount).toBe(100n); |
| 174 | + |
| 175 | + expect(token1Entry).toBeDefined(); |
| 176 | + expect(token1Entry!.amount).toBe(200n); |
| 177 | + |
| 178 | + expect(token2Entry).toBeDefined(); |
| 179 | + expect(token2Entry!.amount).toBe(300n); |
| 180 | + }); |
| 181 | + |
| 182 | + it('should accumulate fees for the same token', async () => { |
| 183 | + const wallet = { |
| 184 | + logger: getDefaultLogger(), |
| 185 | + } as unknown as HathorWallet; |
| 186 | + |
| 187 | + const interpreter = new WalletTxTemplateInterpreter(wallet); |
| 188 | + const tx = await interpreter.build([ |
| 189 | + { type: 'action/fee', token: NATIVE_TOKEN_UID, amount: 50n }, |
| 190 | + { type: 'action/fee', token: NATIVE_TOKEN_UID, amount: 30n }, |
| 191 | + { type: 'action/fee', token: NATIVE_TOKEN_UID, amount: 20n }, |
| 192 | + ]); |
| 193 | + |
| 194 | + expect(tx.headers).toHaveLength(1); |
| 195 | + const feeHeader = tx.getFeeHeader(); |
| 196 | + expect(feeHeader).not.toBeNull(); |
| 197 | + expect(feeHeader!.entries).toHaveLength(1); |
| 198 | + expect(feeHeader!.entries[0].tokenIndex).toBe(0); |
| 199 | + expect(feeHeader!.entries[0].amount).toBe(100n); // 50 + 30 + 20 |
| 200 | + }); |
| 201 | + |
| 202 | + it('should not add FeeHeader when no fees are present', async () => { |
| 203 | + const wallet = { |
| 204 | + logger: getDefaultLogger(), |
| 205 | + } as unknown as HathorWallet; |
| 206 | + |
| 207 | + const interpreter = new WalletTxTemplateInterpreter(wallet); |
| 208 | + const tx = await interpreter.build([]); |
| 209 | + |
| 210 | + expect(tx.headers).toHaveLength(0); |
| 211 | + expect(tx.getFeeHeader()).toBeNull(); |
| 212 | + }); |
| 213 | + }); |
72 | 214 | }); |
0 commit comments