Skip to content

Commit

Permalink
lookup access controller and authority for ritual when adding/removin…
Browse files Browse the repository at this point in the history
…g encryptors
  • Loading branch information
KPrasch committed Aug 7, 2024
1 parent 686ae10 commit 7b53520
Showing 1 changed file with 43 additions and 69 deletions.
112 changes: 43 additions & 69 deletions scripts/manage_subscription.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,33 @@
#!/usr/bin/python3

import click
from ape import Contract
from ape.cli import account_option, ConnectedProviderCommand
from ape.cli import ConnectedProviderCommand, account_option, network_option

from deployment import registry
from deployment.options import (
subscription_contract_option,
domain_option,
access_controller_option,
ritual_id_option,
encryptor_slots_option,
encryptors_option,
ritual_id_option,
subscription_contract_option,
)
from deployment.params import Transactor
from deployment.utils import check_plugins


def _erc20_approve(
amount: int,
erc20: Contract,
receiver: Contract,
transactor: Transactor
amount: int, erc20: Contract, receiver: Contract, transactor: Transactor
) -> None:
"""Approve an ERC20 transfer."""
click.echo(
f"Approving transfer of {amount} {erc20.contract_type.name} "
f"to {receiver.contract_type.name}."
)
transactor.transact(
erc20.approve,
receiver.address,
amount
)
transactor.transact(erc20.approve, receiver.address, amount)


def _calculate_slot_fees(
subscription_contract: Contract,
slots: int
) -> int:
def _calculate_slot_fees(subscription_contract: Contract, slots: int) -> int:
"""Calculate the fees for a given number of encryptor slots."""
duration = subscription_contract.subscriptionPeriodDuration()
encryptor_fees = subscription_contract.encryptorFees(slots, duration)
Expand Down Expand Up @@ -64,30 +55,21 @@ def pay_subscription(account, domain, subscription_contract, encryptor_slots, pe
check_plugins()
transactor = Transactor(account=account)
subscription_contract = registry.get_contract(
contract_name=subscription_contract,
domain=domain
contract_name=subscription_contract, domain=domain
)
erc20 = Contract(subscription_contract.feeToken())
base_fees = subscription_contract.baseFees(period)
slot_fees = _calculate_slot_fees(
subscription_contract=subscription_contract,
slots=encryptor_slots
subscription_contract=subscription_contract, slots=encryptor_slots
)
total_fees = base_fees + slot_fees
_erc20_approve(
amount=total_fees,
erc20=erc20,
receiver=subscription_contract,
transactor=transactor
amount=total_fees, erc20=erc20, receiver=subscription_contract, transactor=transactor
)
click.echo(
f"Paying for subscription period #{period} "
f"with {encryptor_slots} encryptor slots."
)
transactor.transact(
subscription_contract.payForSubscription,
encryptor_slots
f"Paying for subscription period #{period} " f"with {encryptor_slots} encryptor slots."
)
transactor.transact(subscription_contract.payForSubscription, encryptor_slots)


@cli.command(cls=ConnectedProviderCommand)
Expand All @@ -100,74 +82,66 @@ def pay_slots(account, domain, subscription_contract, encryptor_slots):
check_plugins()
transactor = Transactor(account=account)
subscription_contract = registry.get_contract(
contract_name=subscription_contract,
domain=domain
contract_name=subscription_contract, domain=domain
)
erc20 = Contract(subscription_contract.feeToken())
fee = _calculate_slot_fees(
subscription_contract=subscription_contract,
slots=encryptor_slots
)
_erc20_approve(
amount=fee,
erc20=erc20,
receiver=subscription_contract,
transactor=transactor
)
fee = _calculate_slot_fees(subscription_contract=subscription_contract, slots=encryptor_slots)
_erc20_approve(amount=fee, erc20=erc20, receiver=subscription_contract, transactor=transactor)
click.echo(f"Paying for {encryptor_slots} new encryptor slots.")
transactor.transact(
subscription_contract.payForEncryptorSlots,
encryptor_slots
)
transactor.transact(subscription_contract.payForEncryptorSlots, encryptor_slots)


@cli.command(cls=ConnectedProviderCommand)
@account_option()
@network_option(required=True)
@domain_option
@ritual_id_option
@access_controller_option
@encryptors_option
def add_encryptors(account, domain, ritual_id, access_controller, encryptors):
def add_encryptors(account, network, domain, ritual_id, encryptors):
"""Authorize encryptors to the access control contract for a ritual."""
access_controller = registry.get_contract(
contract_name=access_controller,
domain=domain
)
click.echo(f"Connected to {network.name} network.")

# lookup the access controller + authority for the ritual
coordinator = registry.get_contract(contract_name="Coordinator", domain=domain)
ritual = coordinator.rituals(ritual_id)
access_controller = Contract(ritual.access_controller) # uses polygonscan API
if account != ritual.authority:
raise ValueError(f"Only the authority ({ritual.authority}) can authorize encryptors.")

transactor = Transactor(account=account)
click.echo(
f"Adding {len(encryptors)} encryptors "
f"to the {access_controller} "
f"for ritual {ritual_id}."
)
transactor.transact(
access_controller.authorize,
ritual_id,
encryptors
)
transactor.transact(access_controller.authorize, ritual_id, encryptors)


@cli.command(cls=ConnectedProviderCommand)
@account_option()
@network_option(required=True)
@domain_option
@ritual_id_option
@access_controller_option
@encryptors_option
def remove_encryptors(account, domain, ritual_id, access_controller, encryptors):
def remove_encryptors(account, network, domain, ritual_id, encryptors):
"""Deauthorize encryptors from the access control contract for a ritual."""
click.echo(f"Connected to {network.name} network.")
transactor = Transactor(account=account)
access_controller = registry.get_contract(
contract_name=access_controller,
domain=domain
)

# lookup the access controller + authority for the ritual
coordinator = registry.get_contract(contract_name="Coordinator", domain=domain)
ritual = coordinator.rituals(ritual_id)
access_controller = Contract(ritual.access_controller) # uses polygonscan API

if account != ritual.authority:
raise ValueError(f"Only the authority ({ritual.authority}) can remove encryptors.")

click.echo(
f"Removing {len(encryptors)} "
f"encryptors from the {access_controller} "
f"encryptors from the {access_controller.contract_type.name} contract"
f"for ritual {ritual_id}."
)
transactor.transact(
access_controller.authorize,
ritual_id, encryptors
)
transactor.transact(access_controller.authorize, ritual_id, encryptors)


if __name__ == "__main__":
Expand Down

0 comments on commit 7b53520

Please sign in to comment.