|
1 | | -import { ABIMethod } from '@algorandfoundation/algokit-abi' |
| 1 | +import { ABIMethod, ABITupleType, ABIType } from '@algorandfoundation/algokit-abi' |
2 | 2 | import { Address } from '@algorandfoundation/algokit-common' |
3 | 3 | import { Transaction, TransactionType } from '@algorandfoundation/algokit-transact' |
4 | 4 | import { beforeEach, describe, expect, test, vi } from 'vitest' |
@@ -424,4 +424,49 @@ describe('TransactionComposer', () => { |
424 | 424 | expect(resetSpy).toHaveBeenCalled() |
425 | 425 | }) |
426 | 426 | }) |
| 427 | + |
| 428 | + describe('ARC-4 tuple packing', () => { |
| 429 | + const uint8ArrayType = ABIType.from('uint8[]') |
| 430 | + const singleArray = uint8ArrayType.encode([1]) |
| 431 | + const twoArrays = new ABITupleType([uint8ArrayType, uint8ArrayType]).encode([[1], [1]]) |
| 432 | + const threeArrays = new ABITupleType([uint8ArrayType, uint8ArrayType, uint8ArrayType]).encode([[1], [1], [1]]) |
| 433 | + |
| 434 | + const testCases: { numAbiArgs: number; expectedTxnArgs: number; expectedLastArg: Uint8Array }[] = [ |
| 435 | + { numAbiArgs: 1, expectedTxnArgs: 2, expectedLastArg: singleArray }, |
| 436 | + { numAbiArgs: 13, expectedTxnArgs: 14, expectedLastArg: singleArray }, |
| 437 | + { numAbiArgs: 14, expectedTxnArgs: 15, expectedLastArg: singleArray }, |
| 438 | + { numAbiArgs: 15, expectedTxnArgs: 16, expectedLastArg: singleArray }, |
| 439 | + { numAbiArgs: 16, expectedTxnArgs: 16, expectedLastArg: twoArrays }, |
| 440 | + { numAbiArgs: 17, expectedTxnArgs: 16, expectedLastArg: threeArrays }, |
| 441 | + ] |
| 442 | + |
| 443 | + test.each(testCases)( |
| 444 | + 'should handle $numAbiArgs ABI args correctly (expecting $expectedTxnArgs txn args)', |
| 445 | + async ({ numAbiArgs, expectedTxnArgs, expectedLastArg }) => { |
| 446 | + const { algorand, context } = fixture |
| 447 | + const sender = context.testAccount |
| 448 | + |
| 449 | + // Build method signature with the specified number of uint8[] args |
| 450 | + const argsSignature = Array(numAbiArgs).fill('uint8[]').join(',') |
| 451 | + const method = ABIMethod.fromSignature(`args${numAbiArgs}(${argsSignature})void`) |
| 452 | + |
| 453 | + const composer = algorand.newGroup({ populateAppCallResources: false, coverAppCallInnerTransactionFees: false }) |
| 454 | + |
| 455 | + composer.addAppCallMethodCall({ |
| 456 | + appId: 1234n, |
| 457 | + method, |
| 458 | + sender, |
| 459 | + args: Array(numAbiArgs).fill([1]), // Each arg is [1] (a uint8 array with value 1) |
| 460 | + }) |
| 461 | + |
| 462 | + const built = await composer.build() |
| 463 | + const txn = built.transactions[0].txn |
| 464 | + |
| 465 | + const args = txn.appCall?.args ?? [] |
| 466 | + expect(args.length).toBe(expectedTxnArgs) |
| 467 | + expect(args[0]).toEqual(method.getSelector()) |
| 468 | + expect(args[args.length - 1]).toEqual(expectedLastArg) |
| 469 | + }, |
| 470 | + ) |
| 471 | + }) |
427 | 472 | }) |
0 commit comments