Skip to content

Commit

Permalink
Merge branch 'feature-atomicbatching' into feature-localinterchaintest
Browse files Browse the repository at this point in the history
  • Loading branch information
dowlandaiello committed Jul 24, 2024
2 parents 92d18a2 + 6ca2484 commit e434a28
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 41 deletions.
29 changes: 28 additions & 1 deletion src/contracts/auction.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import json
from decimal import Decimal
from typing import Any, List, Optional
from typing import Any, List, Optional, cast
from functools import cached_property
from cosmpy.aerial.contract import LedgerContract
from cosmpy.aerial.wallet import LocalWallet
Expand All @@ -17,10 +17,16 @@
ContractInfo,
try_query_multiple,
try_multiple_clients,
try_multiple_clients_fatal,
try_exec_multiple_fatal,
)
import aiohttp
import grpc
from google.protobuf.message import Message
from cosmwasm.wasm.v1.tx_pb2 import MsgExecuteContract
from cosmpy.crypto.address import Address
from cosmos.base.v1beta1.coin_pb2 import Coin
from cosmpy.aerial.tx import Transaction


class AuctionProvider(WithContract):
Expand Down Expand Up @@ -48,6 +54,8 @@ def __init__(
self.chain_prefix = chain_info["chain_prefix"]
self.chain_fee_denom = chain_info["chain_fee_denom"]
self.chain_transfer_channel_ids = chain_info["chain_transfer_channel_ids"]
self.chain_gas_price = Decimal("0.01")
self.swap_gas_limit = 500000
self.kind = "auction"
self.endpoints = endpoints["http"]
self.session = session
Expand Down Expand Up @@ -137,6 +145,25 @@ def swap_asset_a(self, wallet: LocalWallet, amount: int) -> SubmittedTx:
gas_limit=500000,
)

def swap_msg_asset_a(
self, wallet: LocalWallet, amount: int, min_amount: int
) -> Message:
return cast(
Message,
MsgExecuteContract(
sender=str(Address(wallet.public_key(), prefix=self.chain_prefix)),
contract=self.contract_info.address,
msg=json.dumps({"bid": {}}),
funds=Coin(denom=self.asset_a_denom, amount=amount),
),
)

def submit_swap_tx(self, tx: Transaction) -> SubmittedTx:
return try_multiple_clients_fatal(
self.contract_info.clients,
lambda client: client.broadcast_tx(tx),
)

async def remaining_asset_b(self) -> int:
"""
Gets the amount of the asking asset left in the auction.
Expand Down
11 changes: 11 additions & 0 deletions src/contracts/pool/astroport.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Astroport pools.
"""

from decimal import Decimal
import json
from functools import cached_property
from typing import Any, cast, Optional, List
Expand All @@ -19,6 +20,7 @@
ContractInfo,
try_query_multiple,
try_exec_multiple_fatal,
try_multiple_clients_fatal,
custom_neutron_network_config,
)
import aiohttp
Expand All @@ -27,6 +29,7 @@
from cosmwasm.wasm.v1.tx_pb2 import MsgExecuteContract
from cosmos.base.v1beta1.coin_pb2 import Coin
from cosmpy.crypto.address import Address
from cosmpy.aerial.tx import Transaction


MAX_SPREAD = "0.1"
Expand Down Expand Up @@ -110,6 +113,8 @@ def __init__(
self.chain_prefix = chain_info["chain_prefix"]
self.chain_fee_denom = chain_info["chain_fee_denom"]
self.chain_transfer_channel_ids = chain_info["chain_transfer_channel_ids"]
self.chain_gas_price = Decimal("0.01")
self.swap_gas_limit = 500000
self.kind = "astroport"
self.endpoints = endpoints["http"]
self.session = session
Expand Down Expand Up @@ -295,6 +300,12 @@ def swap_msg_asset_b(
(amount, min_amount),
)

def submit_swap_tx(self, tx: Transaction) -> SubmittedTx:
return try_multiple_clients_fatal(
self.contract_info.clients,
lambda client: client.broadcast_tx(tx),
)

async def simulate_swap_asset_a(
self,
amount: int,
Expand Down
9 changes: 8 additions & 1 deletion src/contracts/pool/osmosis.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Implements a pool provider for osmosis.
"""

from decimal import Decimal
from functools import cached_property
from typing import Any, Optional, List, cast
import urllib3
Expand Down Expand Up @@ -51,7 +52,7 @@ def __init__(
self.chain_fee_denom = chain_info["chain_fee_denom"]
self.chain_transfer_channel_ids = chain_info["chain_transfer_channel_ids"]
self.kind = "osmosis"

self.chain_gas_price = Decimal("0.03")
self.endpoints = endpoints["http"]
self.grpc_endpoints = endpoints["grpc"]
self.address = address
Expand Down Expand Up @@ -257,6 +258,12 @@ def swap_msg_asset_b(
(amount, min_amount),
)

def submit_swap_tx(self, tx: Transaction) -> SubmittedTx:
return try_multiple_clients_fatal(
self.ledgers,
lambda client: client.broadcast_tx(tx),
)

def asset_a(self) -> str:
return self.asset_a_denom

Expand Down
14 changes: 14 additions & 0 deletions src/contracts/pool/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
Defines an interface for all providers of pricing information to fulfill.
"""

from decimal import Decimal
import json
from typing import Any, Optional, cast
from abc import ABC, abstractmethod
from cosmpy.aerial.wallet import LocalWallet
from cosmpy.aerial.tx_helpers import SubmittedTx
from google.protobuf.message import Message
from cosmpy.aerial.tx import Transaction


def cached_pools(
Expand Down Expand Up @@ -43,12 +45,16 @@ class PoolProvider(ABC):

chain_prefix: str

chain_gas_price: Decimal

chain_fee_denom: str

chain_transfer_channel_ids: dict[str, str]

endpoints: list[str]

swap_gas_limit: int

swap_fee: int

kind: str
Expand Down Expand Up @@ -133,6 +139,14 @@ def swap_msg_asset_b(

raise NotImplementedError

@abstractmethod
def submit_swap_tx(self, tx: Transaction) -> SubmittedTx:
"""
Submits a transaction to the blockchain backing this provider.
"""

raise NotImplementedError

@abstractmethod
def asset_a(self) -> str:
"""
Expand Down
13 changes: 12 additions & 1 deletion src/strategies/naive.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
of hops using all available providers.
"""

from itertools import groupby
import asyncio
import random
from typing import List, Union, Optional, Self, AsyncGenerator
Expand All @@ -21,6 +22,7 @@
fmt_route_leg,
recover_funds,
IBC_TRANSFER_GAS,
GAS_DISCOUNT_BATCHED,
)
from src.util import (
DenomChainInfo,
Expand Down Expand Up @@ -240,7 +242,16 @@ async def eval_route(
# Ensure that there is at least 5k of the base chain denom
# at all times
if ctx.cli_args["base_denom"] == "untrn":
gas_base_denom += sum([leg.backend.swap_fee for leg in route])
gas_base_denom += sum(
(
(
sum((leg.backend.swap_fee for leg in legs)) * GAS_DISCOUNT_BATCHED
if len(legs) > 1
else sum((leg.backend.swap_fee for leg in legs))
)
for legs in groupby(route, key=lambda elem: elem.backend.chain_id)
)
)

for i, leg in enumerate(route[:-1]):
next_leg = route[i + 1]
Expand Down
Loading

0 comments on commit e434a28

Please sign in to comment.