@algorandfoundation/algorand-typescript-testing 1.0.0-beta.2 → 1.0.0-beta.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +282 -0
- package/abi-metadata-BA4rupKi.js +90 -0
- package/abi-metadata-BA4rupKi.js.map +1 -0
- package/abi-metadata.d.ts +2 -2
- package/arc4-NIP5xu6D.js +27 -0
- package/arc4-NIP5xu6D.js.map +1 -0
- package/asset-params-C7s8ZU5u.js +845 -0
- package/asset-params-C7s8ZU5u.js.map +1 -0
- package/collections/custom-key-map.d.ts +5 -4
- package/context-helpers/context-manager.d.ts +7 -0
- package/context-helpers/context-util.d.ts +2 -2
- package/context-helpers/internal-context.d.ts +10 -8
- package/contract-G60Xb_gx.js +24 -0
- package/contract-G60Xb_gx.js.map +1 -0
- package/{runtime-helpers-DEtwEuFb.js → crypto-BV_GljWM.js} +885 -837
- package/crypto-BV_GljWM.js.map +1 -0
- package/decode-logs.d.ts +1 -1
- package/encoders.d.ts +4 -3
- package/errors.d.ts +27 -1
- package/impl/acct-params.d.ts +7 -6
- package/impl/app-global.d.ts +2 -2
- package/impl/app-local.d.ts +2 -2
- package/impl/app-params.d.ts +4 -3
- package/impl/asset-holding.d.ts +2 -2
- package/impl/asset-params.d.ts +4 -3
- package/impl/base-32.d.ts +2 -0
- package/impl/base-contract.d.ts +9 -0
- package/impl/base.d.ts +4 -3
- package/impl/block.d.ts +15 -2
- package/impl/box.d.ts +2 -2
- package/impl/compiled.d.ts +2 -2
- package/impl/contract.d.ts +10 -0
- package/impl/crypto.d.ts +22 -12
- package/impl/encoded-types.d.ts +22 -20
- package/impl/ensure-budget.d.ts +2 -1
- package/impl/global.d.ts +7 -3
- package/impl/gtxn.d.ts +9 -2
- package/impl/index.d.ts +2 -0
- package/impl/inner-transactions.d.ts +3 -2
- package/impl/itxn.d.ts +4 -4
- package/impl/log.d.ts +3 -0
- package/impl/logicSigArg.d.ts +3 -2
- package/impl/match.d.ts +1 -1
- package/impl/online-stake.d.ts +2 -0
- package/impl/primitives.d.ts +211 -0
- package/impl/pure.d.ts +37 -30
- package/impl/reference.d.ts +93 -0
- package/impl/scratch.d.ts +4 -4
- package/impl/state.d.ts +15 -14
- package/impl/transactions.d.ts +39 -37
- package/impl/txn.d.ts +4 -3
- package/impl/urange.d.ts +2 -2
- package/impl/voter-params.d.ts +7 -0
- package/index.d.ts +3 -14
- package/index.mjs +831 -3518
- package/index.mjs.map +1 -1
- package/inner-transactions-BAfZjF8Z.js +852 -0
- package/inner-transactions-BAfZjF8Z.js.map +1 -0
- package/internal/arc4.d.ts +2 -0
- package/internal/arc4.mjs +15 -0
- package/internal/arc4.mjs.map +1 -0
- package/internal/index.d.ts +36 -0
- package/internal/index.mjs +169 -0
- package/internal/index.mjs.map +1 -0
- package/internal/op.d.ts +19 -0
- package/internal/op.mjs +16 -0
- package/internal/op.mjs.map +1 -0
- package/op-D2giGNOW.js +1812 -0
- package/op-D2giGNOW.js.map +1 -0
- package/package.json +24 -7
- package/{test-transformer/index.mjs → program-factory-DcJH2vdq.js} +49 -44
- package/program-factory-DcJH2vdq.js.map +1 -0
- package/runtime-helpers-Clcv2xyQ.js +351 -0
- package/runtime-helpers-Clcv2xyQ.js.map +1 -0
- package/runtime-helpers.d.ts +1 -6
- package/runtime-helpers.mjs +7 -4
- package/runtime-helpers.mjs.map +1 -1
- package/set-up.d.ts +21 -1
- package/subcontexts/contract-context.d.ts +17 -5
- package/subcontexts/ledger-context.d.ts +146 -26
- package/subcontexts/transaction-context.d.ts +177 -31
- package/test-execution-context.d.ts +110 -7
- package/test-transformer/jest-transformer.d.ts +37 -0
- package/test-transformer/jest-transformer.mjs +52 -0
- package/test-transformer/jest-transformer.mjs.map +1 -0
- package/test-transformer/node-factory.d.ts +2 -2
- package/test-transformer/program-factory.d.ts +7 -0
- package/test-transformer/visitors.d.ts +1 -1
- package/test-transformer/vitest-transformer.d.ts +34 -0
- package/test-transformer/vitest-transformer.mjs +56 -0
- package/test-transformer/vitest-transformer.mjs.map +1 -0
- package/typescript-helpers-CAukFXpp.js +18 -0
- package/typescript-helpers-CAukFXpp.js.map +1 -0
- package/typescript-helpers.d.ts +4 -0
- package/util.d.ts +50 -20
- package/value-generators/arc4.d.ts +15 -15
- package/value-generators/avm.d.ts +53 -13
- package/value-generators/txn.d.ts +37 -3
- package/impl/account.d.ts +0 -32
- package/impl/application.d.ts +0 -30
- package/impl/asset.d.ts +0 -24
- package/runtime-helpers-DEtwEuFb.js.map +0 -1
- package/test-transformer/index.d.ts +0 -6
- package/test-transformer/index.mjs.map +0 -1
package/README.md
ADDED
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
# Algorand TypeScript Testing
|
|
2
|
+
|
|
3
|
+
[](https://github.com/algorandfoundation/algorand-typescript-testing/)
|
|
4
|
+
[](https://developer.algorand.org/algokit/)
|
|
5
|
+
[](https://github.com/algorandfoundation/algorand-typescript-testing)
|
|
6
|
+
[](https://github.com/algorandfoundation/algorand-typescript-testing/)
|
|
7
|
+
|
|
8
|
+
`algorand-typescript-testing` is a companion package to [Algorand Typescript](https://github.com/algorandfoundation/puya-ts/tree/main/packages/algo-ts) that enables efficient unit testing of Algorand TypeScript smart contracts in an offline environment. This package emulates key AVM behaviors without requiring a network connection, offering fast and reliable testing capabilities with a familiar TypeScript interface.
|
|
9
|
+
|
|
10
|
+
The `algorand-typescript-testing` package provides:
|
|
11
|
+
|
|
12
|
+
- A simple interface for fast and reliable unit testing
|
|
13
|
+
- An offline testing environment that simulates core AVM functionality
|
|
14
|
+
- A familiar TypeScript experience, compatible with testing frameworks like [vitest](https://vitest.dev/), and [jest](https://jestjs.io/)
|
|
15
|
+
|
|
16
|
+
## Quick Start
|
|
17
|
+
|
|
18
|
+
`algorand-typescript` is a prerequisite for `algorand-typescript-testing`, providing stubs and type annotations for Algorand TypeScript syntax. It enhances code completion and type checking when writing smart contracts. Note that this code isn't directly executable in standard Node.js environment; it's compiled by `puya-ts` into TEAL for Algorand Network deployment.
|
|
19
|
+
|
|
20
|
+
Traditionally, testing Algorand smart contracts involved deployment on sandboxed networks and interacting with live instances. While robust, this approach can be inefficient and lacks versatility for testing Algorand TypeScript code.
|
|
21
|
+
|
|
22
|
+
Enter `algorand-typescript-testing`: it leverages TypeScript's rich testing ecosystem for unit testing without network deployment. This enables rapid iteration and granular logic testing.
|
|
23
|
+
|
|
24
|
+
> **NOTE**: While `algorand-typescript-testing` offers valuable unit testing capabilities, it's not a replacement for comprehensive testing. Use it alongside other test types, particularly those running against the actual Algorand Network, for thorough contract validation.
|
|
25
|
+
|
|
26
|
+
### Prerequisites
|
|
27
|
+
|
|
28
|
+
- Python 3.12 or later
|
|
29
|
+
- [Algorand Python](https://github.com/algorandfoundation/puya)
|
|
30
|
+
- Node.js 20.x or later
|
|
31
|
+
- [Algorand TypeScript](https://github.com/algorandfoundation/puya-ts)
|
|
32
|
+
|
|
33
|
+
### Installation
|
|
34
|
+
|
|
35
|
+
`algorand-typescript-testing` is distributed via [npm](https://www.npmjs.com/package/@algorandfoundation/algorand-typescript-testing/). Install the package using `npm`:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
npm i @algorandfoundation/algorand-typescript-testing
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Testing your first contract
|
|
42
|
+
|
|
43
|
+
Let's write a simple contract and test it using the `algorand-typescript-testing` framework.
|
|
44
|
+
|
|
45
|
+
#### Configuring vitest
|
|
46
|
+
|
|
47
|
+
If you are using [vitest](https://vitest.dev/) with [@rollup/plugin-typescript](https://www.npmjs.com/package/@rollup/plugin-typescript) plugin, configure `puyaTsTransformer` as a `before` stage transformer of `typescript` plugin in `vitest.config.mts` file.
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import typescript from '@rollup/plugin-typescript'
|
|
51
|
+
import { defineConfig } from 'vitest/config'
|
|
52
|
+
import { puyaTsTransformer } from '@algorandfoundation/algorand-typescript-testing/vitest-transformer'
|
|
53
|
+
|
|
54
|
+
export default defineConfig({
|
|
55
|
+
esbuild: {},
|
|
56
|
+
test: {
|
|
57
|
+
setupFiles: 'vitest.setup.ts',
|
|
58
|
+
},
|
|
59
|
+
plugins: [
|
|
60
|
+
typescript({
|
|
61
|
+
transformers: {
|
|
62
|
+
before: [puyaTsTransformer],
|
|
63
|
+
},
|
|
64
|
+
}),
|
|
65
|
+
],
|
|
66
|
+
})
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
`algorand-typescript-testing` package also exposes additional equality testers which enables the smart contract developers to write terser test by avoiding type casting in assertions. It can setup in `beforeAll` hook point in the setup file, `vitest.setup.ts`.
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
import { beforeAll, expect } from 'vitest'
|
|
73
|
+
import { addEqualityTesters } from '@algorandfoundation/algorand-typescript-testing'
|
|
74
|
+
|
|
75
|
+
beforeAll(() => {
|
|
76
|
+
addEqualityTesters({ expect })
|
|
77
|
+
})
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
#### Configuring jest
|
|
81
|
+
|
|
82
|
+
If you are using [jest](https://jestjs.io/) with [ts-jest](https://www.npmjs.com/package/ts-jest), [@jest/globals](https://www.npmjs.com/package/@jest/globals) and [ts-node](https://www.npmjs.com/package/ts-node) plugins, configure `puyaTsTransformer` as a `before` stage transformer of `typescript` plugin in `jest.config.ts` file.
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
import { createDefaultEsmPreset, type JestConfigWithTsJest } from 'ts-jest'
|
|
86
|
+
|
|
87
|
+
const presetConfig = createDefaultEsmPreset({})
|
|
88
|
+
const jestConfig: JestConfigWithTsJest = {
|
|
89
|
+
...presetConfig,
|
|
90
|
+
testMatch: ['**/*.test.ts'],
|
|
91
|
+
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
|
|
92
|
+
transform: {
|
|
93
|
+
'^.+\\.tsx?$': [
|
|
94
|
+
'ts-jest',
|
|
95
|
+
{
|
|
96
|
+
useESM: true,
|
|
97
|
+
astTransformers: {
|
|
98
|
+
before: ['node_modules/@algorandfoundation/algorand-typescript-testing/test-transformer/jest-transformer.mjs'],
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
],
|
|
102
|
+
},
|
|
103
|
+
extensionsToTreatAsEsm: ['.ts'],
|
|
104
|
+
}
|
|
105
|
+
export default jestConfig
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
`algorand-typescript-testing` package also exposes additional equality testers which enables the smart contract developers to write terser test by avoiding type casting in assertions. It can setup in `beforeAll` hook point in the setup file, `jest.setup.ts`.
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
import { beforeAll, expect } from '@jest/globals'
|
|
112
|
+
import { addEqualityTesters } from '@algorandfoundation/algorand-typescript-testing'
|
|
113
|
+
|
|
114
|
+
beforeAll(() => {
|
|
115
|
+
addEqualityTesters({ expect })
|
|
116
|
+
})
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
It is also handy to add in a script to run `jest` with `--experimental-vm-modules` flag in `package.json`.
|
|
120
|
+
|
|
121
|
+
```json
|
|
122
|
+
{
|
|
123
|
+
"name": "puya-ts-demo",
|
|
124
|
+
"scripts": {
|
|
125
|
+
"test:jest": "tsc && node --experimental-vm-modules node_modules/jest/bin/jest"
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
There is also a patch file `ts-jest+29.2.5.patch` that needs to be applied to `ts-jest` package to for the `puyaTsTransformer` to work with the test files.
|
|
131
|
+
|
|
132
|
+
1. Place the file in `<rootDir>\patches` folder.
|
|
133
|
+
1. Install [patch-package](https://www.npmjs.com/package/patch-package) package as a dev dependency.
|
|
134
|
+
1. Add `"postinstall": "patch-package",` script in `package.json` file.
|
|
135
|
+
The patch will then be applied with every `npm install` call.
|
|
136
|
+
|
|
137
|
+
```patch
|
|
138
|
+
diff --git a/node_modules/ts-jest/dist/legacy/compiler/ts-compiler.js b/node_modules/ts-jest/dist/legacy/compiler/ts-compiler.js
|
|
139
|
+
index 5198f8f..addb47c 100644
|
|
140
|
+
--- a/node_modules/ts-jest/dist/legacy/compiler/ts-compiler.js
|
|
141
|
+
+++ b/node_modules/ts-jest/dist/legacy/compiler/ts-compiler.js
|
|
142
|
+
@@ -234,7 +234,7 @@ var TsCompiler = /** @class */ (function () {
|
|
143
|
+
var _a;
|
|
144
|
+
// Initialize memory cache for typescript compiler
|
|
145
|
+
this._parsedTsConfig.fileNames
|
|
146
|
+
- .filter(function (fileName) { return constants_1.TS_TSX_REGEX.test(fileName) && !_this.configSet.isTestFile(fileName); })
|
|
147
|
+
+ .filter(function (fileName) { return constants_1.TS_TSX_REGEX.test(fileName); })
|
|
148
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
149
|
+
.forEach(function (fileName) { return _this._fileVersionCache.set(fileName, 0); });
|
|
150
|
+
/* istanbul ignore next */
|
|
151
|
+
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
After the setup, the examples provided using `vitest` can be converted to work with `jest` by simply swapping the `import {...} from 'vitest'` with `import {...} from '@jest/globals'`.
|
|
155
|
+
|
|
156
|
+
#### Contract Definition
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
import { arc4, assert, Bytes, GlobalState, gtxn, LocalState, op, Txn, uint64, Uint64 } from '@algorandfoundation/algorand-typescript'
|
|
160
|
+
|
|
161
|
+
export default class VotingContract extends arc4.Contract {
|
|
162
|
+
topic = GlobalState({ initialValue: 'default_topic', key: Bytes('topic') })
|
|
163
|
+
votes = GlobalState({ initialValue: Uint64(0), key: Bytes('votes') })
|
|
164
|
+
voted = LocalState<uint64>({ key: Bytes('voted') })
|
|
165
|
+
|
|
166
|
+
@arc4.abimethod()
|
|
167
|
+
public setTopic(topic: string): void {
|
|
168
|
+
this.topic.value = topic
|
|
169
|
+
}
|
|
170
|
+
@arc4.abimethod()
|
|
171
|
+
public vote(pay: gtxn.PaymentTxn): boolean {
|
|
172
|
+
assert(op.Global.groupSize === 2, 'Expected 2 transactions')
|
|
173
|
+
assert(pay.amount === 10_000, 'Incorrect payment amount')
|
|
174
|
+
assert(pay.sender === Txn.sender, 'Payment sender must match transaction sender')
|
|
175
|
+
|
|
176
|
+
if (this.voted(Txn.sender).hasValue) {
|
|
177
|
+
return false // Already voted
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
this.votes.value = this.votes.value + 1
|
|
181
|
+
this.voted(Txn.sender).value = 1
|
|
182
|
+
return true
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
@arc4.abimethod({ readonly: true })
|
|
186
|
+
public getVotes(): uint64 {
|
|
187
|
+
return this.votes.value
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
public clearStateProgram(): boolean {
|
|
191
|
+
return true
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
#### Test Definition
|
|
197
|
+
|
|
198
|
+
```typescript
|
|
199
|
+
import { Uint64 } from '@algorandfoundation/algorand-typescript'
|
|
200
|
+
import { TestExecutionContext } from '@algorandfoundation/algorand-typescript-testing'
|
|
201
|
+
import { afterEach, describe, expect, test } from 'vitest'
|
|
202
|
+
import VotingContract from './contract.algo'
|
|
203
|
+
|
|
204
|
+
describe('Voting contract', () => {
|
|
205
|
+
const ctx = new TestExecutionContext()
|
|
206
|
+
afterEach(() => {
|
|
207
|
+
ctx.reset()
|
|
208
|
+
})
|
|
209
|
+
|
|
210
|
+
test('vote function', () => {
|
|
211
|
+
// Initialize the contract within the testing context
|
|
212
|
+
const contract = ctx.contract.create(VotingContract)
|
|
213
|
+
|
|
214
|
+
const voter = ctx.defaultSender
|
|
215
|
+
const payment = ctx.any.txn.payment({
|
|
216
|
+
sender: voter,
|
|
217
|
+
amount: 10_000,
|
|
218
|
+
})
|
|
219
|
+
|
|
220
|
+
const result = contract.vote(payment)
|
|
221
|
+
expect(result).toEqual(true)
|
|
222
|
+
expect(contract.votes.value).toEqual(1)
|
|
223
|
+
expect(contract.voted(voter).value).toEqual(1)
|
|
224
|
+
})
|
|
225
|
+
|
|
226
|
+
test('setTopic function', () => {
|
|
227
|
+
// Initialize the contract within the testing context
|
|
228
|
+
const contract = ctx.contract.create(VotingContract)
|
|
229
|
+
|
|
230
|
+
const newTopic = ctx.any.string(10)
|
|
231
|
+
contract.setTopic(newTopic)
|
|
232
|
+
expect(contract.topic.value).toEqual(newTopic)
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
test('getVotes function', () => {
|
|
236
|
+
// Initialize the contract within the testing context
|
|
237
|
+
const contract = ctx.contract.create(VotingContract)
|
|
238
|
+
|
|
239
|
+
contract.votes.value = 5
|
|
240
|
+
const votes = contract.getVotes()
|
|
241
|
+
expect(votes).toEqual(5)
|
|
242
|
+
})
|
|
243
|
+
})
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
This example demonstrates key aspects of testing with `algorand-typescript-testing` for ARC4-based contracts:
|
|
247
|
+
|
|
248
|
+
1. ARC4 Contract Features:
|
|
249
|
+
|
|
250
|
+
- Use of `arc4.Contract` as the base class for the contract.
|
|
251
|
+
- ABI methods defined using the `@arc4.abimethod` decorator.
|
|
252
|
+
- Readonly method annotation with `@arc4.abimethod({readonly: true})`.
|
|
253
|
+
|
|
254
|
+
2. Testing ARC4 Contracts:
|
|
255
|
+
|
|
256
|
+
- Creation of an `arc4.Contract` instance within the test context.
|
|
257
|
+
- Use of `ctx.any` for generating random test data.
|
|
258
|
+
- Direct invocation of ABI methods on the contract instance.
|
|
259
|
+
|
|
260
|
+
3. Transaction Handling:
|
|
261
|
+
|
|
262
|
+
- Use of `ctx.any.txn` to create test transactions.
|
|
263
|
+
- Passing transaction objects as parameters to contract methods.
|
|
264
|
+
|
|
265
|
+
4. State Verification:
|
|
266
|
+
- Checking global and local state changes after method execution.
|
|
267
|
+
- Verifying return values from ABI methods.
|
|
268
|
+
|
|
269
|
+
> **NOTE**: Thorough testing is crucial in smart contract development due to their immutable nature post-deployment. Comprehensive unit and integration tests ensure contract validity and reliability. Optimizing for efficiency can significantly improve user experience by reducing transaction fees and simplifying interactions. Investing in robust testing and optimization practices is crucial and offers many benefits in the long run.
|
|
270
|
+
|
|
271
|
+
### Next steps
|
|
272
|
+
|
|
273
|
+
To dig deeper into the capabilities of `algorand-typescript-testing`, continue with the following sections.
|
|
274
|
+
|
|
275
|
+
#### Contents
|
|
276
|
+
|
|
277
|
+
- [Testing Guide](./docs/testing-guide/index.md)
|
|
278
|
+
- [Examples](./docs/examples.md)
|
|
279
|
+
- [Coverage](./docs/coverage.md)
|
|
280
|
+
- [FQA](./docs/faq.md)
|
|
281
|
+
- [API Reference](./docs/api.md)
|
|
282
|
+
- [Algorand TypeScript](./docs/algots.md)
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import js_sha512 from 'js-sha512';
|
|
2
|
+
import { al as getArc4TypeName$1 } from './crypto-BV_GljWM.js';
|
|
3
|
+
|
|
4
|
+
const AbiMetaSymbol = Symbol('AbiMetadata');
|
|
5
|
+
const isContractProxy = Symbol('isContractProxy');
|
|
6
|
+
const attachAbiMetadata = (contract, methodName, metadata) => {
|
|
7
|
+
const metadatas = (AbiMetaSymbol in contract ? contract[AbiMetaSymbol] : {});
|
|
8
|
+
metadatas[methodName] = metadata;
|
|
9
|
+
if (!(AbiMetaSymbol in contract)) {
|
|
10
|
+
Object.defineProperty(contract, AbiMetaSymbol, {
|
|
11
|
+
value: metadatas,
|
|
12
|
+
writable: true,
|
|
13
|
+
enumerable: false,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
const copyAbiMetadatas = (sourceContract, targetContract) => {
|
|
18
|
+
const metadatas = getContractAbiMetadata(sourceContract);
|
|
19
|
+
Object.defineProperty(targetContract, AbiMetaSymbol, {
|
|
20
|
+
value: metadatas,
|
|
21
|
+
writable: true,
|
|
22
|
+
enumerable: false,
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
const captureMethodConfig = (contract, methodName, config) => {
|
|
26
|
+
const metadata = getContractMethodAbiMetadata(contract, methodName);
|
|
27
|
+
metadata.onCreate = config?.onCreate ?? 'disallow';
|
|
28
|
+
metadata.allowActions = [].concat(config?.allowActions ?? 'NoOp');
|
|
29
|
+
};
|
|
30
|
+
const getContractAbiMetadata = (contract) => {
|
|
31
|
+
if (contract[isContractProxy]) {
|
|
32
|
+
return contract[AbiMetaSymbol];
|
|
33
|
+
}
|
|
34
|
+
const contractClass = contract.constructor;
|
|
35
|
+
const s = Object.getOwnPropertySymbols(contractClass).find((s) => s.toString() === AbiMetaSymbol.toString());
|
|
36
|
+
const metadatas = (s ? contractClass[s] : AbiMetaSymbol in contractClass ? contractClass[AbiMetaSymbol] : {});
|
|
37
|
+
return metadatas;
|
|
38
|
+
};
|
|
39
|
+
const getContractMethodAbiMetadata = (contract, methodName) => {
|
|
40
|
+
const metadatas = getContractAbiMetadata(contract);
|
|
41
|
+
return metadatas[methodName];
|
|
42
|
+
};
|
|
43
|
+
const getArc4Signature = (metadata) => {
|
|
44
|
+
if (metadata.methodSignature === undefined) {
|
|
45
|
+
const argTypes = metadata.argTypes.map((t) => JSON.parse(t)).map(getArc4TypeName);
|
|
46
|
+
const returnType = getArc4TypeName(JSON.parse(metadata.returnType));
|
|
47
|
+
metadata.methodSignature = `${metadata.methodName}(${argTypes.join(',')})${returnType}`;
|
|
48
|
+
}
|
|
49
|
+
return metadata.methodSignature;
|
|
50
|
+
};
|
|
51
|
+
const getArc4Selector = (metadata) => {
|
|
52
|
+
const hash = js_sha512.sha512_256.array(getArc4Signature(metadata));
|
|
53
|
+
return new Uint8Array(hash.slice(0, 4));
|
|
54
|
+
};
|
|
55
|
+
const getArc4TypeName = (t) => {
|
|
56
|
+
const map = {
|
|
57
|
+
void: 'void',
|
|
58
|
+
account: 'account',
|
|
59
|
+
application: 'application',
|
|
60
|
+
asset: 'asset',
|
|
61
|
+
boolean: 'bool',
|
|
62
|
+
biguint: 'uint512',
|
|
63
|
+
bytes: 'byte[]',
|
|
64
|
+
string: 'string',
|
|
65
|
+
uint64: 'uint64',
|
|
66
|
+
OnCompleteAction: 'uint64',
|
|
67
|
+
TransactionType: 'uint64',
|
|
68
|
+
Transaction: 'txn',
|
|
69
|
+
PaymentTxn: 'pay',
|
|
70
|
+
KeyRegistrationTxn: 'keyreg',
|
|
71
|
+
AssetConfigTxn: 'acfg',
|
|
72
|
+
AssetTransferTxn: 'axfer',
|
|
73
|
+
AssetFreezeTxn: 'afrz',
|
|
74
|
+
ApplicationTxn: 'appl',
|
|
75
|
+
'Tuple(<.*>)?': (t) => `(${Object.values(t.genericArgs)
|
|
76
|
+
.map(getArc4TypeName)
|
|
77
|
+
.join(',')})`,
|
|
78
|
+
};
|
|
79
|
+
const entry = Object.entries(map).find(([k, _]) => new RegExp(`^${k}$`, 'i').test(t.name))?.[1];
|
|
80
|
+
if (entry === undefined) {
|
|
81
|
+
return getArc4TypeName$1(t) ?? t.name;
|
|
82
|
+
}
|
|
83
|
+
if (entry instanceof Function) {
|
|
84
|
+
return entry(t);
|
|
85
|
+
}
|
|
86
|
+
return entry;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export { getContractMethodAbiMetadata as a, getContractAbiMetadata as b, copyAbiMetadatas as c, attachAbiMetadata as d, captureMethodConfig as e, getArc4Selector as g, isContractProxy as i };
|
|
90
|
+
//# sourceMappingURL=abi-metadata-BA4rupKi.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"abi-metadata-BA4rupKi.js","sources":["../src/abi-metadata.ts"],"sourcesContent":["import type { BaseContract, Contract } from '@algorandfoundation/algorand-typescript'\nimport type { AbiMethodConfig, BareMethodConfig, CreateOptions, OnCompleteActionStr } from '@algorandfoundation/algorand-typescript/arc4'\nimport js_sha512 from 'js-sha512'\nimport type { TypeInfo } from './encoders'\nimport { getArc4TypeName as getArc4TypeNameForARC4Encoded } from './impl/encoded-types'\nimport type { DeliberateAny } from './typescript-helpers'\n\nexport interface AbiMetadata {\n methodName: string\n methodSignature: string | undefined\n argTypes: string[]\n returnType: string\n onCreate?: CreateOptions\n allowActions?: OnCompleteActionStr[]\n}\nconst AbiMetaSymbol = Symbol('AbiMetadata')\nexport const isContractProxy = Symbol('isContractProxy')\nexport const attachAbiMetadata = (contract: { new (): Contract }, methodName: string, metadata: AbiMetadata): void => {\n const metadatas: Record<string, AbiMetadata> = (AbiMetaSymbol in contract ? contract[AbiMetaSymbol] : {}) as Record<string, AbiMetadata>\n metadatas[methodName] = metadata\n if (!(AbiMetaSymbol in contract)) {\n Object.defineProperty(contract, AbiMetaSymbol, {\n value: metadatas,\n writable: true,\n enumerable: false,\n })\n }\n}\n\nexport const copyAbiMetadatas = <T extends BaseContract>(sourceContract: T, targetContract: T): void => {\n const metadatas = getContractAbiMetadata(sourceContract)\n Object.defineProperty(targetContract, AbiMetaSymbol, {\n value: metadatas,\n writable: true,\n enumerable: false,\n })\n}\n\nexport const captureMethodConfig = <T extends Contract>(\n contract: T,\n methodName: string,\n config?: AbiMethodConfig<T> | BareMethodConfig,\n): void => {\n const metadata = getContractMethodAbiMetadata(contract, methodName)\n metadata.onCreate = config?.onCreate ?? 'disallow'\n metadata.allowActions = ([] as OnCompleteActionStr[]).concat(config?.allowActions ?? 'NoOp')\n}\n\nexport const hasAbiMetadata = <T extends Contract>(contract: T): boolean => {\n const contractClass = contract.constructor as { new (): T }\n return (\n Object.getOwnPropertySymbols(contractClass).some((s) => s.toString() === AbiMetaSymbol.toString()) || AbiMetaSymbol in contractClass\n )\n}\nexport const getContractAbiMetadata = <T extends BaseContract>(contract: T): Record<string, AbiMetadata> => {\n if ((contract as DeliberateAny)[isContractProxy]) {\n return (contract as DeliberateAny)[AbiMetaSymbol] as Record<string, AbiMetadata>\n }\n const contractClass = contract.constructor as { new (): T }\n const s = Object.getOwnPropertySymbols(contractClass).find((s) => s.toString() === AbiMetaSymbol.toString())\n const metadatas: Record<string, AbiMetadata> = (\n s ? (contractClass as DeliberateAny)[s] : AbiMetaSymbol in contractClass ? contractClass[AbiMetaSymbol] : {}\n ) as Record<string, AbiMetadata>\n return metadatas\n}\n\nexport const getContractMethodAbiMetadata = <T extends BaseContract>(contract: T, methodName: string): AbiMetadata => {\n const metadatas = getContractAbiMetadata(contract)\n return metadatas[methodName]\n}\n\nexport const getArc4Signature = (metadata: AbiMetadata): string => {\n if (metadata.methodSignature === undefined) {\n const argTypes = metadata.argTypes.map((t) => JSON.parse(t) as TypeInfo).map(getArc4TypeName)\n const returnType = getArc4TypeName(JSON.parse(metadata.returnType) as TypeInfo)\n metadata.methodSignature = `${metadata.methodName}(${argTypes.join(',')})${returnType}`\n }\n return metadata.methodSignature\n}\n\nexport const getArc4Selector = (metadata: AbiMetadata): Uint8Array => {\n const hash = js_sha512.sha512_256.array(getArc4Signature(metadata))\n return new Uint8Array(hash.slice(0, 4))\n}\n\nconst getArc4TypeName = (t: TypeInfo): string => {\n const map: Record<string, string | ((t: TypeInfo) => string)> = {\n void: 'void',\n account: 'account',\n application: 'application',\n asset: 'asset',\n boolean: 'bool',\n biguint: 'uint512',\n bytes: 'byte[]',\n string: 'string',\n uint64: 'uint64',\n OnCompleteAction: 'uint64',\n TransactionType: 'uint64',\n Transaction: 'txn',\n PaymentTxn: 'pay',\n KeyRegistrationTxn: 'keyreg',\n AssetConfigTxn: 'acfg',\n AssetTransferTxn: 'axfer',\n AssetFreezeTxn: 'afrz',\n ApplicationTxn: 'appl',\n 'Tuple(<.*>)?': (t) =>\n `(${Object.values(t.genericArgs as Record<string, TypeInfo>)\n .map(getArc4TypeName)\n .join(',')})`,\n }\n const entry = Object.entries(map).find(([k, _]) => new RegExp(`^${k}$`, 'i').test(t.name))?.[1]\n if (entry === undefined) {\n return getArc4TypeNameForARC4Encoded(t) ?? t.name\n }\n if (entry instanceof Function) {\n return entry(t)\n }\n return entry\n}\n"],"names":["getArc4TypeNameForARC4Encoded"],"mappings":";;;AAeA,MAAM,aAAa,GAAG,MAAM,CAAC,aAAa,CAAC;MAC9B,eAAe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AC,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI;;AAEnD,IAAA,IAAI,KAAK,YAAY,QAAQ,EAAE;AAC7B,QAAA,OAAO,KAAK,CAAC,CAAC,CAAC;;AAEjB,IAAA,OAAO,KAAK;AACd,CAAC;;;;"}
|
package/abi-metadata.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { BaseContract, Contract } from '@algorandfoundation/algorand-typescript';
|
|
2
|
-
import { AbiMethodConfig, BareMethodConfig, CreateOptions, OnCompleteActionStr } from '@algorandfoundation/algorand-typescript/arc4';
|
|
1
|
+
import type { BaseContract, Contract } from '@algorandfoundation/algorand-typescript';
|
|
2
|
+
import type { AbiMethodConfig, BareMethodConfig, CreateOptions, OnCompleteActionStr } from '@algorandfoundation/algorand-typescript/arc4';
|
|
3
3
|
export interface AbiMetadata {
|
|
4
4
|
methodName: string;
|
|
5
5
|
methodSignature: string | undefined;
|
package/arc4-NIP5xu6D.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import * as arc4$1 from '@algorandfoundation/algorand-typescript/arc4';
|
|
2
|
+
import { C as Contract, a as abimethod, m as methodSelector } from './contract-G60Xb_gx.js';
|
|
3
|
+
|
|
4
|
+
function _mergeNamespaces(n, m) {
|
|
5
|
+
m.forEach(function (e) {
|
|
6
|
+
e && typeof e !== 'string' && !Array.isArray(e) && Object.keys(e).forEach(function (k) {
|
|
7
|
+
if (k !== 'default' && !(k in n)) {
|
|
8
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
9
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
get: function () { return e[k]; }
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
return Object.freeze(n);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
var arc4 = /*#__PURE__*/_mergeNamespaces({
|
|
20
|
+
__proto__: null,
|
|
21
|
+
Contract: Contract,
|
|
22
|
+
abimethod: abimethod,
|
|
23
|
+
methodSelector: methodSelector
|
|
24
|
+
}, [arc4$1]);
|
|
25
|
+
|
|
26
|
+
export { arc4 as a };
|
|
27
|
+
//# sourceMappingURL=arc4-NIP5xu6D.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"arc4-NIP5xu6D.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|