Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pricing #756

Merged
merged 10 commits into from
Sep 17, 2024
1 change: 1 addition & 0 deletions aea/skills/scaffold/skill.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ models:
class_name: MyModel
dependencies: {}
is_abstract: false
customs: []
11 changes: 6 additions & 5 deletions docs/api/plugins/aea_ledger_ethereum/ethereum.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ Round WEI to equivalent GWEI
#### get`_`base`_`fee`_`multiplier

```python
def get_base_fee_multiplier(base_fee_gwei: int) -> float
def get_base_fee_multiplier(
base_fee_gwei: Union[int, decimal.Decimal]) -> float
```

Returns multiplier value.
Expand All @@ -58,9 +59,9 @@ Returns multiplier value.

```python
def estimate_priority_fee(
web3_object: Web3, base_fee_gwei: int, block_number: int,
priority_fee_estimation_trigger: int, default_priority_fee: int,
fee_history_blocks: int, fee_history_percentile: int,
web3_object: Web3, block_number: int,
default_priority_fee: Optional[int], fee_history_blocks: int,
fee_history_percentile: int,
priority_fee_increase_boundary: int) -> Optional[int]
```

Expand All @@ -73,7 +74,7 @@ Estimate priority fee from base fee.
```python
def get_gas_price_strategy_eip1559(
max_gas_fast: int, fee_history_blocks: int, fee_history_percentile: int,
priority_fee_estimation_trigger: int, default_priority_fee: int,
default_priority_fee: Optional[int],
fallback_estimate: Dict[str,
Optional[int]], priority_fee_increase_boundary: int
) -> Callable[[Web3, TxParams], Dict[str, Wei]]
Expand Down
10 changes: 5 additions & 5 deletions docs/package_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
| contract/fetchai/erc1155/0.22.0 | `bafybeiff7a6xncyad53o2r7lekpnhexcspze6ocy55xtpzqeuacnlpunm4` |
| connection/fetchai/gym/0.19.0 | `bafybeicqqvl4tt3qbulnkoffciagmfd6p3hxxi3i2mrrqtnbycv757pn6y` |
| connection/fetchai/stub/0.21.0 | `bafybeibybboiwgklfiqpkkcw6rwj65s5jalzfzf6mh6fstxdlt6habzwvy` |
| connection/valory/ledger/0.19.0 | `bafybeihynkdraqthjtv74qk3nc5r2xubniqx2hhzpxn7bd4qmlf7q4wruq` |
| connection/valory/ledger/0.19.0 | `bafybeibigvowo52dw3tqwmxkdxtq52qzm6iqw752gpzpi23kgej4k3sum4` |
| connection/valory/http_server/0.22.0 | `bafybeihpgu56ovmq4npazdbh6y6ru5i7zuv6wvdglpxavsckyih56smu7m` |
| connection/valory/p2p_libp2p/0.1.0 | `bafybeic2u7azbwjny2nhaltqnbohlvysx3x6ectzbege7sxwrbzcz4lcma` |
| connection/valory/p2p_libp2p_client/0.1.0 | `bafybeid3xg5k2ol5adflqloy75ibgljmol6xsvzvezebsg7oudxeeolz7e` |
Expand All @@ -26,12 +26,12 @@
| skill/fetchai/error_test_skill/0.1.0 | `bafybeihsbtlpe7h6fsvoxban5rilkmwviwkokul5cqym6atoolirontiyu` |
| skill/fetchai/gym/0.20.0 | `bafybeie7y2fsxfuhsqxqcaluo5exskmrm5q3a6e2hfcskcuvzvxjjhijh4` |
| skill/fetchai/http_echo/0.20.0 | `bafybeicfiri2juaqh3azeit3z3rf44kgxdo6oj4lgxjgvnowq6m7j47qrm` |
| skill/fetchai/erc1155_client/0.28.0 | `bafybeihwk56assrj5xpu5dvyyajw7dgnk65eq2n3idd5pinxp5vortd6wi` |
| skill/fetchai/erc1155_deploy/0.30.0 | `bafybeiewhczeyrrml6shrd5dr556uwqt54aiwrol2fyqtx2n4ymgfbc6mu` |
| skill/fetchai/erc1155_client/0.28.0 | `bafybeigeoxvo3dabzbhjphu2n32wn5bomhv7b6ge6ogcte7qa6h42rwn5a` |
| skill/fetchai/erc1155_deploy/0.30.0 | `bafybeialn4tcp4vs7nousugfjpin5r37vhd2qcmphi3fs7ob66kcwb5gu4` |
| skill/fetchai/error/0.17.0 | `bafybeicboomvykqhel3otyv4qg5t3hzpo6kmn5bk4ljluithhuieu7flsm` |
| skill/fetchai/fipa_dummy_buyer/0.2.0 | `bafybeidgso7lo5ay44mbxsp3lxilrgeek3ye44e6wus2ayq6kyxfvc3vjm` |
| skill/fetchai/generic_buyer/0.26.0 | `bafybeif5q4xb45m5l2h6pikgqdieko4iojna5mou7byub5voajcxwxruo4` |
| skill/fetchai/generic_seller/0.27.0 | `bafybeie3dhsbasxhtkvpakxml6eyccqqpmog3kynyrl5khyrnw6hjjpdj4` |
| skill/fetchai/generic_buyer/0.26.0 | `bafybeienntvkd7blbbc4p624dop7tz4iejtpi36somhohbi4tmxd63ksr4` |
| skill/fetchai/generic_seller/0.27.0 | `bafybeid4y4p2xlnkezvtzhgwfdzrowhul34md64shf3jv5nskpggskc3fm` |
| skill/fetchai/task_test_skill/0.1.0 | `bafybeidv77u2xl52mnxakwvh7fuh46aiwfpteyof4eaptfd4agoi6cdble` |
| agent/fetchai/error_test/0.1.0 | `bafybeiecm675ndzbh35jkejtxn4ughoutztltjhgwzfbp57okabedjmnpq` |
| agent/fetchai/gym_aea/0.25.0 | `bafybeibzn3qomqmkaksgpd3gn6aijffvvw7rojswhoytiovohuc737fvfm` |
Expand Down
2 changes: 1 addition & 1 deletion packages/fetchai/skills/erc1155_client/skill.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fingerprint:
tests/test_strategy.py: bafybeicbxie3v6vue3gcnru6vsvggcgy3shxwrldis5gppizbuhooslcqa
fingerprint_ignore_patterns: []
connections:
- valory/ledger:0.19.0:bafybeihynkdraqthjtv74qk3nc5r2xubniqx2hhzpxn7bd4qmlf7q4wruq
- valory/ledger:0.19.0:bafybeibigvowo52dw3tqwmxkdxtq52qzm6iqw752gpzpi23kgej4k3sum4
contracts:
- fetchai/erc1155:0.22.0:bafybeiff7a6xncyad53o2r7lekpnhexcspze6ocy55xtpzqeuacnlpunm4
protocols:
Expand Down
2 changes: 1 addition & 1 deletion packages/fetchai/skills/erc1155_deploy/skill.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fingerprint:
tests/test_strategy.py: bafybeigxtw2j2c7vl6xhdwos62jbtmx62xfgdyadptm5eewmkesmcooyea
fingerprint_ignore_patterns: []
connections:
- valory/ledger:0.19.0:bafybeihynkdraqthjtv74qk3nc5r2xubniqx2hhzpxn7bd4qmlf7q4wruq
- valory/ledger:0.19.0:bafybeibigvowo52dw3tqwmxkdxtq52qzm6iqw752gpzpi23kgej4k3sum4
contracts:
- fetchai/erc1155:0.22.0:bafybeiff7a6xncyad53o2r7lekpnhexcspze6ocy55xtpzqeuacnlpunm4
protocols:
Expand Down
2 changes: 1 addition & 1 deletion packages/fetchai/skills/generic_buyer/skill.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fingerprint:
tests/test_models.py: bafybeibh72j3n72yseqvmpppucpu5wtidf6ebxbxkfnmrnlh4zv5y5apei
fingerprint_ignore_patterns: []
connections:
- valory/ledger:0.19.0:bafybeihynkdraqthjtv74qk3nc5r2xubniqx2hhzpxn7bd4qmlf7q4wruq
- valory/ledger:0.19.0:bafybeibigvowo52dw3tqwmxkdxtq52qzm6iqw752gpzpi23kgej4k3sum4
contracts: []
protocols:
- fetchai/default:1.0.0:bafybeihdvtmnz7fzy7kwi3wlo6rfl27f6q3g5entplgvq7y23i3v5uoz24
Expand Down
2 changes: 1 addition & 1 deletion packages/fetchai/skills/generic_seller/skill.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fingerprint:
tests/test_models.py: bafybeihabrc22zqssit3fmqhxptosy6qz6mx65ukhf5iayvirfv42xrhoq
fingerprint_ignore_patterns: []
connections:
- valory/ledger:0.19.0:bafybeihynkdraqthjtv74qk3nc5r2xubniqx2hhzpxn7bd4qmlf7q4wruq
- valory/ledger:0.19.0:bafybeibigvowo52dw3tqwmxkdxtq52qzm6iqw752gpzpi23kgej4k3sum4
contracts: []
protocols:
- fetchai/default:1.0.0:bafybeihdvtmnz7fzy7kwi3wlo6rfl27f6q3g5entplgvq7y23i3v5uoz24
Expand Down
10 changes: 5 additions & 5 deletions packages/packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"contract/fetchai/erc1155/0.22.0": "bafybeiff7a6xncyad53o2r7lekpnhexcspze6ocy55xtpzqeuacnlpunm4",
"connection/fetchai/gym/0.19.0": "bafybeicqqvl4tt3qbulnkoffciagmfd6p3hxxi3i2mrrqtnbycv757pn6y",
"connection/fetchai/stub/0.21.0": "bafybeibybboiwgklfiqpkkcw6rwj65s5jalzfzf6mh6fstxdlt6habzwvy",
"connection/valory/ledger/0.19.0": "bafybeihynkdraqthjtv74qk3nc5r2xubniqx2hhzpxn7bd4qmlf7q4wruq",
"connection/valory/ledger/0.19.0": "bafybeibigvowo52dw3tqwmxkdxtq52qzm6iqw752gpzpi23kgej4k3sum4",
"connection/valory/http_server/0.22.0": "bafybeihpgu56ovmq4npazdbh6y6ru5i7zuv6wvdglpxavsckyih56smu7m",
"connection/valory/p2p_libp2p/0.1.0": "bafybeic2u7azbwjny2nhaltqnbohlvysx3x6ectzbege7sxwrbzcz4lcma",
"connection/valory/p2p_libp2p_client/0.1.0": "bafybeid3xg5k2ol5adflqloy75ibgljmol6xsvzvezebsg7oudxeeolz7e",
Expand All @@ -26,12 +26,12 @@
"skill/fetchai/error_test_skill/0.1.0": "bafybeihsbtlpe7h6fsvoxban5rilkmwviwkokul5cqym6atoolirontiyu",
"skill/fetchai/gym/0.20.0": "bafybeie7y2fsxfuhsqxqcaluo5exskmrm5q3a6e2hfcskcuvzvxjjhijh4",
"skill/fetchai/http_echo/0.20.0": "bafybeicfiri2juaqh3azeit3z3rf44kgxdo6oj4lgxjgvnowq6m7j47qrm",
"skill/fetchai/erc1155_client/0.28.0": "bafybeihwk56assrj5xpu5dvyyajw7dgnk65eq2n3idd5pinxp5vortd6wi",
"skill/fetchai/erc1155_deploy/0.30.0": "bafybeiewhczeyrrml6shrd5dr556uwqt54aiwrol2fyqtx2n4ymgfbc6mu",
"skill/fetchai/erc1155_client/0.28.0": "bafybeigeoxvo3dabzbhjphu2n32wn5bomhv7b6ge6ogcte7qa6h42rwn5a",
"skill/fetchai/erc1155_deploy/0.30.0": "bafybeialn4tcp4vs7nousugfjpin5r37vhd2qcmphi3fs7ob66kcwb5gu4",
"skill/fetchai/error/0.17.0": "bafybeicboomvykqhel3otyv4qg5t3hzpo6kmn5bk4ljluithhuieu7flsm",
"skill/fetchai/fipa_dummy_buyer/0.2.0": "bafybeidgso7lo5ay44mbxsp3lxilrgeek3ye44e6wus2ayq6kyxfvc3vjm",
"skill/fetchai/generic_buyer/0.26.0": "bafybeif5q4xb45m5l2h6pikgqdieko4iojna5mou7byub5voajcxwxruo4",
"skill/fetchai/generic_seller/0.27.0": "bafybeie3dhsbasxhtkvpakxml6eyccqqpmog3kynyrl5khyrnw6hjjpdj4",
"skill/fetchai/generic_buyer/0.26.0": "bafybeienntvkd7blbbc4p624dop7tz4iejtpi36somhohbi4tmxd63ksr4",
"skill/fetchai/generic_seller/0.27.0": "bafybeid4y4p2xlnkezvtzhgwfdzrowhul34md64shf3jv5nskpggskc3fm",
"skill/fetchai/task_test_skill/0.1.0": "bafybeidv77u2xl52mnxakwvh7fuh46aiwfpteyof4eaptfd4agoi6cdble",
"agent/fetchai/error_test/0.1.0": "bafybeiecm675ndzbh35jkejtxn4ughoutztltjhgwzfbp57okabedjmnpq",
"agent/fetchai/gym_aea/0.25.0": "bafybeibzn3qomqmkaksgpd3gn6aijffvvw7rojswhoytiovohuc737fvfm",
Expand Down
5 changes: 2 additions & 3 deletions packages/valory/connections/ledger/connection.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fingerprint:
tests/conftest.py: bafybeid7vo7e2m76ey5beeadtbxywxx5ukefd5slwbc362rwmhht6i45ou
tests/test_contract_dispatcher.py: bafybeiag5lnpc7h25w23ash4hk4cowxsy5buxgpr474l3tfewnhf56eqyq
tests/test_ledger.py: bafybeigcedfr3yv3jse3xwrerrgwbelgb56uhgrvdus527d3daekh6dx4m
tests/test_ledger_api.py: bafybeifw5smawex5m2fm6rt4kmunc22kpabalmshh45qb3xnuap33sfgyi
tests/test_ledger_api.py: bafybeiduph6a6apuwdkq432wudxj5wbw5r2gnaoiqk676qcptywhgtbgra
fingerprint_ignore_patterns: []
connections: []
protocols:
Expand All @@ -37,8 +37,7 @@ config:
max_gas_fast: 1500
fee_history_blocks: 10
fee_history_percentile: 5
priority_fee_estimation_trigger: 100
default_priority_fee: 3
default_priority_fee: null
fallback_estimate:
maxFeePerGas: 20000000000
maxPriorityFeePerGas: 3000000000
Expand Down
22 changes: 20 additions & 2 deletions packages/valory/connections/ledger/tests/test_ledger_api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2021-2023 Valory AG
# Copyright 2021-2024 Valory AG
# Copyright 2018-2019 Fetch.AI Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -36,7 +36,9 @@
DEFAULT_GANACHE_CHAIN_ID,
ganache,
)
from eth_typing import BlockNumber
from web3.eth import Eth
from web3.types import FeeHistory, Wei

from aea.common import Address
from aea.configurations.data_types import PublicId
Expand Down Expand Up @@ -98,6 +100,16 @@
],
)

fee_history_mock = patch(
"web3.eth.Eth.fee_history",
return_value=FeeHistory(
baseFeePerGas=[Wei(0)],
gasUsedRatio=[0],
oldestBlock=BlockNumber(0),
reward=[[Wei(0)]],
Adamantios marked this conversation as resolved.
Show resolved Hide resolved
),
)


class LedgerApiDialogues(BaseLedgerApiDialogues):
"""The dialogues class keeps track of all ledger_api dialogues."""
Expand Down Expand Up @@ -220,10 +232,12 @@ async def test_get_state(
)
assert actual_block == expected_block

@fee_history_mock
@pytest.mark.asyncio
@gas_strategies
async def test_get_raw_transaction(
self,
_fee_history_mock: Mock,
gas_strategies: Dict,
ledger_apis_connection: Connection,
update_default_ethereum_ledger_api: None,
Expand Down Expand Up @@ -281,10 +295,14 @@ async def test_get_raw_transaction(
assert isinstance(response_message.raw_transaction, RawTransaction)
assert response_message.raw_transaction.ledger_id == request.terms.ledger_id

@fee_history_mock
@pytest.mark.asyncio
@gas_strategies
async def test_send_signed_transaction_ethereum(
self, gas_strategies: Dict, ledger_apis_connection: LedgerConnection
self,
_fee_history_mock: Mock,
gas_strategies: Dict,
ledger_apis_connection: LedgerConnection,
) -> None:
"""Test send signed transaction with Ethereum APIs."""
ledger_api_dialogues = LedgerApiDialogues(SOME_SKILL_ID)
Expand Down
49 changes: 21 additions & 28 deletions plugins/aea-ledger-ethereum/aea_ledger_ethereum/ethereum.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import math
import threading
import warnings
from collections import defaultdict
from copy import deepcopy
from pathlib import Path
from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Union, cast
Expand Down Expand Up @@ -89,16 +90,13 @@
# Which percentile of effective priority fees to include
FEE_HISTORY_PERCENTILE = 5

# Which base fee to trigger priority fee estimation at (GWEI)
PRIORITY_FEE_ESTIMATION_TRIGGER = 100

# Returned if above trigger is not met (GWEI)
DEFAULT_PRIORITY_FEE = 3
DEFAULT_PRIORITY_FEE = None

# In case something goes wrong fall back to this estimate
FALLBACK_ESTIMATE = {
"maxFeePerGas": to_wei(20, "gwei"),
"maxPriorityFeePerGas": to_wei(DEFAULT_PRIORITY_FEE, "gwei"),
"maxPriorityFeePerGas": to_wei(3, "gwei"),
}

PRIORITY_FEE_INCREASE_BOUNDARY = 200 # percentage
Expand All @@ -107,7 +105,6 @@
"max_gas_fast": MAX_GAS_FAST,
"fee_history_blocks": FEE_HISTORY_BLOCKS,
"fee_history_percentile": FEE_HISTORY_PERCENTILE,
"priority_fee_estimation_trigger": PRIORITY_FEE_ESTIMATION_TRIGGER,
"default_priority_fee": DEFAULT_PRIORITY_FEE,
"fallback_estimate": FALLBACK_ESTIMATE,
"priority_fee_increase_boundary": PRIORITY_FEE_INCREASE_BOUNDARY,
Expand All @@ -129,6 +126,8 @@
EIP1559_POLYGON: DEFAULT_EIP1559_STRATEGY_POLYGON,
}

BASE_FEE_MULTIPLIER = defaultdict(lambda: 1.2, {40: 2.0, 100: 1.6, 200: 1.4})

# The tip increase is the minimum required of 10%.
TIP_INCREASE = 1.1

Expand All @@ -149,32 +148,26 @@ def round_to_whole_gwei(number: Type[int]) -> Wei:
return cast(Wei, to_wei(rounded, "gwei"))


def get_base_fee_multiplier(base_fee_gwei: int) -> float:
def get_base_fee_multiplier(base_fee_gwei: Union[int, decimal.Decimal]) -> float:
"""Returns multiplier value."""

if base_fee_gwei <= 40: # pylint: disable=no-else-return
return 2.0
elif base_fee_gwei <= 100: # pylint: disable=no-else-return
return 1.6
elif base_fee_gwei <= 200: # pylint: disable=no-else-return
return 1.4
else: # pylint: disable=no-else-return
return 1.2
valid_fees = {fee for fee in BASE_FEE_MULTIPLIER if base_fee_gwei <= fee}
# add the base fee in case the valid fees set is empty
Adamantios marked this conversation as resolved.
Show resolved Hide resolved
if not valid_fees:
valid_fees.add(-1)
return BASE_FEE_MULTIPLIER[min(valid_fees)]


def estimate_priority_fee(
web3_object: Web3,
base_fee_gwei: int,
block_number: int,
priority_fee_estimation_trigger: int,
default_priority_fee: int,
default_priority_fee: Optional[int],
fee_history_blocks: int,
fee_history_percentile: int,
priority_fee_increase_boundary: int,
) -> Optional[int]:
"""Estimate priority fee from base fee."""

if base_fee_gwei < priority_fee_estimation_trigger:
if default_priority_fee is not None:
return default_priority_fee

fee_history = web3_object.eth.fee_history(
Expand All @@ -189,7 +182,7 @@ def estimate_priority_fee(

# Calculate percentage increases from between ordered list of fees
percentage_increases = [
((j - i) / i) * 100 for i, j in zip(rewards[:-1], rewards[1:])
((j - i) / i) * 100 if i != 0 else 0 for i, j in zip(rewards[:-1], rewards[1:])
]
highest_increase = max(*percentage_increases)
highest_increase_index = percentage_increases.index(highest_increase)
Expand All @@ -210,8 +203,7 @@ def get_gas_price_strategy_eip1559(
max_gas_fast: int,
fee_history_blocks: int,
fee_history_percentile: int,
priority_fee_estimation_trigger: int,
default_priority_fee: int,
default_priority_fee: Optional[int],
fallback_estimate: Dict[str, Optional[int]],
priority_fee_increase_boundary: int,
) -> Callable[[Web3, TxParams], Dict[str, Wei]]:
Expand Down Expand Up @@ -239,9 +231,7 @@ def eip1559_price_strategy(

estimated_priority_fee = estimate_priority_fee(
web3,
cast(int, base_fee_gwei),
block_number,
priority_fee_estimation_trigger=priority_fee_estimation_trigger,
default_priority_fee=default_priority_fee,
fee_history_blocks=fee_history_blocks,
fee_history_percentile=fee_history_percentile,
Expand All @@ -255,9 +245,12 @@ def eip1559_price_strategy(
return fallback_estimate

max_priority_fee_per_gas = max(
estimated_priority_fee, to_wei(default_priority_fee, "gwei")
estimated_priority_fee,
to_wei(default_priority_fee, "gwei")
if default_priority_fee is not None
else -1,
)
multiplier = get_base_fee_multiplier(cast(int, base_fee_gwei))
multiplier = get_base_fee_multiplier(base_fee_gwei)

potential_max_fee = base_fee * multiplier
max_fee_per_gas = (
Expand Down Expand Up @@ -1327,7 +1320,7 @@ def _try_get_revert_reason(self, tx: TxData, **_kwargs: Any) -> str:
self.api.eth.call(replay_tx, tx["blockNumber"] - 1)
except ContractLogicError as e:
# execution reverted exception
return str(e)
return e.message
except HTTPError as e:
# http exception
raise e
Expand Down
Loading
Loading