Skip to content

Commit

Permalink
Updating smart contract preview transaction to take in explicit block…
Browse files Browse the repository at this point in the history
… number
  • Loading branch information
slundqui committed Aug 22, 2023
1 parent 16941bd commit 41412a5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
14 changes: 10 additions & 4 deletions lib/agent0/agent0/hyperdrive/exec/execute_agent_trades.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ async def async_match_contract_call_to_trade(
fn_args = (trade_amount, min_output, agent.checksum_address, as_underlying)
if trade.slippage_tolerance:
preview_result = smart_contract_preview_transaction(
hyperdrive_contract, agent.checksum_address, "openLong", *fn_args
hyperdrive_contract, agent.checksum_address, "openLong", fn_args
)
min_output = (
FixedPoint(scaled_value=preview_result["bondProceeds"]) * (FixedPoint(1) - trade.slippage_tolerance)
Expand Down Expand Up @@ -276,7 +276,7 @@ async def async_match_contract_call_to_trade(
)
if trade.slippage_tolerance:
preview_result = smart_contract_preview_transaction(
hyperdrive_contract, agent.checksum_address, "closeLong", *fn_args
hyperdrive_contract, agent.checksum_address, "closeLong", fn_args
)
min_output = (
FixedPoint(scaled_value=preview_result["value"]) * (FixedPoint(1) - trade.slippage_tolerance)
Expand All @@ -302,7 +302,7 @@ async def async_match_contract_call_to_trade(
fn_args = (trade_amount, max_deposit, agent.checksum_address, as_underlying)
if trade.slippage_tolerance:
preview_result = smart_contract_preview_transaction(
hyperdrive_contract, agent.checksum_address, "openShort", *fn_args
hyperdrive_contract, agent.checksum_address, "openShort", fn_args
)
max_deposit = (
FixedPoint(scaled_value=preview_result["traderDeposit"])
Expand Down Expand Up @@ -344,7 +344,7 @@ async def async_match_contract_call_to_trade(
)
if trade.slippage_tolerance:
preview_result = smart_contract_preview_transaction(
hyperdrive_contract, agent.checksum_address, "closeShort", *fn_args
hyperdrive_contract, agent.checksum_address, "closeShort", fn_args
)
min_output = (
FixedPoint(scaled_value=preview_result["value"]) * (FixedPoint(1) - trade.slippage_tolerance)
Expand Down Expand Up @@ -391,6 +391,12 @@ async def async_match_contract_call_to_trade(
case HyperdriveActionType.REMOVE_LIQUIDITY:
min_output = 0
fn_args = (trade_amount, min_output, agent.checksum_address, as_underlying)
preview_result = smart_contract_preview_transaction(
hyperdrive_contract,
agent.checksum_address,
"removeLiquidity",
fn_args,
)
trade_result = await async_transact_and_parse_logs(
web3,
hyperdrive_contract,
Expand Down
17 changes: 13 additions & 4 deletions lib/chainsync/chainsync/analysis/calc_pnl.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def calc_single_closeout(
sender = ChecksumAddress(HexAddress(HexStr(address)))
preview_result = None
maturity = 0
block_number = position["blockNumber"].iloc[0]
if tokentype in ["LONG", "SHORT"]:
maturity = position["maturityTime"]
assert isinstance(maturity, Decimal)
Expand All @@ -52,7 +53,9 @@ def calc_single_closeout(
fn_args = (maturity, amount, min_output, address, as_underlying)
# If this fails, keep as nan and continue iterating
try:
preview_result = smart_contract_preview_transaction(contract, sender, "closeLong", *fn_args)
preview_result = smart_contract_preview_transaction(
contract, sender, "closeLong", fn_args, position["blockNumber"]
)
return Decimal(preview_result["value"]) / Decimal(1e18)
except Exception as exception: # pylint: disable=broad-except
logging.warning("Exception caught, ignoring: %s", exception)
Expand All @@ -61,7 +64,9 @@ def calc_single_closeout(
fn_args = (maturity, amount, min_output, address, as_underlying)
# If this fails, keep as nan and continue iterating
try:
preview_result = smart_contract_preview_transaction(contract, sender, "closeShort", *fn_args)
preview_result = smart_contract_preview_transaction(
contract, sender, "closeShort", fn_args, position["blockNumber"]
)
return preview_result["value"] / Decimal(1e18)
except Exception as exception: # pylint: disable=broad-except
logging.warning("Exception caught, ignoring: %s", exception)
Expand All @@ -71,7 +76,9 @@ def calc_single_closeout(
# If this fails, keep as nan and continue iterating
try:
# TODO this function breaks in system tests when calculating pnl of remove liquidity
preview_result = smart_contract_preview_transaction(contract, sender, "removeLiquidity", *fn_args)
preview_result = smart_contract_preview_transaction(
contract, sender, "removeLiquidity", fn_args, position["blockNumber"]
)
return Decimal(
preview_result["baseProceeds"]
+ preview_result["withdrawalShares"]
Expand All @@ -85,7 +92,9 @@ def calc_single_closeout(
fn_args = (amount, min_output, address, as_underlying)
# If this fails, keep as nan and continue iterating
try:
preview_result = smart_contract_preview_transaction(contract, sender, "redeemWithdrawalShares", *fn_args)
preview_result = smart_contract_preview_transaction(
contract, sender, "redeemWithdrawalShares", fn_args, position["blockNumber"]
)
return preview_result["proceeds"] / Decimal(1e18)
except Exception as exception: # pylint: disable=broad-except
logging.warning("Exception caught, ignoring: %s", exception)
Expand Down
12 changes: 9 additions & 3 deletions lib/ethpy/ethpy/base/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ def smart_contract_read(contract: Contract, function_name_or_signature: str, *fn


def smart_contract_preview_transaction(
contract: Contract, signer_address: ChecksumAddress, function_name_or_signature: str, *fn_args
contract: Contract,
signer_address: ChecksumAddress,
function_name_or_signature: str,
fn_args: Sequence,
block_number: BlockNumber | None = None,
) -> dict[str, Any]:
"""Returns the values from a transaction without actually submitting the transaction.
Expand All @@ -83,8 +87,10 @@ def smart_contract_preview_transaction(
The address that would sign the transaction.
function_name_or_signature : str
The name of the function
*fn_args : Unknown
fn_args : Sequence
The arguments passed to the contract method.
block_number : BlockNumber
The block to query the chain at
Returns
-------
Expand All @@ -102,7 +108,7 @@ def smart_contract_preview_transaction(
function = contract.get_function_by_signature(function_name_or_signature)(*fn_args)
else:
function = contract.get_function_by_name(function_name_or_signature)(*fn_args)
return_values = function.call({"from": signer_address})
return_values = function.call({"from": signer_address}, block_identifier=block_number)
if not isinstance(return_values, Sequence): # could be list or tuple
return_values = [return_values]
if contract.abi: # not all contracts have an associated ABI
Expand Down

0 comments on commit 41412a5

Please sign in to comment.