Skip to content

Commit

Permalink
feat: add balances check
Browse files Browse the repository at this point in the history
  • Loading branch information
jmoreira-valory committed Dec 17, 2024
1 parent 41e9e3a commit 13c905a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 13 deletions.
8 changes: 2 additions & 6 deletions operate/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,9 +868,7 @@ async def _withdraw_onchain(request: Request) -> JSONResponse:
if balance == 0:
logger.info(f"No OLAS to drain from master safe: {safe}")
else:
logger.info(
f"Draining {balance} OLAS out of master safe: {safe}"
)
logger.info(f"Draining {balance} OLAS out of master safe: {safe}")
transfer_erc20_from_safe(
ledger_api=ledger_api,
crypto=master_wallet.crypto,
Expand All @@ -885,9 +883,7 @@ async def _withdraw_onchain(request: Request) -> JSONResponse:
if balance == 0:
logger.info(f"No xDAI to drain from master safe: {safe}")
else:
logger.info(
f"Draining {balance} xDAI out of master safe: {safe}"
)
logger.info(f"Draining {balance} xDAI out of master safe: {safe}")
master_wallet.transfer(
to=withdrawal_address,
amount=balance,
Expand Down
2 changes: 1 addition & 1 deletion operate/ledger/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,4 @@

WXDAI = {
Chain.GNOSIS: "0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d",
}
}
2 changes: 1 addition & 1 deletion operate/services/deployment_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def _kill_process(pid: int) -> None:
except OSError:
return
except psutil.AccessDenied:
return
return
time.sleep(1)


Expand Down
73 changes: 68 additions & 5 deletions operate/wallet/master.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
from operate.utils.gnosis import transfer_erc20_from_safe


# TODO Organize exceptions definition
class InsufficientFundsException(Exception):
pass

class MasterWallet(LocalResource):
"""Master wallet."""

Expand Down Expand Up @@ -249,6 +253,16 @@ def _transfer_erc20_from_safe(
amount=amount,
)

def _transfer_erc20_from_eoa(
self,
token: str,
to: str,
amount: int,
chain: Chain,
rpc: t.Optional[str] = None,
) -> None:
raise NotImplementedError()

def transfer(
self,
to: str,
Expand All @@ -258,6 +272,22 @@ def transfer(
rpc: t.Optional[str] = None,
) -> None:
"""Transfer funds to the given account."""
if from_safe:
sender = t.cast(str, self.safes[chain])
sender_str = f"Safe {sender}"
else:
sender = self.crypto.address
sender_str = f"EOA {sender}"

ledger_api = self.ledger_api(chain=chain, rpc=rpc)
balance = ledger_api.get_balance(address=sender)

if balance < amount:
raise InsufficientFundsException(
f"Cannot transfer {amount} native units from {sender_str} to {to} on chain {chain.value.capitalize()}. "
f"Balance of {sender_str} is {balance} native units on chain {chain.value.capitalize()}."
)

if from_safe:
return self._transfer_from_safe(
to=to,
Expand All @@ -283,15 +313,48 @@ def transfer_erc20(
rpc: t.Optional[str] = None,
) -> None:
"""Transfer funds to the given account."""
if not from_safe:
raise NotImplementedError()
return self._transfer_erc20_from_safe(

if from_safe:
sender = t.cast(str, self.safes[chain])
sender_str = f"Safe {sender}"
else:
sender = self.crypto.address
sender_str = f"EOA {sender}"

ledger_api = self.ledger_api(chain=chain, rpc=rpc)
balance = (
registry_contracts.erc20.get_instance(
ledger_api=ledger_api,
contract_address=token,
)
.functions.balanceOf(sender)
.call()
)

tokens = {OLAS[chain]: "OLAS", USDC[chain]: "USDC"}
token_name = tokens.get(token, token)

if balance < amount:
raise InsufficientFundsException(
f"Cannot transfer {amount} {token_name} from {sender_str} to {to} on chain {chain.value.capitalize()}. "
f"Balance of {sender_str} is {balance} {token_name} on chain {chain.value.capitalize()}."
)

if from_safe:
return self._transfer_erc20_from_safe(
token=token,
to=to,
amount=amount,
chain=chain,
rpc=rpc,
)
return self._transfer_erc20_from_eoa(
token=token,
to=to,
amount=amount,
chain=chain,
rpc=rpc,
)
)

@classmethod
def new(
Expand Down Expand Up @@ -443,7 +506,7 @@ def extended_json(self) -> t.Dict:
wallet_json["consistent_backup_owner"] = len(owner_sets) == 1
wallet_json["consistent_backup_owner_count"] = all(
len(owner) == 1 for owner in owner_sets
) or all(len(owner) == 0 for owner in owner_sets)
)
return wallet_json

@classmethod
Expand Down

0 comments on commit 13c905a

Please sign in to comment.