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

[Coin][FungibleAsset] add paired coin/fa info #916

Merged
merged 3 commits into from
Nov 19, 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
15 changes: 15 additions & 0 deletions src/api/hooks/useGetCoinPairedFa.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {useViewFunction} from "./useViewFunction";

export function useGetCoinPairedFa(coinType: string): string | null {
const {data} = useViewFunction("0x1::coin::paired_metadata", [coinType], []);

if (data !== undefined) {
const mappedData = data as [{vec: [{inner: string}]}];
const val = mappedData[0]?.vec[0];
if (val !== undefined && val !== null) {
return val.inner;
}
}

return null;
}
34 changes: 34 additions & 0 deletions src/api/hooks/useGetFaPairedCoin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {useViewFunction} from "./useViewFunction";
import {Hex} from "@aptos-labs/ts-sdk";

const TEXT_DECODER = new TextDecoder();

export function useGetFaPairedCoin(address: string): string | null {
const {data} = useViewFunction("0x1::coin::paired_coin", [], [address]);

if (data !== undefined) {
const mappedData = data as [
{
vec: [
{account_address: string; module_name: string; struct_name: string},
];
},
];
const val = mappedData[0]?.vec[0];
if (val !== undefined && val !== null) {
return (
val.account_address +
"::" +
hexToUtf8(val.module_name) +
"::" +
hexToUtf8(val.struct_name)
);
}
}

return null;
}

function hexToUtf8(hexWith0x: string): string {
return TEXT_DECODER.decode(Hex.fromHexString(hexWith0x).toUint8Array());
}
4 changes: 4 additions & 0 deletions src/pages/Coin/Index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {CoinData} from "./Components/CoinData";
import {useGetCoinSupplyLimit} from "../../api/hooks/useGetCoinSupplyLimit";
import {useGetCoinList} from "../../api/hooks/useGetCoinList";
import {findCoinData} from "../Transaction/Tabs/BalanceChangeTab";
import {useGetCoinPairedFa} from "../../api/hooks/useGetCoinPairedFa";

const TAB_VALUES_FULL: TabValue[] = ["info", "holders", "transactions"];

Expand Down Expand Up @@ -43,6 +44,7 @@ export default function CoinPage() {
isLoading,
} = useGetAccountResource(address, `0x1::coin::CoinInfo<${struct}>`);
const supply = useGetCoinSupplyLimit(struct);
const pairedFa = useGetCoinPairedFa(struct);

if (error === null) {
error = infoError;
Expand Down Expand Up @@ -71,6 +73,7 @@ export default function CoinPage() {
data={data as CoinData | undefined}
tabValues={tabValues}
supply={supply}
pairedFa={pairedFa}
coinData={coinData}
/>
<Error struct={struct} error={error} />
Expand All @@ -81,6 +84,7 @@ export default function CoinPage() {
data={data as CoinData | undefined}
tabValues={tabValues}
supply={supply}
pairedFa={pairedFa}
coinData={coinData}
/>
)}
Expand Down
6 changes: 6 additions & 0 deletions src/pages/Coin/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type TabPanelProps = {
struct: string;
data: CoinData | undefined;
supply: bigint | null;
pairedFa: string | null;
coinData: CoinDescription | undefined;
};

Expand All @@ -62,6 +63,7 @@ function TabPanel({
struct,
data,
supply,
pairedFa,
coinData,
}: TabPanelProps): JSX.Element {
const TabComponent = TabComponents[value];
Expand All @@ -70,6 +72,7 @@ function TabPanel({
struct={struct}
data={data}
supply={supply}
pairedFa={pairedFa}
coinData={coinData}
/>
);
Expand All @@ -80,6 +83,7 @@ type CoinTabsProps = {
data: CoinData | undefined;
tabValues?: TabValue[];
supply: bigint | null;
pairedFa: string | null;
coinData: CoinDescription | undefined;
};

Expand All @@ -89,6 +93,7 @@ export default function CoinTabs({
data,
tabValues = TAB_VALUES,
supply,
pairedFa,
coinData,
}: CoinTabsProps): JSX.Element {
const {tab, modulesTab} = useParams();
Expand Down Expand Up @@ -130,6 +135,7 @@ export default function CoinTabs({
struct={struct}
data={data}
supply={supply}
pairedFa={pairedFa}
coinData={coinData}
/>
</Box>
Expand Down
15 changes: 15 additions & 0 deletions src/pages/Coin/Tabs/InfoTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ type InfoTabProps = {
struct: string;
data: CoinData | undefined;
supply: bigint | null;
pairedFa: string | null;
coinData: CoinDescription | undefined;
};

export default function InfoTab({
struct,
data,
supply,
pairedFa,
coinData,
}: InfoTabProps) {
if (!data || Array.isArray(data)) {
Expand Down Expand Up @@ -69,11 +71,24 @@ export default function InfoTab({
title={"Creator:"}
value={
<HashButton
size="large"
hash={struct.split("::")[0]}
type={HashType.ACCOUNT}
/>
}
/>
{pairedFa && (
<ContentRow
title={"Paired FA:"}
value={
<HashButton
size="large"
hash={pairedFa}
type={HashType.FUNGIBLE_ASSET}
/>
}
/>
)}
</ContentBox>
)}
</Box>
Expand Down
3 changes: 3 additions & 0 deletions src/pages/FungibleAsset/Index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {FaMetadata, useGetFaMetadata} from "../../api/hooks/useGetFaMetadata";
import {useGetFASupply} from "../../api/hooks/useGetFaSupply";
import {CoinDescription, useGetCoinList} from "../../api/hooks/useGetCoinList";
import {findCoinData} from "../Transaction/Tabs/BalanceChangeTab";
import {useGetFaPairedCoin} from "../../api/hooks/useGetFaPairedCoin";

const TAB_VALUES_FULL: TabValue[] = ["info", "holders", "transactions"];

Expand Down Expand Up @@ -44,6 +45,7 @@ export default function FAPage() {
const {data: allCoinData} = useGetCoinList();
const metadata = useGetFaMetadata(address);
const supply = useGetFASupply(address);
const pairedCoin = useGetFaPairedCoin(address);
const isLoading = false;

const coinData = findCoinData(allCoinData?.data, address);
Expand All @@ -52,6 +54,7 @@ export default function FAPage() {
coinData: coinData,
metadata: metadata,
supply: supply,
pairedCoin: pairedCoin,
};

const tabValues = isGraphqlClientSupported ? TAB_VALUES_FULL : TAB_VALUES;
Expand Down
16 changes: 15 additions & 1 deletion src/pages/FungibleAsset/Tabs/InfoTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,22 @@
/>
<ContentRow
title={"Object Details:"}
value={<HashButton hash={address} type={HashType.OBJECT} />}
value={
<HashButton size="large" hash={address} type={HashType.OBJECT} />
}
/>
{data.pairedCoin && (

Check failure on line 101 in src/pages/FungibleAsset/Tabs/InfoTab.tsx

View workflow job for this annotation

GitHub Actions / lint

Property 'pairedCoin' does not exist on type 'FACombinedData'.
<ContentRow
title={"Paired Coin:"}
value={
<HashButton
size="large"
hash={data.pairedCoin}

Check failure on line 107 in src/pages/FungibleAsset/Tabs/InfoTab.tsx

View workflow job for this annotation

GitHub Actions / lint

Property 'pairedCoin' does not exist on type 'FACombinedData'.
type={HashType.COIN}
/>
}
/>
)}
</ContentBox>
)}
</Box>
Expand Down
Loading