Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fireblocks] Add a way to cancel transaction #198

Merged
merged 2 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions chainio/clients/fireblocks/cancel_transaction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package fireblocks

import (
"context"
"encoding/json"
"fmt"
"strings"
)

type CancelTransactionResponse struct {
Success bool `json:"success"`
}

func (f *client) CancelTransaction(ctx context.Context, txID string) (bool, error) {
f.logger.Debug("Fireblocks cancel transaction", "txID", txID)
url := fmt.Sprintf("/v1/transactions/%s/cancel", txID)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this is a path, not a url

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

res, err := f.makeRequest(ctx, "POST", url, nil)
if err != nil {
return false, fmt.Errorf("error making request: %w", err)
}
var response CancelTransactionResponse
err = json.NewDecoder(strings.NewReader(string(res))).Decode(&response)
if err != nil {
return false, fmt.Errorf("error parsing response body: %w", err)
}

return response.Success, nil
}
7 changes: 5 additions & 2 deletions chainio/clients/fireblocks/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ var AssetIDByChain = map[uint64]AssetID{
17000: AssetIDHolETH, // holesky
}

type FireblocksTxID string

type Client interface {
// ContractCall makes a ContractCall request to the Fireblocks API.
// It signs and broadcasts a transaction and returns the transaction ID and status.
// ref: https://developers.fireblocks.com/reference/post_transactions
ContractCall(ctx context.Context, body *ContractCallRequest) (*ContractCallResponse, error)
// CancelTransaction makes a CancelTransaction request to the Fireblocks API
// It cancels a transaction by its transaction ID.
// It returns true if the transaction was successfully canceled.
// ref: https://developers.fireblocks.com/reference/post_transactions-txid-cancel
CancelTransaction(ctx context.Context, txID string) (bool, error)
// ListContracts makes a ListContracts request to the Fireblocks API
// It returns a list of whitelisted contracts and their assets for the account.
// This call is used to get the contract ID for a whitelisted contract, which is needed as destination account ID by NewContractCallRequest in a ContractCall
Expand Down
10 changes: 10 additions & 0 deletions chainio/clients/fireblocks/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ func TestContractCall(t *testing.T) {
t.Logf("txID: %s, status: %s", resp.ID, resp.Status)
}

func TestCancelTransaction(t *testing.T) {
t.Skip("skipping test as it's meant for manual runs only")

c := newFireblocksClient(t)
txID := "FILL_ME_IN"
success, err := c.CancelTransaction(context.Background(), txID)
assert.NoError(t, err)
t.Logf("txID: %s, success: %t", txID, success)
}

func TestListVaultAccounts(t *testing.T) {
t.Skip("skipping test as it's meant for manual runs only")

Expand Down
15 changes: 15 additions & 0 deletions chainio/clients/mocks/fireblocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions chainio/clients/wallet/fireblocks_wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ func (t *fireblocksWallet) SendTransaction(ctx context.Context, tx *types.Transa
return res.ID, nil
}

func (t *fireblocksWallet) CancelTransactionBroadcast(ctx context.Context, txID TxID) (bool, error) {
return t.fireblocksClient.CancelTransaction(ctx, string(txID))
}

func (t *fireblocksWallet) GetTransactionReceipt(ctx context.Context, txID TxID) (*types.Receipt, error) {
fireblockTx, err := t.fireblocksClient.GetTransaction(ctx, txID)
if err != nil {
Expand Down
Loading