From c2b3f4faa2f3b9df129d4e242f7a3fb604d3ceb4 Mon Sep 17 00:00:00 2001 From: Brandon Berhent Date: Wed, 10 Jul 2024 09:21:11 -0400 Subject: [PATCH] Fix auto receive for unopened accounts --- libs/wallet/blocks.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/libs/wallet/blocks.go b/libs/wallet/blocks.go index 09cb5f3..4a07776 100644 --- a/libs/wallet/blocks.go +++ b/libs/wallet/blocks.go @@ -238,7 +238,32 @@ func (w *NanoWallet) createSendBlock(wallet *ent.Wallet, sender *ent.Account, am // Get account info accountInfo, err := w.RpcClient.MakeAccountInfoRequest(sender.Address) - if err != nil { + if errors.Is(err, nanorpc.ErrAccountNotFound) && w.Config.Wallet.AutoReceiveOnSend != nil && *w.Config.Wallet.AutoReceiveOnSend { + // See if account has a pending balance to open the accountt + bal, err := w.RpcClient.MakeAccountBalanceRequest(sender.Address) + if err != nil { + return nil, err + } + receivable, ok := big.NewInt(0).SetString(bal.Receivable, 10) + if !ok { + return nil, errors.New("Unable to parse receivable amount") + } + if receivable.Cmp(sendAmount) < 0 { + return nil, ErrInsufficientBalance + } + receivedCount, err := w.receiveAll(wallet, sender, bpowKey) + if err != nil { + return nil, err + } + if receivedCount == 0 { + return nil, ErrInsufficientBalance + } + // Re-get accountInfo + accountInfo, err = w.RpcClient.MakeAccountInfoRequest(sender.Address) + if err != nil { + return nil, err + } + } else if err != nil { return nil, err }