-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Allow PAYG using base (#258)
* Feature: handle superfuild flow * feat: control of message / flow before starting the instance * fix: mypy issue * fix: add setuptools for ci * Add unit test in pair programming * fixup! Add unit test in pair programming * Refactor: handle_flow_reduction and handle_flow into update_flow * Fix: add type annotations * fixup! Merge branch 'master' into 1yam-payg * fixup! Add unit test in pair programming * fixup! Add unit test in pair programming * Fix raised error when no flow to reduce + dumby fix echo * Add qemu_support, confidential_support and stream_reward_address checks * Fix flow outputs * Fixes after tests * Feature: Allow PAYG on bae * Fix instance confidential adding payment_chain arg * fix: mypy issue * Merge from master * Fix PAYG + balance checks * Remove default rootfs to allow prompting * Fix: Update to latest Aleph-SDK version * Fix name / crn_name * Fix: Remove BASE chain for now and ceil flow to up number to ensure to have enough tokens. * Fix: Solve code quality issues. * Fix: Solve mypy issues. --------- Co-authored-by: Hugo Herter <[email protected]> Co-authored-by: philogicae <[email protected]> Co-authored-by: Andres D. Molins <[email protected]>
- Loading branch information
1 parent
740b156
commit 7a806cf
Showing
7 changed files
with
250 additions
and
19 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import logging | ||
from decimal import Decimal | ||
from enum import Enum | ||
|
||
from aleph.sdk.chains.ethereum import ETHAccount | ||
from aleph.sdk.conf import settings | ||
from aleph_message.models import Chain | ||
from click import echo | ||
from eth_utils.currency import to_wei | ||
from superfluid import Web3FlowInfo | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def from_wei(wei_value: Decimal) -> Decimal: | ||
"""Converts the given wei value to ether.""" | ||
return wei_value / Decimal(10**settings.TOKEN_DECIMALS) | ||
|
||
|
||
class FlowUpdate(str, Enum): | ||
REDUCE = "reduce" | ||
INCREASE = "increase" | ||
|
||
|
||
async def update_flow(account: ETHAccount, receiver: str, flow: Decimal, update_type: FlowUpdate): | ||
""" | ||
Update the flow of a Superfluid stream between a sender and receiver. | ||
This function either increases or decreases the flow rate between the sender and receiver, | ||
based on the update_type. If no flow exists and the update type is augmentation, it creates a new flow | ||
with the specified rate. If the update type is reduction and the reduction amount brings the flow to zero | ||
or below, the flow is deleted. | ||
:param account: The SuperFluid account instance used to interact with the blockchain. | ||
:param chain: The blockchain chain to interact with. | ||
:param receiver: Address of the receiver in hexadecimal format. | ||
:param flow: The flow rate to be added or removed (in ether). | ||
:param update_type: The type of update to perform (augmentation or reduction). | ||
:return: The transaction hash of the executed operation (create, update, or delete flow). | ||
""" | ||
|
||
# Retrieve current flow info | ||
flow_info: Web3FlowInfo = await account.get_flow(receiver) | ||
|
||
current_flow_rate_wei: Decimal = Decimal(flow_info["flowRate"] or "0") | ||
flow_rate_wei: int = to_wei(flow, "ether") | ||
|
||
if update_type == FlowUpdate.INCREASE: | ||
if current_flow_rate_wei > 0: | ||
# Update existing flow by augmenting the rate | ||
new_flow_rate_wei = current_flow_rate_wei + flow_rate_wei | ||
new_flow_rate_ether = from_wei(new_flow_rate_wei) | ||
return await account.update_flow(receiver, new_flow_rate_ether) | ||
else: | ||
# Create a new flow if none exists | ||
return await account.create_flow(receiver, flow) | ||
elif update_type == FlowUpdate.REDUCE: | ||
if current_flow_rate_wei > 0: | ||
# Reduce the existing flow | ||
new_flow_rate_wei = current_flow_rate_wei - flow_rate_wei | ||
if new_flow_rate_wei > 0: | ||
new_flow_rate_ether = from_wei(new_flow_rate_wei) | ||
return await account.update_flow(receiver, new_flow_rate_ether) | ||
else: | ||
# Delete the flow if the new flow rate is zero or negative | ||
return await account.delete_flow(receiver) | ||
else: | ||
echo("No existing flow to stop. Skipping...") |
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
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
Oops, something went wrong.