Skip to content

Commit

Permalink
init blockcypher for ltc
Browse files Browse the repository at this point in the history
Signed-off-by: Philemon Ukane <ukanephilemon@gmail.com>
  • Loading branch information
ukane-philemon committed Nov 9, 2024
1 parent b6bcb6f commit 7e2dc3f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 47 deletions.
76 changes: 30 additions & 46 deletions libwallet/instantswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/crypto-power/cryptopower/libwallet/utils"
"github.com/crypto-power/cryptopower/ui/values"
"github.com/crypto-power/instantswap/blockexplorer"
_ "github.com/crypto-power/instantswap/blockexplorer/blockcypher"

Check failure on line 19 in libwallet/instantswap.go

View workflow job for this annotation

GitHub Actions / Build

blank-imports: a blank import should be only in a main or test package, or have a comment justifying it (revive)

Check warning on line 19 in libwallet/instantswap.go

View workflow job for this annotation

GitHub Actions / Build

blank-imports: a blank import should be only in a main or test package, or have a comment justifying it (revive)
_ "github.com/crypto-power/instantswap/blockexplorer/btcexplorer" //nolint:revive
_ "github.com/crypto-power/instantswap/blockexplorer/dcrexplorer"
)
Expand Down Expand Up @@ -236,11 +237,6 @@ func (mgr *AssetsManager) StartScheduler(ctx context.Context, params instantswap
return mgr.InstantSwap.SchedulerCtx.Err()
}

// assetHasExplorer holds whether or not the receiving asset has an
// instantswap explorer implemented. We only needed to do this for LTC.
// TODO: Remove when instantswap has implemented an LTC explorer.
assetHasExplorer := true

// depending on the block time for the asset, the order may take a while to complete
// so we wait for the estimated block time before checking the order status
switch params.Order.ToCurrency {
Expand All @@ -251,7 +247,6 @@ func (mgr *AssetsManager) StartScheduler(ctx context.Context, params instantswap
log.Info("Order Scheduler: waiting for dcr block time (5 minutes)")
time.Sleep(DCRBlockTime)
case utils.LTCWalletAsset.String():
assetHasExplorer = false
log.Info("Order Scheduler: waiting for ltc block time (~3 minutes)")
time.Sleep(LTCBlockTime)
}
Expand All @@ -276,53 +271,41 @@ func (mgr *AssetsManager) StartScheduler(ctx context.Context, params instantswap
isRefunded = true
}

var isCompleted bool
var amountReceivedMatch bool

if assetHasExplorer {
log.Info("Order Scheduler: instantiate block explorer")
// verify that the order was completed successfully from the blockchain explorer
config := blockexplorer.Config{
EnableOutput: false,
Symbol: params.Order.ToCurrency,
}
explorer, err := blockexplorer.NewExplorer(config) // TODO: Confirm if this still works as intended
if err != nil {
log.Error("error instantiating block explorer: ", err.Error())
return errors.E(op, err)
}
log.Info("Order Scheduler: instantiate block explorer")
// verify that the order was completed successfully from the blockchain explorer
config := blockexplorer.Config{
EnableOutput: false,
Symbol: params.Order.ToCurrency,
}
explorer, err := blockexplorer.NewExplorer(config) // TODO: Confirm if this still works as intended
if err != nil {
log.Error("error instantiating block explorer: ", err.Error())
return errors.E(op, err)
}

verificationInfo := blockexplorer.TxVerifyRequest{
TxId: orderInfo.TxID,
Amount: orderInfo.ReceiveAmount,
CreatedAt: orderInfo.CreatedAt,
Address: orderInfo.DestinationAddress,
Confirms: DefaultConfirmations,
}
verificationInfo := blockexplorer.TxVerifyRequest{
TxId: orderInfo.TxID,
Amount: orderInfo.ReceiveAmount,
CreatedAt: orderInfo.CreatedAt,
Address: orderInfo.DestinationAddress,
Confirms: DefaultConfirmations,
}

log.Infof("Order Scheduler: verifying transaction with ID: %s", orderInfo.TxID)
verification, err := explorer.VerifyTransaction(verificationInfo)
if err != nil {
log.Error("error verifying transaction: ", err.Error())
return errors.E(op, err)
}
log.Infof("Order Scheduler: verifying transaction with ID: %s", orderInfo.TxID)
verification, err := explorer.VerifyTransaction(verificationInfo)
if err != nil {
log.Error("error verifying transaction: ", err.Error())
return errors.E(op, err)
}

amountReceivedMatch = verification.BlockExplorerAmount.ToCoin() == orderInfo.ReceiveAmount
if verification.Verified && !amountReceivedMatch {
if verification.Verified {
if verification.BlockExplorerAmount.ToCoin() != orderInfo.ReceiveAmount {
log.Infof("received amount: %f", verification.BlockExplorerAmount.ToCoin())
log.Infof("expected amount: %f", orderInfo.ReceiveAmount)
log.Error("received amount does not match the expected amount")
return errors.E(op, err)
}

isCompleted = verification.Verified && amountReceivedMatch
} else {
// Instantswap asset explorer not implemented, use order status
// to determine whether an order has been completed.
isCompleted = orderInfo.Status == api.OrderStatusCompleted
}

if isCompleted {
if isRefunded {
log.Info("order was refunded successfully")
} else {
Expand Down Expand Up @@ -367,7 +350,8 @@ func (mgr *AssetsManager) IsOrderSchedulerRunning() bool {
return mgr.InstantSwap.CancelOrderScheduler != nil
}

// GetShedulerRuntime returns the duration the order scheduler has been running.
func (mgr *AssetsManager) GetShedulerRuntime() string {
// GetSchedulerRuntime returns the duration the order scheduler has been
// running.
func (mgr *AssetsManager) GetSchedulerRuntime() string {
return time.Since(mgr.InstantSwap.SchedulerStartTime).Round(time.Second).String()
}
2 changes: 1 addition & 1 deletion ui/page/exchange/create_order_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,7 @@ func (pg *CreateOrderPage) orderSchedulerLayout(gtx C) D {
}.Layout(gtx, pg.Theme.Icons.TimerIcon.Layout12dp)
}),
layout.Rigid(func(gtx C) D {
title := pg.Theme.Label(textSize16, pg.AssetsManager.GetShedulerRuntime())
title := pg.Theme.Label(textSize16, pg.AssetsManager.GetSchedulerRuntime())
title.Color = pg.Theme.Color.GrayText2
return title.Layout(gtx)
}),
Expand Down

0 comments on commit 7e2dc3f

Please sign in to comment.