-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: web3 and cosmos basic rest calls
- Loading branch information
Showing
31 changed files
with
13,376 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
v20.12.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.phony: build docs | ||
|
||
dev-docs: | ||
@source /opt/homebrew/opt/nvm/nvm.sh && nvm use && npm run docs:dev | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Vivi [Hanchon's crypto toolkit] | ||
|
||
Here is a list of tools to avoid rewritting the same code one million times. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Client | ||
|
||
## Builder | ||
|
||
To interact with web3 and cosmos endpoints we need a client: | ||
|
||
```go | ||
client := requester.NewClient() | ||
``` | ||
|
||
:::info | ||
The client defaults to Evmos Mainnet | ||
::: | ||
|
||
The client exposes a set of setters to change the default endpoints: | ||
|
||
- `WithUnsecureWeb3Endpoint(endpoint string)` | ||
- `WithUnsecureCosmosRestEndpoint(endpoint string)` | ||
- `WithSecureWeb3Endpoint(endpoint string, auth string)` | ||
- `WithSecureCosmosRestEndpoint(endpoint string, auth string)` | ||
|
||
:::info | ||
The auth string should be the string that goes after the `Authorization` string in the HTTP request, i.e., `Bearer 1337` | ||
::: | ||
|
||
## General Requests | ||
|
||
### Get | ||
|
||
There are two functions exposed, they are mostly used internally: | ||
|
||
- `c.SendGetRequestEasyJSON(endpoint, url, res, auth)`: It uses a `easyjson.Unmarshaller` to quickly decode the response | ||
- `c.SendGetRequest(endpoint, url, auth)`: It returns the response as a byte array if the status code is 200 | ||
|
||
### Post | ||
|
||
There is a function exposed, it is mostly used internally: | ||
|
||
- `c.SendPostRequestEasyJSON(endpoint, url, res, auth)`: It uses a `easyjson.Unmarshaller` to quickly decode the response | ||
|
||
:::info | ||
To unmarshall responses in a quick way with `easyjson`, it requires running the program against the file that has the struct and it'll create the `_easyjson.go` next to it with the required functions. | ||
|
||
```sh | ||
easyjson --all lib/types/cosmos/block_cosmos.go | ||
``` | ||
|
||
::: | ||
|
||
## Blockchain Requests | ||
|
||
- [Web3 Requests](/lib/requester/web3) | ||
- [Cosmos Requests](/lib/requester/cosmos) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Cosmos Requester | ||
|
||
List of supported rest calls to the Cosmos Rest endpoint | ||
|
||
## GetBlockByNumber | ||
|
||
Sending the height returns the block data. | ||
|
||
```go | ||
c.GetBlockCosmos(height) | ||
``` | ||
|
||
## GetTransaction | ||
|
||
Sending the transaction hash returns the transaction data | ||
|
||
```go | ||
c.GetCosmosTx(hash string) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Web3 Requester | ||
|
||
List of supported rest calls to the Web3 endpoint | ||
|
||
## GetBlockByNumber | ||
|
||
Sending the height and if you need the transactions or not will return the block data. | ||
|
||
```go | ||
c.GetBlockByNumber(height, withTransactions) | ||
``` | ||
|
||
## GetTransactionTrace | ||
|
||
Defaults the tracer to `callTracer` | ||
|
||
```go | ||
c.GetTransactionTrace(hash) | ||
``` | ||
|
||
## GetTransactionReceipt | ||
|
||
Get the receipt for a given transaction hash | ||
|
||
```go | ||
c.GetTransactionReceipt(hash) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package requester | ||
|
||
import ( | ||
cosmostypes "github.com/hanchon/vivi/lib/types/cosmos" | ||
) | ||
|
||
func (c *Client) GetBlockCosmos(height string) (*cosmostypes.CosmosBlockResult, error) { | ||
// TODO: add pagination support | ||
var result cosmostypes.CosmosBlockResult | ||
return &result, c.SendGetRequestEasyJSON( | ||
c.CosmosRestEndpoint, | ||
"/cosmos/tx/v1beta1/txs/block/"+height+"?pagination.count_total=true", | ||
&result, | ||
c.CosmosRestAuth, | ||
) | ||
} | ||
|
||
func (c *Client) GetCosmosTx(hash string) (*cosmostypes.TxRestResponseForEvents, error) { | ||
// TODO: add the 0x prefix if not included in the hash string | ||
var result cosmostypes.TxRestResponseForEvents | ||
return &result, c.SendGetRequestEasyJSON( | ||
c.CosmosRestEndpoint, | ||
"/cosmos/tx/v1beta1/txs/"+hash, | ||
&result, | ||
c.CosmosRestAuth, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package requester | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/mailru/easyjson" | ||
"github.com/valyala/fasthttp" | ||
) | ||
|
||
func (c *Client) SendGetRequestEasyJSON(endpoint string, url string, res easyjson.Unmarshaler, auth string) error { | ||
req := fasthttp.AcquireRequest() | ||
req.SetRequestURI(endpoint + url) | ||
req.Header.SetMethod(fasthttp.MethodGet) | ||
if auth != "" { | ||
req.Header.Add("Authorization", auth) | ||
} | ||
resp := fasthttp.AcquireResponse() | ||
err := c.Client.Do(req, resp) | ||
fasthttp.ReleaseRequest(req) | ||
defer fasthttp.ReleaseResponse(resp) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if resp.StatusCode() != http.StatusOK { | ||
return errors.New("incorrect status code: " + strconv.Itoa(resp.StatusCode())) | ||
} | ||
|
||
return easyjson.Unmarshal(resp.Body(), res) | ||
} | ||
|
||
func (c *Client) SendGetRequest(endpoint string, url string, auth string) ([]byte, error) { | ||
req := fasthttp.AcquireRequest() | ||
req.SetRequestURI(endpoint + url) | ||
req.Header.SetMethod(fasthttp.MethodGet) | ||
if auth != "" { | ||
req.Header.Add("Authorization", auth) | ||
} | ||
resp := fasthttp.AcquireResponse() | ||
err := c.Client.Do(req, resp) | ||
fasthttp.ReleaseRequest(req) | ||
defer fasthttp.ReleaseResponse(resp) | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
|
||
if resp.StatusCode() != http.StatusOK { | ||
return []byte{}, errors.New("incorrect status code: " + strconv.Itoa(resp.StatusCode())) | ||
} | ||
|
||
ret := make([]byte, len(resp.Body())) | ||
copy(ret, resp.Body()) | ||
return ret, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package requester | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/mailru/easyjson" | ||
"github.com/valyala/fasthttp" | ||
) | ||
|
||
func (c *Client) SendPostRequestEasyJSON(endpoint string, body []byte, res easyjson.Unmarshaler, auth string) error { | ||
req := fasthttp.AcquireRequest() | ||
req.SetRequestURI(endpoint) | ||
req.Header.SetMethod(fasthttp.MethodPost) | ||
req.Header.SetContentTypeBytes([]byte("application/json")) | ||
if auth != "" { | ||
req.Header.Add("Authorization", auth) | ||
} | ||
req.SetBodyRaw(body) | ||
resp := fasthttp.AcquireResponse() | ||
err := c.Client.DoTimeout(req, resp, c.Client.ReadTimeout) | ||
fasthttp.ReleaseRequest(req) | ||
defer fasthttp.ReleaseResponse(resp) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
statusCode := resp.StatusCode() | ||
if statusCode != http.StatusOK { | ||
return fmt.Errorf("status code is not ok: " + strconv.Itoa(statusCode)) | ||
} | ||
|
||
respBody := resp.Body() | ||
return easyjson.Unmarshal(respBody, res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package requester | ||
|
||
import ( | ||
"fmt" | ||
|
||
web3types "github.com/hanchon/vivi/lib/types/web3" | ||
) | ||
|
||
func (c *Client) GetBlockByNumber(height string, withTransactions bool) (*web3types.BlockByNumberWithTransactions, error) { | ||
var receipt web3types.BlockByNumberWithTransactions | ||
return &receipt, c.SendPostRequestEasyJSON( | ||
c.Web3Endpoint, | ||
[]byte(fmt.Sprintf(`{"method":"eth_getBlockByNumber","params":["%s",%t],"id":1,"jsonrpc":"2.0"}`, height, withTransactions)), | ||
&receipt, | ||
c.Web3Auth, | ||
) | ||
} | ||
|
||
func (c *Client) GetTransactionTrace(hash string) (*web3types.TraceTransactionResult, error) { | ||
var trace web3types.TraceTransactionResult | ||
return &trace, c.SendPostRequestEasyJSON( | ||
c.Web3Endpoint, | ||
[]byte(`{"method":"debug_traceTransaction","params":["`+hash+`", {"tracer": "callTracer"}],"id":1,"jsonrpc":"2.0"}`), | ||
&trace, | ||
c.Web3Auth, | ||
) | ||
} | ||
|
||
func (c *Client) GetTransactionReceipt(hash string) (*web3types.TxReceipt, error) { | ||
var receipt web3types.TxReceipt | ||
return &receipt, c.SendPostRequestEasyJSON( | ||
c.Web3Endpoint, | ||
[]byte(`{"method":"eth_getTransactionReceipt","params":["`+hash+`"],"id":1,"jsonrpc":"2.0"}`), | ||
&receipt, | ||
c.Web3Auth, | ||
) | ||
} |
Oops, something went wrong.