-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2347 from near/ft-integrator-docs
docs: add docs for fungible token discovery and display
- Loading branch information
Showing
18 changed files
with
109 additions
and
72 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
NEAR Wallet Fungible Token ([NEP-141](https://nomicon.io/Standards/FungibleToken/Core.html)) Discovery and Display | ||
=== | ||
|
||
NEAR Wallet discovers fungible tokens using a range of indexer queries and displays them using data in the token's metadata per the `ft_metadata` spec ([NEP-148](https://nomicon.io/Standards/FungibleToken/Metadata.html)) | ||
|
||
## Contents | ||
|
||
1. [NEAR Wallet fungible token discovery](#NEAR-Wallet-fungible-token-discovery) | ||
2. [NEAR Wallet fungible token display](#NEAR-Wallet-fungible-token-display) | ||
|
||
## NEAR Wallet fungible token discovery | ||
The wallet will consider contracts as fungible tokens if they meet any of the following conditions: | ||
|
||
1. Any account makes a call to it with one of the following methods and the `receiver_id` property of the `args` is the user's account ID. | ||
* `ft_transfer` | ||
* `ft_transfer_call` | ||
* `ft_mint` | ||
|
||
2. The bridge token factory account makes a call to it with the `mint` method with the user's account ID as the `account_id` property of the `args`. | ||
3. The user's account ID calls a method named `storage_deposit` OR any method prefixed with `ft_` on it. | ||
|
||
The wallet will then make a view call to `ft_balance_of` on the considered contract and list it as a fungible token if it returns a value that is more than zero. | ||
|
||
## NEAR Wallet fungible token display | ||
|
||
The wallet will display the token using the following properties returned by the contract's `ft_metaddata`: | ||
* `icon`: An icon will be rendered for the fungible token if a data url is supplied. | ||
* `name`: Displayed as the fungible token's title (e.g. Banana) with a fallback to the `symbol` property. | ||
* `symbol`: Displayed to represent the user's balance (e.g. BANANA). | ||
* `decimals`: Used to show the proper significant digits of a token. This concept is explained well in this [OpenZeppelin post](https://docs.openzeppelin.com/contracts/3.x/erc20#a-note-on-decimals). | ||
|
||
The wallet will also display the balance returned by `ft_balance_of`: | ||
|
||
<img src="./assets/fungible-token-display.png" width="500"> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
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
45 changes: 18 additions & 27 deletions
45
packages/frontend/src/hooks/fungibleTokensIncludingNEAR.js
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 |
---|---|---|
@@ -1,35 +1,26 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { useSelector } from 'react-redux'; | ||
import { useSelector } from "react-redux"; | ||
|
||
import { selectAccountId, selectBalance } from '../redux/slices/account'; | ||
import { selectNearTokenFiatValueUSD } from '../redux/slices/tokenFiatValues'; | ||
import { selectTokensWithMetadataForAccountId } from '../redux/slices/tokens'; | ||
import { selectAccountId, selectBalance } from "../redux/slices/account"; | ||
import { selectNearTokenFiatValueUSD } from "../redux/slices/tokenFiatValues"; | ||
import { selectTokensWithMetadataForAccountId } from "../redux/slices/tokens"; | ||
|
||
const fungibleTokensIncludingNEAR = (tokens, balance, nearTokenFiatValueUSD) => { | ||
return [ | ||
{ | ||
balance, | ||
symbol: 'NEAR', | ||
usd: nearTokenFiatValueUSD | ||
}, | ||
...Object.values(tokens) | ||
]; | ||
const useNEARAsTokenWithMetadata = () => { | ||
const nearBalance = useSelector(selectBalance); | ||
const nearTokenFiatValueUSD = useSelector(selectNearTokenFiatValueUSD); | ||
|
||
return { | ||
balance: nearBalance?.balanceAvailable || "", | ||
onChainFTMetadata: { symbol: "NEAR" }, | ||
coingeckoMetadata: { usd: nearTokenFiatValueUSD }, | ||
}; | ||
}; | ||
|
||
export const useFungibleTokensIncludingNEAR = function () { | ||
const balance = useSelector(selectBalance); | ||
const nearAsToken = useNEARAsTokenWithMetadata(); | ||
const accountId = useSelector(selectAccountId); | ||
const tokens = useSelector((state) => selectTokensWithMetadataForAccountId(state, { accountId })); | ||
const nearTokenFiatValueUSD = useSelector(selectNearTokenFiatValueUSD); | ||
|
||
const balanceToDisplay = balance?.balanceAvailable; | ||
const [fungibleTokensList, setFungibleTokensList] = useState( | ||
() => fungibleTokensIncludingNEAR(tokens, balanceToDisplay) | ||
const fungibleTokens = useSelector((state) => | ||
selectTokensWithMetadataForAccountId(state, { accountId }) | ||
); | ||
|
||
useEffect(() => { | ||
setFungibleTokensList(fungibleTokensIncludingNEAR(tokens, balanceToDisplay, nearTokenFiatValueUSD)); | ||
}, [tokens, balanceToDisplay]); | ||
|
||
return fungibleTokensList; | ||
}; | ||
return [nearAsToken, ...fungibleTokens]; | ||
}; |
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