Skip to content

Commit

Permalink
Merge pull request #23 from metastellar-io/iwaki
Browse files Browse the repository at this point in the history
modify fetching asset info to be flexible
  • Loading branch information
mymiracle0118 authored Jul 4, 2024
2 parents c79447b + 3dde068 commit 1d5348a
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 143 deletions.
16 changes: 8 additions & 8 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { AssetAccount } from "$lib/types.js";

export const assetInfo: AssetAccount = {
code: "Spunks",
issuer: "GCAR7OJULRZBOMMUPJ33YMI3O6GPBMKHWVWQAD2BHSH6NZ25QYMI44UP"
}

// export const assetInfo: AssetAccount = {
// code: "CYBERMAN007",
// issuer: "GABSMRCVPJJQZ3PDYKR4B3UCU26JBMJFSWEQVQAHPDEFHDMSJI3IQ7IT"
// }
// code: "Spunks",
// issuer: "GCAR7OJULRZBOMMUPJ33YMI3O6GPBMKHWVWQAD2BHSH6NZ25QYMI44UP"
// }

export const assetInfo: AssetAccount = {
code: "CYBERMAN007",
issuer: "GABSMRCVPJJQZ3PDYKR4B3UCU26JBMJFSWEQVQAHPDEFHDMSJI3IQ7IT"
}
55 changes: 0 additions & 55 deletions src/lib/nftPoster/nftModal.svelte

This file was deleted.

85 changes: 9 additions & 76 deletions src/lib/nftPoster/nftPoster.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import { twMerge } from 'tailwind-merge';
import Frame from "../frame/frame.svelte";
import type {AssetAccount, AssetRaw, AssetMetaData, SizeType} from '$lib/types.js';
import type {AssetAccount, AssetRaw, AssetStatistics, AssetFlag, AssetMetaData, links, SizeType} from '$lib/types.js';
import {getMetadata} from '$lib/utility.ts';
import {baseURL} from '$lib/constants.js';
let showModal = false;
Expand Down Expand Up @@ -46,84 +47,16 @@
let assetInfo: AssetRaw;
let assetMetadata: AssetMetaData;
async function fetchToml(request: string, code: string) {
console.log("request", request);
try {
const res = await fetch(request);
if(res.ok) {
let result = await res.text();
// result = result.replaceAll(" ", "");
// console.log(result);
const assetMetadataArray = result.split('[[CURRENCIES]]')
.filter(Boolean) // Filter empty elements resulting from splitting
.slice(1) // Slice away the first element, which includes VERSION and NETWORK_PASSPHRASE
.map(entry => ({
code: entry.match(/code\s*=\s*['"]*([^'"]*)['"]*/)?.[1],
issuer: entry.match(/issuer\s*=\s*['"]*([^'"]*)['"]*/)?.[1],
// anchor_asset_type: entry.match(/anchor_asset_type\s*=\s*['"]*([^'"]*)['"]*/)?.[1],
name: entry.match(/name\s*=\s*['"]*([^'"]*)['"]*/)?.[1],
desc: entry.match(/desc\s*=\s*['"]*([^'"]*)['"]*/)?.[1],
image: entry.match(/image\s*=\s*['"]*([^'"]*)['"]*/)?.[1],
// display_decimals: entry.match(/display_decimals\s*=\s*(\d*\.*\d*)/)?.[1]
}));
// console.log(assetMetadataArray);
const assetMetadata = assetMetadataArray.filter(entry => !code || entry.code?.includes(code));
console.log("assetMetadata", assetMetadata);
if(assetMetadata == undefined || assetMetadata[0] == undefined) {
return {
result: false,
data: null,
error: null
}
}
return {
result: true,
data: assetMetadata[0],
error: null
}
}
} catch (error) {
return {
result: false,
data: null,
error: error
}
}
async function getNFTData(assetAccount: AssetAccount) {
const data = await getMetadata(assetAccount);
if(data?.result) {
assetInfo = data.data?.asset_raw as AssetRaw;
assetMetadata = data.data?.metadata as AssetMetaData;
}
}
async function getMetadata (assetAccount: AssetAccount) {
if (assetAccount == undefined) return;
let request = baseURL + "assets?asset_code=" + assetAccount.code + "&asset_issuer=" + assetAccount.issuer;
try {
const res = await fetch(request);
if(res.ok){
const result = await res.json();
if(result?._embedded == undefined) {
return;
}
assetInfo = result._embedded.records[0];
// console.log(assetInfo._links.toml.href);
const metaDataInfo = await fetchToml(assetInfo._links.toml.href, assetAccount.code);
console.log(metaDataInfo);
// return metaDataInfo;
if(metaDataInfo && metaDataInfo.result) {
assetMetadata = metaDataInfo.data as AssetMetaData;
// console.log(assetMetadata);
}
}
} catch (e) {
console.log('nft metadata request error', e);
// return {
// result: false,
// data: null,
// error: e
// }
}
};
onMount(() => {
getMetadata(assetAccount);
getNFTData(assetAccount);
});
let cardClass: string;
Expand Down
12 changes: 8 additions & 4 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export interface links {
toml: Toml;
}

export interface AssetExtra {
export interface AssetStatistics {
authorized: number;
authorized_to_maintain_liabilities: number;
unauthorized: number;
Expand All @@ -196,16 +196,20 @@ export interface AssetRaw {
asset_type: string; // This asset's type. Either credit_alphanum4 or credit_alphanum12.
asset_code: string; // This asset's code
asset_issuer: string; // The Stellar address of this asset’s issuer.
accounts: AssetExtra; // The number of accounts grouped by each trustline flag state.
paging_token: string; // The paging token code + address + type
contract_id: string; // The contract id
num_accounts: number; // The number of accounts that have issued a trustline to this asset. If the auth_required flag for this asset's issuer is set to true, this number only includes the accounts who have both set up a trustline to the asset and have been authorized to hold the asset. This will be deprecated in Horizon v3.
accounts: AssetStatistics; // The number of accounts grouped by each trustline flag state.
num_claimable_balances: number; // The current number of claimable_balances for this asset.
num_contracts: number; // The current number of Soroban contracts holding this asset.
num_liquidity_pools: number; // The current number of liquidity pools holding this asset.
balances: AssetExtra; // The number of units issued for this asset grouped by each trustline flag state.
balances: AssetStatistics; // The number of units issued for this asset grouped by each trustline flag state.
num_archived_contracts: number;
claimable_balances_amount: string; // The number of units for this asset held by all claimable balances.
contracts_amount: string; // The number of units for this asset held by all Soroban contracts.
liquidity_pools_amount: string; // The number of units for this asset held by all liquidity pools.
archived_contracts_amount: string;
amount: string; // The number of authorized units issued for this asset. This will be deprecated in Horizon v3.
num_accounts: number; // The number of accounts that have issued a trustline to this asset. If the auth_required flag for this asset's issuer is set to true, this number only includes the accounts who have both set up a trustline to the asset and have been authorized to hold the asset. This will be deprecated in Horizon v3.
flags: AssetFlag;
_links: links;
}
Expand Down
131 changes: 131 additions & 0 deletions src/lib/utility.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import type { AssetAccount, AssetRaw, AssetStatistics, AssetFlag, AssetMetaData, links, SizeType } from '$lib/types.js';
import { baseURL } from '$lib/constants.js';

function convertDataToAssetRaw(_data: any): AssetRaw {
return {
_links: _data?._links as links,
asset_type: _data?.asset_type,
asset_code: _data?.asset_code,
asset_issuer: _data?.asset_issuer,
paging_token: _data?.paging_token,
contract_id: _data?.contract_id,
num_accounts: _data?.num_accounts,
accounts: _data?.accounts as AssetStatistics,
num_claimable_balances: _data?.num_claimable_balances,
num_contracts: _data?.num_contracts,
num_liquidity_pools: _data?.num_liquidity_pools,
balances: _data?.balances as AssetStatistics,
num_archived_contracts: _data?.num_archived_contracts,
claimable_balances_amount: _data?.claimable_balances_amount,
contracts_amount: _data?.contracts_amount,
liquidity_pools_amount: _data?.liquidity_pools_amount,
archived_contracts_amount: _data?.archived_contracts_amount,
amount: _data?.amount,
flags: _data?.flags as AssetFlag,
};
}

function convertDataToAssetMetadata(_data: any): AssetMetaData {
return {
code: _data?.code,
issuer: _data?.issuer,
decimals: _data?.decimals,
anchored: _data?.anchored,
name: _data?.name,
desc: _data?.desc,
image: _data?.image,
};
}

async function fetchToml(request: string, code: string) {
try {
const res = await fetch(request);
if (res.ok) {
let result = await res.text();
// result = result.replaceAll(" ", "");
// console.log(result);
const assetMetadataArray = result.split('[[CURRENCIES]]')
.filter(Boolean) // Filter empty elements resulting from splitting
.slice(1) // Slice away the first element, which includes VERSION and NETWORK_PASSPHRASE
.map(entry => ({
code: entry.match(/code\s*=\s*['"]*([^'"]*)['"]*/)?.[1],
issuer: entry.match(/issuer\s*=\s*['"]*([^'"]*)['"]*/)?.[1],
anchored: entry.match(/is_asset_anchored\s*=\s*['"]*(\w*)['"]*/)?.[1],
name: entry.match(/name\s*=\s*['"]*([^'"]*)['"]*/)?.[1],
desc: entry.match(/desc\s*=\s*['"]*([^'"]*)['"]*/)?.[1],
image: entry.match(/image\s*=\s*['"]*([^'"]*)['"]*/)?.[1],
decimals: entry.match(/display_decimals\s*=\s*(\d*[.]*\d*)/)?.[1]
}));
// console.log(assetMetadataArray);
const assetMetadata = assetMetadataArray.filter(entry => !code || entry.code?.includes(code));
console.log("assetMetadata", assetMetadata);
if (assetMetadata == undefined || assetMetadata[0] == undefined) {
return {
result: false,
data: null,
error: null
}
}
return {
result: true,
data: assetMetadata[0],
error: null
}
}
} catch (error) {
return {
result: false,
data: null,
error: error
}
}
}

export async function getMetadata(assetAccount: AssetAccount) {
if (assetAccount == undefined) return;
let request = baseURL + "assets?asset_code=" + assetAccount.code + "&asset_issuer=" + assetAccount.issuer;
try {
const res = await fetch(request);
if (res.ok) {
const result = await res.json();
if (result?._embedded == undefined) {
return {
result: false,
data: null,
error: null
};
}

const data: AssetRaw = convertDataToAssetRaw(result._embedded.records[0]);
const metaDataInfo = await fetchToml(data?._links?.toml?.href, assetAccount?.code);
console.log("metadata info", metaDataInfo);
if (metaDataInfo?.result) {
const metadata: AssetMetaData = convertDataToAssetMetadata(metaDataInfo.data);
return {
result: true,
data: {
asset_raw: data,
metadata: metadata
},
error: null
};
}

return {
result: true,
data: {
asset_raw: data,
metadata: null
},
error: null
};
}
} catch (e) {
// console.log('nft metadata request error', e);
return {
result: false,
data: null,
error: e
}
}
}

0 comments on commit 1d5348a

Please sign in to comment.