/
swap.go
135 lines (102 loc) · 2.51 KB
/
swap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2023 The AthanorLabs/atomic-swap Authors
// SPDX-License-Identifier: LGPL-3.0-only
package rpcclient
import (
"fmt"
"github.com/athanorlabs/atomic-swap/common/types"
"github.com/athanorlabs/atomic-swap/rpc"
)
// GetOngoingSwap calls swap_getOngoing
func (c *Client) GetOngoingSwap(id *types.Hash) (*rpc.GetOngoingResponse, error) {
const (
method = "swap_getOngoing"
)
req := &rpc.GetOngoingRequest{
OfferID: id,
}
res := &rpc.GetOngoingResponse{}
if err := c.post(method, req, res); err != nil {
return nil, err
}
return res, nil
}
// GetPastSwap calls swap_getPast
func (c *Client) GetPastSwap(id *types.Hash) (*rpc.GetPastResponse, error) {
const (
method = "swap_getPast"
)
req := &rpc.GetPastRequest{
OfferID: id,
}
res := &rpc.GetPastResponse{}
if err := c.post(method, req, res); err != nil {
return nil, err
}
return res, nil
}
// GetStatus calls swap_getStatus
func (c *Client) GetStatus(id types.Hash) (*rpc.GetStatusResponse, error) {
const (
method = "swap_getStatus"
)
req := &rpc.GetStatusRequest{
ID: id,
}
res := &rpc.GetStatusResponse{}
if err := c.post(method, req, res); err != nil {
return nil, err
}
return res, nil
}
// ClearOffers calls swap_clearOffers
func (c *Client) ClearOffers(offerIDs []types.Hash) error {
const (
method = "swap_clearOffers"
)
req := &rpc.ClearOffersRequest{
OfferIDs: offerIDs,
}
if err := c.post(method, req, nil); err != nil {
return fmt.Errorf("failed to call %s: %w", method, err)
}
return nil
}
// Claim calls swap_claim
func (c *Client) Claim(offerID types.Hash) (*rpc.ManualTransactionResponse, error) {
const (
method = "swap_claim"
)
req := &rpc.ManualTransactionRequest{
OfferID: offerID,
}
res := &rpc.ManualTransactionResponse{}
if err := c.post(method, req, res); err != nil {
return nil, err
}
return res, nil
}
// Refund calls swap_refund
func (c *Client) Refund(offerID types.Hash) (*rpc.ManualTransactionResponse, error) {
const (
method = "swap_refund"
)
req := &rpc.ManualTransactionRequest{
OfferID: offerID,
}
res := &rpc.ManualTransactionResponse{}
if err := c.post(method, req, res); err != nil {
return nil, err
}
return res, nil
}
// SuggestedExchangeRate calls swap_suggestedExchangeRate
func (c *Client) SuggestedExchangeRate() (*rpc.SuggestedExchangeRateResponse, error) {
const (
method = "swap_suggestedExchangeRate"
)
res := &rpc.SuggestedExchangeRateResponse{}
if err := c.post(method, nil, res); err != nil {
return nil, err
}
return res, nil
}