bitcoin

package
v1.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 14, 2024 License: MIT Imports: 47 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BridgeDepositServiceName = "BitcoinBridgeDepositService"
	BatchDepositWaitTimeout  = 10 * time.Second
	DepositErrTimeout        = 20 * time.Second
	BatchDepositLimit        = 100
	WaitMinedTimeout         = 20 * time.Minute
	HandleDepositTimeout     = 1 * time.Second
	DepositRetry             = 10 // temp fix, Increase retry times
)
View Source
const (
	BridgeWithdrawServiceName = "BitcoinBridgeWithdrawService"
	WithdrawHandleTime        = 10
	WithdrawTXConfirmTime     = 60 * 5
)
View Source
const (
	// tx type
	TxTypeTransfer = "transfer" // btc transfer
	TxTypeWithdraw = "withdraw" // btc withdraw
)
View Source
const (
	ServiceName = "BitcoinIndexerService"

	NewBlockWaitTimeout = 60 * time.Second

	IndexTxTimeout    = 100 * time.Millisecond
	IndexBlockTimeout = 2 * time.Second
)
View Source
const (
	TransferServiceName      = "BitcoinBridgeTransferService"
	BatchTransferWaitTimeout = 60 * time.Second
)

Variables

View Source
var (
	ErrBridgeDepositTxHashExist                 = errors.New("non-repeatable processing")
	ErrBridgeDepositContractInsufficientBalance = errors.New("insufficient balance")
	ErrBridgeWaitMinedStatus                    = errors.New("tx wait mined status failed")
	ErrBridgeFromGasInsufficient                = errors.New("gas required exceeds allowanc")
	ErrAAAddressNotFound                        = errors.New("address not found")
	ErrOldNonceToHeight                         = errors.New("old nonce params to height")
	ErrBridgeBtcAddressWhiteList                = errors.New("whitelist btc address")
)
View Source
var (
	ErrParsePkScript         = errors.New("parse pkscript err")
	ErrDecodeListenAddress   = errors.New("decode listen address err")
	ErrTargetConfirmations   = errors.New("target confirmation number was not reached")
	ErrParsePubKey           = errors.New("parse pubkey failed, not found pubkey or nonsupport ")
	ErrParsePkScriptNullData = errors.New("parse pkscript null data err")
)
View Source
var ErrServerStop = errors.New("server stop")

Functions

This section is empty.

Types

type B2ExplorerStatus

type B2ExplorerStatus struct {
	GasPrices struct {
		Fast    float64 `json:"fast"`
		Slow    float64 `json:"slow"`
		Average float64 `json:"average"`
	} `json:"gas_prices"`
}

type Bridge

type Bridge struct {
	EthRPCURL            string
	EthPrivKey           *ecdsa.PrivateKey
	ContractAddress      common.Address
	ABI                  string
	BaseGasPriceMultiple int64
	B2ExplorerURL        string

	// aa server
	AAPubKeyAPI string
	// whitelist not bridge
	BtcAddressWhiteList []string
	// contains filtered or unexported fields
}

Bridge bridge TODO: only L1 -> L2, More calls may be supported later

func NewBridge

func NewBridge(bridgeCfg config.BridgeConfig, abiFileDir string, log log.Logger, bitcoinParam *chaincfg.Params) (*Bridge, error)

NewBridge new bridge

func (*Bridge) ABIPack

func (b *Bridge) ABIPack(abiData string, method string, args ...interface{}) ([]byte, error)

ABIPack the given method name to conform the ABI. Method call's data

func (*Bridge) BitcoinAddressToEthAddress

func (b *Bridge) BitcoinAddressToEthAddress(bitcoinAddress b2types.BitcoinFrom) (string, error)

BitcoinAddressToEthAddress bitcoin address to eth address

func (*Bridge) Deposit

func (b *Bridge) Deposit(
	hash string,
	bitcoinAddress b2types.BitcoinFrom,
	amount int64,
	oldTx *types.Transaction,
	nonce uint64,
	resetNonce bool,
) (*types.Transaction, []byte, string, string, error)

Deposit to ethereum

func (*Bridge) EnableEoaTransfer

func (b *Bridge) EnableEoaTransfer() bool

func (*Bridge) FromAddress

func (b *Bridge) FromAddress() string

func (*Bridge) TransactionByHash

func (b *Bridge) TransactionByHash(hash string) (*types.Transaction, bool, error)

func (*Bridge) TransactionReceipt

func (b *Bridge) TransactionReceipt(hash string) (*types.Receipt, error)

func (*Bridge) Transfer

func (b *Bridge) Transfer(bitcoinAddress b2types.BitcoinFrom,
	amount int64,
	oldTx *types.Transaction,
	nonce uint64,
	resetNonce bool,
) (*types.Transaction, string, error)

Transfer to ethereum TODO: temp handle, future remove

func (*Bridge) WaitMined

func (b *Bridge) WaitMined(ctx context.Context, tx *types.Transaction, _ []byte) (*types.Receipt, error)

WaitMined wait tx mined

type BridgeDepositService

type BridgeDepositService struct {
	service.BaseService
	// contains filtered or unexported fields
}

BridgeDepositService l1->l2

func NewBridgeDepositService

func NewBridgeDepositService(
	bridge types.BITCOINBridge,
	btcIndexer types.BITCOINTxIndexer,
	db *gorm.DB,
	logger log.Logger,
) *BridgeDepositService

NewBridgeDepositService returns a new service instance.

func (*BridgeDepositService) CheckDeposit

func (bis *BridgeDepositService) CheckDeposit()

func (*BridgeDepositService) Deposit

func (bis *BridgeDepositService) Deposit()

func (*BridgeDepositService) EoaTransfer

func (bis *BridgeDepositService) EoaTransfer(deposit model.Deposit, oldTx *ethTypes.Transaction, nonce uint64, resetNonce bool) error

func (*BridgeDepositService) HandleDeposit

func (bis *BridgeDepositService) HandleDeposit(deposit model.Deposit, oldTx *ethTypes.Transaction, nonce uint64, resetNonce bool) error

func (*BridgeDepositService) HandleEoaTransfer

func (bis *BridgeDepositService) HandleEoaTransfer() error

func (*BridgeDepositService) HandleUnconfirmedDeposit

func (bis *BridgeDepositService) HandleUnconfirmedDeposit(deposit model.Deposit) error

HandleUnconfirmedDeposit 1. tx mined, update status 2. tx not mined, isPending, need reset gasprice 3. tx not mined, tx not mempool, need retry send tx

func (*BridgeDepositService) HandleUnconfirmedEoa

func (bis *BridgeDepositService) HandleUnconfirmedEoa(deposit model.Deposit) error

HandleUnconfirmedEoa

func (*BridgeDepositService) OnStart

func (bis *BridgeDepositService) OnStart() error

OnStart

func (*BridgeDepositService) OnStop

func (bis *BridgeDepositService) OnStop()

func (*BridgeDepositService) UnconfirmedDeposit

func (bis *BridgeDepositService) UnconfirmedDeposit() error

func (*BridgeDepositService) UnconfirmedEoa

func (bis *BridgeDepositService) UnconfirmedEoa() error

func (*BridgeDepositService) WaitMined

func (bis *BridgeDepositService) WaitMined(ctx1 context.Context, b2Tx *ethTypes.Transaction, deposit model.Deposit) error

type BridgeWithdrawService

type BridgeWithdrawService struct {
	service.BaseService
	// contains filtered or unexported fields
}

BridgeWithdrawService indexes transactions for json-rpc service.

func NewBridgeWithdrawService

func NewBridgeWithdrawService(
	btcCli *rpcclient.Client,
	ethCli *ethclient.Client,
	config *config.BitcoinConfig,
	db *gorm.DB,
	auditDB *gorm.DB,
	log log.Logger,
) *BridgeWithdrawService

NewBridgeWithdrawService returns a new service instance.

func (*BridgeWithdrawService) HandleWithdraw added in v1.1.0

func (bis *BridgeWithdrawService) HandleWithdraw()

OnStart implements service.Service

func (*BridgeWithdrawService) OnStart

func (bis *BridgeWithdrawService) OnStart() error

OnStart implements service.Service

func (*BridgeWithdrawService) OnStop added in v1.1.0

func (bis *BridgeWithdrawService) OnStop()

type DepositData

type DepositData struct {
	Caller      string `json:"caller"`
	ToAddress   string `json:"to_address"`
	Amount      string `json:"amount"`
	Timestamp   int64  `json:"timestamp"`
	BlockNumber int64  `json:"block_number"`
	LogIndex    int    `json:"log_index"`
	TxHash      string `json:"tx_hash"`
}

type EpsResponse

type EpsResponse struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

type EpsService

type EpsService struct {
	EthRPCURL string
	// contains filtered or unexported fields
}

EpsService eps service

func NewEpsService

func NewEpsService(
	bridgeCfg config.BridgeConfig,
	config config.EpsConfig,
	log log.Logger,
	db *gorm.DB,
) (*EpsService, error)

NewEpsService new eps

func (*EpsService) Deposit

func (e *EpsService) Deposit(data DepositData) error

func (*EpsService) GetTransactionByHash

func (e *EpsService) GetTransactionByHash(txHash string) (*EthTransactionResponse, error)

func (*EpsService) OnStart

func (e *EpsService) OnStart() error

type EthRequest

type EthRequest struct {
	Jsonrpc string   `json:"jsonrpc"`
	Method  string   `json:"method"`
	Params  []string `json:"params"`
	ID      int      `json:"id"`
}

type EthResponse

type EthResponse struct {
	Jsonrpc string                 `json:"jsonrpc"`
	ID      int                    `json:"id"`
	Result  EthTransactionResponse `json:"result"`
}

type EthTransactionResponse

type EthTransactionResponse struct {
	BlockNumber      string `json:"blockNumber"`
	From             string `json:"from"`
	To               string `json:"to"`
	TransactionIndex string `json:"transactionIndex"`
	Value            string `json:"value"`
}

type Indexer

type Indexer struct {
	// contains filtered or unexported fields
}

Indexer bitcoin indexer, parse and forward data

func NewBitcoinIndexer

func NewBitcoinIndexer(
	log log.Logger,
	client *rpcclient.Client,
	chainParams *chaincfg.Params,
	listenAddress string,
	targetConfirmations uint64,
) (*Indexer, error)

NewBitcoinIndexer new bitcoin indexer

func (*Indexer) BlockChainInfo

func (b *Indexer) BlockChainInfo() (*btcjson.GetBlockChainInfoResult, error)

BlockChainInfo get block chain info

func (*Indexer) CheckConfirmations

func (b *Indexer) CheckConfirmations(hash string) error

func (*Indexer) LatestBlock

func (b *Indexer) LatestBlock() (int64, error)

LatestBlock get latest block height in the longest block chain.

func (*Indexer) ParseAddress

func (b *Indexer) ParseAddress(pkScript []byte) (string, error)

parseAddress from pkscript parse address

func (*Indexer) ParseBlock

func (b *Indexer) ParseBlock(height int64, txIndex int64) ([]*types.BitcoinTxParseResult, *wire.BlockHeader, error)

ParseBlock parse block data by block height NOTE: Currently, only transfer transactions are supported.

type IndexerService

type IndexerService struct {
	service.BaseService
	// contains filtered or unexported fields
}

IndexerService indexes transactions for json-rpc service.

func NewIndexerService

func NewIndexerService(
	txIdxr types.BITCOINTxIndexer,

	db *gorm.DB,
	logger log.Logger,
) *IndexerService

NewIndexerService returns a new service instance.

func (*IndexerService) HandleResults

func (bis *IndexerService) HandleResults(
	txResults []*types.BitcoinTxParseResult,
	btcIndex model.BtcIndex,
	btcBlockTime time.Time,
	currentBlock int64,
) (int64, int64, error)

func (*IndexerService) OnStart

func (bis *IndexerService) OnStart() error

OnStart

func (*IndexerService) SaveParsedResult

func (bis *IndexerService) SaveParsedResult(
	parseResult *types.BitcoinTxParseResult,
	btcBlockNumber int64,
	b2TxStatus int,
	btcBlockTime time.Time,
	btcIndex model.BtcIndex,
) error

save index tx to db

func (*IndexerService) ToInFroms

func (bis *IndexerService) ToInFroms(a []types.BitcoinFrom, s string) bool

type TransferService added in v1.1.0

type TransferService struct {
	service.BaseService
	// contains filtered or unexported fields
}

TransferService for btc transfer

func NewTransferService added in v1.1.0

func NewTransferService(
	cfg *config.TransferConfig,
	db *gorm.DB,
	log log.Logger,
	sinohopeAPI features.TransactionAPI,
) *TransferService

NewTransferService returns a new service instance.

func (*TransferService) GetFeeRate added in v1.1.0

func (bis *TransferService) GetFeeRate() (*model.FeeRates, error)

func (*TransferService) GetMempoolURL added in v1.1.0

func (bis *TransferService) GetMempoolURL() string

func (*TransferService) HandleTransfer added in v1.1.0

func (bis *TransferService) HandleTransfer()

func (*TransferService) IsBTCLiveNetAddress added in v1.1.0

func (bis *TransferService) IsBTCLiveNetAddress(address string) bool

func (*TransferService) IsBTCTestNetAddress added in v1.1.0

func (bis *TransferService) IsBTCTestNetAddress(address string) bool

func (*TransferService) OnStart added in v1.1.0

func (bis *TransferService) OnStart() error

OnStart implements service.Service

func (*TransferService) OnStop added in v1.1.0

func (bis *TransferService) OnStop()

func (*TransferService) QueryTransactionsByRequestIDs added in v1.1.0

func (bis *TransferService) QueryTransactionsByRequestIDs(requestID string) (bool, error)

func (*TransferService) Transfer added in v1.1.0

func (bis *TransferService) Transfer(requestID string, to string, amount string) (*common.CreateSettlementTxResData, string, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL