github.com/algorand/go-algorand-sdk@v1.24.0/client/algod/wrappers.go (about) 1 package algod 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 "io" 8 "io/ioutil" 9 "net/http" 10 "strings" 11 12 "github.com/algorand/go-algorand-sdk/client/algod/models" 13 "github.com/algorand/go-algorand-sdk/types" 14 ) 15 16 // Status retrieves the StatusResponse from the running node 17 // the StatusResponse includes data like the consensus version and current round 18 // Deprecated: v1 algod client is deprecated, please use the v2 algod client. 19 func (client Client) Status(headers ...*Header) (response models.NodeStatus, err error) { 20 err = client.get(&response, "/status", nil, headers) 21 return 22 } 23 24 // HealthCheck does a health check on the the potentially running node, 25 // returning an error if the API is down 26 // Deprecated: v1 algod client is deprecated, please use the v2 algod client. 27 func (client Client) HealthCheck(headers ...*Header) error { 28 return client.get(nil, "/health", nil, headers) 29 } 30 31 // StatusAfterBlock waits for a block to occur then returns the StatusResponse after that block 32 // blocks on the node end 33 // Deprecated: v1 algod client is deprecated, please use the v2 algod client. 34 func (client Client) StatusAfterBlock(blockNum uint64, headers ...*Header) (response models.NodeStatus, err error) { 35 err = client.get(&response, fmt.Sprintf("/status/wait-for-block-after/%d", blockNum), nil, headers) 36 return 37 } 38 39 // Deprecated: v1 algod client is deprecated, please use the v2 algod client. 40 type pendingTransactionsParams struct { 41 Max uint64 `url:"max"` 42 } 43 44 // GetPendingTransactions asks algod for a snapshot of current pending txns on the node, bounded by maxTxns. 45 // If maxTxns = 0, fetches as many transactions as possible. 46 // Deprecated: v1 algod client is deprecated, please use the v2 algod client. 47 func (client Client) GetPendingTransactions(maxTxns uint64, headers ...*Header) (response models.PendingTransactions, err error) { 48 err = client.get(&response, fmt.Sprintf("/transactions/pending"), pendingTransactionsParams{maxTxns}, headers) 49 return 50 } 51 52 // Versions retrieves the VersionResponse from the running node 53 // the VersionResponse includes data like version number and genesis ID 54 // Deprecated: v1 algod client is deprecated, please use the v2 algod client. 55 func (client Client) Versions(headers ...*Header) (response models.Version, err error) { 56 err = client.get(&response, "/versions", nil, headers) 57 return 58 } 59 60 // LedgerSupply gets the supply details for the specified node's Ledger 61 // Deprecated: v1 algod client is deprecated, please use the v2 algod client. 62 func (client Client) LedgerSupply(headers ...*Header) (response models.Supply, err error) { 63 err = client.get(&response, "/ledger/supply", nil, headers) 64 return 65 } 66 67 // Deprecated: v1 algod client is deprecated, please use the v2 algod client. 68 type transactionsByAddrParams struct { 69 FirstRound uint64 `url:"firstRound,omitempty"` 70 LastRound uint64 `url:"lastRound,omitempty"` 71 FromDate string `url:"fromDate,omitempty"` 72 ToDate string `url:"toDate,omitempty"` 73 Max uint64 `url:"max,omitempty"` 74 } 75 76 // TransactionsByAddr returns all transactions for a PK [addr] in the [first, 77 // last] rounds range. 78 // Deprecated: v1 algod client is deprecated, please use the v2 algod client. 79 func (client Client) TransactionsByAddr(addr string, first, last uint64, headers ...*Header) (response models.TransactionList, err error) { 80 params := transactionsByAddrParams{FirstRound: first, LastRound: last} 81 err = client.get(&response, fmt.Sprintf("/account/%s/transactions", addr), params, headers) 82 return 83 } 84 85 // TransactionsByAddrLimit returns the last [limit] number of transaction for a PK [addr]. 86 // Deprecated: v1 algod client is deprecated, please use the v2 algod client. 87 func (client Client) TransactionsByAddrLimit(addr string, limit uint64, headers ...*Header) (response models.TransactionList, err error) { 88 params := transactionsByAddrParams{Max: limit} 89 err = client.get(&response, fmt.Sprintf("/account/%s/transactions", addr), params, headers) 90 return 91 } 92 93 // TransactionsByAddrForDate returns all transactions for a PK [addr] in the [first, 94 // last] date range. Dates are of the form "2006-01-02". 95 // Deprecated: v1 algod client is deprecated, please use the v2 algod client. 96 func (client Client) TransactionsByAddrForDate(addr string, first, last string, headers ...*Header) (response models.TransactionList, err error) { 97 params := transactionsByAddrParams{FromDate: first, ToDate: last} 98 err = client.get(&response, fmt.Sprintf("/account/%s/transactions", addr), params, headers) 99 return 100 } 101 102 // AccountInformation also gets the AccountInformationResponse associated with the passed address 103 // Deprecated: v1 algod client is deprecated, please use the v2 algod client. 104 func (client Client) AccountInformation(address string, headers ...*Header) (response models.Account, err error) { 105 err = client.get(&response, fmt.Sprintf("/account/%s", address), nil, headers) 106 return 107 } 108 109 // AssetInformation also gets the AssetInformationResponse associated with the passed asset creator and index 110 // Deprecated: v1 algod client is deprecated, please use the v2 algod client. 111 func (client Client) AssetInformation(index uint64, headers ...*Header) (response models.AssetParams, err error) { 112 err = client.get(&response, fmt.Sprintf("/asset/%d", index), nil, headers) 113 return 114 } 115 116 // TransactionInformation gets information about a specific transaction involving a specific account 117 // it will only return information about transactions submitted to the node queried 118 // Deprecated: v1 algod client is deprecated, please use the v2 algod client. 119 func (client Client) TransactionInformation(accountAddress, transactionID string, headers ...*Header) (response models.Transaction, err error) { 120 transactionID = stripTransaction(transactionID) 121 err = client.get(&response, fmt.Sprintf("/account/%s/transaction/%s", accountAddress, transactionID), nil, headers) 122 return 123 } 124 125 // PendingTransactionInformation gets information about a recently issued 126 // transaction. There are several cases when this might succeed: 127 // 128 // - transaction committed (CommittedRound > 0) 129 // - transaction still in the pool (CommittedRound = 0, PoolError = "") 130 // - transaction removed from pool due to error (CommittedRound = 0, PoolError != "") 131 // 132 // Or the transaction may have happened sufficiently long ago that the 133 // node no longer remembers it, and this will return an error. 134 // Deprecated: v1 algod client is deprecated, please use the v2 algod client. 135 func (client Client) PendingTransactionInformation(transactionID string, headers ...*Header) (response models.Transaction, err error) { 136 transactionID = stripTransaction(transactionID) 137 err = client.get(&response, fmt.Sprintf("/transactions/pending/%s", transactionID), nil, headers) 138 return 139 } 140 141 // TransactionByID gets a transaction by its ID. Works only if the indexer is enabled on the node 142 // being queried. 143 // Deprecated: v1 algod client is deprecated, please use the v2 algod client. 144 func (client Client) TransactionByID(transactionID string, headers ...*Header) (response models.Transaction, err error) { 145 transactionID = stripTransaction(transactionID) 146 err = client.get(&response, fmt.Sprintf("/transaction/%s", transactionID), nil, headers) 147 return 148 } 149 150 // SuggestedFee gets the recommended transaction fee from the node 151 // Deprecated: v1 algod client is deprecated, please use the v2 algod client. 152 func (client Client) SuggestedFee(headers ...*Header) (response models.TransactionFee, err error) { 153 err = client.get(&response, "/transactions/fee", nil, headers) 154 return 155 } 156 157 // SuggestedParams gets the suggested transaction parameters 158 // Deprecated: v1 algod client is deprecated, please use the v2 algod client. 159 func (client Client) SuggestedParams(headers ...*Header) (response models.TransactionParams, err error) { 160 err = client.get(&response, "/transactions/params", nil, headers) 161 return 162 } 163 164 // BuildSuggestedParams gets the suggested transaction parameters and 165 // builds a types.SuggestedParams to pass to transaction builders (see package future) 166 // Deprecated: v1 algod client is deprecated, please use the v2 algod client. 167 func (client Client) BuildSuggestedParams(headers ...*Header) (response types.SuggestedParams, err error) { 168 var httpResponse models.TransactionParams 169 err = client.get(&httpResponse, "/transactions/params", nil, headers) 170 response.FlatFee = false 171 response.Fee = types.MicroAlgos(httpResponse.Fee) 172 response.GenesisID = httpResponse.GenesisID 173 response.GenesisHash = httpResponse.GenesisHash 174 response.FirstRoundValid = types.Round(httpResponse.LastRound) 175 response.LastRoundValid = types.Round(httpResponse.LastRound + 1000) 176 response.ConsensusVersion = httpResponse.ConsensusVersion 177 return 178 } 179 180 // SendRawTransaction gets the bytes of a SignedTxn and broadcasts it to the network 181 // Deprecated: v1 algod client is deprecated, please use the v2 algod client. 182 func (client Client) SendRawTransaction(stx []byte, headers ...*Header) (response models.TransactionID, err error) { 183 // Set default Content-Type, if not the user didn't specify it. 184 addContentType := true 185 for _, header := range headers { 186 if strings.ToLower(header.Key) == "content-type" { 187 addContentType = false 188 break 189 } 190 } 191 if addContentType { 192 headers = append(headers, &Header{"Content-Type", "application/x-binary"}) 193 } 194 err = client.post(&response, "/transactions", stx, headers) 195 return 196 } 197 198 // Block gets the block info for the given round 199 // Deprecated: v1 algod client is deprecated, please use the v2 algod client. 200 func (client Client) Block(round uint64, headers ...*Header) (response models.Block, err error) { 201 err = client.get(&response, fmt.Sprintf("/block/%d", round), nil, headers) 202 return 203 } 204 205 // Deprecated: v1 algod client is deprecated, please use the v2 algod client. 206 func responseReadAll(resp *http.Response, maxContentLength int64) (body []byte, err error) { 207 if resp.ContentLength > 0 { 208 // more efficient path if we know the ContentLength 209 if maxContentLength > 0 && resp.ContentLength > maxContentLength { 210 return nil, errors.New("Content too long") 211 } 212 body = make([]byte, resp.ContentLength) 213 _, err = io.ReadFull(resp.Body, body) 214 return 215 } 216 217 return ioutil.ReadAll(resp.Body) 218 } 219 220 // BlockRaw gets the raw block msgpack bytes for the given round 221 // Deprecated: v1 algod client is deprecated, please use the v2 algod client. 222 func (client Client) BlockRaw(round uint64, headers ...*Header) (blockbytes []byte, err error) { 223 var resp *http.Response 224 request := struct { 225 Raw string `url:"raw"` 226 }{Raw: "1"} 227 resp, err = client.submitFormRaw(fmt.Sprintf("/block/%d", round), request, "GET", false, headers) 228 if err != nil { 229 return 230 } 231 defer resp.Body.Close() 232 // Current blocks are about 1MB. 10MB should be a safe backstop. 233 return responseReadAll(resp, 10000000) 234 } 235 236 // Deprecated: v1 algod client is deprecated, please use the v2 algod client. 237 func (client Client) doGetWithQuery(ctx context.Context, path string, queryArgs map[string]string) (result string, err error) { 238 queryURL := client.serverURL 239 queryURL.Path = path 240 241 req, err := http.NewRequest("GET", queryURL.String(), nil) 242 if err != nil { 243 return 244 } 245 q := req.URL.Query() 246 for k, v := range queryArgs { 247 q.Add(k, v) 248 } 249 req.URL.RawQuery = q.Encode() 250 251 req.Header.Set(authHeader, client.apiToken) 252 for _, header := range client.headers { 253 req.Header.Add(header.Key, header.Value) 254 } 255 256 httpClient := http.Client{} 257 resp, err := httpClient.Do(req.WithContext(ctx)) 258 if err != nil { 259 return 260 } 261 defer resp.Body.Close() 262 263 err = extractError(resp) 264 if err != nil { 265 return 266 } 267 268 bytes, err := ioutil.ReadAll(resp.Body) 269 if err != nil { 270 return 271 } 272 result = string(bytes) 273 return 274 }