Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: integrate token swap logic into WZCN burn command #316

Merged
merged 12 commits into from
Oct 26, 2023
47 changes: 40 additions & 7 deletions cmd/bridge-burn-eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cmd
import (
"context"
"fmt"
"github.com/0chain/common/core/currency"
"github.com/ethereum/go-ethereum/core/types"
"time"

"github.com/0chain/gosdk/zcnbridge"
Expand All @@ -24,17 +26,49 @@ func commandBurnEth(b *zcnbridge.BridgeClient, args ...*Arg) {
retries := GetRetries(args)
amount := GetAmount(args)

// Increase Allowance
tokenBalance, err := b.GetTokenBalance()
if err != nil {
ExitWithError(err, "failed to retrieve current token balance")
}

tokenBalanceZCN, err := currency.Coin(tokenBalance.Int64()).ToZCN()
if err != nil {
ExitWithError(err, "failed to convert current token balance to ZCN")
}

var (
transaction *types.Transaction
hash string
status int
)

if tokenBalanceZCN < float64(amount) {
transaction, err = b.Swap(context.Background(), amount, time.Now().Add(time.Minute*3))
if err != nil {
ExitWithError(err, "failed to execute Swap")
}

hash = transaction.Hash().Hex()
status, err = zcnbridge.ConfirmEthereumTransaction(hash, retries, time.Second)
if err != nil {
ExitWithError(fmt.Sprintf("Failed to confirm Swap: hash = %s, error = %v", hash, err))
}

if status == 1 {
fmt.Printf("Verification: Swap [OK]: %s\n", hash)
} else {
ExitWithError(fmt.Sprintf("Verification: Swap [FAILED]: %s\n", hash))
}
}

// Example: https://ropsten.etherscan.io/tx/0xa28266fb44cfc2aa27b26bd94e268e40d065a05b1a8e6339865f826557ff9f0e
fmt.Println("Starting IncreaseBurnerAllowance transaction")
transaction, err := b.IncreaseBurnerAllowance(context.Background(), zcnbridge.Wei(amount))
transaction, err = b.IncreaseBurnerAllowance(context.Background(), amount)
if err != nil {
ExitWithError(err, "failed to execute IncreaseBurnerAllowance")
}

hash := transaction.Hash().Hex()
status, err := zcnbridge.ConfirmEthereumTransaction(hash, retries, time.Second)
hash = transaction.Hash().Hex()
status, err = zcnbridge.ConfirmEthereumTransaction(hash, retries, time.Second)
if err != nil {
ExitWithError(fmt.Sprintf("Failed to confirm IncreaseBurnerAllowance: hash = %s, error = %v", hash, err))
}
Expand All @@ -45,9 +79,8 @@ func commandBurnEth(b *zcnbridge.BridgeClient, args ...*Arg) {
ExitWithError(fmt.Sprintf("Verification: IncreaseBurnerAllowance [FAILED]: %s\n", hash))
}

// Burn Eth

fmt.Println("Starting WZCN burn transaction")

transaction, err = b.BurnWZCN(context.Background(), amount)
if err != nil {
ExitWithError(err, "failed to burn WZCN tokens")
Expand Down
35 changes: 23 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module github.com/0chain/zwalletcli

require (
github.com/0chain/gosdk v1.10.1-0.20231016103614-0f61a49368d5
github.com/ethereum/go-ethereum v1.10.26
github.com/0chain/common v0.0.6-0.20230127095721-8df4d1d72565
github.com/0chain/gosdk v1.10.1-0.20231026205111-b15f9736c813
github.com/ethereum/go-ethereum v1.13.2
github.com/icza/bitio v1.1.0
github.com/olekukonko/tablewriter v0.0.5
github.com/pkg/errors v0.9.1
Expand All @@ -13,15 +14,20 @@ require (
)

require (
github.com/0chain/common v0.0.6-0.20230127095721-8df4d1d72565 // indirect
github.com/0chain/errors v1.0.3 // indirect
github.com/Luzifer/go-openssl/v3 v3.1.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/bits-and-blooms/bitset v1.9.0 // indirect
github.com/btcsuite/btcd v0.23.4 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/btcsuite/btcd/btcutil v1.1.3 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2 // indirect
github.com/deckarep/golang-set v1.8.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
github.com/consensys/bavard v0.1.13 // indirect
github.com/consensys/gnark-crypto v0.12.1 // indirect
github.com/crate-crypto/go-kzg-4844 v0.6.0 // indirect
github.com/deckarep/golang-set/v2 v2.1.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/ethereum/c-kzg-4844 v0.3.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-stack/stack v1.8.1 // indirect
Expand All @@ -33,6 +39,7 @@ require (
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/herumi/bls-go-binary v1.31.0 // indirect
github.com/holiman/uint256 v1.2.3 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/klauspost/reedsolomon v1.11.7 // indirect
Expand All @@ -42,17 +49,18 @@ require (
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/philhofer/fwd v1.1.2-0.20210722190033-5c56ac6d0bb9 // indirect
github.com/remeh/sizedwaitgroup v1.0.0 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/rjeczalik/notify v0.9.3 // indirect
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/supranational/blst v0.3.11 // indirect
github.com/tinylib/msgp v1.1.6 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
Expand All @@ -63,17 +71,20 @@ require (
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/exp v0.0.0-20230810033253-352e893a4cad // indirect
golang.org/x/mod v0.11.0 // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/tools v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
)

go 1.20

// temporary, for development
//replace github.com/0chain/gosdk => ../gosdk
// replace github.com/0chain/gosdk => ../gosdk
Loading
Loading