Skip to content

Commit 469b6dc

Browse files
committed
✨ feat: support for multichain
1 parent 2a0ff92 commit 469b6dc

File tree

10 files changed

+78
-58
lines changed

10 files changed

+78
-58
lines changed

README.md

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,38 @@ You can find specific documentation on a per-product basis below.
3535
## SDK Documentation
3636
You can learn more about the Transpose SDK and how it works below.
3737

38+
### Updating Chain ID
39+
If you want to change the chain ID of your query, you can do so by setting the `chain_id` or `chain` properties of the `Transpose` object. For example, if you want to query the Ethereum mainnet, you can do so by running the following code:
40+
41+
```python
42+
from transpose import Transpose
43+
api = Transpose(api_key="YOUR_API_KEY", chain_id=1)
44+
```
45+
46+
or
47+
```python
48+
from transpose import Transpose
49+
api = Transpose(api_key="YOUR_API_KEY", chain_id="ethereum")
50+
```
51+
52+
if you wish to change the chain ID of an existing `Transpose` object, you can do so by running the following code:
53+
54+
```python
55+
api.set_chain("ethereum")
56+
```
57+
58+
or
59+
60+
```python
61+
api.set_chain(1)
62+
```
63+
64+
#### Currently supported chains
65+
| Chain ID | Chain Name |
66+
| :------: | :--------: |
67+
| 1 | Ethereum |
68+
| 137 | Polygon |
69+
3870
### SDK Classes
3971
The Transpose SDK uses custom classes to represent API responses:
4072

@@ -124,11 +156,11 @@ api.bulk_request(endpoint_response, requests_per_second, results_to_fetch)
124156
Here is an example of how to use ``bulk_request``:
125157

126158
```python
127-
all_blocks_by_miner = api.bulk_request(api.block.blocks_by_date(mined_after='2022-01-01 00:00:00', miner='0x00192Fb10dF37c9FB26829eb2CC623cd1BF599E8', limit=500))
159+
recent_blocks_since = api.bulk_request(api.block.blocks_by_date(block_timestamp_after='2022-01-01 00:00:00', limit=500))
128160

129-
print(len(all_blocks_by_miner))
161+
print(len(recent_blocks_since))
130162

131-
>>> 53046
163+
>>> 500
132164
```
133165
</details>
134166

demo/plotting.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
api = Transpose('API_KEY')
88

9-
mined_after=(datetime.now() - timedelta(minutes=60)).astimezone(timezone.utc).strftime("%Y-%m-%d %H:%M:%S")
10-
historical_blocks = api.block.blocks_by_date(mined_after=mined_after, order="desc", limit=500)
9+
block_timestamp_after=(datetime.now() - timedelta(minutes=60)).astimezone(timezone.utc).strftime("%Y-%m-%d %H:%M:%S")
10+
historical_blocks = api.block.blocks_by_date(block_timestamp_after=block_timestamp_after, order="desc", limit=500)
1111
historical_base_gas_prices = [block.base_fee_per_gas / 1000000000 for block in historical_blocks]
1212

1313
chart = Plot(title="Hourly Gas Prices on Ethereum")

docs/block.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ The **Account Model** represents a single account. This includes both externally
4444

4545
| SDK Method | Endpoint URL | Returns |
4646
| ------------------------------------------------------------------------------------- | -------------------------------- | ------------- |
47-
| `block.blocks_by_number(block_number_above, block_number_below, miner, order, limit)` | `GET /v0/block/blocks-by-number` | `List[Block]` |
48-
| `block.blocks_by_date(mined_after, mined_before, miner, order, limit)` | `GET /v0/block/blocks-by-date` | `List[Block]` |
47+
| `block.blocks_by_number(block_number_above, block_number_below, order, limit)` | `GET /v0/block/blocks-by-number` | `List[Block]` |
48+
| `block.blocks_by_date(block_timestamp_after, block_timestamp_before, order, limit)` | `GET /v0/block/blocks-by-date` | `List[Block]` |
4949

5050
### Block Model
5151

@@ -75,7 +75,7 @@ The **Block Model** represents a single block. The **Block Model** follows the f
7575
| total_fees_saved | The amount of transaction fees saved by transactions in the block (in Wei). | `integer` |
7676
| transaction_count | The number of transactions in the block. | `integer` |
7777
| miner | The address of the miner who mined the block. | `string` |
78-
| mining_reward | The amount rewarded to the miner of the block (in Wei). | `integer` |
78+
| block_reward | The amount rewarded to the miner of the block (in Wei). | `integer` |
7979
| uncle_count | The number of uncle blocks included in the block. | `integer` |
8080
| uncles | The uncle blocks included in the block (maximum 2 uncles per block). | `array` |
8181

@@ -88,7 +88,7 @@ The **Block Model** represents a single block. The **Block Model** follows the f
8888
| `block.transactions_by_hash(transaction_hashes)` | `GET /v0/block/transactions-by-hash` | `List[Transaction]` |
8989
| `block.transactions_by_account(account_address, occurred_after, occurred_before, direction, order, limit)` | `GET /v0/block/transactions-by-account` | `List[Transaction]` |
9090
| `block.transactions_by_block(block_number_above, block_number_below, order, limit)` | `GET /v0/block/transactions-by-block` | `List[Transaction]` |
91-
| `block.transactions_by_date(occurred_after, occurred_before, miner, order, limit)` | `GET /v0/block/transactions-by-date` | `List[Transaction]` |
91+
| `block.transactions_by_date(occurred_after, occurred_before, order, limit)` | `GET /v0/block/transactions-by-date` | `List[Transaction]` |
9292

9393
### Transaction Model
9494

tests/test_block_blocks_by_date.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ def test_basic():
44
try:
55
api = Transpose(api_key)
66

7-
blocks = api.block.blocks_by_date(mined_after='2020-01-01 00:00:00',)
7+
blocks = api.block.blocks_by_date(block_timestamp_after='2020-01-01 00:00:00',)
88

99
assert len(blocks) >= 1
1010

@@ -15,7 +15,7 @@ def test_cursor():
1515
try:
1616
api = Transpose(api_key)
1717

18-
blocks = api.block.blocks_by_date(mined_after='2020-01-01 00:00:00',)
18+
blocks = api.block.blocks_by_date(block_timestamp_after='2020-01-01 00:00:00',)
1919

2020
assert len(blocks) >= 1
2121
assert api._next != None
@@ -24,17 +24,5 @@ def test_cursor():
2424

2525
assert len(blocks) >= 1
2626

27-
except Exception:
28-
assert False
29-
30-
def test_miner():
31-
try:
32-
api = Transpose(api_key)
33-
34-
blocks = api.block.blocks_by_date(mined_after='2020-01-01 00:00:00', miner='0x00192Fb10dF37c9FB26829eb2CC623cd1BF599E8')
35-
36-
assert len(blocks) >= 1
37-
assert not any(block.miner != '0x00192Fb10dF37c9FB26829eb2CC623cd1BF599E8' for block in blocks)
38-
3927
except Exception:
4028
assert False

tests/test_block_blocks_by_number.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,5 @@ def test_cursor():
2424

2525
assert len(blocks) >= 1
2626

27-
except Exception:
28-
assert False
29-
30-
def test_miner():
31-
try:
32-
api = Transpose(api_key)
33-
34-
blocks = api.block.blocks_by_number(block_number_above=0, miner='0x00192Fb10dF37c9FB26829eb2CC623cd1BF599E8')
35-
36-
assert len(blocks) >= 1
37-
assert not any(block.miner != '0x00192Fb10dF37c9FB26829eb2CC623cd1BF599E8' for block in blocks)
38-
3927
except Exception:
4028
assert False
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
from ..constants import BLOCK_API_ENDPOINTS
22

3-
def _blocks_by_date(mined_after:str or int='1970-01-01T00:00:00Z',
4-
mined_before: str or int='2050-01-01T00:00:00Z',
5-
miner: str = None,
3+
def _blocks_by_date(block_timestamp_after:str or int='1970-01-01T00:00:00Z',
4+
block_timestamp_before: str or int='2050-01-01T00:00:00Z',
65
order: str = 'asc',
76
limit: int = 10,) -> str:
8-
base_url = '{}?mined_after={}&mined_before={}&order={}&limit={}'.format(BLOCK_API_ENDPOINTS['blocks_by_date'], mined_after, mined_before, order, limit)
9-
10-
if miner != None:
11-
base_url += '&miner={}'.format(miner)
7+
base_url = '{}?block_timestamp_after={}&block_timestamp_before={}&order={}&limit={}'.format(BLOCK_API_ENDPOINTS['blocks_by_date'], block_timestamp_after, block_timestamp_before, order, limit)
128

139
return base_url

transpose/src/api/block/_blocks_by_number.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22

33
def _blocks_by_number(block_number_above: int = 0,
44
block_number_below: int = 1000000000,
5-
miner: str = None,
65
order: str = 'asc',
76
limit: int = 10,) -> str:
87
base_url = '{}?block_number_above={}&block_number_below={}&order={}&limit={}'.format(BLOCK_API_ENDPOINTS['blocks_by_number'], block_number_above, block_number_below, order, limit)
9-
10-
if miner != None:
11-
base_url += '&miner={}'.format(miner)
128

139
return base_url

transpose/src/api/block/base.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,18 @@ def contracts_by_creator(self,
5454
def blocks_by_number(self,
5555
block_number_above: int = 0,
5656
block_number_below: int = 1000000000,
57-
miner: str = None,
5857
order: str = 'asc',
5958
limit: int = 10) -> List[BlockModel]:
60-
return self.super.perform_authorized_request(BlockModel, _blocks_by_number(block_number_above=block_number_above, block_number_below=block_number_below, miner=miner, order=order, limit=limit))
59+
return self.super.perform_authorized_request(BlockModel, _blocks_by_number(block_number_above=block_number_above, block_number_below=block_number_below, order=order, limit=limit))
6160

6261
# Get Blocks by Date
6362
# https://api.transpose.io/v0/block/blocks-by-date
6463
def blocks_by_date(self,
65-
mined_after:str or int='1970-01-01T00:00:00Z',
66-
mined_before: str or int='2050-01-01T00:00:00Z',
67-
miner: str = None,
64+
block_timestamp_after:str or int='1970-01-01T00:00:00Z',
65+
block_timestamp_before: str or int='2050-01-01T00:00:00Z',
6866
order: str = 'asc',
6967
limit: int = 10) -> List[BlockModel]:
70-
return self.super.perform_authorized_request(BlockModel, _blocks_by_date(mined_after=mined_after, mined_before=mined_before, miner=miner, order=order, limit=limit))
68+
return self.super.perform_authorized_request(BlockModel, _blocks_by_date(block_timestamp_after=block_timestamp_after, block_timestamp_before=block_timestamp_before, order=order, limit=limit))
7169

7270
# Get Transactions by Hash
7371
# https://api.transpose.io/v0/block/transactions-by-hash

transpose/src/base.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,25 @@
1212

1313
# base class for the Transpose python SDK
1414
class Transpose:
15-
def __init__(self, api_key: str, debug: bool=False, host: str=None) -> None:
15+
def __init__(self, api_key: str, debug: bool=False, host: str=None, chain_id: int=0, chain: str="ethereum") -> None:
1616
self._next = None
1717
self._next_class_name = None
1818
self.host = host if host else 'https://api.transpose.io'
1919
self.verbose = debug
2020

21+
if chain.lower() == "ethereum":
22+
self.chain_id = 1
23+
24+
if chain.lower() == "polygon":
25+
self.chain_id = 137
26+
27+
if chain_id != 0:
28+
self.chain_id = chain_id
29+
2130
# verifies that the API key is valid
2231
if self.perform_authorized_request(Block, 'https://api.transpose.io/v0/block/blocks-by-number?block_number_below=1', api_key):
2332
self.api_key = api_key
24-
33+
2534
# define the subclasses
2635
self.ens = ENS(self)
2736
self.nft = NFT(self)
@@ -37,6 +46,17 @@ def __init__(self, api_key: str, debug: bool=False, host: str=None) -> None:
3746
def next(self) -> str:
3847
return self.perform_authorized_request(self._next_class_name, self._next)
3948

49+
def set_chain_ID(self, chain_id: int=0, chain: str="ethereum") -> None:
50+
if chain.lower() == "ethereum":
51+
self.chain_id = 1
52+
53+
if chain.lower() == "polygon":
54+
self.chain_id = 137
55+
56+
if chain_id != 0:
57+
self.chain_id = chain_id
58+
59+
4060
# this can be renamed later. Pagination helper function to get many
4161
def bulk_request(self, endpoint_response: List, requests_per_second: int=None, results_to_fetch: int=999999999999) -> List:
4262
api_response_data = endpoint_response
@@ -63,9 +83,11 @@ def perform_authorized_request(self, model: type, endpoint: str, api_key: str=No
6383
'Accept': 'application/json',
6484
}
6585

86+
# add chain_id to the request
87+
endpoint += f'&chain_id={self.chain_id}'
88+
6689
# if in verbose mode, log the endpoint
6790
print("\n{}\n {}\n".format(endpoint.replace("https://api.transpose.io", self.host).split("?")[0], "\n ".join(endpoint.split("?")[1].split("&")))) if self.verbose else None
68-
print(json.dump(request_headers)) if self.verbose else None
6991
request = requests.get(endpoint.replace("https://api.transpose.io", self.host), headers=request_headers)
7092

7193
# check for a successful response

transpose/src/util/models.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ def __init__(self, _data: object):
7474
self.total_fees_rewarded: int = None
7575
self.total_fees_saved: int = None
7676
self.transaction_count: int = None
77-
self.miner: str = None
78-
self.mining_reward: int = None
77+
self.block_reward: str = None
78+
self.block_reward: int = None
7979
self.uncle_count: int = None
8080
self.uncles: List[object] or object = None
8181

8282
super().__init__(_data)
8383

8484
def __repr__(self) -> str:
85-
return '<BlockObject: block_number="{}" block_hash="{}" miner="{}">'.format(self.block_number, self.block_hash, self.miner)
85+
return '<BlockObject: block_number="{}" block_hash="{}" block_reward="{}">'.format(self.block_number, self.block_hash, self.block_reward)
8686

8787
class Transaction(TransposeModel):
8888
def __init__(self, _data: object):

0 commit comments

Comments
 (0)