Skip to content

Commit

Permalink
Merge pull request #257 from TxnLab/chore/ui-deprecated-algokit-methods
Browse files Browse the repository at this point in the history
chore(ui): update deprecated algokit methods
  • Loading branch information
drichar authored Jul 29, 2024
2 parents dc2e664 + ea9d362 commit 112441d
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 27 deletions.
4 changes: 2 additions & 2 deletions ui/src/api/algod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as algokit from '@algorandfoundation/algokit-utils'
import { AlgoAmount } from '@algorandfoundation/algokit-utils/types/amount'
import { ClientManager } from '@algorandfoundation/algokit-utils/types/client-manager'
import {
AccountAssetInformation,
AccountBalance,
Expand All @@ -15,7 +15,7 @@ import {
import { getAlgodConfigFromViteEnvironment } from '@/utils/network/getAlgoClientConfigs'

const algodConfig = getAlgodConfigFromViteEnvironment()
const algodClient = algokit.getAlgoClient({
const algodClient = ClientManager.getAlgodClient({
server: algodConfig.server,
port: algodConfig.port,
token: algodConfig.token,
Expand Down
4 changes: 2 additions & 2 deletions ui/src/api/clients.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as algokit from '@algorandfoundation/algokit-utils'
import { TransactionSignerAccount } from '@algorandfoundation/algokit-utils/types/account'
import { ClientManager } from '@algorandfoundation/algokit-utils/types/client-manager'
import algosdk from 'algosdk'
import { FEE_SINK } from '@/constants/accounts'
import { StakingPoolClient } from '@/contracts/StakingPoolClient'
Expand All @@ -10,7 +10,7 @@ import { getAlgodConfigFromViteEnvironment } from '@/utils/network/getAlgoClient
import { ParamsCache } from '@/utils/paramsCache'

const algodConfig = getAlgodConfigFromViteEnvironment()
const algodClient = algokit.getAlgoClient({
const algodClient = ClientManager.getAlgodClient({
server: algodConfig.server,
port: algodConfig.port,
token: algodConfig.token,
Expand Down
3 changes: 2 additions & 1 deletion ui/src/api/contracts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as algokit from '@algorandfoundation/algokit-utils'
import { TransactionSignerAccount } from '@algorandfoundation/algokit-utils/types/account'
import { AlgoAmount } from '@algorandfoundation/algokit-utils/types/amount'
import { ClientManager } from '@algorandfoundation/algokit-utils/types/client-manager'
import { QueryClient } from '@tanstack/react-query'
import algosdk from 'algosdk'
import { fetchAccountBalance, fetchAsset, isOptedInToAsset } from '@/api/algod'
Expand Down Expand Up @@ -49,7 +50,7 @@ import { ParamsCache } from '@/utils/paramsCache'
import { encodeCallParams } from '@/utils/tests/abi'

const algodConfig = getAlgodConfigFromViteEnvironment()
const algodClient = algokit.getAlgoClient({
const algodClient = ClientManager.getAlgodClient({
server: algodConfig.server,
port: algodConfig.port,
token: algodConfig.token,
Expand Down
4 changes: 2 additions & 2 deletions ui/src/utils/balanceChecker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as algokit from '@algorandfoundation/algokit-utils'
import { ClientManager } from '@algorandfoundation/algokit-utils/types/client-manager'
import algosdk from 'algosdk'
import { formatAlgoAmount } from '@/utils/format'
import { getAlgodConfigFromViteEnvironment } from '@/utils/network/getAlgoClientConfigs'
Expand Down Expand Up @@ -30,7 +30,7 @@ export class BalanceChecker {
this.address = address

const algodConfig = getAlgodConfigFromViteEnvironment()
this.algodClient = algokit.getAlgoClient({
this.algodClient = ClientManager.getAlgodClient({
server: algodConfig.server,
port: algodConfig.port,
token: algodConfig.token,
Expand Down
22 changes: 13 additions & 9 deletions ui/src/utils/development.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as algokit from '@algorandfoundation/algokit-utils'
import { AlgorandClient } from '@algorandfoundation/algokit-utils'
import { getTestAccount } from '@algorandfoundation/algokit-utils/testing'
import { AlgoAmount } from '@algorandfoundation/algokit-utils/types/amount'
import { ClientManager } from '@algorandfoundation/algokit-utils/types/client-manager'
import { QueryClient } from '@tanstack/react-query'
import { useRouter } from '@tanstack/react-router'
import algosdk from 'algosdk'
Expand All @@ -17,12 +19,21 @@ import { getAlgodConfigFromViteEnvironment } from '@/utils/network/getAlgoClient
import { ParamsCache } from '@/utils/paramsCache'

const algodConfig = getAlgodConfigFromViteEnvironment()
const algodClient = algokit.getAlgoClient({

const algodClient = ClientManager.getAlgodClient({
server: algodConfig.server,
port: algodConfig.port,
token: algodConfig.token,
})

const kmdClient = ClientManager.getKmdClient({
server: algodConfig.server,
port: algodConfig.port,
token: algodConfig.token,
})

const algorandClient = AlgorandClient.fromClients({ algod: algodClient, kmd: kmdClient })

async function wait(ms: number) {
return new Promise((resolve) => {
setTimeout(resolve, ms)
Expand All @@ -48,16 +59,9 @@ export async function incrementRoundNumberBy(rounds: number) {

// console.log(`Increment round number start: ${result.startRound}`)

const kmdClient = algokit.getAlgoKmdClient({
server: algodConfig.server,
port: algodConfig.port,
token: algodConfig.token,
})

const testAccount = await getTestAccount(
{ initialFunds: AlgoAmount.Algos(10), suppressLog: true },
algodClient,
kmdClient,
algorandClient,
)

let txnId = ''
Expand Down
20 changes: 11 additions & 9 deletions ui/src/utils/paramsCache.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as algokit from '@algorandfoundation/algokit-utils'
import { ClientManager } from '@algorandfoundation/algokit-utils/types/client-manager'
import algosdk from 'algosdk'
import { ParamsCache } from '@/utils/paramsCache'

Expand All @@ -14,12 +14,14 @@ const mockParams: algosdk.SuggestedParams = {
const mockGetTransactionParams = vi.fn().mockResolvedValue(mockParams)

// Mock algokit-utils
vi.mock('@algorandfoundation/algokit-utils', () => ({
getAlgoClient: vi.fn(() => ({
getTransactionParams: vi.fn(() => ({
do: mockGetTransactionParams,
vi.mock('@algorandfoundation/algokit-utils/types/client-manager', () => ({
ClientManager: {
getAlgodClient: vi.fn(() => ({
getTransactionParams: vi.fn(() => ({
do: mockGetTransactionParams,
})),
})),
})),
},
}))

// Mock getAlgodConfigFromViteEnvironment
Expand All @@ -39,18 +41,18 @@ beforeEach(() => {
describe('ParamsCache', () => {
it('should fetch and cache transaction parameters', async () => {
const params = await ParamsCache.getSuggestedParams()
expect(algokit.getAlgoClient).toHaveBeenCalledTimes(1) // First call
expect(ClientManager.getAlgodClient).toHaveBeenCalledTimes(1) // First call
expect(params).toEqual(mockParams)

// Simulate another call within 5 minutes
const cachedParams = await ParamsCache.getSuggestedParams()
expect(algokit.getAlgoClient).toHaveBeenCalledTimes(1) // No second call (cached)
expect(ClientManager.getAlgodClient).toHaveBeenCalledTimes(1) // No second call (cached)
expect(cachedParams).toEqual(params)
})

it('should refresh cached parameters after expiration', async () => {
const initialParams = await ParamsCache.getSuggestedParams()
expect(algokit.getAlgoClient).toHaveBeenCalledTimes(1) // First call
expect(ClientManager.getAlgodClient).toHaveBeenCalledTimes(1) // First call
expect(initialParams).toEqual(mockParams)

// Mock Date.now to simulate cache expiration (5 minutes later)
Expand Down
4 changes: 2 additions & 2 deletions ui/src/utils/paramsCache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as algokit from '@algorandfoundation/algokit-utils'
import { ClientManager } from '@algorandfoundation/algokit-utils/types/client-manager'
import algosdk from 'algosdk'
import { getAlgodConfigFromViteEnvironment } from '@/utils/network/getAlgoClientConfigs'

Expand All @@ -23,7 +23,7 @@ export class ParamsCache {

private constructor() {
const algodConfig = getAlgodConfigFromViteEnvironment()
this.client = algokit.getAlgoClient({
this.client = ClientManager.getAlgodClient({
server: algodConfig.server,
port: algodConfig.port,
token: algodConfig.token,
Expand Down

0 comments on commit 112441d

Please sign in to comment.