This repository has been archived by the owner on Jan 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from O3Labs/feature/newapi
better utxo api
- Loading branch information
Showing
11 changed files
with
272 additions
and
60 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
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
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,95 @@ | ||
package o3 | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
const apiEndpoint = "https://platform.o3.network/api" | ||
|
||
type NEONetWork string | ||
|
||
var NEOMainNet = "main" | ||
var NEOTestNet = "test" | ||
|
||
type O3APIInterface interface { | ||
GetNEOUTXO(address string) UTXOResponse | ||
GetNEOClimableGAS(address string) ClaimableGASResponse | ||
} | ||
|
||
type O3Client struct { | ||
APIBaseEndpoint url.URL | ||
neoNetwork string | ||
} | ||
|
||
func DefaultO3APIClient() *O3Client { | ||
u, err := url.Parse(apiEndpoint) | ||
if err != nil { | ||
return nil | ||
} | ||
return &O3Client{APIBaseEndpoint: *u} | ||
} | ||
|
||
func APIClientWithNEOTestnet() *O3Client { | ||
u, err := url.Parse(apiEndpoint) | ||
if err != nil { | ||
return nil | ||
} | ||
return &O3Client{APIBaseEndpoint: *u, neoNetwork: "test"} | ||
} | ||
|
||
//make sure all method interface is implemented | ||
var _ O3APIInterface = (*O3Client)(nil) | ||
|
||
func (n *O3Client) makeGETRequest(endpoint string, out interface{}) error { | ||
|
||
fullEndpointString := fmt.Sprintf("%v%v", n.APIBaseEndpoint.String(), endpoint) | ||
fullEndpoint, _ := url.Parse(fullEndpointString) | ||
|
||
if n.neoNetwork == "test" { | ||
log.Printf("network = test") | ||
q := fullEndpoint.Query() | ||
q.Set("network", n.neoNetwork) | ||
fullEndpoint.RawQuery = q.Encode() | ||
} | ||
|
||
log.Printf("%v", fullEndpoint.String()) | ||
|
||
req, err := http.NewRequest("GET", fullEndpoint.String(), nil) | ||
if err != nil { | ||
return err | ||
} | ||
req.Header.Add("content-type", "application/json") | ||
res, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
defer res.Body.Close() | ||
err = json.NewDecoder(res.Body).Decode(&out) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (o *O3Client) GetNEOUTXO(address string) UTXOResponse { | ||
response := UTXOResponse{} | ||
err := o.makeGETRequest(fmt.Sprintf("/v1/neo/%v/utxo", address), &response) | ||
if err != nil { | ||
return response | ||
} | ||
return response | ||
} | ||
func (o *O3Client) GetNEOClimableGAS(address string) ClaimableGASResponse { | ||
response := ClaimableGASResponse{} | ||
|
||
err := o.makeGETRequest(fmt.Sprintf("/v1/neo/%v/claimablegas", address), &response) | ||
if err != nil { | ||
return response | ||
} | ||
return response | ||
} |
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,38 @@ | ||
package o3_test | ||
|
||
import ( | ||
"log" | ||
"testing" | ||
|
||
"github.com/o3labs/neo-utils/neoutils/o3" | ||
) | ||
|
||
func TestO3NEOUTXO(t *testing.T) { | ||
client := o3.DefaultO3APIClient() | ||
response := client.GetNEOUTXO("ANk325vGG5kcc6Dcnk6zkoEBHY4E6es2nY") | ||
if response.Code != 200 { | ||
t.Fail() | ||
} | ||
log.Printf("%+v", response) | ||
} | ||
|
||
func TestO3NEOUTXOTestnet(t *testing.T) { | ||
client := o3.APIClientWithNEOTestnet() | ||
response := client.GetNEOUTXO("ANk325vGG5kcc6Dcnk6zkoEBHY4E6es2nY") | ||
if response.Code != 200 { | ||
t.Fail() | ||
} | ||
log.Printf("%+v", response) | ||
} | ||
|
||
func TestO3GetClaimableGAS(t *testing.T) { | ||
client := o3.DefaultO3APIClient() | ||
response := client.GetNEOClimableGAS("ANk325vGG5kcc6Dcnk6zkoEBHY4E6es2nY") | ||
if response.Code != 200 { | ||
t.Fail() | ||
} | ||
log.Printf("%+v", response.Result.Data.Gas) | ||
for _, v := range response.Result.Data.Claims { | ||
log.Printf("%+v", v) | ||
} | ||
} |
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,45 @@ | ||
package o3 | ||
|
||
type Response struct { | ||
Code int `json:"code"` | ||
} | ||
|
||
type ErrorResponse struct { | ||
Error struct { | ||
Message string `json:"message"` | ||
Type string `json:"type"` | ||
} `json:"error"` | ||
} | ||
|
||
type UTXOResponse struct { | ||
Response | ||
*ErrorResponse | ||
Result struct { | ||
Data []UTXOResultData `json:"data"` | ||
} `json:"result"` | ||
} | ||
|
||
type UTXOResultData struct { | ||
Asset string `json:"asset"` | ||
Index int `json:"index"` | ||
Txid string `json:"txid"` | ||
Value string `json:"value"` | ||
CreatedAtBlock int `json:"createdAtBlock"` | ||
} | ||
|
||
type ClaimableGASResponse struct { | ||
Response | ||
*ErrorResponse | ||
Result struct { | ||
Data struct { | ||
Gas string `json:"gas"` | ||
Claims []struct { | ||
Asset string `json:"asset"` | ||
Index int `json:"index"` | ||
Txid string `json:"txid"` | ||
Value string `json:"value"` | ||
CreatedAtBlock int `json:"createdAtBlock"` | ||
} `json:"claims"` | ||
} `json:"data"` | ||
} `json:"result"` | ||
} |
Oops, something went wrong.