Skip to content

Commit

Permalink
Merge pull request #71 from terra-money/fix/estimate-fee
Browse files Browse the repository at this point in the history
ceil estimated fee
  • Loading branch information
Geoff Lee authored Feb 20, 2022
2 parents 97b84f5 + b5508a5 commit 6d88b3b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ license = "MIT"
packages = [{ include = "terra_sdk" }]
readme = "README.md"
repository = "https://github.com/terra-money/terra.py"
version = "2.0.2"
version = "2.0.3"

[tool.poetry.dependencies]
aiohttp = "^3.8.1"
Expand Down
23 changes: 7 additions & 16 deletions terra_sdk/client/lcd/api/tx.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import base64
import copy
import urllib.parse
from typing import List, Optional, Union, Dict
from typing import List, Optional
from multidict import CIMultiDict
import attr

Expand All @@ -14,11 +13,9 @@
SyncTxBroadcastResult,
)
from terra_sdk.core.msg import Msg
from terra_sdk.core.block import Block
from terra_sdk.core.tx import AuthInfo, Fee, SignerData, SignMode, Tx, TxBody, TxInfo
from terra_sdk.util.hash import hash_amino
from terra_sdk.util.json import JSONSerializable
from . import tendermint

from ._base import BaseAsyncAPI, sync_bind

Expand Down Expand Up @@ -47,6 +44,7 @@ class CreateTxOptions:
fee (Optional[Fee], optional): transaction fee. If ``None``, will be estimated.
See more on `fee estimation`_.
memo (str, optional): optional short string to include with transaction.
gas (str, optional): gas limit to set per-transaction; set to "auto" to calculate sufficient gas automatically
gas_prices (Coins.Input, optional): gas prices for fee estimation.
gas_adjustment (Numeric.Input, optional): gas adjustment for fee estimation.
fee_denoms (List[str], optional): list of denoms to use for fee after estimation.
Expand All @@ -62,7 +60,7 @@ class CreateTxOptions:
gas: Optional[str] = attr.ib(default=None)
gas_prices: Optional[Coins.Input] = attr.ib(default=None)
gas_adjustment: Optional[Numeric.Output] = attr.ib(
default=1, converter=Numeric.parse
default=0, converter=Numeric.parse
)
fee_denoms: Optional[List[str]] = attr.ib(default=None)
account_number: Optional[int] = attr.ib(default=None)
Expand Down Expand Up @@ -140,15 +138,8 @@ async def create(
chain ID, account number, sequence and fee estimation.
Args:
sender (AccAddress): transaction sender's account address
msgs (List[Msg]): list of messages to include
fee (Optional[Fee], optional): fee to use (estimates if empty).
memo (str, optional): memo to use. Defaults to "".
gas_prices (Optional[Coins.Input], optional): gas prices for fee estimation.
gas_adjustment (Optional[Numeric.Input], optional): gas adjustment for fee estimation.
fee_denoms (Optional[List[str]], optional): list of denoms to use for gas fee when estimating.
account_number (Optional[int], optional): account number to use.
sequence (Optional[int], optional): sequence number to use.
signers (List[SingerOptions]): options about signers
options (CreateTxOptions): options about creating a tx
Returns:
Tx: unsigned tx
Expand Down Expand Up @@ -214,12 +205,12 @@ async def estimate_fee(
tx.append_empty_signatures(signers)

gas = options.gas
if gas is None or gas == "auto" or gas == 0:
if gas is None or gas == "auto" or int(gas) == 0:
opt = copy.deepcopy(options)
opt.gas_adjustment = gas_adjustment
gas = str(self.estimate_gas(tx, opt))

fee_amount = gas_prices_coins.mul(gas).to_int_coins() if gas_prices_coins else Coins.from_str('0uusd')
fee_amount = gas_prices_coins.mul(gas).to_int_ceil_coins() if gas_prices_coins else Coins.from_str('0uusd')

return Fee(Numeric.parse(gas), fee_amount, "", "")

Expand Down
2 changes: 1 addition & 1 deletion terra_sdk/client/lcd/lcdclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __init__(

default_price, default_adjustment = get_default(chain_id)
self.gas_prices = Coins(gas_prices) if gas_prices else default_price
self.gas_adjustment = gas_adjustment if gas_adjustment else gas_adjustment
self.gas_adjustment = gas_adjustment if gas_adjustment else default_adjustment

self.auth = AsyncAuthAPI(self)
self.bank = AsyncBankAPI(self)
Expand Down
5 changes: 5 additions & 0 deletions terra_sdk/core/coin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import math
import re
from dataclasses import dataclass
from typing import Union
Expand Down Expand Up @@ -50,6 +51,10 @@ def to_int_coin(self) -> Coin:
"""Creates a new :class:`Coin` with an ``int`` amount."""
return Coin(self.denom, int(self.amount))

def to_int_ceil_coin(self) -> Coin:
"""Turns the :class:`coin` into an ``int`` coin with ceiling the amount."""
return Coin(self.denom, int(math.ceil(self.amount)))

def to_dec_coin(self) -> Coin:
"""Creates a new :class:`Coin` with a :class:`Dec` amount."""
return Coin(self.denom, Dec(self.amount))
Expand Down
4 changes: 4 additions & 0 deletions terra_sdk/core/coins.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ def to_int_coins(self) -> Coins:
"""Creates new set of :class:`Coins` that have ``int`` amounts."""
return Coins(c.to_int_coin() for c in self)

def to_int_ceil_coins(self) -> Coins:
"""Creates a new :class:`Coins` object with all ``int`` coins with ceiling the amount"""
return Coins(c.to_int_ceil_coin() for c in self)

def add(self, addend: Union[Coin, Coins]) -> Coins:
"""Performs addition, which combines the sets of Coin objects. Coins of similar denoms
will be merged into one Coin representing the denom.
Expand Down

0 comments on commit 6d88b3b

Please sign in to comment.