本文整理匯總了Golang中github.com/skycoin/skycoin-exchange/src/server/engine.Exchange.GetBtcFee方法的典型用法代碼示例。如果您正苦於以下問題:Golang Exchange.GetBtcFee方法的具體用法?Golang Exchange.GetBtcFee怎麽用?Golang Exchange.GetBtcFee使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/skycoin/skycoin-exchange/src/server/engine.Exchange
的用法示例。
在下文中一共展示了Exchange.GetBtcFee方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: createBtcWithdrawTx
// createBtcWithdrawTx create withdraw transaction.
// amount is the number of coins that want to withdraw.
// toAddr is the address that the coins will be sent to.
func createBtcWithdrawTx(egn engine.Exchange, amount uint64, toAddr string) (*BtcTxResult, error) {
uxs, err := egn.ChooseUtxos(bitcoin.Type, amount+egn.GetBtcFee(), ChooseUtxoTm)
if err != nil {
return nil, err
}
utxos := uxs.([]bitcoin.Utxo)
for _, u := range utxos {
logger.Debug("using utxos: txid:%s vout:%d addr:%s", u.GetTxid(), u.GetVout(), u.GetAddress())
}
var success bool
defer func() {
if !success {
// put utxos back to pool if withdraw failed.
go func() { egn.PutUtxos(bitcoin.Type, utxos) }()
}
}()
var totalAmounts uint64
for _, u := range utxos {
totalAmounts += u.GetAmount()
}
fee := egn.GetBtcFee()
outAddrs := []bitcoin.TxOut{}
chgAmt := totalAmounts - fee - amount
chgAddr := ""
if chgAmt > 0 {
// generate a change address
chgAddr = egn.GetNewAddress(bitcoin.Type)
outAddrs = append(outAddrs,
bitcoin.TxOut{Addr: toAddr, Value: amount},
bitcoin.TxOut{Addr: chgAddr, Value: chgAmt})
} else {
outAddrs = append(outAddrs, bitcoin.TxOut{Addr: toAddr, Value: amount})
}
// change utxo to UtxoWithkey
utxoKeys, err := makeBtcUtxoWithkeys(utxos, egn)
if err != nil {
return nil, err
}
logger.Debug("creating transaction...")
tx, err := bitcoin.NewTransaction(utxoKeys, outAddrs)
if err != nil {
logger.Error(err.Error())
return nil, err
}
success = true
rlt := BtcTxResult{
Tx: tx,
UsingUtxos: utxos[:],
ChangeAddr: chgAddr,
}
return &rlt, nil
}
示例2: createBtcTxInOut
func createBtcTxInOut(ee engine.Exchange, a account.Accounter, amount uint64, outAddr string) (*txInOutResult, error) {
var rlt txInOutResult
// verify the outAddr
if _, err := cipher.BitcoinDecodeBase58Address(outAddr); err != nil {
return nil, errors.New("invalid bitcoin address")
}
var err error
// decrease balance and check if the balance is sufficient.
if err := a.DecreaseBalance("bitcoin", amount+ee.GetBtcFee()); err != nil {
return nil, err
}
var utxos []bitcoin.Utxo
// choose sufficient utxos.
uxs, err := ee.ChooseUtxos("bitcoin", amount+ee.GetBtcFee(), ChooseUtxoTm)
if err != nil {
return nil, err
}
utxos = uxs.([]bitcoin.Utxo)
for _, u := range utxos {
logger.Debug("using utxos: txid:%s vout:%d addr:%s", u.GetTxid(), u.GetVout(), u.GetAddress())
rlt.TxIns = append(rlt.TxIns, coin.TxIn{
Txid: u.GetTxid(),
Vout: u.GetVout(),
})
}
var totalAmounts uint64
for _, u := range utxos {
totalAmounts += u.GetAmount()
}
fee := ee.GetBtcFee()
txOuts := []bitcoin.TxOut{}
chgAmt := totalAmounts - fee - amount
chgAddr := ""
if chgAmt > 0 {
// generate a change address
chgAddr = ee.GetNewAddress(bitcoin.Type)
txOuts = append(txOuts,
bitcoin.TxOut{Addr: outAddr, Value: amount},
bitcoin.TxOut{Addr: chgAddr, Value: chgAmt})
} else {
txOuts = append(txOuts, bitcoin.TxOut{Addr: outAddr, Value: amount})
}
rlt.TxOuts = txOuts
rlt.Teardown = func() {
a.IncreaseBalance(bitcoin.Type, amount+ee.GetBtcFee())
ee.PutUtxos(bitcoin.Type, utxos)
}
return &rlt, nil
}