Skip to content

Commit

Permalink
without new return args
Browse files Browse the repository at this point in the history
  • Loading branch information
buck54321 authored and martonp committed Nov 27, 2023
1 parent 75cdf16 commit 2b1feea
Show file tree
Hide file tree
Showing 16 changed files with 99 additions and 73 deletions.
12 changes: 6 additions & 6 deletions client/asset/btc/btc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4209,23 +4209,23 @@ func (btc *baseWallet) EstimateRegistrationTxFee(feeRate uint64) uint64 {
// Withdraw withdraws funds to the specified address. Fees are subtracted from
// the value. feeRate is in units of sats/byte.
// Withdraw satisfies asset.Withdrawer.
func (btc *baseWallet) Withdraw(address string, value, feeRate uint64) (string, asset.Coin, error) {
func (btc *baseWallet) Withdraw(address string, value, feeRate uint64) (asset.Coin, error) {
txHash, vout, sent, err := btc.send(address, value, btc.feeRateWithFallback(feeRate), true)
if err != nil {
return "", nil, err
return nil, err
}
return txHash.String(), NewOutput(txHash, vout, sent), nil
return NewOutput(txHash, vout, sent), nil
}

// Send sends the exact value to the specified address. This is different from
// Withdraw, which subtracts the tx fees from the amount sent. feeRate is in
// units of sats/byte.
func (btc *baseWallet) Send(address string, value, feeRate uint64) (string, asset.Coin, error) {
func (btc *baseWallet) Send(address string, value, feeRate uint64) (asset.Coin, error) {
txHash, vout, sent, err := btc.send(address, value, btc.feeRateWithFallback(feeRate), false)
if err != nil {
return "", nil, err
return nil, err
}
return txHash.String(), NewOutput(txHash, vout, sent), nil
return NewOutput(txHash, vout, sent), nil
}

// SendTransaction broadcasts a valid fully-signed transaction.
Expand Down
6 changes: 3 additions & 3 deletions client/asset/btc/btc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3729,11 +3729,11 @@ func testSender(t *testing.T, senderType tSenderType, segwit bool, walletType st
wallet, node, shutdown := tNewWallet(segwit, walletType)
defer shutdown()
const feeSuggestion = 100
sender := func(addr string, val uint64) (string, asset.Coin, error) {
sender := func(addr string, val uint64) (asset.Coin, error) {
return wallet.Send(addr, val, defaultFee)
}
if senderType == tWithdrawSender {
sender = func(addr string, val uint64) (string, asset.Coin, error) {
sender = func(addr string, val uint64) (asset.Coin, error) {
return wallet.Withdraw(addr, val, feeSuggestion)
}
}
Expand Down Expand Up @@ -3895,7 +3895,7 @@ func testSender(t *testing.T, senderType tSenderType, segwit bool, walletType st
node.listUnspent = test.unspents
wallet.bondReserves.Store(test.bondReserves)

_, _, err := sender(addr.String(), test.val)
_, err := sender(addr.String(), test.val)
if test.expectErr {
if err == nil {
t.Fatalf("%s: no error for expected error", test.name)
Expand Down
4 changes: 2 additions & 2 deletions client/asset/btc/livetest/livetest.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ func Run(t *testing.T, cfg *Config) {

// Test Send.
tLogger.Info("Testing Send")
_, coin, err := rig.secondWallet.Send(address, cfg.LotSize, defaultFee)
coin, err := rig.secondWallet.Send(address, cfg.LotSize, defaultFee)
if err != nil {
t.Fatalf("error sending: %v", err)
}
Expand All @@ -573,7 +573,7 @@ func Run(t *testing.T, cfg *Config) {
// Test Withdraw.
withdrawer, _ := rig.secondWallet.Wallet.(asset.Withdrawer)
tLogger.Info("Testing Withdraw")
_, coin, err = withdrawer.Withdraw(address, cfg.LotSize, defaultFee)
coin, err = withdrawer.Withdraw(address, cfg.LotSize, defaultFee)
if err != nil {
t.Fatalf("error withdrawing: %v", err)
}
Expand Down
20 changes: 12 additions & 8 deletions client/asset/dcr/dcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,10 @@ func (op *output) ID() dex.Bytes {
return toCoinID(op.txHash(), op.vout())
}

func (op *output) TxID() string {
return op.txHash().String()
}

// String is a string representation of the coin.
func (op *output) String() string {
return op.pt.String()
Expand Down Expand Up @@ -4271,31 +4275,31 @@ func (dcr *ExchangeWallet) SendTransaction(rawTx []byte) ([]byte, error) {
// Withdraw withdraws funds to the specified address. Fees are subtracted from
// the value. feeRate is in units of atoms/byte.
// Withdraw satisfies asset.Withdrawer.
func (dcr *ExchangeWallet) Withdraw(address string, value, feeRate uint64) (string, asset.Coin, error) {
func (dcr *ExchangeWallet) Withdraw(address string, value, feeRate uint64) (asset.Coin, error) {
addr, err := stdaddr.DecodeAddress(address, dcr.chainParams)
if err != nil {
return "", nil, fmt.Errorf("invalid address: %s", address)
return nil, fmt.Errorf("invalid address: %s", address)
}
msgTx, sentVal, err := dcr.withdraw(addr, value, dcr.feeRateWithFallback(feeRate))
if err != nil {
return "", nil, err
return nil, err
}
return msgTx.CachedTxHash().String(), newOutput(msgTx.CachedTxHash(), 0, sentVal, wire.TxTreeRegular), nil
return newOutput(msgTx.CachedTxHash(), 0, sentVal, wire.TxTreeRegular), nil
}

// Send sends the exact value to the specified address. This is different from
// Withdraw, which subtracts the tx fees from the amount sent. feeRate is in
// units of atoms/byte.
func (dcr *ExchangeWallet) Send(address string, value, feeRate uint64) (string, asset.Coin, error) {
func (dcr *ExchangeWallet) Send(address string, value, feeRate uint64) (asset.Coin, error) {
addr, err := stdaddr.DecodeAddress(address, dcr.chainParams)
if err != nil {
return "", nil, fmt.Errorf("invalid address: %s", address)
return nil, fmt.Errorf("invalid address: %s", address)
}
msgTx, sentVal, err := dcr.sendToAddress(addr, value, dcr.feeRateWithFallback(feeRate))
if err != nil {
return "", nil, err
return nil, err
}
return msgTx.CachedTxHash().String(), newOutput(msgTx.CachedTxHash(), 0, sentVal, wire.TxTreeRegular), nil
return newOutput(msgTx.CachedTxHash(), 0, sentVal, wire.TxTreeRegular), nil
}

// ValidateSecret checks that the secret satisfies the contract.
Expand Down
12 changes: 6 additions & 6 deletions client/asset/dcr/dcr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3365,14 +3365,14 @@ func testSender(t *testing.T, senderType tSenderType) {
var unspentVal uint64 = 100e8
const feeSuggestion = 100
funName := "Send"
sender := func(addr string, val uint64) (string, asset.Coin, error) {
sender := func(addr string, val uint64) (asset.Coin, error) {
return wallet.Send(addr, val, feeSuggestion)
}
if senderType == tWithdrawSender {
funName = "Withdraw"
// For withdraw, test with unspent total = withdraw value
unspentVal = sendVal
sender = func(addr string, val uint64) (string, asset.Coin, error) {
sender = func(addr string, val uint64) (asset.Coin, error) {
return wallet.Withdraw(addr, val, feeSuggestion)
}
}
Expand All @@ -3391,29 +3391,29 @@ func testSender(t *testing.T, senderType tSenderType) {
}}
//node.unspent = append(node.unspent, node.unspent[0])

_, _, err := sender(addr, sendVal)
_, err := sender(addr, sendVal)
if err != nil {
t.Fatalf(funName+" error: %v", err)
}

// invalid address
_, _, err = sender("badaddr", sendVal)
_, err = sender("badaddr", sendVal)
if err == nil {
t.Fatalf("no error for bad address: %v", err)
}

// GetRawChangeAddress error
if senderType == tSendSender { // withdraw test does not get a change address
node.changeAddrErr = tErr
_, _, err = sender(addr, sendVal)
_, err = sender(addr, sendVal)
if err == nil {
t.Fatalf("no error for rawchangeaddress: %v", err)
}
node.changeAddrErr = nil
}

// good again
_, _, err = sender(addr, sendVal)
_, err = sender(addr, sendVal)
if err != nil {
t.Fatalf(funName+" error afterwards: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions client/asset/dcr/simnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,14 +571,14 @@ func runTest(t *testing.T, splitTx bool) {
}

// Test Send
_, coin, err := rig.beta().Send(alphaAddress, 1e8, defaultFee)
coin, err := rig.beta().Send(alphaAddress, 1e8, defaultFee)
if err != nil {
t.Fatalf("error sending fees: %v", err)
}
tLogger.Infof("fee paid with tx %s", coin.String())

// Test Withdraw
_, coin, err = rig.beta().Withdraw(alphaAddress, 5e7, tDCR.MaxFeeRate/4)
coin, err = rig.beta().Withdraw(alphaAddress, 5e7, tDCR.MaxFeeRate/4)
if err != nil {
t.Fatalf("error withdrawing: %v", err)
}
Expand Down
24 changes: 14 additions & 10 deletions client/asset/eth/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,10 @@ func (c *coin) ID() dex.Bytes {
return c.id[:]
}

func (c *coin) TxID() string {
return c.String()
}

// String is a string representation of the coin.
func (c *coin) String() string {
return c.id.String()
Expand Down Expand Up @@ -3144,14 +3148,14 @@ func (w *assetWallet) SwapConfirmations(ctx context.Context, coinID dex.Bytes, c

// Send sends the exact value to the specified address. The provided fee rate is
// ignored since all sends will use an internally derived fee rate.
func (w *ETHWallet) Send(addr string, value, _ uint64) (string, asset.Coin, error) {
func (w *ETHWallet) Send(addr string, value, _ uint64) (asset.Coin, error) {
if err := isValidSend(addr, value, false); err != nil {
return "", nil, err
return nil, err
}

maxFee, maxFeeRate, err := w.canSend(value, true, false)
if err != nil {
return "", nil, err
return nil, err
}
// TODO: Subtract option.
// if avail < value+maxFee {
Expand All @@ -3160,37 +3164,37 @@ func (w *ETHWallet) Send(addr string, value, _ uint64) (string, asset.Coin, erro

tx, err := w.sendToAddr(common.HexToAddress(addr), value, maxFeeRate)
if err != nil {
return "", nil, err
return nil, err
}

txHash := tx.Hash()
w.addToTxHistory(tx.Nonce(), -int64(value), maxFee, 0, w.assetID, txHash[:], asset.Send)

return txHash.String(), &coin{id: txHash, value: value}, nil
return &coin{id: txHash, value: value}, nil
}

// Send sends the exact value to the specified address. Fees are taken from the
// parent wallet. The provided fee rate is ignored since all sends will use an
// internally derived fee rate.
func (w *TokenWallet) Send(addr string, value, _ uint64) (string, asset.Coin, error) {
func (w *TokenWallet) Send(addr string, value, _ uint64) (asset.Coin, error) {
if err := isValidSend(addr, value, false); err != nil {
return "", nil, err
return nil, err
}

maxFee, maxFeeRate, err := w.canSend(value, true, false)
if err != nil {
return "", nil, err
return nil, err
}

tx, err := w.sendToAddr(common.HexToAddress(addr), value, maxFeeRate)
if err != nil {
return "", nil, err
return nil, err
}

txHash := tx.Hash()
w.addToTxHistory(tx.Nonce(), -int64(value), maxFee, 0, w.assetID, txHash[:], asset.Send)

return txHash.String(), &coin{id: txHash, value: value}, nil
return &coin{id: txHash, value: value}, nil
}

// ValidateSecret checks that the secret satisfies the contract.
Expand Down
6 changes: 3 additions & 3 deletions client/asset/eth/eth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4340,7 +4340,7 @@ func testSend(t *testing.T, assetID uint32) {
node.tokenContractor.bal = dexeth.GweiToWei(val - test.sendAdj)
node.bal = dexeth.GweiToWei(tokenFees - test.feeAdj)
}
_, coin, err := w.Send(test.addr, val, 0)
coin, err := w.Send(test.addr, val, 0)
if test.wantErr {
if err == nil {
t.Fatalf("expected error for test %v", test.name)
Expand Down Expand Up @@ -5253,15 +5253,15 @@ func testEstimateVsActualSendFees(t *testing.T, assetID uint32) {
if err != nil {
t.Fatalf("error converting canSend to gwei: %v", err)
}
_, _, err = w.Send(testAddr, canSendGwei, 0)
_, err = w.Send(testAddr, canSendGwei, 0)
if err != nil {
t.Fatalf("error sending: %v", err)
}
} else {
tokenVal := uint64(10e9)
node.tokenContractor.bal = dexeth.GweiToWei(tokenVal)
node.bal = dexeth.GweiToWei(fee)
_, _, err = w.Send(testAddr, tokenVal, 0)
_, err = w.Send(testAddr, tokenVal, 0)
if err != nil {
t.Fatalf("error sending: %v", err)
}
Expand Down
4 changes: 4 additions & 0 deletions client/asset/eth/fundingcoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func (c *fundingCoin) ID() dex.Bytes {
return []byte(c.addr.String())
}

func (c *fundingCoin) TxID() string {
return ""
}

// Value returns the value reserved in the funding coin.
func (c *fundingCoin) Value() uint64 {
return c.amt
Expand Down
8 changes: 6 additions & 2 deletions client/asset/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ type Wallet interface {
TransactionConfirmations(ctx context.Context, txID string) (confs uint32, err error)
// Send sends the exact value to the specified address. This is different
// from Withdraw, which subtracts the tx fees from the amount sent.
Send(address string, value, feeRate uint64) (txID string, coin Coin, err error)
Send(address string, value, feeRate uint64) (coin Coin, err error)
// EstimateRegistrationTxFee returns an estimate for the tx fee needed to
// pay the registration fee using the provided feeRate.
EstimateRegistrationTxFee(feeRate uint64) uint64
Expand Down Expand Up @@ -643,7 +643,7 @@ type Recoverer interface {
type Withdrawer interface {
// Withdraw withdraws funds to the specified address. Fees are subtracted
// from the value.
Withdraw(address string, value, feeRate uint64) (txID string, coin Coin, err error)
Withdraw(address string, value, feeRate uint64) (coin Coin, err error)
}

// Sweeper is a wallet that can clear the entire balance of the wallet/account
Expand Down Expand Up @@ -1222,6 +1222,10 @@ type Coin interface {
Value() uint64
}

type TxCoin interface {
TxID() string
}

type RecoveryCoin interface {
// RecoveryID is an ID that can be used to re-establish funding state during
// startup. If a Coin implements RecoveryCoin, the RecoveryID will be used
Expand Down
6 changes: 3 additions & 3 deletions client/asset/zec/zec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2115,12 +2115,12 @@ func (w *zecWallet) EstimateSendTxFee(
return
}

func (w *zecWallet) Send(address string, value, feeRate uint64) (string, asset.Coin, error) {
func (w *zecWallet) Send(address string, value, feeRate uint64) (asset.Coin, error) {
txHash, vout, sent, err := w.send(address, value, false)
if err != nil {
return "", nil, err
return nil, err
}
return txHash.String(), btc.NewOutput(txHash, vout, sent), nil
return btc.NewOutput(txHash, vout, sent), nil
}

// TransactionConfirmations gets the number of confirmations for the specified
Expand Down
15 changes: 10 additions & 5 deletions client/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -4335,7 +4335,7 @@ func (c *Core) Register(form *RegisterForm) (*RegisterResult, error) {
"Do NOT manually send funds to this address even if this fails.",
regRes.Address, dc.acct.id, regRes.Fee, regFeeAssetSymbol)
feeRate := c.feeSuggestionAny(feeAsset.ID, dc)
_, coin, err := wallet.Send(regRes.Address, regRes.Fee, feeRate)
coin, err := wallet.Send(regRes.Address, regRes.Fee, feeRate)
if err != nil {
return nil, newError(feeSendErr, "error paying registration fee: %w", err)
}
Expand Down Expand Up @@ -5486,13 +5486,12 @@ func (c *Core) Send(pw []byte, assetID uint32, value uint64, address string, sub
}

var coin asset.Coin
var txID string
feeSuggestion := c.feeSuggestionAny(assetID)
if !subtract {
txID, coin, err = wallet.Wallet.Send(address, value, feeSuggestion)
coin, err = wallet.Wallet.Send(address, value, feeSuggestion)
} else {
if withdrawer, isWithdrawer := wallet.Wallet.(asset.Withdrawer); isWithdrawer {
txID, coin, err = withdrawer.Withdraw(address, value, feeSuggestion)
coin, err = withdrawer.Withdraw(address, value, feeSuggestion)
} else {
return "", nil, fmt.Errorf("wallet does not support subtracting network fee from withdraw amount")
}
Expand All @@ -5508,7 +5507,13 @@ func (c *Core) Send(pw []byte, assetID uint32, value uint64, address string, sub
c.notify(newSendNote(TopicSendSuccess, subject, details, db.Success))

c.updateAssetBalance(assetID)
return txID, coin, nil

txCoin, is := coin.(asset.TxCoin)
if !is {
return "", nil, fmt.Errorf("Send successful, but returned coin is not a TxCoin")
}

return txCoin.TxID(), coin, nil
}

// TransactionConfirmations returns the number of confirmations of
Expand Down
Loading

0 comments on commit 2b1feea

Please sign in to comment.