Skip to content

Commit

Permalink
Fix: using Chaininfo on settings for better standarization
Browse files Browse the repository at this point in the history
  • Loading branch information
1yam committed Aug 27, 2024
1 parent f2cda51 commit 23ec31a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 22 deletions.
34 changes: 34 additions & 0 deletions src/aleph/vm/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
is_command_available,
)

from src.aleph.vm.orchestrator.payment import ChainInfo

logger = logging.getLogger(__name__)

Url = NewType("Url", str)
Expand Down Expand Up @@ -232,6 +234,38 @@ class Settings(BaseSettings):
)
PAYMENT_PRICING_AGGREGATE: str = "" # TODO: Missing


STREAM_CHAINS: Dict[Union[Chain, str], ChainInfo] = {
# TESTNETS
"SEPOLIA": ChainInfo(
chain_id=11155111,
rpc="https://eth-sepolia.public.blastapi.io",
token="0xc4bf5cbdabe595361438f8c6a187bdc330539c60",
super_token="0x22064a21fee226d8ffb8818e7627d5ff6d0fc33a",
active=False,
testnet=True
),
# MAINNETS
Chain.ETH: ChainInfo(
chain_id=1,
rpc="https://eth-mainnet.public.blastapi.io",
token="0x27702a26126e0B3702af63Ee09aC4d1A084EF628",
active=False,
),
Chain.AVAX: ChainInfo(
chain_id=43114,
rpc="https://api.avax.network/ext/bc/C/rpc",
token="0xc0Fbc4967259786C743361a5885ef49380473dCF",
super_token="0xc0Fbc4967259786C743361a5885ef49380473dCF",
),
Chain.BASE: ChainInfo(
chain_id=8453,
rpc="https://base-mainnet.public.blastapi.io",
token="0xc0Fbc4967259786C743361a5885ef49380473dCF",
super_token="0xc0Fbc4967259786C743361a5885ef49380473dCF",
)
}

PAYMENT_CHAIN_ID: Dict[Chain, int] = {
Chain.AVAX: 43114,
Chain.BASE: 8453,
Expand Down
43 changes: 21 additions & 22 deletions src/aleph/vm/orchestrator/payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from aleph_message.models import Chain, ItemHash, PaymentType
from eth_typing import HexAddress
from eth_utils import from_wei
from pydantic import BaseModel
from superfluid import CFA_V1, Web3FlowInfo

from aleph.vm.conf import settings
Expand All @@ -17,6 +18,19 @@
logger = logging.getLogger(__name__)


class ChainInfo(BaseModel):
"""
A chain information.
"""

chain_id: int
rpc: str
token: str
super_token: Optional[str] = None
testnet: bool = False
active: bool = True


async def fetch_balance_of_address(address: str) -> Decimal:
"""
Get the balance of the user from the PyAleph API.
Expand Down Expand Up @@ -83,39 +97,24 @@ async def fetch_execution_hold_price(item_hash: ItemHash) -> Decimal:

class InvalidAddressError(ValueError):
"""The blockchain address could not be parsed."""

pass


def get_rpc_for_chain(chain: Chain):
"""Returns the RPC to use for a given Ethereum based blockchain"""
if not chain:
return None

if chain in settings.PAYMENT_RPC_API:
return settings.PAYMENT_RPC_API[chain]
else:
raise ValueError(f"Unknown RPC for chain {chain}")


def get_chain_id_for_chain(chain: Chain):
"""Returns the chain ID of a given Ethereum based blockchain"""
if not chain:
return None

if chain in settings.PAYMENT_CHAIN_ID:
return settings.PAYMENT_CHAIN_ID[chain]
def get_chain(chain: str) -> ChainInfo:
if chain in settings.STREAM_CHAINS.keys():
return settings.STREAM_CHAINS[chain]
else:
raise ValueError(f"Unknown chain id for chain {chain}")


async def get_stream(sender: str, receiver: str, chain) -> Decimal:
async def get_stream(sender: str, receiver: str, chain: str) -> Decimal:
"""
Get the stream of the user from the Superfluid API.
See https://community.aleph.im/t/pay-as-you-go-using-superfluid/98/11
"""
chain_id = get_chain_id_for_chain(chain)
rpc = get_rpc_for_chain(chain)
chain_info: ChainInfo = get_chain(chain=chain)
chain_id = chain_info.chain_id
rpc = chain_info.rpc
superfluid_instance = CFA_V1(rpc, chain_id)

try:
Expand Down

0 comments on commit 23ec31a

Please sign in to comment.