Skip to content

Commit

Permalink
fix(events): check parsed amounts (#312)
Browse files Browse the repository at this point in the history
* Check parsed amounts of events
* Update E2E
* Fix NPE in read-only handler

Signed-off-by: failfmi <[email protected]>
  • Loading branch information
failfmi authored Oct 11, 2021
1 parent 2f5e347 commit 609f8f8
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 66 deletions.
10 changes: 9 additions & 1 deletion app/process/watcher/evm/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ func (ew *Watcher) handleBurnLog(eventLog *router.RouterBurn, q qi.Queue) {
return
}
}
if properAmount.Cmp(big.NewInt(0)) == 0 {
ew.logger.Errorf("[%s] - Insufficient amount provided: Event Amount [%s] and Proper Amount [%s].", eventLog.Raw.TxHash, eventLog.Amount, properAmount)
return
}

burnEvent := &transfer.Transfer{
TransactionId: fmt.Sprintf("%s-%d", eventLog.Raw.TxHash, eventLog.Raw.Index),
Expand Down Expand Up @@ -339,14 +343,18 @@ func (ew *Watcher) handleLockLog(eventLog *router.RouterLock, q qi.Queue) {
return
}

properAmount := eventLog.Amount
properAmount := new(big.Int).Sub(eventLog.Amount, eventLog.ServiceFee)
if eventLog.TargetChain.Int64() == 0 {
properAmount, err = ew.contracts.RemoveDecimals(eventLog.Amount, eventLog.Token)
if err != nil {
ew.logger.Errorf("[%s] - Failed to adjust [%s] amount [%s] decimals between chains.", eventLog.Raw.TxHash, eventLog.Token, eventLog.Amount)
return
}
}
if properAmount.Cmp(big.NewInt(0)) == 0 {
ew.logger.Errorf("[%s] - Insufficient amount provided: Event Amount [%s] and Proper Amount [%s].", eventLog.Raw.TxHash, eventLog.Amount, properAmount)
return
}

tr := &transfer.Transfer{
TransactionId: fmt.Sprintf("%s-%d", eventLog.Raw.TxHash, eventLog.Raw.Index),
Expand Down
1 change: 1 addition & 0 deletions app/process/watcher/evm/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var (
Token: common.HexToAddress("0x0000000000000000000000000000000000000000"),
Receiver: hederaAcc.ToBytes(),
Amount: big.NewInt(1),
ServiceFee: big.NewInt(0),
}
burnLog = &router.RouterBurn{
TargetChain: big.NewInt(0),
Expand Down
1 change: 1 addition & 0 deletions app/services/read-only/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func (s Service) FindTransfer(
response, err := fetch()
if err != nil {
s.logger.Errorf("[%s] - Failed to get token burn transactions after timestamp. Error: [%s]", transferID, err)
continue
}

finished := false
Expand Down
13 changes: 10 additions & 3 deletions app/services/transfers/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ import (
"database/sql"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/hashgraph/hedera-sdk-go/v2"
"github.com/hashgraph/hedera-state-proof-verifier-go/stateproof"
hedera_mirror_node "github.com/limechain/hedera-eth-bridge-validator/app/clients/hedera/mirror-node/model"
"github.com/limechain/hedera-eth-bridge-validator/app/domain/client"
"github.com/limechain/hedera-eth-bridge-validator/app/domain/repository"
"github.com/limechain/hedera-eth-bridge-validator/app/domain/service"
big_numbers "github.com/limechain/hedera-eth-bridge-validator/app/helper/big-numbers"
hederahelper "github.com/limechain/hedera-eth-bridge-validator/app/helper/hedera"
"github.com/limechain/hedera-eth-bridge-validator/app/helper/memo"
"github.com/limechain/hedera-eth-bridge-validator/app/helper/sync"
Expand All @@ -36,6 +38,7 @@ import (
"github.com/limechain/hedera-eth-bridge-validator/app/persistence/entity/status"
"github.com/limechain/hedera-eth-bridge-validator/config"
log "github.com/sirupsen/logrus"
"math/big"
"strconv"
"strings"
)
Expand Down Expand Up @@ -184,15 +187,19 @@ func (ts *Service) ProcessNativeTransfer(tm model.Transfer) error {
}

func (ts *Service) ProcessWrappedTransfer(tm model.Transfer) error {
intAmount, err := strconv.ParseInt(tm.Amount, 10, 64)
amount, err := big_numbers.ToBigInt(tm.Amount)
if err != nil {
ts.logger.Errorf("[%s] - Failed to parse amount. Error: [%s]", tm.TransactionId, err)
return err
}

properAmount, err := ts.contractServices[tm.TargetChainId].RemoveDecimals(amount, common.HexToAddress(tm.TargetAsset))
if properAmount.Cmp(big.NewInt(0)) == 0 {
return errors.New(fmt.Sprintf("removed decimals resolves to 0, initial value [%s]", amount))
}
status := make(chan string)
onExecutionBurnSuccess, onExecutionBurnFail := ts.scheduledBurnTxExecutionCallbacks(tm.TransactionId, &status)
onTokenBurnSuccess, onTokenBurnFail := ts.scheduledBurnTxMinedCallbacks(&status)
ts.scheduledService.ExecuteScheduledBurnTransaction(tm.TransactionId, tm.SourceAsset, intAmount, &status, onExecutionBurnSuccess, onExecutionBurnFail, onTokenBurnSuccess, onTokenBurnFail)
ts.scheduledService.ExecuteScheduledBurnTransaction(tm.TransactionId, tm.SourceAsset, properAmount.Int64(), &status, onExecutionBurnSuccess, onExecutionBurnFail, onTokenBurnSuccess, onTokenBurnFail)

statusBlocker:
for {
Expand Down
Loading

0 comments on commit 609f8f8

Please sign in to comment.