Skip to content

Commit 85308a9

Browse files
authored
Merge pull request #475 from alephium/fetch-token-metadata-doc
Add doc for fetching fungible token metadata
2 parents 5231c8a + 8ec7fdd commit 85308a9

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

docs/integration/exchange.md

+86
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,92 @@ const depositInfo = getDepositInfo(tx)
209209

210210
You can filter the deposit information sent to your exchange address by `targetAddress` and `tokenId`.
211211

212+
## Fetching Fungible Token Metadata
213+
214+
You can use the API provided by the `@alephium/web3 SDK` to fetch fungible token metadata:
215+
216+
```typescript
217+
import { NodeProvider } from '@alephium/web3'
218+
219+
const tokenId = '1a281053ba8601a658368594da034c2e99a0fb951b86498d05e76aedfe666800'
220+
const nodeProvider = new NodeProvider('http://127.0.0.1:12973')
221+
const metadata = await nodeProvider.fetchFungibleTokenMetaData(tokenId)
222+
// {
223+
// symbol: '4159494e',
224+
// name: '4159494e',
225+
// decimals: 18,
226+
// totalSupply: 1693518090594172274149304n
227+
// }
228+
```
229+
230+
The `symbol` and `name` are hex-encoded UTF-8 strings.
231+
232+
You can also use the raw endpoint to fetch fungible token metadata:
233+
234+
```shell
235+
curl -X 'POST' \
236+
'http://127.0.0.1:12973/contracts/multicall-contract' \
237+
-H 'accept: application/json' \
238+
-H 'Content-Type: application/json' \
239+
-d '{
240+
"calls": [
241+
{
242+
"group": 0,
243+
"address": "vT49PY8ksoUL6NcXiZ1t2wAmC7tTPRfFfER8n3UCLvXy",
244+
"methodIndex": 0
245+
},
246+
{
247+
"group": 0,
248+
"address": "vT49PY8ksoUL6NcXiZ1t2wAmC7tTPRfFfER8n3UCLvXy",
249+
"methodIndex": 1
250+
},
251+
{
252+
"group": 0,
253+
"address": "vT49PY8ksoUL6NcXiZ1t2wAmC7tTPRfFfER8n3UCLvXy",
254+
"methodIndex": 2
255+
},
256+
{
257+
"group": 0,
258+
"address": "vT49PY8ksoUL6NcXiZ1t2wAmC7tTPRfFfER8n3UCLvXy",
259+
"methodIndex": 3
260+
}
261+
]
262+
}'
263+
```
264+
265+
All contracts that follow the fungible token standard can use this request to fetch the token metadata. This request calls the contract methods indexed at 0, 1, 2, and 3, which respectively return the token symbol, token name, token decimals, and token total supply.
266+
267+
This request uses the token address instead of the token ID. You can refer to the code below to convert the token ID to the token address:
268+
269+
```typescript
270+
const tokenAddress = base58.encode([0x03, ...hexToBytes(tokenId)])
271+
```
272+
273+
The following includes part of the response information for simplicity:
274+
275+
```json
276+
{
277+
"results": [
278+
{
279+
"type": "CallContractSucceeded",
280+
"returns": [{ "type": "ByteVec", "value": "4159494e" }]
281+
},
282+
{
283+
"type": "CallContractSucceeded",
284+
"returns": [{ "type": "ByteVec", "value": "4159494e" }]
285+
},
286+
{
287+
"type": "CallContractSucceeded",
288+
"returns": [{ "type": "U256", "value": "18" }],
289+
},
290+
{
291+
"type": "CallContractSucceeded",
292+
"returns": [{ "type": "U256", "value": "1693518090594172274149304" }]
293+
}
294+
]
295+
}
296+
```
297+
212298
## Block APIs
213299

214300
### Get block hash with transaction ID

docs/sdk/transaction.md

+3
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,14 @@ console.log('unsigned transaction', buildTxResult.unsignedTx)
9595
You can also use `builtTransferTx` to build a token transfer transaction:
9696

9797
```typescript
98+
import { DUST_AMOUNT } from '@alephium/web3'
99+
98100
const buildTxResult = await builder.buildTransferTx(
99101
{
100102
signerAddress: senderAddress,
101103
destinations: [{
102104
address: receiverAddress,
105+
attoAlphAmount: DUST_AMOUNT,
103106
tokens: [{
104107
id: '19246e8c2899bc258a1156e08466e3cdd3323da756d8a543c7fc911847b96f00',
105108
amount: 1000000000000000000n

0 commit comments

Comments
 (0)