-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Norman Meier <[email protected]>
- Loading branch information
Showing
21 changed files
with
495 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { Decimal } from "@cosmjs/math"; | ||
|
||
import { useCoingeckoPrices } from "./useCoingeckoPrices"; | ||
import { useNSMintAvailability } from "./useNSMintAvailability"; | ||
import { useNSMintPrice } from "./useNSMintPrice"; | ||
import { useVaultNFTInfo } from "./useVaultNFTInfo"; | ||
import { getCosmosNetwork, getNativeCurrency, getNftId } from "../networks"; | ||
import { CoingeckoCoin } from "../utils/coingecko"; | ||
import { prettyPrice } from "../utils/coins"; | ||
|
||
export type NSAvailability = | ||
| { | ||
availability: "loading"; | ||
} | ||
| { | ||
availability: "invalid"; | ||
} | ||
| { | ||
availability: "mint" | "market"; | ||
price: { | ||
denom: string; | ||
amount: string; | ||
}; | ||
usdPrice?: number; | ||
prettyPrice: string; | ||
} | ||
| { | ||
availability: "none"; | ||
}; | ||
|
||
// TODO: support gno | ||
|
||
export const useNSAvailability = ( | ||
networkId: string | undefined, | ||
name: string, | ||
): NSAvailability => { | ||
const cosmosNetwork = getCosmosNetwork(networkId); | ||
const tokenId = name + cosmosNetwork?.nameServiceTLD; | ||
const { nsMintPrice, isLoading: isLoadingNSMintPrice } = useNSMintPrice( | ||
networkId, | ||
tokenId, | ||
); | ||
const { nameAvailable, loading: isLoadingNameAvailability } = | ||
useNSMintAvailability(networkId, tokenId); | ||
const { vaultNFTInfo, isLoading: isLoadingVaultNFTInfo } = useVaultNFTInfo( | ||
getNftId(networkId, cosmosNetwork?.nameServiceContractAddress, tokenId), | ||
); | ||
const requestedPrices: CoingeckoCoin[] = []; | ||
if (nsMintPrice) { | ||
requestedPrices.push({ networkId, denom: nsMintPrice.denom }); | ||
} | ||
if (vaultNFTInfo) { | ||
requestedPrices.push({ networkId, denom: vaultNFTInfo.denom }); | ||
} | ||
const { prices } = useCoingeckoPrices(requestedPrices); | ||
|
||
// TODO: handle already owned case | ||
|
||
if ( | ||
isLoadingNSMintPrice || | ||
isLoadingNameAvailability || | ||
isLoadingVaultNFTInfo | ||
) { | ||
return { | ||
availability: "loading", | ||
}; | ||
} | ||
|
||
if (nsMintPrice?.invalid) { | ||
return { | ||
availability: "invalid", | ||
}; | ||
} | ||
|
||
if (nameAvailable && nsMintPrice) { | ||
const currency = getNativeCurrency(networkId, nsMintPrice.denom); | ||
const currencyUSDPrice = prices[currency?.coingeckoId || ""]?.usd; | ||
const usdPrice = currencyUSDPrice | ||
? currencyUSDPrice * | ||
Decimal.fromAtomics( | ||
nsMintPrice.amount, | ||
currency?.decimals || 0, | ||
).toFloatApproximation() | ||
: undefined; | ||
return { | ||
availability: "mint", | ||
price: nsMintPrice, | ||
prettyPrice: prettyPrice( | ||
cosmosNetwork?.id, | ||
nsMintPrice.amount, | ||
nsMintPrice.denom, | ||
), | ||
usdPrice, | ||
}; | ||
} | ||
|
||
if (vaultNFTInfo) { | ||
const currency = getNativeCurrency(networkId, vaultNFTInfo.denom); | ||
const currencyUSDPrice = prices[currency?.coingeckoId || ""]?.usd; | ||
const usdPrice = currencyUSDPrice | ||
? currencyUSDPrice * | ||
Decimal.fromAtomics( | ||
vaultNFTInfo.amount, | ||
getNativeCurrency(networkId, vaultNFTInfo.denom)?.decimals || 0, | ||
).toFloatApproximation() | ||
: undefined; | ||
return { | ||
availability: "market", | ||
price: { | ||
denom: vaultNFTInfo.denom, | ||
amount: vaultNFTInfo.amount, | ||
}, | ||
prettyPrice: prettyPrice( | ||
cosmosNetwork?.id, | ||
vaultNFTInfo.amount, | ||
vaultNFTInfo.denom, | ||
), | ||
usdPrice, | ||
}; | ||
} | ||
|
||
return { | ||
availability: "none", | ||
}; | ||
}; |
2 changes: 1 addition & 1 deletion
2
packages/hooks/useNSNameAvailability.ts → packages/hooks/useNSMintAvailability.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { useQuery } from "@tanstack/react-query"; | ||
|
||
import { TeritoriNftVaultQueryClient } from "../contracts-clients/teritori-nft-vault/TeritoriNftVault.client"; | ||
import { | ||
getCosmosNetwork, | ||
mustGetNonSigningCosmWasmClient, | ||
parseNftId, | ||
} from "../networks"; | ||
|
||
export const useVaultNFTInfo = (nftId: string | undefined) => { | ||
const { data, ...other } = useQuery( | ||
["vaultNFTInfo", nftId], | ||
async () => { | ||
console.log("getting nft info of ", nftId); | ||
|
||
const [nftNetwork, nftContractAddress, tokenId] = parseNftId(nftId); | ||
if (!nftNetwork || !nftContractAddress || !tokenId) { | ||
return null; | ||
} | ||
|
||
console.log("parsed nft info", nftNetwork, nftContractAddress, tokenId); | ||
|
||
const network = getCosmosNetwork(nftNetwork.id); | ||
if (!network?.vaultContractAddress) { | ||
return null; | ||
} | ||
|
||
console.log("network nft info", network); | ||
|
||
const client = await mustGetNonSigningCosmWasmClient(network.id); | ||
const vaultClient = new TeritoriNftVaultQueryClient( | ||
client, | ||
network?.vaultContractAddress, | ||
); | ||
return await vaultClient.nftInfo({ | ||
nftContractAddr: nftContractAddress, | ||
nftTokenId: tokenId, | ||
}); | ||
}, | ||
{ staleTime: Infinity }, | ||
); | ||
|
||
return { vaultNFTInfo: data, ...other }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.