Skip to content

Commit d6f6f24

Browse files
committed
backend: add SendTransaction implementation for blockbook.
1 parent 52ee531 commit d6f6f24

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

backend/coins/eth/blockbook/blockbook.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@ import (
2929
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/eth/rpcclient"
3030
ethtypes "github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/eth/types"
3131
"github.com/BitBoxSwiss/bitbox-wallet-app/util/errp"
32+
"github.com/BitBoxSwiss/bitbox-wallet-app/util/logging"
3233
"github.com/ethereum/go-ethereum"
3334
"github.com/ethereum/go-ethereum/common"
35+
"github.com/ethereum/go-ethereum/common/hexutil"
3436
"github.com/ethereum/go-ethereum/core/types"
37+
"github.com/sirupsen/logrus"
3538
"golang.org/x/time/rate"
3639
)
3740

@@ -45,6 +48,8 @@ type Blockbook struct {
4548
url string
4649
httpClient *http.Client
4750
limiter *rate.Limiter
51+
// TODO remove before merging into master?
52+
log *logrus.Entry
4853
}
4954

5055
// NewBlockbook creates a new instance of EtherScan.
@@ -54,6 +59,7 @@ func NewBlockbook(chainId string, httpClient *http.Client) *Blockbook {
5459
url: "https://bb1.shiftcrypto.io/api/",
5560
httpClient: httpClient,
5661
limiter: rate.NewLimiter(rate.Limit(callsPerSec), 1),
62+
log: logging.Get().WithField("ETH Client", "Blockbook"),
5763
}
5864
}
5965

@@ -129,7 +135,33 @@ func (blockbook *Blockbook) Transactions(blockTipHeight *big.Int, address common
129135

130136
// SendTransaction implements rpc.Interface.
131137
func (blockbook *Blockbook) SendTransaction(ctx context.Context, tx *types.Transaction) error {
132-
return fmt.Errorf("Not yet implemented")
138+
params := url.Values{}
139+
140+
result := struct {
141+
Txid string `json:"result,omitempty"`
142+
Error struct {
143+
Message string `json:"message,omitempty"`
144+
} `json:"error,omitempty"`
145+
}{}
146+
147+
encodedTx, err := tx.MarshalBinary()
148+
if err != nil {
149+
blockbook.log.Errorf("Failed to marshal transaction: %v", err)
150+
return errp.WithStack(err)
151+
}
152+
153+
if err := blockbook.call(ctx, path.Join("sendtx", hexutil.Encode(encodedTx)), params, &result); err != nil {
154+
blockbook.log.Errorf("Failed to send transaction: %v", err)
155+
return errp.WithStack(err)
156+
}
157+
158+
if result.Error.Message != "" {
159+
blockbook.log.Errorf("Error sending transaction: %s", result.Error.Message)
160+
return errp.Newf("error sending transaction: %s", result.Error.Message)
161+
}
162+
163+
blockbook.log.Infof("Transaction sent: %s", result.Txid)
164+
return nil
133165
}
134166

135167
// ERC20Balance implements rpc.Interface.

0 commit comments

Comments
 (0)