Skip to content

Commit

Permalink
WIP TestDepositBTCRefund
Browse files Browse the repository at this point in the history
  • Loading branch information
swift1337 committed Jun 18, 2024
1 parent 1ddaa2d commit ecb37c7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 80 deletions.
8 changes: 8 additions & 0 deletions e2e/e2etests/e2etests.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const (
Test transfer of Bitcoin asset across chains
*/
TestBitcoinDepositName = "bitcoin_deposit"
TestBitcoinDepositRefund = "bitcoin_deposit_refund"
TestBitcoinWithdrawSegWitName = "bitcoin_withdraw_segwit"
TestBitcoinWithdrawTaprootName = "bitcoin_withdraw_taproot"
TestBitcoinWithdrawLegacyName = "bitcoin_withdraw_legacy"
Expand Down Expand Up @@ -332,6 +333,13 @@ var AllE2ETests = []runner.E2ETest{
},
TestBitcoinDeposit,
),
runner.NewE2ETest(
TestBitcoinDepositRefund,
"deposit Bitcoin into ZEVM; expect refund", []runner.ArgDefinition{
{Description: "amount in btc", DefaultValue: "0.001"},
},
TestDepositBTCRefund,
),
runner.NewE2ETest(
TestBitcoinWithdrawSegWitName,
"withdraw BTC from ZEVM to a SegWit address",
Expand Down
38 changes: 12 additions & 26 deletions e2e/e2etests/helper_bitcoin.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package e2etests

import (
"fmt"
"math/big"
"strconv"

"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcutil"
"github.com/stretchr/testify/require"

"github.com/zeta-chain/zetacore/e2e/runner"
"github.com/zeta-chain/zetacore/e2e/utils"
Expand Down Expand Up @@ -55,50 +55,36 @@ func withdrawBTCZRC20(r *runner.E2ERunner, to btcutil.Address, amount *big.Int)
r.BTCZRC20Addr,
big.NewInt(amount.Int64()*2),
) // approve more to cover withdraw fee
if err != nil {
panic(err)
}
require.NoError(r, err)

receipt := utils.MustWaitForTxReceipt(r.Ctx, r.ZEVMClient, tx, r.Logger, r.ReceiptTimeout)
if receipt.Status != 1 {
panic(fmt.Errorf("approve receipt status is not 1"))
}
require.Equal(r, uint64(1), receipt.Status, "approve receipt status is not 1")

// mine blocks if testing on regnet
stop := r.MineBlocksIfLocalBitcoin()

// withdraw 'amount' of BTC from ZRC20 to BTC address
tx, err = r.BTCZRC20.Withdraw(r.ZEVMAuth, []byte(to.EncodeAddress()), amount)
if err != nil {
panic(err)
}
require.NoError(r, err)

receipt = utils.MustWaitForTxReceipt(r.Ctx, r.ZEVMClient, tx, r.Logger, r.ReceiptTimeout)
if receipt.Status != 1 {
panic(fmt.Errorf("withdraw receipt status is not 1"))
}
require.Equal(r, uint64(1), receipt.Status, "withdraw receipt status is not 1")

// mine 10 blocks to confirm the withdraw tx
// mine 10 blocks to confirm the withdrawal tx
_, err = r.GenerateToAddressIfLocalBitcoin(10, to)
if err != nil {
panic(err)
}
require.NoError(r, err)

// get cctx and check status
cctx := utils.WaitCctxMinedByInboundHash(r.Ctx, receipt.TxHash.Hex(), r.CctxClient, r.Logger, r.CctxTimeout)
if cctx.CctxStatus.Status != crosschaintypes.CctxStatus_OutboundMined {
panic(fmt.Errorf("cctx status is not OutboundMined"))
}
require.Equal(r, crosschaintypes.CctxStatus_OutboundMined, cctx.CctxStatus.Status, "cctx status is not OutboundMined")

// get bitcoin tx according to the outTxHash in cctx
outTxHash := cctx.GetCurrentOutboundParam().Hash
hash, err := chainhash.NewHashFromStr(outTxHash)
if err != nil {
panic(err)
}
require.NoError(r, err)

rawTx, err := r.BtcRPCClient.GetRawTransactionVerbose(hash)
if err != nil {
panic(err)
}
require.NoError(r, err)
r.Logger.Info("raw tx:")
r.Logger.Info(" TxIn: %d", len(rawTx.Vin))
for idx, txIn := range rawTx.Vin {
Expand Down
93 changes: 39 additions & 54 deletions e2e/e2etests/test_bitcoin_deposit_refund.go
Original file line number Diff line number Diff line change
@@ -1,56 +1,41 @@
package e2etests

// DepositBTCRefund ...
// TODO: define e2e test
// https://github.com/zeta-chain/node-private/issues/79
//func DepositBTCRefund(r *runner.E2ERunner) {
// r.Logger.InfoLoud("Deposit BTC with invalid memo; should be refunded")
// btc := r.BtcRPCClient
// utxos, err := r.BtcRPCClient.ListUnspent()
// if err != nil {
// panic(err)
// }
// spendableAmount := 0.0
// spendableUTXOs := 0
// for _, utxo := range utxos {
// if utxo.Spendable {
// spendableAmount += utxo.Amount
// spendableUTXOs++
// }
// }
// r.Logger.Info("ListUnspent:")
// r.Logger.Info(" spendableAmount: %f", spendableAmount)
// r.Logger.Info(" spendableUTXOs: %d", spendableUTXOs)
// r.Logger.Info("Now sending two txs to TSS address...")
// _, err = r.SendToTSSFromDeployerToDeposit(r.BTCTSSAddress, 1.1, utxos[:2], btc, r.BTCDeployerAddress)
// if err != nil {
// panic(err)
// }
// _, err = r.SendToTSSFromDeployerToDeposit(r.BTCTSSAddress, 0.05, utxos[2:4], btc, r.BTCDeployerAddress)
// if err != nil {
// panic(err)
// }
//
// r.Logger.Info("testing if the deposit into BTC ZRC20 is successful...")
//
// // check if the deposit is successful
// initialBalance, err := r.BTCZRC20.BalanceOf(&bind.CallOpts{}, r.DeployerAddress)
// if err != nil {
// panic(err)
// }
// for {
// time.Sleep(3 * time.Second)
// balance, err := r.BTCZRC20.BalanceOf(&bind.CallOpts{}, r.DeployerAddress)
// if err != nil {
// panic(err)
// }
// diff := big.NewInt(0)
// diff.Sub(balance, initialBalance)
// if diff.Cmp(big.NewInt(1.15*btcutil.SatoshiPerBitcoin)) != 0 {
// r.Logger.Info("waiting for BTC balance to show up in ZRC contract... current bal %d", balance)
// } else {
// r.Logger.Info("BTC balance is in ZRC contract! Success")
// break
// }
// }
//}
import (
"strconv"

"github.com/stretchr/testify/require"
"github.com/zeta-chain/zetacore/e2e/runner"
"github.com/zeta-chain/zetacore/e2e/utils"
)

func TestDepositBTCRefund(r *runner.E2ERunner, args []string) {
// ARRANGE
// Given amount to send
require.Len(r, args, 1)
amount := parseFloat(r, args[0])

// Given BTC address
r.SetBtcAddress(r.Name, false)

// Given a list of UTXOs
utxos, err := r.BtcRPCClient.ListUnspent()
require.NoError(r, err)
require.NotEmpty(r, utxos)

// ACT
// Send a single UTXO to TSS address
txHash, err := r.SendToTSSFromDeployerWithMemo(amount, utxos, []byte("gibberish-memo"))
require.NotEmpty(r, err)

// Wait for processing in zetaclient
utils.WaitCctxMinedByInboundHash(r.Ctx, txHash.String(), r.CctxClient, r.Logger, r.CctxTimeout)

// ASSERT
// todo...
}

func parseFloat(t require.TestingT, s string) float64 {
f, err := strconv.ParseFloat(s, 64)
require.NoError(t, err, "unable to parse float %q", s)
return f
}

0 comments on commit ecb37c7

Please sign in to comment.