From 52dfe05ab10861b0c7b3b693e7010a76bb5f623c Mon Sep 17 00:00:00 2001
From: Piotr Roslaniec
Date: Thu, 29 Apr 2021 15:32:04 +0200
Subject: [PATCH] Add deposit workaround
---
nucypher/blockchain/eth/actors.py | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/nucypher/blockchain/eth/actors.py b/nucypher/blockchain/eth/actors.py
index e2e74217f..acfa17b21 100644
--- a/nucypher/blockchain/eth/actors.py
+++ b/nucypher/blockchain/eth/actors.py
@@ -609,8 +609,6 @@ class Staker(NucypherTokenActor):
raise self.InsufficientTokens(f"Insufficient token balance ({token_balance}) "
f"for new stake initialization of {amount}")
- self._ensure_token_allowance(amount)
-
# Write to blockchain
new_stake = Stake.initialize_stake(staking_agent=self.staking_agent,
economics=self.economics,
@@ -766,6 +764,9 @@ class Staker(NucypherTokenActor):
def _deposit(self, amount: int, lock_periods: int) -> TxReceipt:
"""Public facing method for token locking."""
+ self.token_agent.approve_transfer(amount=0,
+ spender_address=self.staking_agent.contract_address,
+ transacting_power=self.transacting_power)
receipt = self.token_agent.approve_and_call(amount=amount,
target_address=self.staking_agent.contract_address,
transacting_power=self.transacting_power,
@@ -789,13 +790,13 @@ class Staker(NucypherTokenActor):
def _deposit_and_increase(self, stake_index: int, amount: int) -> TxReceipt:
"""Public facing method for deposit and increasing stake."""
- self._ensure_token_allowance(amount)
+ self._ensure_allowance_at_least(amount)
receipt = self.staking_agent.deposit_and_increase(transacting_power=self.transacting_power,
stake_index=stake_index,
amount=amount)
return receipt
- def _ensure_token_allowance(self, amount):
+ def _ensure_allowance_at_least(self, amount):
current_allowance = self.token_agent.get_allowance(owner=self.checksum_address,
spender=self.staking_agent.contract.address)
if amount > current_allowance: