-
-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathTokenId.ts
More file actions
129 lines (121 loc) · 3.56 KB
/
Copy pathTokenId.ts
File metadata and controls
129 lines (121 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import * as AbiParameters from '../core/AbiParameters.js'
import * as Address from '../core/Address.js'
import * as Hash from '../core/Hash.js'
import * as Hex from '../core/Hex.js'
import * as TempoAddress from './TempoAddress.js'
import * as TokenAddress from './TokenAddress.js'
const tip20Prefix = TokenAddress.prefix
const tip20PrefixSize = 12
export type TokenId = bigint
export type TokenIdOrAddress<addressType = Address.Address> =
| TokenId
| addressType
/**
* Converts a token ID or address to a token ID.
*
* TIP-20 is Tempo's native token standard for stablecoins with deterministic addresses
* derived from sequential token IDs (prefix `0x20c0`).
*
* [TIP-20 Token Standard](https://docs.tempo.xyz/protocol/tip20/overview)
*
* @example
* ```ts twoslash
* import { TokenId } from 'ox/tempo'
*
* const tokenId = TokenId.from(1n)
* ```
*
* @param tokenIdOrAddress - The token ID or address.
* @returns The token ID.
*/
export function from(tokenIdOrAddress: TokenIdOrAddress | number): TokenId {
if (
typeof tokenIdOrAddress === 'bigint' ||
typeof tokenIdOrAddress === 'number'
)
return BigInt(tokenIdOrAddress)
return fromAddress(tokenIdOrAddress)
}
/**
* Converts a TIP-20 token address to a token ID.
*
* [TIP-20 Token Standard](https://docs.tempo.xyz/protocol/tip20/overview)
*
* @example
* ```ts twoslash
* import { TokenId } from 'ox/tempo'
*
* const tokenId = TokenId.fromAddress('0x20c0000000000000000000000000000000000001')
* ```
*
* @param address - The token address.
* @returns The token ID.
*/
export function fromAddress(address: TempoAddress.Address): TokenId {
const resolved = TempoAddress.resolve(address)
if (!TokenAddress.isTip20(resolved)) throw new Error('invalid tip20 address.')
return Hex.toBigInt(Hex.slice(resolved, tip20PrefixSize))
}
/**
* Converts a TIP-20 token ID to an address.
*
* [TIP-20 Token Standard](https://docs.tempo.xyz/protocol/tip20/overview)
*
* @example
* ```ts twoslash
* import { TokenId } from 'ox/tempo'
*
* const address = TokenId.toAddress(1n)
* ```
*
* @param tokenId - The token ID.
* @returns The address.
*/
export function toAddress(
tokenId: TokenIdOrAddress<TempoAddress.Address>,
): Address.Address {
if (typeof tokenId === 'string') {
const resolved = TempoAddress.resolve(tokenId as TempoAddress.Address)
Address.assert(resolved)
return resolved
}
const tokenIdHex = Hex.fromNumber(tokenId, { size: 8 })
return Hex.concat(tip20Prefix, tokenIdHex)
}
/**
* Computes a deterministic TIP-20 token address from a sender address and salt.
*
* The address is computed as: `TIP20_PREFIX (12 bytes) || keccak256(abi.encode(sender, salt))[:8]`
*
* [TIP-20 Token Standard](https://docs.tempo.xyz/protocol/tip20/overview)
*
* @example
* ```ts twoslash
* import { TokenId } from 'ox/tempo'
*
* const id = TokenId.compute({
* sender: '0x1234567890123456789012345678901234567890',
* salt: '0x0000000000000000000000000000000000000000000000000000000000000001',
* })
* ```
*
* @param value - The sender address and salt.
* @returns The computed TIP-20 token id.
*/
export function compute(value: compute.Value): bigint {
const hash = Hash.keccak256(
AbiParameters.encode(AbiParameters.from('address, bytes32'), [
TempoAddress.resolve(value.sender),
value.salt,
]),
)
return Hex.toBigInt(Hex.slice(hash, 0, 8))
}
export declare namespace compute {
export type Value = {
/** The salt (32 bytes). */
salt: Hex.Hex
/** The sender address. Accepts both hex and Tempo bech32m addresses. */
sender: TempoAddress.Address
}
}