Skip to content

Commit

Permalink
Fix UDTC allowance calculation
Browse files Browse the repository at this point in the history
The UDTC allowance was calculated as a delta of required - current
balance, but the `approve` call expects a total value. This lead to
insufficient balance on the last deposit call.

Fixes: #238
  • Loading branch information
ulope authored and brainbot-devops committed Sep 5, 2019
1 parent 7cf827a commit 7041b21
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
1 change: 1 addition & 0 deletions scenario_player/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ def _initialize_nodes(
for address, balance in balance_per_node.items()
if balance < NODE_ACCOUNT_BALANCE_MIN
}
log.debug("Node eth balances", balances=balance_per_node, low_balances=low_balances)
if low_balances:
log.info("Funding nodes", nodes=low_balances.keys())
fund_tx = set()
Expand Down
2 changes: 1 addition & 1 deletion scenario_player/services/rpc/blueprints/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def transact_call(key, data):

contract_proxy = rpc_client.new_contract_proxy(contract_abi, data["contract_address"])

log.debug("Transacting..", **data)
log.debug("Transacting..", action=action, **data)

args = data["amount"], data["target_address"]
if action != "mintFor":
Expand Down
5 changes: 5 additions & 0 deletions scenario_player/services/rpc/blueprints/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
"""
from flask import Blueprint, request
from structlog import get_logger

from scenario_player.services.common.metrics import REDMetricsTracker
from scenario_player.services.rpc.schemas.transactions import SendTransactionSchema

log = get_logger(__name__)


transactions_blueprint = Blueprint("transactions_view", __name__)


Expand Down Expand Up @@ -77,6 +81,7 @@ def new_transaction():
data = transaction_send_schema.validate_and_deserialize(request.get_json())
rpc_client, _ = data.pop("client"), data.pop("client_id")

log.debug("Performing transaction", params=data)
result = rpc_client.send_transaction(**data)

return transaction_send_schema.jsonify({"tx_hash": result})
38 changes: 23 additions & 15 deletions scenario_player/utils/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ def __init__(self, runner, address=None):
self.interface = ServiceInterface(runner.yaml.spaas)
self.gas_limit = GAS_LIMIT_FOR_TOKEN_CONTRACT_CALL * 2

def __repr__(self):
return f"<{self.name}>"

@property
def name(self):
return f"{self.__class__.__name__}@{to_checksum_address(self.address)}"

@property
def client_id(self):
return self.config.spaas.rpc.client_id
Expand Down Expand Up @@ -70,23 +77,24 @@ def mint(
:attr:`.DEFAULT_TOKEN_BALANCE_MIN` and :attr:`.DEFAULT_TOKEN_BALANCE_FUND`
if those settings are absent.
"""
local_log = log.bind(contract=self.name)
balance = self.balance
if required_balance is None:
required_balance = self.config.token.min_balance
log.debug(
local_log.debug(
"Checking necessity of mint request",
required_balance=required_balance,
actual_balance=balance,
)
if not balance < required_balance:
log.debug("Mint call not required - sufficient funds")
local_log.debug("Mint call not required - sufficient funds")
return

if max_fund_amount is None:
max_fund_amount = self.config.token.max_funding

mint_amount = max_fund_amount - balance
log.debug("Minting required - insufficient funds.")
local_log.debug("Minting required - insufficient funds.", mint_amount=mint_amount)
params = {"amount": mint_amount, "target_address": target_address}
params.update(kwargs)
return self.transact("mint", params)
Expand All @@ -104,7 +112,7 @@ class Token(Contract):
"""

def __init__(self, scenario_runner, data_path: pathlib.Path):
super(Token, self).__init__(scenario_runner)
super().__init__(scenario_runner)
self._token_file = data_path.joinpath("token.info")
self.contract_data = {}
self.deployment_receipt = None
Expand Down Expand Up @@ -346,9 +354,7 @@ class UserDepositContract(Contract):
"""

def __init__(self, scenario_runner, contract_proxy, token_proxy):
super(UserDepositContract, self).__init__(
scenario_runner, address=contract_proxy.contract_address
)
super().__init__(scenario_runner, address=contract_proxy.contract_address)
self.contract_proxy = contract_proxy
self.token_proxy = token_proxy
self.tx_hashes = set()
Expand Down Expand Up @@ -399,19 +405,20 @@ def update_allowance(self) -> Union[Tuple[str, int], None]:
required_allowance = self.config.settings.services.udc.token.balance_per_node * node_count

log.debug(
"Checking necessity of deposit request",
required_balance=required_allowance,
actual_balance=udt_allowance,
"Checking UDTC allowance",
required_allowance=required_allowance,
required_per_node=self.config.settings.services.udc.token.balance_per_node,
node_count=node_count,
actual_allowance=udt_allowance,
)

if not udt_allowance < required_allowance:
log.debug("allowance update call not required - sufficient allowance")
log.debug("UDTC allowance sufficient")
return

log.debug("allowance update call required - insufficient allowance")
allow_amount = required_allowance - udt_allowance
log.debug("UDTC allowance insufficient, updating")
params = {
"amount": allow_amount,
"amount": required_allowance,
"target_address": self.checksum_address,
"contract_address": self.ud_token_address,
}
Expand All @@ -432,14 +439,15 @@ def deposit(self, target_address) -> Union[str, None]:
max_funding = self.config.settings.services.udc.token.max_funding
log.debug(
"Checking necessity of deposit request",
target_address=target_address,
required_balance=min_deposit,
actual_balance=balance,
)
if not balance < min_deposit:
log.debug("deposit call not required - sufficient funds")
return

log.debug("deposit call required - insufficient funds")
log.debug("deposit call required - insufficient funds", target_address=target_address)
deposit_amount = total_deposit + (max_funding - balance)
params = {"amount": deposit_amount, "target_address": target_address}
return self.transact("deposit", params)

0 comments on commit 7041b21

Please sign in to comment.