-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_coins.py
74 lines (62 loc) · 2.45 KB
/
fetch_coins.py
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
import requests
import json
import argparse
import os
import time
def get_coins(owner, url, coin_type="0x2::sui::SUI", cursor=None, limit=1000):
payload = {
"jsonrpc": "2.0",
"id": 1,
"method": "suix_getCoins",
"params": [owner, coin_type, cursor, limit]
}
headers = {'content-type': 'application/json'}
response = requests.post(url, data=json.dumps(payload), headers=headers).json()
return response
def dump_to_json(coins, counter):
with open(f'coins/{counter}.json', 'w') as f:
json.dump(coins, f)
def fetch_coins(owner, url, coin_type="0x2::sui::SUI", cursor=None, limit=500):
coins = []
counter = 0
try:
while True:
response = get_coins(owner, url, coin_type, cursor, limit)
if response:
fetched_coins = response['result']['data']
coins_to_merge = [
coin for coin in fetched_coins # SuiCoinObject
]
coins.extend(coins_to_merge)
has_next_page = response['result']['hasNextPage']
cursor = response['result']['nextCursor']
print(f"nextCursor: {cursor}")
if not has_next_page or len(coins) >= limit * 50:
while len(coins) >= limit * 50:
dump_to_json(coins[:limit * 50], counter)
coins = coins[limit * 50:]
counter += 1
if not has_next_page and coins:
dump_to_json(coins, counter)
coins = []
counter += 1
break
else:
break
except Exception as e:
print(f"Error: {e}")
finally:
if coins:
dump_to_json(coins, counter)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--rpc-url", type=str, help="RPC URL to use", default="https://fullnode.testnet.sui.io:443")
parser.add_argument("--owner", type=str, help="Signer address to use. str repr of SuiAddress.", required=True)
args = parser.parse_args()
os.makedirs("coins", exist_ok=True)
start = time.time()
fetch_coins(args.owner, args.rpc_url)
end = time.time()
print(f"Time taken: {end - start} seconds")
if __name__ == "__main__":
main()