diff --git a/docs/capabilities/dispenser-client.md b/docs/capabilities/dispenser-client.md index 594d3ff8..07c60882 100644 --- a/docs/capabilities/dispenser-client.md +++ b/docs/capabilities/dispenser-client.md @@ -15,11 +15,11 @@ If both methods are used, the constructor argument takes precedence. import * as algokit from '@algorandfoundation/algokit-utils' // Using constructor argument -const client = algokit.getDispenserApiTestnetClient({ authToken: 'your_auth_token' }) +const client = algokit.getTestNetDispenserApiClient({ authToken: 'your_auth_token' }) // Using environment variable process.env['ALGOKIT_DISPENSER_ACCESS_TOKEN'] = 'your_auth_token' -const client = algokit.getDispenserApiTestnetClient() +const client = algokit.getTestNetDispenserApiClient() ``` ## Funding an Account diff --git a/docs/capabilities/transfer.md b/docs/capabilities/transfer.md index 0cc433b4..bd3be8fc 100644 --- a/docs/capabilities/transfer.md +++ b/docs/capabilities/transfer.md @@ -21,13 +21,13 @@ The `ensureFunded` function automatically funds an account to maintain a minimum - All properties in [`SendTransactionParams`](./transaction.md#sendtransactionparams) - `accountToFund: SendTransactionFrom | string` - The account that is to be funded -- `fundingSource?: SendTransactionFrom | DispenserApiTestnetClient` - The account that is the source of funds or a dispenser API client. If not specified, it will use the [dispenser](./account.md#dispenser) +- `fundingSource?: SendTransactionFrom | TestNetDispenserApiClient` - The account that is the source of funds or a dispenser API client. If not specified, it will use the [dispenser](./account.md#dispenser) - `minSpendingBalance: AlgoAmount` - The minimum balance of ALGOs that the account should have available to spend (i.e., on top of the minimum balance requirement) - `minFundingIncrement?: AlgoAmount` - When issuing a funding amount, the minimum amount to transfer. This avoids many small transfers if this function gets called often on an active account - `transactionParams?: SuggestedParams` - The optional [transaction parameters](./transaction.md#transaction-params) - `note?: TransactionNote` - The [transaction note](./transaction.md#transaction-notes) -The function calls Algod to find the current balance and minimum balance requirement, calculates the difference between those two numbers, and checks to see if it's more than the `minSpendingBalance`. If so, it will send the difference, or the `minFundingIncrement` if that is specified. If the `fundingSource` is an instance of `DispenserApiTestnetClient`, the function will use the dispenser API to fund the account. Refer to [algokit-cli documentation](https://github.com/algorandfoundation/algokit-cli/blob/main/docs/features/dispenser.md#ci-access-token) for details on obtaining an access token for AlgoKit TestNet Dispenser API. +The function calls Algod to find the current balance and minimum balance requirement, calculates the difference between those two numbers, and checks to see if it's more than the `minSpendingBalance`. If so, it will send the difference, or the `minFundingIncrement` if that is specified. If the `fundingSource` is an instance of `TestNetDispenserApiClient`, the function will use the dispenser API to fund the account. Refer to [algokit-cli documentation](https://github.com/algorandfoundation/algokit-cli/blob/main/docs/features/dispenser.md#ci-access-token) for details on obtaining an access token for AlgoKit TestNet Dispenser API. ## `transferAsset` diff --git a/docs/code/classes/types_dispenser_client.DispenserApiTestnetClient.md b/docs/code/classes/types_dispenser_client.TestNetDispenserApiClient.md similarity index 74% rename from docs/code/classes/types_dispenser_client.DispenserApiTestnetClient.md rename to docs/code/classes/types_dispenser_client.TestNetDispenserApiClient.md index f81fd841..fc053f5d 100644 --- a/docs/code/classes/types_dispenser_client.DispenserApiTestnetClient.md +++ b/docs/code/classes/types_dispenser_client.TestNetDispenserApiClient.md @@ -1,10 +1,10 @@ -[@algorandfoundation/algokit-utils](../README.md) / [types/dispenser-client](../modules/types_dispenser_client.md) / DispenserApiTestnetClient +[@algorandfoundation/algokit-utils](../README.md) / [types/dispenser-client](../modules/types_dispenser_client.md) / TestNetDispenserApiClient -# Class: DispenserApiTestnetClient +# Class: TestNetDispenserApiClient -[types/dispenser-client](../modules/types_dispenser_client.md).DispenserApiTestnetClient +[types/dispenser-client](../modules/types_dispenser_client.md).TestNetDispenserApiClient -`DispenserApiTestnetClient` is a class that provides methods to interact with the [Algorand TestNet Dispenser API](https://github.com/algorandfoundation/algokit/blob/main/docs/testnet_api.md). +`TestNetDispenserApiClient` is a class that provides methods to interact with the [Algorand TestNet Dispenser API](https://github.com/algorandfoundation/algokit/blob/main/docs/testnet_api.md). It allows you to fund an address with Algos, refund a transaction, and get the funding limit for the Algo asset. The class requires an authentication token and a request timeout to be initialized. The authentication token can be provided @@ -27,7 +27,7 @@ limit - Sends a request to the dispenser API to get the funding limit for the Al **`Example`** ```typescript -const client = new DispenserApiTestnetClient({ authToken: 'your_auth_token', requestTimeout: 30 }); +const client = new TestNetDispenserApiClient({ authToken: 'your_auth_token', requestTimeout: 30 }); const fundResponse = await client.fund('your_address', 100); const limitResponse = await client.getLimit(); await client.refund('your_transaction_id'); @@ -41,40 +41,40 @@ If neither the environment variable 'ALGOKIT_DISPENSER_ACCESS_TOKEN' nor the aut ### Constructors -- [constructor](types_dispenser_client.DispenserApiTestnetClient.md#constructor) +- [constructor](types_dispenser_client.TestNetDispenserApiClient.md#constructor) ### Properties -- [\_authToken](types_dispenser_client.DispenserApiTestnetClient.md#_authtoken) -- [\_requestTimeout](types_dispenser_client.DispenserApiTestnetClient.md#_requesttimeout) +- [\_authToken](types_dispenser_client.TestNetDispenserApiClient.md#_authtoken) +- [\_requestTimeout](types_dispenser_client.TestNetDispenserApiClient.md#_requesttimeout) ### Accessors -- [authToken](types_dispenser_client.DispenserApiTestnetClient.md#authtoken) -- [requestTimeout](types_dispenser_client.DispenserApiTestnetClient.md#requesttimeout) +- [authToken](types_dispenser_client.TestNetDispenserApiClient.md#authtoken) +- [requestTimeout](types_dispenser_client.TestNetDispenserApiClient.md#requesttimeout) ### Methods -- [fund](types_dispenser_client.DispenserApiTestnetClient.md#fund) -- [getLimit](types_dispenser_client.DispenserApiTestnetClient.md#getlimit) -- [processDispenserRequest](types_dispenser_client.DispenserApiTestnetClient.md#processdispenserrequest) -- [refund](types_dispenser_client.DispenserApiTestnetClient.md#refund) +- [fund](types_dispenser_client.TestNetDispenserApiClient.md#fund) +- [getLimit](types_dispenser_client.TestNetDispenserApiClient.md#getlimit) +- [processDispenserRequest](types_dispenser_client.TestNetDispenserApiClient.md#processdispenserrequest) +- [refund](types_dispenser_client.TestNetDispenserApiClient.md#refund) ## Constructors ### constructor -• **new DispenserApiTestnetClient**(`params`) +• **new TestNetDispenserApiClient**(`params`) #### Parameters | Name | Type | | :------ | :------ | -| `params` | ``null`` \| [`DispenserApiTestnetClientParams`](../interfaces/types_dispenser_client.DispenserApiTestnetClientParams.md) | +| `params` | ``null`` \| [`TestNetDispenserApiClientParams`](../interfaces/types_dispenser_client.TestNetDispenserApiClientParams.md) | #### Defined in -[src/types/dispenser-client.ts:61](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/dispenser-client.ts#L61) +[src/types/dispenser-client.ts:63](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/dispenser-client.ts#L63) ## Properties @@ -84,7 +84,7 @@ If neither the environment variable 'ALGOKIT_DISPENSER_ACCESS_TOKEN' nor the aut #### Defined in -[src/types/dispenser-client.ts:58](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/dispenser-client.ts#L58) +[src/types/dispenser-client.ts:60](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/dispenser-client.ts#L60) ___ @@ -94,7 +94,7 @@ ___ #### Defined in -[src/types/dispenser-client.ts:59](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/dispenser-client.ts#L59) +[src/types/dispenser-client.ts:61](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/dispenser-client.ts#L61) ## Accessors @@ -110,7 +110,7 @@ The authentication token used for API requests. #### Defined in -[src/types/dispenser-client.ts:77](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/dispenser-client.ts#L77) +[src/types/dispenser-client.ts:79](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/dispenser-client.ts#L79) ___ @@ -126,7 +126,7 @@ The timeout for API requests, in seconds. #### Defined in -[src/types/dispenser-client.ts:81](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/dispenser-client.ts#L81) +[src/types/dispenser-client.ts:83](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/dispenser-client.ts#L83) ## Methods @@ -151,7 +151,7 @@ DispenserFundResponse: An object containing the transaction ID and funded amount #### Defined in -[src/types/dispenser-client.ts:142](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/dispenser-client.ts#L142) +[src/types/dispenser-client.ts:144](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/dispenser-client.ts#L144) ___ @@ -169,7 +169,7 @@ DispenserLimitResponse: An object containing the funding limit amount. #### Defined in -[src/types/dispenser-client.ts:168](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/dispenser-client.ts#L168) +[src/types/dispenser-client.ts:170](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/dispenser-client.ts#L170) ___ @@ -196,7 +196,7 @@ The API response. #### Defined in -[src/types/dispenser-client.ts:95](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/dispenser-client.ts#L95) +[src/types/dispenser-client.ts:97](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/dispenser-client.ts#L97) ___ @@ -218,4 +218,4 @@ Sends a refund request to the dispenser API for the specified refundTxnId. #### Defined in -[src/types/dispenser-client.ts:159](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/dispenser-client.ts#L159) +[src/types/dispenser-client.ts:161](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/dispenser-client.ts#L161) diff --git a/docs/code/interfaces/types_dispenser_client.DispenserApiTestnetClientParams.md b/docs/code/interfaces/types_dispenser_client.DispenserApiTestnetClientParams.md deleted file mode 100644 index 68ce405b..00000000 --- a/docs/code/interfaces/types_dispenser_client.DispenserApiTestnetClientParams.md +++ /dev/null @@ -1,32 +0,0 @@ -[@algorandfoundation/algokit-utils](../README.md) / [types/dispenser-client](../modules/types_dispenser_client.md) / DispenserApiTestnetClientParams - -# Interface: DispenserApiTestnetClientParams - -[types/dispenser-client](../modules/types_dispenser_client.md).DispenserApiTestnetClientParams - -## Table of contents - -### Properties - -- [authToken](types_dispenser_client.DispenserApiTestnetClientParams.md#authtoken) -- [requestTimeout](types_dispenser_client.DispenserApiTestnetClientParams.md#requesttimeout) - -## Properties - -### authToken - -• **authToken**: `string` - -#### Defined in - -[src/types/dispenser-client.ts:27](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/dispenser-client.ts#L27) - -___ - -### requestTimeout - -• **requestTimeout**: ``null`` \| `number` - -#### Defined in - -[src/types/dispenser-client.ts:28](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/dispenser-client.ts#L28) diff --git a/docs/code/interfaces/types_dispenser_client.DispenserFundResponse.md b/docs/code/interfaces/types_dispenser_client.DispenserFundResponse.md index f85c8e6f..fac713db 100644 --- a/docs/code/interfaces/types_dispenser_client.DispenserFundResponse.md +++ b/docs/code/interfaces/types_dispenser_client.DispenserFundResponse.md @@ -19,7 +19,7 @@ #### Defined in -[src/types/dispenser-client.ts:19](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/dispenser-client.ts#L19) +[src/types/dispenser-client.ts:21](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/dispenser-client.ts#L21) ___ @@ -29,4 +29,4 @@ ___ #### Defined in -[src/types/dispenser-client.ts:18](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/dispenser-client.ts#L18) +[src/types/dispenser-client.ts:20](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/dispenser-client.ts#L20) diff --git a/docs/code/interfaces/types_dispenser_client.DispenserLimitResponse.md b/docs/code/interfaces/types_dispenser_client.DispenserLimitResponse.md index 6e286f73..5aaa95d9 100644 --- a/docs/code/interfaces/types_dispenser_client.DispenserLimitResponse.md +++ b/docs/code/interfaces/types_dispenser_client.DispenserLimitResponse.md @@ -18,4 +18,4 @@ #### Defined in -[src/types/dispenser-client.ts:23](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/dispenser-client.ts#L23) +[src/types/dispenser-client.ts:25](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/dispenser-client.ts#L25) diff --git a/docs/code/interfaces/types_dispenser_client.TestNetDispenserApiClientParams.md b/docs/code/interfaces/types_dispenser_client.TestNetDispenserApiClientParams.md new file mode 100644 index 00000000..de317c91 --- /dev/null +++ b/docs/code/interfaces/types_dispenser_client.TestNetDispenserApiClientParams.md @@ -0,0 +1,32 @@ +[@algorandfoundation/algokit-utils](../README.md) / [types/dispenser-client](../modules/types_dispenser_client.md) / TestNetDispenserApiClientParams + +# Interface: TestNetDispenserApiClientParams + +[types/dispenser-client](../modules/types_dispenser_client.md).TestNetDispenserApiClientParams + +## Table of contents + +### Properties + +- [authToken](types_dispenser_client.TestNetDispenserApiClientParams.md#authtoken) +- [requestTimeout](types_dispenser_client.TestNetDispenserApiClientParams.md#requesttimeout) + +## Properties + +### authToken + +• **authToken**: `string` + +#### Defined in + +[src/types/dispenser-client.ts:29](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/dispenser-client.ts#L29) + +___ + +### requestTimeout + +• **requestTimeout**: ``null`` \| `number` + +#### Defined in + +[src/types/dispenser-client.ts:30](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/dispenser-client.ts#L30) diff --git a/docs/code/interfaces/types_transfer.EnsureFundedParams.md b/docs/code/interfaces/types_transfer.EnsureFundedParams.md index ba3f4e95..581f877e 100644 --- a/docs/code/interfaces/types_transfer.EnsureFundedParams.md +++ b/docs/code/interfaces/types_transfer.EnsureFundedParams.md @@ -78,7 +78,7 @@ ___ ### fundingSource -• `Optional` **fundingSource**: [`SendTransactionFrom`](../modules/types_transaction.md#sendtransactionfrom) \| [`DispenserApiTestnetClient`](../classes/types_dispenser_client.DispenserApiTestnetClient.md) +• `Optional` **fundingSource**: [`SendTransactionFrom`](../modules/types_transaction.md#sendtransactionfrom) \| [`TestNetDispenserApiClient`](../classes/types_dispenser_client.TestNetDispenserApiClient.md) The account to use as a funding source, will default to using the dispenser account returned by `algokit.getDispenserAccount` diff --git a/docs/code/modules/index.md b/docs/code/modules/index.md index 73162f5a..f230c56b 100644 --- a/docs/code/modules/index.md +++ b/docs/code/modules/index.md @@ -53,13 +53,13 @@ - [getCreatorAppsByName](index.md#getcreatorappsbyname) - [getDefaultLocalNetConfig](index.md#getdefaultlocalnetconfig) - [getDispenserAccount](index.md#getdispenseraccount) -- [getDispenserApiTestnetClient](index.md#getdispenserapitestnetclient) - [getIndexerConfigFromEnvironment](index.md#getindexerconfigfromenvironment) - [getKmdWalletAccount](index.md#getkmdwalletaccount) - [getLocalNetDispenserAccount](index.md#getlocalnetdispenseraccount) - [getOrCreateKmdWalletAccount](index.md#getorcreatekmdwalletaccount) - [getSenderAddress](index.md#getsenderaddress) - [getSenderTransactionSigner](index.md#getsendertransactionsigner) +- [getTestNetDispenserApiClient](index.md#gettestnetdispenserapiclient) - [getTransactionParams](index.md#gettransactionparams) - [getTransactionWithSigner](index.md#gettransactionwithsigner) - [isLocalNet](index.md#islocalnet) @@ -1399,42 +1399,6 @@ If running on LocalNet then it will return the default dispenser account automat ___ -### getDispenserApiTestnetClient - -▸ **getDispenserApiTestnetClient**(`params?`): [`DispenserApiTestnetClient`](../classes/types_dispenser_client.DispenserApiTestnetClient.md) - -Create a new DispenserApiTestnetClient instance. -Refer to [docs](https://github.com/algorandfoundation/algokit/blob/main/docs/testnet_api.md) on guidance to obtain an access token. - -#### Parameters - -| Name | Type | Default value | Description | -| :------ | :------ | :------ | :------ | -| `params` | ``null`` \| [`DispenserApiTestnetClientParams`](../interfaces/types_dispenser_client.DispenserApiTestnetClientParams.md) | `null` | An object containing parameters for the DispenserApiTestnetClient class. Or null if you want the client to load the access token from the environment variable `ALGOKIT_DISPENSER_ACCESS_TOKEN`. | - -#### Returns - -[`DispenserApiTestnetClient`](../classes/types_dispenser_client.DispenserApiTestnetClient.md) - -An instance of the DispenserApiTestnetClient class. - -**`Example`** - -```ts -const client = algokit.getDispenserApiTestnetClient( - { - authToken: 'your_auth_token', - requestTimeout: 15, - } -) -``` - -#### Defined in - -[src/dispenser-client.ts:19](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/dispenser-client.ts#L19) - -___ - ### getIndexerConfigFromEnvironment ▸ **getIndexerConfigFromEnvironment**(): [`AlgoClientConfig`](../interfaces/types_network_client.AlgoClientConfig.md) @@ -1594,6 +1558,42 @@ A transaction signer ___ +### getTestNetDispenserApiClient + +▸ **getTestNetDispenserApiClient**(`params?`): [`TestNetDispenserApiClient`](../classes/types_dispenser_client.TestNetDispenserApiClient.md) + +Create a new TestNetDispenserApiClient instance. +Refer to [docs](https://github.com/algorandfoundation/algokit/blob/main/docs/testnet_api.md) on guidance to obtain an access token. + +#### Parameters + +| Name | Type | Default value | Description | +| :------ | :------ | :------ | :------ | +| `params` | ``null`` \| [`TestNetDispenserApiClientParams`](../interfaces/types_dispenser_client.TestNetDispenserApiClientParams.md) | `null` | An object containing parameters for the TestNetDispenserApiClient class. Or null if you want the client to load the access token from the environment variable `ALGOKIT_DISPENSER_ACCESS_TOKEN`. | + +#### Returns + +[`TestNetDispenserApiClient`](../classes/types_dispenser_client.TestNetDispenserApiClient.md) + +An instance of the TestNetDispenserApiClient class. + +**`Example`** + +```ts +const client = algokit.getTestNetDispenserApiClient( + { + authToken: 'your_auth_token', + requestTimeout: 15, + } +) +``` + +#### Defined in + +[src/dispenser-client.ts:19](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/dispenser-client.ts#L19) + +___ + ### getTransactionParams ▸ **getTransactionParams**(`params`, `algod`): `Promise`<`SuggestedParamsWithMinFee` \| { `fee`: `number` ; `firstRound`: `number` ; `flatFee?`: `boolean` ; `genesisHash`: `string` ; `genesisID`: `string` ; `lastRound`: `number` }\> diff --git a/docs/code/modules/types_dispenser_client.md b/docs/code/modules/types_dispenser_client.md index 2ae89639..014fde5a 100644 --- a/docs/code/modules/types_dispenser_client.md +++ b/docs/code/modules/types_dispenser_client.md @@ -6,10 +6,10 @@ ### Classes -- [DispenserApiTestnetClient](../classes/types_dispenser_client.DispenserApiTestnetClient.md) +- [TestNetDispenserApiClient](../classes/types_dispenser_client.TestNetDispenserApiClient.md) ### Interfaces -- [DispenserApiTestnetClientParams](../interfaces/types_dispenser_client.DispenserApiTestnetClientParams.md) - [DispenserFundResponse](../interfaces/types_dispenser_client.DispenserFundResponse.md) - [DispenserLimitResponse](../interfaces/types_dispenser_client.DispenserLimitResponse.md) +- [TestNetDispenserApiClientParams](../interfaces/types_dispenser_client.TestNetDispenserApiClientParams.md) diff --git a/src/dispenser-client.ts b/src/dispenser-client.ts index 87a2c1eb..ef227d16 100644 --- a/src/dispenser-client.ts +++ b/src/dispenser-client.ts @@ -1,21 +1,21 @@ -import { DispenserApiTestnetClient, DispenserApiTestnetClientParams } from './types/dispenser-client' +import { TestNetDispenserApiClient, TestNetDispenserApiClientParams } from './types/dispenser-client' /** - * Create a new DispenserApiTestnetClient instance. + * Create a new TestNetDispenserApiClient instance. * Refer to [docs](https://github.com/algorandfoundation/algokit/blob/main/docs/testnet_api.md) on guidance to obtain an access token. * - * @param params An object containing parameters for the DispenserApiTestnetClient class. + * @param params An object containing parameters for the TestNetDispenserApiClient class. * Or null if you want the client to load the access token from the environment variable `ALGOKIT_DISPENSER_ACCESS_TOKEN`. * @example - * const client = algokit.getDispenserApiTestnetClient( + * const client = algokit.getTestNetDispenserApiClient( * { * authToken: 'your_auth_token', * requestTimeout: 15, * } * ) * - * @returns An instance of the DispenserApiTestnetClient class. + * @returns An instance of the TestNetDispenserApiClient class. */ -export function getDispenserApiTestnetClient(params: DispenserApiTestnetClientParams | null = null) { - return new DispenserApiTestnetClient(params) +export function getTestNetDispenserApiClient(params: TestNetDispenserApiClientParams | null = null) { + return new TestNetDispenserApiClient(params) } diff --git a/src/transfer.spec.ts b/src/transfer.spec.ts index e476d4ea..93f69ef2 100644 --- a/src/transfer.spec.ts +++ b/src/transfer.spec.ts @@ -1,11 +1,9 @@ import { describe, test } from '@jest/globals' import algosdk, { TransactionType } from 'algosdk' -import fetchMock, { enableFetchMocks } from 'jest-fetch-mock' import invariant from 'tiny-invariant' import * as algokit from './' import { algorandFixture } from './testing' import { ensureFundsAndOptIn, generateTestAsset, optIn } from './testing/asset' -enableFetchMocks() describe('transfer', () => { const localnet = algorandFixture() @@ -292,11 +290,13 @@ describe('transfer', () => { test('ensureFunded uses dispenser api with access token sucessfully', async () => { process.env.ALGOKIT_DISPENSER_ACCESS_TOKEN = 'dummy_token' - // Mock the fetch response - fetchMock.mockResponseOnce(JSON.stringify({ txID: 'dummy_tx_id', amount: 200_000 })) - const algodClient = algokit.getAlgoClient(algokit.getAlgoNodeConfig('testnet', 'algod')) - const dispenserClient = algokit.getDispenserApiTestnetClient() + const dispenserClient = algokit.getTestNetDispenserApiClient() + Object.assign(dispenserClient, { + fund: jest.fn().mockImplementation(() => { + return Promise.resolve({ txId: 'dummy_tx_id', amount: 200_000 }) + }), + }) const accountToFund = algosdk.generateAccount() @@ -318,11 +318,13 @@ describe('transfer', () => { test('ensureFunded uses dispenser api and fails with rejected response', async () => { process.env.ALGOKIT_DISPENSER_ACCESS_TOKEN = 'dummy_token' - // Mock the fetch response - fetchMock.mockRejectOnce(new Error('dummy_error')) - const algodClient = algokit.getAlgoClient(algokit.getAlgoNodeConfig('testnet', 'algod')) - const dispenserClient = algokit.getDispenserApiTestnetClient() + const dispenserClient = algokit.getTestNetDispenserApiClient() + Object.assign(dispenserClient, { + fund: jest.fn().mockImplementation(() => { + return Promise.reject(new Error('dummy_error')) + }), + }) const accountToFund = algosdk.generateAccount() await expect( diff --git a/src/transfer.ts b/src/transfer.ts index 58561a4f..08b403df 100644 --- a/src/transfer.ts +++ b/src/transfer.ts @@ -3,13 +3,13 @@ import { Config, getDispenserAccount, microAlgos } from './' import { isTestNet } from './network-client' import { encodeTransactionNote, getSenderAddress, getTransactionParams, sendTransaction } from './transaction' import { AlgoAmount } from './types/amount' -import { DispenserApiTestnetClient } from './types/dispenser-client' +import { TestNetDispenserApiClient } from './types/dispenser-client' import { SendTransactionResult, TransactionNote } from './types/transaction' import { AlgoTransferParams, EnsureFundedParams, EnsureFundedReturnType, TransferAssetParams } from './types/transfer' import { calculateFundAmount } from './util' async function fundUsingDispenserApi( - dispenserClient: DispenserApiTestnetClient, + dispenserClient: TestNetDispenserApiClient, addressToFund: string, fundAmount: number, ): Promise { @@ -44,7 +44,7 @@ async function fundUsingTransfer({ note: TransactionNote kmd?: Kmd }): Promise { - if (funding.fundingSource instanceof DispenserApiTestnetClient) { + if (funding.fundingSource instanceof TestNetDispenserApiClient) { throw new Error('Dispenser API client is not supported in this context.') } @@ -132,7 +132,7 @@ export async function ensureFunded( ) if (fundAmount !== null) { - if ((await isTestNet(algod)) && fundingSource instanceof DispenserApiTestnetClient) { + if ((await isTestNet(algod)) && fundingSource instanceof TestNetDispenserApiClient) { return fundUsingDispenserApi(fundingSource, addressToFund, fundAmount) as Promise } else { return fundUsingTransfer({ diff --git a/src/types/dispenser-client.spec.ts b/src/types/dispenser-client.spec.ts index b625adfe..d468f822 100644 --- a/src/types/dispenser-client.spec.ts +++ b/src/types/dispenser-client.spec.ts @@ -1,12 +1,12 @@ -import fetchMock, { enableFetchMocks } from 'jest-fetch-mock' -import { DispenserApiTestnetClient } from './dispenser-client' -enableFetchMocks() +jest.mock('cross-fetch') +import crossFetch from 'cross-fetch' +import { TestNetDispenserApiClient } from './dispenser-client' -describe('DispenserApiTestnetClient', () => { +describe('TestNetDispenserApiClient', () => { const env = process.env beforeEach(async () => { - jest.resetModules() + jest.resetAllMocks() process.env = { ...env } }) @@ -15,9 +15,12 @@ describe('DispenserApiTestnetClient', () => { }) it('should fund account with algos with auth token', async () => { const mockResponse = { txID: 'dummy_tx_id', amount: 1 } - fetchMock.mockResponseOnce(JSON.stringify(mockResponse)) + jest.mocked(crossFetch).mockResolvedValueOnce({ + json: () => Promise.resolve(mockResponse), + ok: true, + } as Response) - const dispenserClient = new DispenserApiTestnetClient({ authToken: 'dummy_auth_token', requestTimeout: null }) + const dispenserClient = new TestNetDispenserApiClient({ authToken: 'dummy_auth_token', requestTimeout: null }) const address = 'dummy_address' const amount = 1 @@ -27,9 +30,13 @@ describe('DispenserApiTestnetClient', () => { }) it('should register refund with auth token', async () => { - fetchMock.mockResponseOnce(JSON.stringify({})) + const fetchMock = jest.mocked(crossFetch) + fetchMock.mockResolvedValueOnce({ + json: () => Promise.resolve({}), + ok: true, + } as Response) - const dispenserClient = new DispenserApiTestnetClient({ authToken: 'dummy_auth_token', requestTimeout: null }) + const dispenserClient = new TestNetDispenserApiClient({ authToken: 'dummy_auth_token', requestTimeout: null }) const refundTxnId = 'dummy_txn_id' await dispenserClient.refund(refundTxnId) @@ -45,26 +52,30 @@ describe('DispenserApiTestnetClient', () => { it('should get limit with auth token', async () => { const amount = 10000000 const mockResponse = { amount: amount } - fetchMock.mockResponseOnce(JSON.stringify(mockResponse)) + const fetchMock = jest.mocked(crossFetch) + fetchMock.mockResolvedValueOnce({ + json: () => Promise.resolve(mockResponse), + ok: true, + } as Response) - const dispenserClient = new DispenserApiTestnetClient({ authToken: 'dummy_auth_token', requestTimeout: null }) + const dispenserClient = new TestNetDispenserApiClient({ authToken: 'dummy_auth_token', requestTimeout: null }) const response = await dispenserClient.getLimit() expect(response.amount).toEqual(amount) }) it('should throw error when no auth token provided', () => { - expect(() => new DispenserApiTestnetClient(null)).toThrow() + expect(() => new TestNetDispenserApiClient(null)).toThrow() }) it('should init with environment variable', () => { process.env.ALGOKIT_DISPENSER_ACCESS_TOKEN = 'dummy_token' - const client = new DispenserApiTestnetClient(null) + const client = new TestNetDispenserApiClient(null) expect(client.authToken).toEqual('dummy_token') }) it('should init with argument over environment variable', () => { process.env.ALGOKIT_DISPENSER_ACCESS_TOKEN = 'dummy_token' - const client = new DispenserApiTestnetClient({ authToken: 'test_value_2', requestTimeout: null }) + const client = new TestNetDispenserApiClient({ authToken: 'test_value_2', requestTimeout: null }) expect(client.authToken).toEqual('test_value_2') }) }) diff --git a/src/types/dispenser-client.ts b/src/types/dispenser-client.ts index d95bb11c..0c6f7588 100644 --- a/src/types/dispenser-client.ts +++ b/src/types/dispenser-client.ts @@ -1,3 +1,5 @@ +import fetch from 'cross-fetch' + const baseUrl = 'https://api.dispenser.algorandfoundation.tools' const dispenserRequestTimeout = 15 const dispenserAccessTokenKey = 'ALGOKIT_DISPENSER_ACCESS_TOKEN' @@ -23,13 +25,13 @@ export interface DispenserLimitResponse { amount: number } -export interface DispenserApiTestnetClientParams { +export interface TestNetDispenserApiClientParams { authToken: string requestTimeout: number | null } /** - * `DispenserApiTestnetClient` is a class that provides methods to interact with the [Algorand TestNet Dispenser API](https://github.com/algorandfoundation/algokit/blob/main/docs/testnet_api.md). + * `TestNetDispenserApiClient` is a class that provides methods to interact with the [Algorand TestNet Dispenser API](https://github.com/algorandfoundation/algokit/blob/main/docs/testnet_api.md). * It allows you to fund an address with Algos, refund a transaction, and get the funding limit for the Algo asset. * * The class requires an authentication token and a request timeout to be initialized. The authentication token can be provided @@ -46,7 +48,7 @@ export interface DispenserApiTestnetClientParams { * * @example * ```typescript - * const client = new DispenserApiTestnetClient({ authToken: 'your_auth_token', requestTimeout: 30 }); + * const client = new TestNetDispenserApiClient({ authToken: 'your_auth_token', requestTimeout: 30 }); * const fundResponse = await client.fund('your_address', 100); * const limitResponse = await client.getLimit(); * await client.refund('your_transaction_id'); @@ -54,11 +56,11 @@ export interface DispenserApiTestnetClientParams { * * @throws {Error} If neither the environment variable 'ALGOKIT_DISPENSER_ACCESS_TOKEN' nor the authToken parameter were provided. */ -export class DispenserApiTestnetClient { +export class TestNetDispenserApiClient { private _authToken: string private _requestTimeout: number - constructor(params: DispenserApiTestnetClientParams | null) { + constructor(params: TestNetDispenserApiClientParams | null) { const authTokenFromEnv = process.env[dispenserAccessTokenKey] if (params?.authToken) { diff --git a/src/types/transfer.ts b/src/types/transfer.ts index 633d5311..9a251584 100644 --- a/src/types/transfer.ts +++ b/src/types/transfer.ts @@ -1,6 +1,6 @@ import { SuggestedParams } from 'algosdk' import { AlgoAmount } from './amount' -import { DispenserApiTestnetClient } from './dispenser-client' +import { TestNetDispenserApiClient } from './dispenser-client' import { SendTransactionFrom, SendTransactionParams, TransactionNote } from './transaction' /** Parameters for `transferAlgos` call. */ @@ -22,7 +22,7 @@ export interface EnsureFundedParams extends SendTransactionParams { /** The account to fund */ accountToFund: SendTransactionFrom | string /** The account to use as a funding source, will default to using the dispenser account returned by `algokit.getDispenserAccount` */ - fundingSource?: SendTransactionFrom | DispenserApiTestnetClient + fundingSource?: SendTransactionFrom | TestNetDispenserApiClient /** The minimum balance of ALGOs that the account should have available to spend (i.e. on top of minimum balance requirement) */ minSpendingBalance: AlgoAmount /** When issuing a funding amount, the minimum amount to transfer (avoids many small transfers if this gets called often on an active account) */ diff --git a/tests/setup.ts b/tests/setup.ts index bc0322cd..e69de29b 100644 --- a/tests/setup.ts +++ b/tests/setup.ts @@ -1,6 +0,0 @@ -import { Config } from '../src/' -import { consoleLogger } from '../src/types/logging' - -Config.configure({ - logger: consoleLogger, -})