|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import asyncio |
| 4 | +from decimal import Decimal |
| 5 | +from typing import TYPE_CHECKING, Optional |
| 6 | + |
| 7 | +from eth_utils import to_normalized_address, to_wei |
| 8 | +from superfluid import CFA_V1, Operation, Web3FlowInfo |
| 9 | +from web3 import Web3 |
| 10 | +from web3.types import TxParams |
| 11 | + |
| 12 | +from aleph.sdk.conf import settings |
| 13 | + |
| 14 | +if TYPE_CHECKING: |
| 15 | + from aleph.sdk.chains.ethereum import LocalAccount |
| 16 | + |
| 17 | + |
| 18 | +async def sign_and_send_transaction( |
| 19 | + account: LocalAccount, tx_params: TxParams, rpc: str |
| 20 | +) -> str: |
| 21 | + """ |
| 22 | + Sign and broadcast a transaction using the provided ETHAccount |
| 23 | +
|
| 24 | + @param tx_params - Transaction parameters |
| 25 | + @param rpc - RPC URL |
| 26 | + @returns - str - The transaction hash |
| 27 | + """ |
| 28 | + web3 = Web3(Web3.HTTPProvider(rpc)) |
| 29 | + |
| 30 | + def sign_and_send(): |
| 31 | + signed_txn = account.sign_transaction(tx_params) |
| 32 | + transaction_hash = web3.eth.send_raw_transaction(signed_txn.rawTransaction) |
| 33 | + return transaction_hash.hex() |
| 34 | + |
| 35 | + # Sending a transaction is done over HTTP(S) and implemented using a blocking |
| 36 | + # API in `web3.eth`. This runs it in a non-blocking asyncio executor. |
| 37 | + loop = asyncio.get_running_loop() |
| 38 | + transaction_hash = await loop.run_in_executor(None, sign_and_send) |
| 39 | + return transaction_hash |
| 40 | + |
| 41 | + |
| 42 | +async def execute_operation_with_account( |
| 43 | + account: LocalAccount, operation: Operation |
| 44 | +) -> str: |
| 45 | + """ |
| 46 | + Execute an operation using the provided ETHAccount |
| 47 | +
|
| 48 | + @param operation - Operation instance from the library |
| 49 | + @returns - str - The transaction hash |
| 50 | + @returns - str - The transaction hash |
| 51 | + """ |
| 52 | + populated_transaction = operation._get_populated_transaction_request( |
| 53 | + operation.rpc, account.key |
| 54 | + ) |
| 55 | + transaction_hash = await sign_and_send_transaction( |
| 56 | + account, populated_transaction, operation.rpc |
| 57 | + ) |
| 58 | + return transaction_hash |
| 59 | + |
| 60 | + |
| 61 | +class Superfluid: |
| 62 | + """ |
| 63 | + Wrapper around the Superfluid APIs in order to CRUD Superfluid flows between two accounts. |
| 64 | + """ |
| 65 | + account: Optional[LocalAccount] |
| 66 | + |
| 67 | + def __init__( |
| 68 | + self, |
| 69 | + rpc=settings.AVAX_RPC, |
| 70 | + chain_id=settings.AVAX_CHAIN_ID, |
| 71 | + account: Optional[LocalAccount] = None, |
| 72 | + ): |
| 73 | + self.cfaV1Instance = CFA_V1(rpc, chain_id) |
| 74 | + self.account = account |
| 75 | + |
| 76 | + async def create_flow(self, sender: str, receiver: str, flow: Decimal) -> str: |
| 77 | + """Create a Superfluid flow between two addresses.""" |
| 78 | + if not self.account: |
| 79 | + raise ValueError("An account is required to create a flow") |
| 80 | + return await execute_operation_with_account( |
| 81 | + account=self.account, |
| 82 | + operation=self.cfaV1Instance.create_flow( |
| 83 | + sender=to_normalized_address(sender), |
| 84 | + receiver=to_normalized_address(receiver), |
| 85 | + super_token=settings.AVAX_ALEPH_SUPER_TOKEN, |
| 86 | + flow_rate=to_wei(Decimal(flow), "ether"), |
| 87 | + ), |
| 88 | + ) |
| 89 | + |
| 90 | + async def get_flow(self, sender: str, receiver: str) -> Web3FlowInfo: |
| 91 | + """Fetch information about the Superfluid flow between two addresses.""" |
| 92 | + return self.cfaV1Instance.get_flow( |
| 93 | + sender=to_normalized_address(sender), |
| 94 | + receiver=to_normalized_address(receiver), |
| 95 | + super_token=settings.AVAX_ALEPH_SUPER_TOKEN, |
| 96 | + ) |
| 97 | + |
| 98 | + async def delete_flow(self, sender: str, receiver: str) -> str: |
| 99 | + """Delete the Supefluid flow between two addresses.""" |
| 100 | + if not self.account: |
| 101 | + raise ValueError("An account is required to delete a flow") |
| 102 | + return await execute_operation_with_account( |
| 103 | + account=self.account, |
| 104 | + operation=self.cfaV1Instance.delete_flow( |
| 105 | + sender=to_normalized_address(sender), |
| 106 | + receiver=to_normalized_address(receiver), |
| 107 | + super_token=settings.AVAX_ALEPH_SUPER_TOKEN, |
| 108 | + ), |
| 109 | + ) |
| 110 | + |
| 111 | + async def update_flow(self, sender: str, receiver: str, flow: Decimal) -> str: |
| 112 | + """Update the flow of a Superfluid flow between two addresses.""" |
| 113 | + if not self.account: |
| 114 | + raise ValueError("An account is required to update a flow") |
| 115 | + return await execute_operation_with_account( |
| 116 | + account=self.account, |
| 117 | + operation=self.cfaV1Instance.update_flow( |
| 118 | + sender=to_normalized_address(sender), |
| 119 | + receiver=to_normalized_address(receiver), |
| 120 | + super_token=settings.AVAX_ALEPH_SUPER_TOKEN, |
| 121 | + flow_rate=to_wei(Decimal(flow), "ether"), |
| 122 | + ), |
| 123 | + ) |
0 commit comments