Skip to content

Commit

Permalink
refactor getCapacityInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
claireolmstead committed Feb 6, 2024
1 parent 0fdb68f commit 55aefe6
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 46 deletions.
26 changes: 7 additions & 19 deletions src/components/Capacity.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,18 @@
import { dotApi, storeChainInfo, storeCurrentAction } from '$lib/stores';
import { user } from '$lib/stores/userStore';
import type { ApiPromise } from '@polkadot/api';
import { getMsaEpochAndCapacityInfo } from '$lib/polkadotApi';
import { getCapacityInfo, type CapacityDetails } from '$lib/polkadotApi';
import { balanceToHuman } from '$lib/utils.js';
import ListCard from './ListCard.svelte';
import { ActionForms } from '$lib/storeTypes.js';
import { onMount } from 'svelte';
import { afterUpdate } from 'svelte';
type CapacityDetails = {
remainingCapacity: bigint;
totalTokensStaked: bigint;
totalCapacityIssued: bigint;
lastReplenishedEpoch: bigint;
};
const defaultDetails: CapacityDetails = {
remainingCapacity: 0n,
totalCapacityIssued: 0n,
totalTokensStaked: 0n,
lastReplenishedEpoch: 0n,
};
let capacityDetails: CapacityDetails;
let capacityDetails: CapacityDetails = defaultDetails;
onMount(async () => {
let { capacityDetails } = await getMsaEpochAndCapacityInfo($dotApi.api as ApiPromise, $user.address);
capacityDetails = { ...defaultDetails, ...capacityDetails };
afterUpdate(async () => {
if ($user?.msaId && $user?.msaId > 0) {
capacityDetails = await getCapacityInfo($dotApi.api as ApiPromise, $user.msaId);
}
});
function showStake() {
Expand Down
5 changes: 3 additions & 2 deletions src/components/Provider.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import type { AccountBalances } from '$lib/polkadotApi';
import ListCard from './ListCard.svelte';
import { ActionForms } from '$lib/storeTypes.js';
import { afterUpdate } from 'svelte';
import { onMount } from 'svelte';
let accountBalances: AccountBalances = { free: 0n, reserved: 0n, frozen: 0n };
afterUpdate(async () => {
onMount(async () => {
console.log($dotApi.selectedEndpoint);
if ($dotApi.api) {
accountBalances = await getBalances($dotApi.api, $user.address);
}
Expand Down
7 changes: 6 additions & 1 deletion src/components/ProviderLogin.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
$: canConnect = $user?.network != null && $providerAccountsStore.size > 0 && $user?.address !== '';
async function connect() {
await createAndConnectToApi($user.network?.endpoint?.toString()!);
if (!$user.network?.endpoint?.origin) {
alert('Error connecting to endpoint.');
return;
}
console.log('HERE USER', $user);
await createAndConnectToApi($user.network?.endpoint?.origin);
$isLoggedIn = true;
pageContent.dashboard();
}
Expand Down
58 changes: 34 additions & 24 deletions src/lib/polkadotApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,19 @@ async function updateChainInfo(api: ApiPromise) {
}

export async function createAndConnectToApi(networkEndpoint: string) {
dotApi.subscribe(async (apiState: DotApi) => await apiState.api?.disconnect());

let initializedDotApi = await createApi(networkEndpoint);
dotApi.set((currentApi: DotApi) => (currentApi = initializedDotApi));
console.log('*** createAndConnectToApi ****', initializedDotApi.selectedEndpoint);
console.log('*** here 3 selectedEndpoint****', initializedDotApi);

dotApi.set(initializedDotApi);

await updateChainInfo(initializedDotApi.api!);
await updateChainInfo(initializedDotApi.api);
}

export async function createApi(networkEndpoint: string): Promise<DotApi> {
const wsProvider = new WsProvider(networkEndpoint);
console.log('*** wsProvider networkEndpoint ****', wsProvider.endpoint);

const apiPromise = await ApiPromise.create({
provider: wsProvider,
throwOnConnect: true,
Expand All @@ -45,7 +48,6 @@ export async function createApi(networkEndpoint: string): Promise<DotApi> {
selectedEndpoint: networkEndpoint,
options,
};

return initializedDotApi;
}

Expand Down Expand Up @@ -83,24 +85,32 @@ export async function getMsaInfo(apiPromise: ApiPromise, publicKey: string): Pro
return msaInfo;
}

export async function getMsaEpochAndCapacityInfo(
apiPromise: ApiPromise,
accountId: string
): Promise<{ epochNumber: bigint; msaInfo: MsaInfo; capacityDetails: any }> {
const msaInfo = await getMsaInfo(apiPromise, accountId);
const epochNumber = await getEpoch(apiPromise);
let capacityDetails;
if (msaInfo.msaId > 0) {
const providerRegistry: Option<any> = await apiPromise.query.msa.providerToRegistryEntry(msaInfo.msaId);
if (providerRegistry.isSome) {
const details: any = (await apiPromise.query.capacity.capacityLedger(msaInfo.msaId)).unwrapOrDefault();
capacityDetails = {
remainingCapacity: details.remainingCapacity.toBigInt(),
totalTokensStaked: details.totalTokensStaked.toBigInt(),
totalCapacityIssued: details.totalCapacityIssued.toBigInt(),
lastReplenishedEpoch: details.lastReplenishedEpoch.toBigInt(),
};
}
export type CapacityDetails = {
remainingCapacity: bigint;
totalTokensStaked: bigint;
totalCapacityIssued: bigint;
lastReplenishedEpoch: bigint;
};

export async function getCapacityInfo(apiPromise: ApiPromise, msaId: number): Promise<CapacityDetails> {
let capacityDetails: CapacityDetails = {
remainingCapacity: 0n,
totalCapacityIssued: 0n,
totalTokensStaked: 0n,
lastReplenishedEpoch: 0n,
};

const providerRegistry: Option<any> = await apiPromise.query.msa.providerToRegistryEntry(msaId);

if (providerRegistry.isSome) {
const details: any = (await apiPromise.query.capacity.capacityLedger(msaId)).unwrapOrDefault();
capacityDetails = {
remainingCapacity: details.remainingCapacity.toBigInt(),
totalTokensStaked: details.totalTokensStaked.toBigInt(),
totalCapacityIssued: details.totalCapacityIssued.toBigInt(),
lastReplenishedEpoch: details.lastReplenishedEpoch.toBigInt(),
};
}
return { msaInfo, epochNumber, capacityDetails };

return capacityDetails;
}

0 comments on commit 55aefe6

Please sign in to comment.