Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Circulating Supply API Script #15093

Open
andreumont opened this issue Nov 15, 2024 · 0 comments
Open

Circulating Supply API Script #15093

andreumont opened this issue Nov 15, 2024 · 0 comments

Comments

@andreumont
Copy link

from http.server import BaseHTTPRequestHandler, HTTPServer
import json
from web3 import Web3

Connect to the Base network

web3 = Web3(Web3.HTTPProvider('https://base.drpc.org'))

Convert token contract address to checksum format

TOKEN_CONTRACT_ADDRESS = web3.to_checksum_address('0xac27fa800955849d6d17cc8952ba9dd6eaa66187')

Convert excluded addresses to checksum format

EXCLUDED_ADDRESSES = [
web3.to_checksum_address('0x3074517c5F5428f42C74543C68001E0Ca86FE7dd'), # Unlock Protocol Foundation
web3.to_checksum_address('0x12be7322070cFA75E2f001C6B3d6Ac8C2efEF5Ea'), # Swapping Contract
web3.to_checksum_address('0xB34567C4cA697b39F72e1a8478f285329A98ed1b'), # DAO Timelock Contract
web3.to_checksum_address('0x9ef81f4e2f2f15ff1c0c3f8c9ecc636580025242') # Uniswap Pool
]

Total supply of the token

TOTAL_SUPPLY = 1_000_000_000

Minimum ABI for fetching balances from ERC-20 contract

ERC20_ABI = [
{
"constant": True,
"inputs": [{"name": "_owner", "type": "address"}],
"name": "balanceOf",
"outputs": [{"name": "balance", "type": "uint256"}],
"type": "function"
}
]

Instantiate the ERC-20 token contract

token_contract = web3.eth.contract(address=TOKEN_CONTRACT_ADDRESS, abi=ERC20_ABI)

def get_balance(address):
"""Fetches the balance of a specified address."""
return token_contract.functions.balanceOf(address).call()

def calculate_circulating_supply():
"""Calculates the circulating supply of the token."""
excluded_balance = sum(get_balance(addr) for addr in EXCLUDED_ADDRESSES)
circulating_supply = TOTAL_SUPPLY - excluded_balance / (10**18) # Assuming 18 decimals
return circulating_supply

class RequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
# Check if the request path is /circulating_supply
if self.path == '/circulating_supply':
try:
circulating_supply = calculate_circulating_supply()
response = {"result": circulating_supply}
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(response).encode())
except Exception as e:
# Send error response in case of failure
self.send_response(500)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps({"error": str(e)}).encode())
else:
# Handle 404 Not Found for other paths
self.send_response(404)
self.end_headers()
self.wfile.write(b'{"error": "Endpoint not found"}')

def run(server_class=HTTPServer, handler_class=RequestHandler, port=8000):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print(f'Starting httpd server on port {port}...')
httpd.serve_forever()

if name == 'main':
run()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant