|
| 1 | +import { Account, Bytes } from '@algorandfoundation/algorand-typescript' |
| 2 | +import { TestExecutionContext } from '@algorandfoundation/algorand-typescript-testing' |
| 3 | +import { Address, interpretAsArc4 } from '@algorandfoundation/algorand-typescript/arc4' |
| 4 | +import { ABIType, encodeAddress } from 'algosdk' |
| 5 | +import { afterEach, describe, expect, test } from 'vitest' |
| 6 | +import { ABI_RETURN_VALUE_LOG_PREFIX } from '../../src/constants' |
| 7 | +import { asUint8Array } from '../util' |
| 8 | + |
| 9 | +const abiAddressType = ABIType.from('address') |
| 10 | +const testData = [ |
| 11 | + Bytes.fromHex('00'.repeat(32)), |
| 12 | + Bytes.fromHex('01'.repeat(32)), |
| 13 | + Bytes.fromHex('ff'.repeat(32)), |
| 14 | + Bytes.fromHex(`${'00'.repeat(31)}ff`), |
| 15 | +] |
| 16 | + |
| 17 | +describe('arc4.Address', async () => { |
| 18 | + const ctx = new TestExecutionContext() |
| 19 | + afterEach(async () => { |
| 20 | + ctx.reset() |
| 21 | + }) |
| 22 | + |
| 23 | + test.each(testData)('create address from bytes', async (value) => { |
| 24 | + const sdkResult = abiAddressType.encode(asUint8Array(value)) |
| 25 | + const result = new Address(value) |
| 26 | + expect(result.bytes).toEqual(sdkResult) |
| 27 | + }) |
| 28 | + test.each(testData)('create address from str', async (value) => { |
| 29 | + const stringValue = encodeAddress(asUint8Array(value)) |
| 30 | + const sdkResult = abiAddressType.encode(stringValue) |
| 31 | + const result = new Address(stringValue) |
| 32 | + expect(result.bytes).toEqual(sdkResult) |
| 33 | + }) |
| 34 | + test.each(testData)('create address from Account', async (value) => { |
| 35 | + const accountValue = Account(value) |
| 36 | + const sdkResult = abiAddressType.encode(asUint8Array(accountValue.bytes)) |
| 37 | + const result = new Address(accountValue) |
| 38 | + expect(result.bytes).toEqual(sdkResult) |
| 39 | + }) |
| 40 | + |
| 41 | + test.each(testData)('get item from address created from bytes', async (value) => { |
| 42 | + const uint8ArrayValue = asUint8Array(value) |
| 43 | + const result = new Address(value) |
| 44 | + let i = 0 |
| 45 | + for (const entry of result) { |
| 46 | + expect(entry.native).toEqual(uint8ArrayValue[i++]) |
| 47 | + } |
| 48 | + expect(result.length).toEqual(uint8ArrayValue.length) |
| 49 | + }) |
| 50 | + test.each(testData)('get item from address created from str', async (value) => { |
| 51 | + const uint8ArrayValue = asUint8Array(value) |
| 52 | + const stringValue = encodeAddress(uint8ArrayValue) |
| 53 | + const result = new Address(stringValue) |
| 54 | + let i = 0 |
| 55 | + for (const entry of result) { |
| 56 | + expect(entry.native).toEqual(uint8ArrayValue[i++]) |
| 57 | + } |
| 58 | + expect(result.length).toEqual(uint8ArrayValue.length) |
| 59 | + }) |
| 60 | + test.each(testData)('get item from address created from Account', async (value) => { |
| 61 | + const uint8ArrayValue = asUint8Array(value) |
| 62 | + const accountValue = Account(value) |
| 63 | + const result = new Address(accountValue) |
| 64 | + let i = 0 |
| 65 | + for (const entry of result) { |
| 66 | + expect(entry.native).toEqual(uint8ArrayValue[i++]) |
| 67 | + } |
| 68 | + expect(result.length).toEqual(uint8ArrayValue.length) |
| 69 | + }) |
| 70 | + |
| 71 | + test.each(testData)('fromBytes method', async (value) => { |
| 72 | + const sdkResult = abiAddressType.encode(asUint8Array(value)) |
| 73 | + const result = interpretAsArc4<Address>(value) |
| 74 | + expect(result.bytes).toEqual(sdkResult) |
| 75 | + }) |
| 76 | + |
| 77 | + test.each(testData)('fromLog method', async (value) => { |
| 78 | + const sdkResult = abiAddressType.encode(asUint8Array(value)) |
| 79 | + const paddedValue = Bytes([...asUint8Array(ABI_RETURN_VALUE_LOG_PREFIX), ...asUint8Array(value)]) |
| 80 | + const result = interpretAsArc4<Address>(paddedValue, 'log') |
| 81 | + expect(result.bytes).toEqual(sdkResult) |
| 82 | + }) |
| 83 | +}) |
0 commit comments