Last active
November 10, 2022 04:48
-
-
Save 0xlxy/60034648058f2fc1d10f22c1bc85d794 to your computer and use it in GitHub Desktop.
AWS Lambda + RDS to refresh & load all collection txns into sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import multiprocessing.dummy as mp | |
import pymysql | |
import requests | |
class TransactionsTable: | |
def __init__(self, collections): | |
self.collections = collections | |
for collection in self.collections: | |
self.collection = collection | |
self.main() | |
self.updateDB() | |
def main(self): | |
response = self.getData(1) | |
pool = mp.Pool(10) | |
pool.map(self.getData, range(2, int(response['response']['total_pages']) + 1)) | |
pool.close() | |
pool.join() | |
def getData(self, page): | |
payload = { | |
"contract_address": self.collection["address"], | |
"page": page, | |
"page_size": 1000 | |
} | |
headers = { | |
"accept": "application/json", | |
"content-type": "application/json", | |
"x-api-key": "" | |
} | |
response = requests.post("https://api.gallop.run/v1/data/eth/getCollectionTransactions", json=payload, headers=headers).json() | |
self.collection["txns"] += response['response']['transactions'] | |
return response | |
def updateDB(self): | |
connection = pymysql.connect( | |
host="transactions.cspjhlwr0o1p.us-west-1.rds.amazonaws.com", | |
user="admin", | |
passwd="YeZ0Nw3V8TDnp923CSDY", | |
db="transactions" | |
) | |
with connection: | |
txn_hashs = [] | |
with connection.cursor() as cursor: | |
cursor.execute(f"SELECT * FROM {self.collection['name']}") | |
transactions = cursor.fetchall() | |
for transaction in transactions: | |
txn_hashs.append(transaction[10]) | |
with connection.cursor() as cursor: | |
for d in self.collection["txns"]: | |
if not d['txn_hash'] in txn_hashs: | |
cursor.execute(f"INSERT INTO {self.collection['name']} (contract_address, collection_name, token_id, from_address, to_address, eth_value, usd_value, exchange, block_number, block_timestamp, txn_hash, num_of_tokens, txn_hash_idx) VALUES ('{d['contract_address']}', '{d['collection_name']}', '{d['token_id']}', '{d['from_address']}', '{d['to_address']}', '{d['eth_value']}', '{d['usd_value']}', '{d['exchange']}', '{d['block_number']}', '{d['block_timestamp']}', '{d['txn_hash']}', '{d['num_of_tokens']}', '{d['txn_hash_idx']}')") | |
connection.commit() | |
COLLECTIONS = [ | |
{ "name": "mfers", "address": "0x79FCDEF22feeD20eDDacbB2587640e45491b757f", "txns": [] }, | |
{ "name": "moonbirdsoddities", "address": "0x1792a96E5668ad7C167ab804a100ce42395Ce54D", "txns": [], }, | |
{ "name": "boredapeyachtclub", "address": "0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D", "txns": [], }, | |
{ "name": "mekaverse", "address": "0x9A534628B4062E123cE7Ee2222ec20B86e16Ca8F", "txns": [] }, | |
{ "name": "invisiblefriends", "address": "0x59468516a8259058baD1cA5F8f4BFF190d30E066", "txns": [], }, | |
{ "name": "azuki", "address": "0xED5AF388653567Af2F388E6224dC7C4b3241C544", "txns": [] }, | |
{ "name": "doodles", "address": "0x8a90CAb2b38dba80c64b7734e58Ee1dB38B8992e", "txns": [] }, | |
{ "name": "mutantapeyachtclub", "address": "0x60E4d786628Fea6478F785A6d7e704777c86a7c6", "txns": [], }, | |
{ "name": "beanzofficial", "address": "0x306b1ea3ecdf94aB739F1910bbda052Ed4A9f949", "txns": [], }, | |
{ "name": "cryptocoven", "address": "0x5180db8F5c931aaE63c74266b211F580155ecac8", "txns": [], }, | |
{ "name": "worldofwomen", "address": "0xe785E82358879F061BC3dcAC6f0444462D4b5330", "txns": [], }, | |
{ "name": "clonex", "address": "0x49cF6f5d44E70224e2E23fDcdd2C053F30aDA28B", "txns": [] }, | |
{ "name": "moonbirds", "address": "0x23581767a106ae21c074b2276D25e5C3e136a68b", "txns": [] }, | |
] | |
def lambda_handler(event, context): | |
TransactionsTable(COLLECTIONS) | |
return "success" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE DATABASE transactions; | |
USE transactions; | |
CREATE TABLE mfers ( | |
contract_address VARCHAR(100), | |
collection_name VARCHAR(100), | |
token_id VARCHAR(25), | |
from_address VARCHAR(100), | |
to_address VARCHAR(100), | |
eth_value FLOAT(14, 8), | |
usd_value FLOAT(14, 8), | |
exchange VARCHAR(100), | |
block_number INT, | |
block_timestamp VARCHAR(100), | |
txn_hash VARCHAR(100), | |
num_of_tokens INT, | |
txn_hash_idx INT | |
); | |
CREATE TABLE moonbirdsoddities LIKE mfers; | |
CREATE TABLE boredapeyachtclub LIKE mfers; | |
CREATE TABLE mekaverse LIKE mfers; | |
CREATE TABLE invisiblefriends LIKE mfers; | |
CREATE TABLE azuki LIKE mfers; | |
CREATE TABLE doodles LIKE mfers; | |
CREATE TABLE mutantapeyachtclub LIKE mfers; | |
CREATE TABLE beanzofficial LIKE mfers; | |
CREATE TABLE cryptocoven LIKE mfers; | |
CREATE TABLE worldofwomen LIKE mfers; | |
CREATE TABLE clonex LIKE mfers; | |
CREATE TABLE moonbirds LIKE mfers; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment