|
| 1 | +import { AppSpec } from '@algorandfoundation/algokit-utils/types/app-spec' |
| 2 | +import { arc4, BigUint, biguint, Bytes, bytes, emit, Uint64, uint64 } from '@algorandfoundation/algorand-typescript' |
| 3 | +import { TestExecutionContext } from '@algorandfoundation/algorand-typescript-testing' |
| 4 | +import { afterEach, describe, expect, it } from 'vitest' |
| 5 | +import { MAX_UINT512, MAX_UINT64 } from '../../src/constants' |
| 6 | +import appSpecJson from '../artifacts/arc4-primitive-ops/data/Arc4PrimitiveOpsContract.arc32.json' |
| 7 | +import { getAlgorandAppClient, getAvmResultLog } from '../avm-invoker' |
| 8 | + |
| 9 | +import { asBigUintCls, asNumber, asUint8Array } from '../../src/util' |
| 10 | + |
| 11 | +class Swapped { |
| 12 | + a: string |
| 13 | + b: biguint |
| 14 | + c: uint64 |
| 15 | + d: bytes |
| 16 | + e: uint64 |
| 17 | + f: boolean |
| 18 | + g: bytes |
| 19 | + h: string |
| 20 | + |
| 21 | + constructor(a: string, b: biguint, c: uint64, d: bytes, e: uint64, f: boolean, g: bytes, h: string) { |
| 22 | + this.a = a |
| 23 | + this.b = b |
| 24 | + this.c = c |
| 25 | + this.d = d |
| 26 | + this.e = e |
| 27 | + this.f = f |
| 28 | + this.g = g |
| 29 | + this.h = h |
| 30 | + } |
| 31 | +} |
| 32 | +class SwappedArc4 extends arc4.Struct<{ |
| 33 | + m: arc4.UintN<64> |
| 34 | + n: arc4.UintN<256> |
| 35 | + o: arc4.UFixedNxM<32, 8> |
| 36 | + p: arc4.UFixedNxM<256, 16> |
| 37 | + q: arc4.Bool |
| 38 | + r: arc4.StaticArray<arc4.UintN8, 3> |
| 39 | + s: arc4.DynamicArray<arc4.UintN16> |
| 40 | + t: arc4.Tuple<[arc4.UintN32, arc4.UintN64, arc4.Str]> |
| 41 | +}> {} |
| 42 | + |
| 43 | +describe('arc4.emit', async () => { |
| 44 | + const appClient = await getAlgorandAppClient(appSpecJson as AppSpec) |
| 45 | + const ctx = new TestExecutionContext() |
| 46 | + |
| 47 | + afterEach(async () => { |
| 48 | + ctx.reset() |
| 49 | + }) |
| 50 | + |
| 51 | + it('should emit the correct values', async () => { |
| 52 | + const test_data = new Swapped('hello', BigUint(MAX_UINT512), Uint64(MAX_UINT64), Bytes('world'), 16, false, Bytes('test'), 'greetings') |
| 53 | + |
| 54 | + const test_data_arc4 = new SwappedArc4({ |
| 55 | + m: new arc4.UintN64(42), |
| 56 | + n: new arc4.UintN256(512), |
| 57 | + o: new arc4.UFixedNxM<32, 8>('42.94967295'), |
| 58 | + p: new arc4.UFixedNxM<256, 16>('25.5'), |
| 59 | + q: new arc4.Bool(true), |
| 60 | + r: new arc4.StaticArray(new arc4.UintN8(1), new arc4.UintN8(2), new arc4.UintN8(3)), |
| 61 | + s: new arc4.DynamicArray(new arc4.UintN16(1), new arc4.UintN16(2), new arc4.UintN16(3)), |
| 62 | + t: new arc4.Tuple(new arc4.UintN32(1), new arc4.UintN64(2), new arc4.Str('hello')), |
| 63 | + }) |
| 64 | + const avm_result = await getAvmResultLog( |
| 65 | + { appClient }, |
| 66 | + 'verify_emit', |
| 67 | + test_data.a, |
| 68 | + test_data.b.valueOf(), |
| 69 | + test_data.c.valueOf(), |
| 70 | + asUint8Array(test_data.d), |
| 71 | + test_data.e, |
| 72 | + test_data.f, |
| 73 | + asUint8Array(test_data.g), |
| 74 | + test_data.h, |
| 75 | + test_data_arc4.m.native.valueOf(), |
| 76 | + test_data_arc4.n.native.valueOf(), |
| 77 | + asBigUintCls(test_data_arc4.o.bytes).asBigInt(), |
| 78 | + asBigUintCls(test_data_arc4.p.bytes).asBigInt(), |
| 79 | + test_data_arc4.q.native, |
| 80 | + asUint8Array(test_data_arc4.r.bytes), |
| 81 | + asUint8Array(test_data_arc4.s.bytes), |
| 82 | + asUint8Array(test_data_arc4.t.bytes), |
| 83 | + ) |
| 84 | + |
| 85 | + expect(avm_result).toBeInstanceOf(Array) |
| 86 | + const avmLogs = avm_result?.map(Bytes) |
| 87 | + |
| 88 | + const dummy_app = ctx.any.application() |
| 89 | + const app_txn = ctx.any.txn.applicationCall({ appId: dummy_app }) |
| 90 | + ctx.txn.createScope([app_txn]).execute(() => { |
| 91 | + emit(test_data_arc4) |
| 92 | + emit( |
| 93 | + 'Swapped', |
| 94 | + test_data.a, |
| 95 | + test_data.b, |
| 96 | + test_data.c, |
| 97 | + test_data.d, |
| 98 | + test_data.e, |
| 99 | + test_data.f, |
| 100 | + test_data.g, |
| 101 | + test_data.h, |
| 102 | + test_data_arc4.m, |
| 103 | + test_data_arc4.n, |
| 104 | + test_data_arc4.o, |
| 105 | + test_data_arc4.p, |
| 106 | + test_data_arc4.q, |
| 107 | + test_data_arc4.r, |
| 108 | + test_data_arc4.s, |
| 109 | + test_data_arc4.t, |
| 110 | + ) |
| 111 | + emit( |
| 112 | + 'Swapped(string,uint512,uint64,byte[],uint64,bool,byte[],string,uint64,uint256,ufixed32x8,ufixed256x16,bool,uint8[3],uint16[],(uint32,uint64,string))', |
| 113 | + test_data.a, |
| 114 | + test_data.b, |
| 115 | + test_data.c, |
| 116 | + test_data.d, |
| 117 | + test_data.e, |
| 118 | + test_data.f, |
| 119 | + test_data.g, |
| 120 | + test_data.h, |
| 121 | + test_data_arc4.m, |
| 122 | + test_data_arc4.n, |
| 123 | + test_data_arc4.o, |
| 124 | + test_data_arc4.p, |
| 125 | + test_data_arc4.q, |
| 126 | + test_data_arc4.r, |
| 127 | + test_data_arc4.s, |
| 128 | + test_data_arc4.t, |
| 129 | + ) |
| 130 | + const arc4_result = [...Array(asNumber(app_txn.numLogs)).keys()].fill(0).map((_, i) => app_txn.logs(i)) |
| 131 | + |
| 132 | + expect(arc4_result[0]).toEqual(avmLogs![0]) |
| 133 | + expect(arc4_result[1]).toEqual(avmLogs![1]) |
| 134 | + expect(arc4_result[1]).toEqual(arc4_result[2]) |
| 135 | + expect(arc4_result[2]).toEqual(avmLogs![2]) |
| 136 | + }) |
| 137 | + }) |
| 138 | +}) |
0 commit comments