Skip to content

Commit 131f9d3

Browse files
committed
chore: chain refactoring
1 parent de98c79 commit 131f9d3

20 files changed

+460
-377
lines changed

app/api/common/annotations.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
COIN_TYPE_DESCRIPTION = "SLIP-44 coin type"
2-
CHAIN_ID_DESCRIPTION = "0x-prefixed unique hex identifier for the blockchain network (e.g. '0x1' for Ethereum, '0x2105' for Base)"
1+
COIN_TYPE_DESCRIPTION = "SLIP-44 coin type (e.g. 'ETH', 'BTC', 'SOL', etc.)"
2+
CHAIN_ID_DESCRIPTION = "Unique identifier for the blockchain network (e.g. '0x1' for Ethereum, '0x65' for Solana, 'bitcoin_mainnet' for Bitcoin, etc.)"
33
ADDRESS_DESCRIPTION = "0x-prefixed contract address of the token in case of EVM chains, base58 encoded address for Solana"
4-
VS_CURRENCY_DESCRIPTION = "The target currency to get the price in (e.g. 'USD', 'EUR')"
4+
VS_CURRENCY_DESCRIPTION = (
5+
"The target fiat currency to get the price in (e.g. 'USD', 'EUR')"
6+
)

app/api/common/models.py

Lines changed: 121 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,124 @@ class CoinType(str, Enum):
2121
ZEC = "ZEC"
2222

2323

24-
class ChainId(str, Enum):
25-
ARBITRUM = "0xa4b1"
26-
AVALANCHE = "0xa86a"
27-
BASE = "0x2105"
28-
BNB_CHAIN = "0x38"
29-
ETHEREUM = "0x1"
30-
OPTIMISM = "0xa"
31-
POLYGON = "0x89"
32-
SOLANA = "0x65"
33-
34-
35-
ChainIdCoinTypeMap = {
36-
ChainId.ARBITRUM: CoinType.ETH,
37-
ChainId.AVALANCHE: CoinType.ETH,
38-
ChainId.BASE: CoinType.ETH,
39-
ChainId.BNB_CHAIN: CoinType.ETH,
40-
ChainId.ETHEREUM: CoinType.ETH,
41-
ChainId.OPTIMISM: CoinType.ETH,
42-
ChainId.POLYGON: CoinType.ETH,
43-
ChainId.SOLANA: CoinType.SOL,
44-
}
24+
class _c(BaseModel):
25+
coin: CoinType
26+
chain_id: str
27+
simplehash_id: str
28+
alchemy_id: str
29+
has_nft_support: bool
30+
31+
32+
class Chain(Enum):
33+
# EVM chains
34+
ETHEREUM = _c(
35+
coin=CoinType.ETH,
36+
chain_id="0x1",
37+
simplehash_id="ethereum",
38+
alchemy_id="eth-mainnet",
39+
has_nft_support=True,
40+
)
41+
ARBITRUM = _c(
42+
coin=CoinType.ETH,
43+
chain_id="0xa4b1",
44+
simplehash_id="arbitrum",
45+
alchemy_id="arb-mainnet",
46+
has_nft_support=True,
47+
)
48+
AVALANCHE = _c(
49+
coin=CoinType.ETH,
50+
chain_id="0xa86a",
51+
simplehash_id="avalanche",
52+
alchemy_id="avax-mainnet",
53+
has_nft_support=True,
54+
)
55+
BASE = _c(
56+
coin=CoinType.ETH,
57+
chain_id="0x2105",
58+
simplehash_id="base",
59+
alchemy_id="base-mainnet",
60+
has_nft_support=True,
61+
)
62+
BNB_CHAIN = _c(
63+
coin=CoinType.ETH,
64+
chain_id="0x38",
65+
simplehash_id="bsc",
66+
alchemy_id="bnb-mainnet",
67+
has_nft_support=False,
68+
)
69+
OPTIMISM = _c(
70+
coin=CoinType.ETH,
71+
chain_id="0xa",
72+
simplehash_id="optimism",
73+
alchemy_id="opt-mainnet",
74+
has_nft_support=True,
75+
)
76+
POLYGON = _c(
77+
coin=CoinType.ETH,
78+
chain_id="0x89",
79+
simplehash_id="polygon",
80+
alchemy_id="polygon-mainnet",
81+
has_nft_support=True,
82+
)
83+
84+
# Non-EVM chains
85+
BITCOIN = _c(
86+
coin=CoinType.BTC,
87+
chain_id="bitcoin_mainnet",
88+
simplehash_id="bitcoin",
89+
alchemy_id="bitcoin-mainnet",
90+
has_nft_support=False,
91+
)
92+
SOLANA = _c(
93+
coin=CoinType.SOL,
94+
chain_id="0x65",
95+
simplehash_id="solana",
96+
alchemy_id="sol-mainnet",
97+
has_nft_support=True,
98+
)
99+
FILECOIN = _c(
100+
coin=CoinType.FIL,
101+
chain_id="f",
102+
simplehash_id="filecoin",
103+
alchemy_id="filecoin-mainnet",
104+
has_nft_support=False,
105+
)
106+
CARDANO = _c(
107+
coin=CoinType.ADA,
108+
chain_id="cardano_mainnet",
109+
simplehash_id="cardano",
110+
alchemy_id="cardano-mainnet",
111+
has_nft_support=False,
112+
)
113+
ZCASH = _c(
114+
coin=CoinType.ZEC,
115+
chain_id="zcash_mainnet",
116+
simplehash_id="zcash",
117+
alchemy_id="zcash-mainnet",
118+
has_nft_support=False,
119+
)
120+
121+
def __getattr__(self, name):
122+
"""Delegate attribute access to the chain info"""
123+
# Only delegate specific attributes to avoid recursion
124+
if name in [
125+
"coin",
126+
"chain_id",
127+
"simplehash_id",
128+
"alchemy_id",
129+
"has_nft_support",
130+
]:
131+
return getattr(self.value, name)
132+
133+
return super().__getattr__(name)
134+
135+
@staticmethod
136+
def get(coin: str, chain_id: str):
137+
for chain in Chain:
138+
if chain.coin.value == coin.upper() and chain.chain_id == chain_id.lower():
139+
return chain
140+
141+
return None
142+
143+
def __repr__(self):
144+
return f"<{self.__class__.__name__}.{self.name}: coin={self.coin.value} chain_id={self.chain_id}>"

app/api/nft/models.py

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -90,29 +90,6 @@ class AlchemyNFTResponse(BaseModel):
9090
model_config = ConfigDict(alias_generator=to_camel)
9191

9292

93-
class AlchemyChain(str, Enum):
94-
ETHEREUM = "eth-mainnet"
95-
POLYGON = "polygon-mainnet"
96-
BASE = "base-mainnet"
97-
OPTIMISM = "opt-mainnet"
98-
ARBITRUM = "arb-mainnet"
99-
SOLANA = "sol-mainnet"
100-
BSC = "bnb-mainnet"
101-
AVALANCHE = "avax-mainnet"
102-
103-
104-
# SimpleHash Models
105-
class SimpleHashChain(str, Enum):
106-
ETHEREUM = "ethereum"
107-
BSC = "bsc"
108-
AVALANCHE = "avalanche"
109-
POLYGON = "polygon"
110-
BASE = "base"
111-
OPTIMISM = "optimism"
112-
ARBITRUM = "arbitrum"
113-
SOLANA = "solana"
114-
115-
11693
class SimpleHashTokenType(str, Enum):
11794
ERC721 = "ERC721"
11895
ERC1155 = "ERC1155"
@@ -139,7 +116,7 @@ class SimpleHashExtraMetadata(BaseModel):
139116

140117

141118
class SimpleHashNFT(BaseModel):
142-
chain: SimpleHashChain
119+
chain: str
143120
contract_address: str
144121
token_id: str | None = None
145122
name: str | None = None

0 commit comments

Comments
 (0)