Conversation
|
|
||
| /* | ||
| */ | ||
| const released = released_tokens_of_user.get(token).get(user); |
There was a problem hiding this comment.
It is this variable released that is important to prevent double payments. The contract needs to copy/increase released from sender to recipient.
| total_released_tokens.get(token) + payment | ||
| ); | ||
|
|
||
| + last_balance_of_user_for_tokens[token][user] = user_balance; |
There was a problem hiding this comment.
At the end of withdraw transaction, the user's token balance is recorded here. This value will always be referenced inwithdraw transaction to detect any change in the balance.
| const Withdraw = Withdraw(Registry(registry).registries('Withdraw')); | ||
|
|
||
| function updateReleasedTokens(token, user, current_balance) { | ||
| if (last_balance_of_user_for_tokens[token][user] === current_balance) { |
There was a problem hiding this comment.
If the recorded balance and the latest balance match, nothing is done.
| updateReleasedTokens(token, history.from, Property.balanceOf(history.from)); | ||
|
|
||
| const released_tokens = released_tokens_of_user.get(token).get(history.from); | ||
| const part_of_released = released_tokens * history.preBalanceOfSender / history.amount; |
There was a problem hiding this comment.
To copy the sender's released to the recipient, calculate part_of_released .
| const part_of_released = released_tokens * history.preBalanceOfSender / history.amount; | ||
| released_tokens_of_user.get(token).set( | ||
| user, | ||
| part_of_released + released_tokens_of_user.get(token).get(user) |
There was a problem hiding this comment.
Add part_of_released to the recipient's released.
|
|
||
| i = i - 1; | ||
| calculated = calculated + history.amount; | ||
| done = last_balance_of_user_for_tokens[token][user] + calculated === current_balance; |
There was a problem hiding this comment.
If the calculated transfers + recorded balance matches the user's latest balance, the loop ends.
| history.amount = current_balance - history.preBalanceOfRecipient; | ||
| } | ||
|
|
||
| updateReleasedTokens(token, history.from, Property.balanceOf(history.from)); |
There was a problem hiding this comment.
We are making recursive call here which will re-trigger the loop from the beginning without updating the released_tokens_of_user state. So, base case might get skipped and it can get stuck in infinite loop?
<<DON'T MARGE THIS PR. IT'S JUST FOR DISCUSSION>>