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 1 commit
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;
}
31 changes: 31 additions & 0 deletions src/api/hooks/useGetFaPairedCoin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {useViewFunction} from "./useViewFunction";

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 Buffer.from(hexWith0x.slice(2), "hex").toString("utf8");
gregnazario marked this conversation as resolved.
Show resolved Hide resolved
}
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"];

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 @@ -48,6 +48,7 @@ type TabPanelProps = {
struct: string;
data: CoinData | undefined;
supply: bigint | null;
pairedFa: string | null;
coinData: CoinDescription | undefined;
};

Expand All @@ -56,6 +57,7 @@ function TabPanel({
struct,
data,
supply,
pairedFa,
coinData,
}: TabPanelProps): JSX.Element {
const TabComponent = TabComponents[value];
Expand All @@ -64,6 +66,7 @@ function TabPanel({
struct={struct}
data={data}
supply={supply}
pairedFa={pairedFa}
coinData={coinData}
/>
);
Expand All @@ -74,6 +77,7 @@ type CoinTabsProps = {
data: CoinData | undefined;
tabValues?: TabValue[];
supply: bigint | null;
pairedFa: string | null;
coinData: CoinDescription | undefined;
};

Expand All @@ -83,6 +87,7 @@ export default function CoinTabs({
data,
tabValues = TAB_VALUES,
supply,
pairedFa,
coinData,
}: CoinTabsProps): JSX.Element {
const {tab, modulesTab} = useParams();
Expand Down Expand Up @@ -124,6 +129,7 @@ export default function CoinTabs({
struct={struct}
data={data}
supply={supply}
pairedFa={pairedFa}
coinData={coinData}
/>
</Box>
Expand Down
10 changes: 10 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 @@ -74,6 +76,14 @@ export default function InfoTab({
/>
}
/>
{pairedFa && (
<ContentRow
title={"Paired FA:"}
value={
<HashButton 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 {useGetFaMetadata} from "../../api/hooks/useGetFaMetadata";
import {useGetFASupply} from "../../api/hooks/useGetFaSupply";
import {useGetCoinList} from "../../api/hooks/useGetCoinList";
import {findCoinData} from "../Transaction/Tabs/BalanceChangeTab";
import {useGetFaPairedCoin} from "../../api/hooks/useGetFaPairedCoin";

const TAB_VALUES_FULL: TabValue[] = ["info"];

Expand All @@ -38,6 +39,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 @@ -46,6 +48,7 @@ export default function FAPage() {
coinData: coinData,
metadata: metadata,
supply: supply,
pairedCoin: pairedCoin,
};

const tabValues = isGraphqlClientSupported ? TAB_VALUES_FULL : TAB_VALUES;
Expand Down
6 changes: 6 additions & 0 deletions src/pages/FungibleAsset/Tabs/InfoTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ export default function InfoTab({address, data}: InfoTabProps) {
title={"Object Details:"}
value={<HashButton hash={address} type={HashType.OBJECT} />}
/>
{data.pairedCoin && (
<ContentRow
title={"Paired Coin:"}
value={<HashButton hash={data.pairedCoin} type={HashType.COIN} />}
/>
)}
</ContentBox>
)}
</Box>
Expand Down