import { signals as apophisSignals, type CosmosNetworkConfig } from '@apophis-sdk/core';
import { CosmWasm } from '@apophis-sdk/cosmwasm';
import { toast } from '@kiruse/cosmos-components';
import { Decimal } from '@kiruse/decimal';
import { useComputed } from 'preact/signals';
import { useAsyncComputed } from '~/hooks/useAsyncComputed.js';
import { impersonateAddress, refreshCounter } from '~/state.js';
function TokenRecovery({ address }: { address: string }) {
const userAddress = useComputed(() => impersonateAddress.value || apophisSignals.address.value);
const stake = useAsyncComputed(async () => {
// refreshCounter is used to trigger re-calculation after a tx
// b/c blockchain queries aren't reactive
console.log('Refresh counter:', refreshCounter.value);
try {
const network = apophisSignals.network.value as CosmosNetworkConfig;
if (!network) throw new Error('No network selected');
if (!address) throw new Error('Failed to get Membership Contract address');
if (!userAddress.value) throw new Error('Please provide an address');
const { weight } = await CosmWasm.query.smart<any>(
network,
address,
{ user_weight: { user: userAddress.value } },
);
const { claims: pending } = await CosmWasm.query.smart<any>(
network,
address,
{ claims: { user: userAddress.value } },
);
const { claims: claimable } = await CosmWasm.query.smart<any>(
network,
address,
{ releasable_claims: { user: userAddress.value } },
);
return {
total: BigInt(weight),
pending,
claimable,
}
} catch (err) {
toast.errorlink(err);
throw err;
}
});
// The decimals here (6) could be pulled from the network config, but I was too lazy
const stakedTokens = useComputed(() => new Decimal(stake.value?.total ?? 0, 6));
// Claims are objects with an amount and a timestamp, so we just add them up here
const pendingTokens = useComputed(() =>
stake.value.pending.reduce((acc, claim) => acc.add(new Decimal(BigInt(claim.amount), 6)), new Decimal(0, 6))
);
const claimableTokens = useComputed(() =>
stake.value.claimable.reduce((acc, claim) => acc.add(new Decimal(BigInt(claim.amount), 6)), new Decimal(0, 6))
);
// ... more code
return (
<>
{/* ... more code ... */}
{!!stake.value && (
<>
<p class="mb-2">
{/* Again, the denom could be pulled from the network config, but I was too lazy */}
You have <cosmos-balance value={stakedTokens} denom="tokens" /> tokens staked,{' '}
<cosmos-balance value={pendingTokens} denom="tokens" /> pending, and{' '}
<cosmos-balance value={claimableTokens} denom="tokens" /> claimable.
</p>
</>
)}
</>
);
}