Skip to content

Commit

Permalink
Merge pull request #262 from polywrap/nerfzael/discord-fixes
Browse files Browse the repository at this point in the history
Intent deserialization and amount formatting fixes
  • Loading branch information
nerfZael authored Jun 18, 2024
2 parents 17ad64d + aca7a32 commit 4d4e8cd
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 9 deletions.
42 changes: 41 additions & 1 deletion autotx/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
from supabase.lib.client_options import ClientOptions

from autotx import models
from autotx.intents import BuyIntent, Intent, SellIntent, SendIntent
from autotx.token import Token
from autotx.transactions import Transaction, TransactionBase
from autotx.utils.ethereum.eth_address import ETHAddress

SUPABASE_URL = os.getenv("SUPABASE_URL")
SUPABASE_KEY = os.getenv("SUPABASE_SERVICE_ROLE_KEY")
Expand Down Expand Up @@ -110,6 +113,43 @@ def get(self, task_id: str) -> models.Task | None:

task_data = result.data[0]

def load_intent(intent_data: dict[str, Any]) -> Intent:
if intent_data["type"] == "send":
return SendIntent.create(
receiver=ETHAddress(intent_data["receiver"]),
token=Token(
symbol=intent_data["token"]["symbol"],
address=intent_data["token"]["address"]
),
amount=intent_data["amount"]
)
elif intent_data["type"] == "buy":
return BuyIntent.create(
from_token=Token(
symbol=intent_data["from_token"]["symbol"],
address=intent_data["from_token"]["address"]
),
to_token=Token(
symbol=intent_data["to_token"]["symbol"],
address=intent_data["to_token"]["address"]
),
amount=intent_data["amount"]
)
elif intent_data["type"] == "sell":
return SellIntent.create(
from_token=Token(
symbol=intent_data["from_token"]["symbol"],
address=intent_data["from_token"]["address"]
),
to_token=Token(
symbol=intent_data["to_token"]["symbol"],
address=intent_data["to_token"]["address"]
),
amount=intent_data["amount"]
)
else:
raise Exception(f"Unknown intent type: {intent_data['type']}")

return models.Task(
id=task_data["id"],
prompt=task_data["prompt"],
Expand All @@ -120,7 +160,7 @@ def get(self, task_id: str) -> models.Task | None:
running=task_data["running"],
error=task_data["error"],
messages=json.loads(task_data["messages"]),
intents=json.loads(task_data["intents"])
intents=[load_intent(intent) for intent in json.loads(task_data["intents"])]
)

def get_all(self) -> list[models.Task]:
Expand Down
12 changes: 7 additions & 5 deletions autotx/intents.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from autotx.utils.ethereum.eth_address import ETHAddress
from autotx.utils.ethereum.lifi.swap import build_swap_transaction
from autotx.utils.ethereum.networks import NetworkInfo
from autotx.utils.format_amount import format_amount

class IntentType(str, Enum):
SEND = "send"
Expand All @@ -41,7 +42,7 @@ def create(cls, token: Token, amount: float, receiver: ETHAddress) -> 'SendInten
token=token,
amount=amount,
receiver=receiver.hex,
summary=f"Transfer {amount} {token.symbol} to {receiver}",
summary=f"Transfer {format_amount(amount)} {token.symbol} to {receiver}",
)

def build_transactions(self, web3: Web3, network: NetworkInfo, smart_wallet_address: ETHAddress) -> list[Transaction]:
Expand Down Expand Up @@ -75,13 +76,13 @@ def create(cls, from_token: Token, to_token: Token, amount: float) -> 'BuyIntent
from_token=from_token,
to_token=to_token,
amount=amount,
summary=f"Buy {amount} {to_token.symbol} with {from_token.symbol}",
summary=f"Buy {format_amount(amount)} {to_token.symbol} with {from_token.symbol}",
)

def build_transactions(self, web3: Web3, network: NetworkInfo, smart_wallet_address: ETHAddress) -> list[Transaction]:
transactions = build_swap_transaction(
web3,
Decimal(self.amount),
Decimal(str(self.amount)),
ETHAddress(self.from_token.address),
ETHAddress(self.to_token.address),
smart_wallet_address,
Expand All @@ -103,13 +104,14 @@ def create(cls, from_token: Token, to_token: Token, amount: float) -> 'SellInten
from_token=from_token,
to_token=to_token,
amount=amount,
summary=f"Sell {amount} {from_token.symbol} for {to_token.symbol}",
summary=f"Sell {format_amount(amount)} {from_token.symbol} for {to_token.symbol}",
)

def build_transactions(self, web3: Web3, network: NetworkInfo, smart_wallet_address: ETHAddress) -> list[Transaction]:

transactions = build_swap_transaction(
web3,
Decimal(self.amount),
Decimal(str(self.amount)),
ETHAddress(self.from_token.address),
ETHAddress(self.to_token.address),
smart_wallet_address,
Expand Down
7 changes: 4 additions & 3 deletions autotx/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Any, Union

from autotx.token import Token
from autotx.utils.format_amount import format_amount

class TransactionType(str, Enum):
SEND = "send"
Expand All @@ -27,7 +28,7 @@ def create(cls, token: Token, amount: float, receiver: str, params: dict[str, An
amount=amount,
receiver=receiver,
params=params,
summary=f"Transfer {amount} {token.symbol} to {receiver}",
summary=f"Transfer {format_amount(amount)} {token.symbol} to {receiver}",
)

class ApproveTransaction(TransactionBase):
Expand All @@ -43,7 +44,7 @@ def create(cls, token: Token, amount: float, spender: str, params: dict[str, Any
amount=amount,
spender=spender,
params=params,
summary=f"Approve {amount} {token.symbol} to {spender}"
summary=f"Approve {format_amount(amount)} {token.symbol} to {spender}"
)

class SwapTransaction(TransactionBase):
Expand All @@ -61,7 +62,7 @@ def create(cls, from_token: Token, to_token: Token, from_amount: float, to_amoun
from_amount=from_amount,
to_amount=to_amount,
params=params,
summary=f"Swap {from_amount} {from_token.symbol} for at least {to_amount} {to_token.symbol}"
summary=f"Swap {format_amount(from_amount)} {from_token.symbol} for at least {format_amount(to_amount)} {to_token.symbol}"
)

Transaction = Union[SendTransaction, ApproveTransaction, SwapTransaction]
5 changes: 5 additions & 0 deletions autotx/utils/format_amount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from decimal import Decimal


def format_amount(amount: float) -> str:
return format(Decimal(str(amount)), "f")

0 comments on commit 4d4e8cd

Please sign in to comment.