github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/v2/rest/funding.go (about) 1 package rest 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "path" 7 8 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/common" 9 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/fundingcredit" 10 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/fundingloan" 11 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/fundingoffer" 12 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/fundingtrade" 13 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/notification" 14 ) 15 16 // KeepFundingRequest - data structure for constructing keep funding request payload 17 type KeepFundingRequest struct { 18 Type string `json:"type"` 19 ID int `json:"id"` 20 } 21 22 // FundingService manages the Funding endpoint. 23 type FundingService struct { 24 requestFactory 25 Synchronous 26 } 27 28 // Retreive all of the active fundign offers 29 // see https://docs.bitfinex.com/reference#rest-auth-funding-offers for more info 30 func (fs *FundingService) Offers(symbol string) (*fundingoffer.Snapshot, error) { 31 req, err := fs.requestFactory.NewAuthenticatedRequest(common.PermissionRead, path.Join("funding/offers", symbol)) 32 if err != nil { 33 return nil, err 34 } 35 raw, err := fs.Request(req) 36 if err != nil { 37 return nil, err 38 } 39 offers, err := fundingoffer.SnapshotFromRaw(raw) 40 if err != nil { 41 return nil, err 42 } 43 return offers, nil 44 } 45 46 // Retreive all of the past in-active funding offers 47 // see https://docs.bitfinex.com/reference#rest-auth-funding-offers-hist for more info 48 func (fs *FundingService) OfferHistory(symbol string) (*fundingoffer.Snapshot, error) { 49 req, err := fs.requestFactory.NewAuthenticatedRequest(common.PermissionRead, path.Join("funding/offers", symbol, "hist")) 50 if err != nil { 51 return nil, err 52 } 53 raw, err := fs.Request(req) 54 if err != nil { 55 return nil, err 56 } 57 offers, err := fundingoffer.SnapshotFromRaw(raw) 58 if err != nil { 59 return nil, err 60 } 61 return offers, nil 62 } 63 64 // Retreive all of the active funding loans 65 // see https://docs.bitfinex.com/reference#rest-auth-funding-loans for more info 66 func (fs *FundingService) Loans(symbol string) (*fundingloan.Snapshot, error) { 67 req, err := fs.requestFactory.NewAuthenticatedRequest(common.PermissionRead, path.Join("funding/loans", symbol)) 68 if err != nil { 69 return nil, err 70 } 71 raw, err := fs.Request(req) 72 if err != nil { 73 return nil, err 74 } 75 loans, err := fundingloan.SnapshotFromRaw(raw) 76 if err != nil { 77 return nil, err 78 } 79 return loans, nil 80 } 81 82 // Retreive all of the past in-active funding loans 83 // see https://docs.bitfinex.com/reference#rest-auth-funding-loans-hist for more info 84 func (fs *FundingService) LoansHistory(symbol string) (*fundingloan.Snapshot, error) { 85 req, err := fs.requestFactory.NewAuthenticatedRequest(common.PermissionRead, path.Join("funding/loans", symbol, "hist")) 86 if err != nil { 87 return nil, err 88 } 89 raw, err := fs.Request(req) 90 if err != nil { 91 return nil, err 92 } 93 loans, err := fundingloan.SnapshotFromRaw(raw) 94 if err != nil { 95 return nil, err 96 } 97 return loans, nil 98 } 99 100 // Retreive all of the active credits used in positions 101 // see https://docs.bitfinex.com/reference#rest-auth-funding-credits for more info 102 func (fs *FundingService) Credits(symbol string) (*fundingcredit.Snapshot, error) { 103 req, err := fs.requestFactory.NewAuthenticatedRequest(common.PermissionRead, path.Join("funding/credits", symbol)) 104 if err != nil { 105 return nil, err 106 } 107 raw, err := fs.Request(req) 108 if err != nil { 109 return nil, err 110 } 111 loans, err := fundingcredit.SnapshotFromRaw(raw) 112 if err != nil { 113 return nil, err 114 } 115 return loans, nil 116 } 117 118 // Retreive all of the past in-active credits used in positions 119 // see https://docs.bitfinex.com/reference#rest-auth-funding-credits-hist for more info 120 func (fs *FundingService) CreditsHistory(symbol string) (*fundingcredit.Snapshot, error) { 121 req, err := fs.requestFactory.NewAuthenticatedRequest(common.PermissionRead, path.Join("funding/credits", symbol, "hist")) 122 if err != nil { 123 return nil, err 124 } 125 raw, err := fs.Request(req) 126 if err != nil { 127 return nil, err 128 } 129 loans, err := fundingcredit.SnapshotFromRaw(raw) 130 if err != nil { 131 return nil, err 132 } 133 return loans, nil 134 } 135 136 // Retreive all of the matched funding trades 137 // see https://docs.bitfinex.com/reference#rest-auth-funding-trades-hist for more info 138 func (fs *FundingService) Trades(symbol string) (*fundingtrade.Snapshot, error) { 139 req, err := fs.requestFactory.NewAuthenticatedRequest(common.PermissionRead, path.Join("funding/trades", symbol, "hist")) 140 if err != nil { 141 return nil, err 142 } 143 144 raw, err := fs.Request(req) 145 if err != nil { 146 return nil, err 147 } 148 149 fts, err := fundingtrade.SnapshotFromRaw(raw) 150 if err != nil { 151 return nil, err 152 } 153 154 return fts, nil 155 } 156 157 // Submits a request to create a new funding offer 158 // see https://docs.bitfinex.com/reference#submit-funding-offer for more info 159 func (fs *FundingService) SubmitOffer(fo *fundingoffer.SubmitRequest) (*notification.Notification, error) { 160 bytes, err := fo.ToJSON() 161 if err != nil { 162 return nil, err 163 } 164 req, err := fs.requestFactory.NewAuthenticatedRequestWithBytes(common.PermissionWrite, path.Join("funding/offer/submit"), bytes) 165 if err != nil { 166 return nil, err 167 } 168 raw, err := fs.Request(req) 169 if err != nil { 170 return nil, err 171 } 172 return notification.FromRaw(raw) 173 } 174 175 // Submits a request to cancel the given offer 176 // see https://docs.bitfinex.com/reference#cancel-funding-offer for more info 177 func (fs *FundingService) CancelOffer(fc *fundingoffer.CancelRequest) (*notification.Notification, error) { 178 bytes, err := fc.ToJSON() 179 if err != nil { 180 return nil, err 181 } 182 req, err := fs.requestFactory.NewAuthenticatedRequestWithBytes(common.PermissionWrite, "funding/offer/cancel", bytes) 183 if err != nil { 184 return nil, err 185 } 186 raw, err := fs.Request(req) 187 if err != nil { 188 return nil, err 189 } 190 return notification.FromRaw(raw) 191 } 192 193 // KeepFunding - toggle to keep funding taken. Specify loan for unused funding and credit for used funding. 194 // see https://docs.bitfinex.com/reference#rest-auth-keep-funding for more info 195 func (fs *FundingService) KeepFunding(args KeepFundingRequest) (*notification.Notification, error) { 196 if args.Type != "credit" && args.Type != "loan" { 197 return nil, fmt.Errorf("Expected type: credit or loan, got: %s", args.Type) 198 } 199 200 bytes, err := json.Marshal(args) 201 if err != nil { 202 return nil, err 203 } 204 205 req, err := fs.requestFactory.NewAuthenticatedRequestWithBytes( 206 common.PermissionWrite, 207 path.Join("funding", "keep"), 208 bytes, 209 ) 210 if err != nil { 211 return nil, err 212 } 213 214 raw, err := fs.Request(req) 215 if err != nil { 216 return nil, err 217 } 218 219 return notification.FromRaw(raw) 220 }