Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ui): reduce suggestedParams requests #120

Merged
merged 7 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions ui/src/api/algod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,24 @@ const algodClient = algokit.getAlgoClient({
token: algodConfig.token,
})

export async function getAccountInformation(
export async function fetchAccountInformation(
address: string,
exclude: Exclude = 'none',
): Promise<AccountInformation> {
const accountInfo = await algodClient.accountInformation(address).exclude(exclude).do()
return accountInfo as AccountInformation
}

export async function getAccountBalance(
export async function fetchAccountBalance(
address: string,
availableBalance = false,
): Promise<number> {
const accountInfo = await getAccountInformation(address, 'all')
const accountInfo = await fetchAccountInformation(address, 'all')

return availableBalance ? accountInfo.amount - accountInfo['min-balance'] : accountInfo.amount
}

export async function getAsset(assetId: number): Promise<Asset> {
export async function fetchAsset(assetId: number): Promise<Asset> {
const asset = await algodClient.getAssetByID(assetId).do()
return asset as Asset
}
Expand All @@ -46,7 +46,7 @@ export async function fetchBalance(address: string | null): Promise<AccountBalan
if (!address) {
throw new Error('No address provided')
}
const accountInfo = await getAccountInformation(address, 'all')
const accountInfo = await fetchAccountInformation(address, 'all')

const amount = accountInfo.amount
const minimum = accountInfo['min-balance']
Expand All @@ -63,7 +63,7 @@ export async function fetchAssetHoldings(address: string | null): Promise<AssetH
if (!address) {
throw new Error('No address provided')
}
const accountInfo = await getAccountInformation(address)
const accountInfo = await fetchAccountInformation(address)
const assets = accountInfo.assets || []
return assets
}
Expand Down Expand Up @@ -91,7 +91,7 @@ export async function fetchAssetCreatorHoldings(
const batches = chunkArray(assetHoldings, batchSize)

for (const batch of batches) {
const promises = batch.map((holding) => getAsset(holding['asset-id']))
const promises = batch.map((holding) => fetchAsset(holding['asset-id']))
const assets = await Promise.all(promises)
const assetCreatorHoldings = assets.map((asset, index) => {
return {
Expand Down
89 changes: 89 additions & 0 deletions ui/src/api/clients.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import * as algokit from '@algorandfoundation/algokit-utils'
import { TransactionSignerAccount } from '@algorandfoundation/algokit-utils/types/account'
import algosdk from 'algosdk'
import { FEE_SINK } from '@/constants/accounts'
import { StakingPoolClient } from '@/contracts/StakingPoolClient'
import { ValidatorRegistryClient } from '@/contracts/ValidatorRegistryClient'
import { makeEmptyTransactionSigner } from '@/lib/makeEmptyTransactionSigner'
import { getRetiAppIdFromViteEnvironment } from '@/utils/env'
import { getAlgodConfigFromViteEnvironment } from '@/utils/network/getAlgoClientConfigs'
import { ParamsCache } from '@/utils/paramsCache'

const algodConfig = getAlgodConfigFromViteEnvironment()
const algodClient = algokit.getAlgoClient({
server: algodConfig.server,
port: algodConfig.port,
token: algodConfig.token,
})

const RETI_APP_ID = getRetiAppIdFromViteEnvironment()

export async function getValidatorClient(
signer: algosdk.TransactionSigner,
activeAddress: string,
): Promise<ValidatorRegistryClient> {
const params = await ParamsCache.getSuggestedParams()

return new ValidatorRegistryClient(
{
sender: { signer, addr: activeAddress },
resolveBy: 'id',
id: RETI_APP_ID,
params,
},
algodClient,
)
}

export async function getSimulateValidatorClient(
senderAddr: string = FEE_SINK,
authAddr?: string,
): Promise<ValidatorRegistryClient> {
const params = await ParamsCache.getSuggestedParams()

return new ValidatorRegistryClient(
{
sender: { addr: senderAddr, signer: makeEmptyTransactionSigner(authAddr) },
resolveBy: 'id',
id: RETI_APP_ID,
params,
},
algodClient,
)
}

export async function getStakingPoolClient(
poolAppId: number | bigint,
signer: algosdk.TransactionSigner,
activeAddress: string,
): Promise<StakingPoolClient> {
const params = await ParamsCache.getSuggestedParams()

return new StakingPoolClient(
{
sender: { signer, addr: activeAddress } as TransactionSignerAccount,
resolveBy: 'id',
id: poolAppId,
params,
},
algodClient,
)
}

export async function getSimulateStakingPoolClient(
poolAppId: number | bigint,
senderAddr: string = FEE_SINK,
authAddr?: string,
): Promise<StakingPoolClient> {
const params = await ParamsCache.getSuggestedParams()

return new StakingPoolClient(
{
sender: { addr: senderAddr, signer: makeEmptyTransactionSigner(authAddr) },
resolveBy: 'id',
id: poolAppId,
params,
},
algodClient,
)
}
Loading
Loading