Skip to content

Commit

Permalink
feature: add solscan (#9)
Browse files Browse the repository at this point in the history
Co-authored-by: colin <[email protected]>
  • Loading branch information
colin755 and ToBeBetter755 authored Sep 24, 2024
1 parent 4bae9c4 commit efa7cfa
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 1 deletion.
53 changes: 52 additions & 1 deletion explorer/base/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ type OklinkEnvelope struct {
Msg string `json:"msg"`
Data json.RawMessage `json:"data"`
}
type SolScanEnvelope struct {
Success bool `json:"success"`
Data json.RawMessage `json:"data"`
Errors struct {
Code int `json:"code"`
Message string `json:"message"`
} `json:"errors"`
}

type BaseClient struct {
coon *http.Client
Expand Down Expand Up @@ -113,7 +121,15 @@ func (bc *BaseClient) Call(name, module, action, apiUrl string, param map[string
}
req.Header.Set("Ok-Access-Key", bc.key)
}

if name == "solscan" {
fmt.Println("apiKey and name", "apiKey:", bc.key, "name:", name)
req, httpErr = http.NewRequest(http.MethodGet, bc.CraftSolScanURL(apiUrl, param), http.NoBody)
if httpErr != nil {
err = common.WrapErr(httpErr, "http.NewRequest")
return
}
req.Header.Set("token", bc.key)
}
if bc.Verbose {
var reqDump []byte
reqDump, err = httputil.DumpRequestOut(req, false)
Expand Down Expand Up @@ -164,6 +180,11 @@ func (bc *BaseClient) Call(name, module, action, apiUrl string, param map[string
if err != nil {
fmt.Printf("handle oklink err", "err", err)
}
} else if name == "solscan" {
err = bc.HandleSolScanResponse(content.Bytes(), outcome)
if err != nil {
fmt.Printf("handle oklink err", "err", err)
}
} else {
fmt.Printf("unsuport type")
}
Expand Down Expand Up @@ -192,6 +213,16 @@ func (bc *BaseClient) CraftOkLinkURL(apiUrl string) (URL string) {
return
}

func (bc *BaseClient) CraftSolScanURL(apiUrl string, param map[string]interface{}) (URL string) {
q := url.Values{}
for k, v := range param {
q[k] = common.ExtractValue(v)
}
URL = bc.baseURL + apiUrl + q.Encode()
fmt.Println("CraftSolScanURL", URL)
return
}

func (bc *BaseClient) HandleEtherscanResponse(action string, data []byte, outcome interface{}) error {
var etherscanEnvelope EtherscanEnvelope
err := json.Unmarshal(data, &etherscanEnvelope)
Expand Down Expand Up @@ -234,3 +265,23 @@ func (bc *BaseClient) HandleOklinkResponse(data []byte, outcome interface{}) err
}
return nil
}

func (bc *BaseClient) HandleSolScanResponse(data []byte, outcome interface{}) error {
var solscan SolScanEnvelope
err := json.Unmarshal(data, &solscan)
if err != nil {
err = common.WrapErr(err, "json unmarshal solscan envelope")
return err
}
fmt.Println("Parse solscan data success", "code", solscan.Success)
if solscan.Success != true {
err = fmt.Errorf("solscan scan server: %s", solscan.Errors.Message)
return err
}
err = json.Unmarshal(solscan.Data, outcome)
if err != nil {
err = common.WrapErr(err, "json unmarshal solscan outcome")
return err
}
return nil
}
26 changes: 26 additions & 0 deletions explorer/solscan/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package solscan

import (
"github.com/dapplink-labs/chain-explorer-api/common"
"github.com/dapplink-labs/chain-explorer-api/common/account"
"math/big"
"strconv"
)

func (cea *ChainExplorerAdaptor) GetAccountBalance(req *account.AccountBalanceRequest) (*account.AccountBalanceResponse, error) {

var responseData []AddressSummaryData

cea.baseClient.Call("solscan", "", "", "/v1.0/account/tokens", map[string]any{
"account": req.Account[0],
}, &responseData)

return &account.AccountBalanceResponse{
Account: req.Account[0],
Balance: (*common.BigInt)(big.NewInt(responseData[0].Amount)),
BalanceStr: strconv.FormatInt(responseData[0].Amount, 10),
Symbol: responseData[0].TokenAccount,
ContractAddress: responseData[0].TokenAddress,
TokenId: responseData[0].TokenAccount,
}, nil
}
1 change: 1 addition & 0 deletions explorer/solscan/block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package solscan
22 changes: 22 additions & 0 deletions explorer/solscan/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package solscan

import (
"time"

"github.com/dapplink-labs/chain-explorer-api/explorer"
"github.com/dapplink-labs/chain-explorer-api/explorer/base"
)

const ChainExplorerName = "solscan"

type ChainExplorerAdaptor struct {
explorer.ChainExplorerAdaptor
baseClient *base.BaseClient
}

func NewChainExplorerAdaptor(key string, baseURL string, verbose bool, timeout time.Duration) (*ChainExplorerAdaptor, error) {
baseClient := base.NewBaseClient(key, baseURL, verbose, timeout)
return &ChainExplorerAdaptor{
baseClient: baseClient,
}, nil
}
12 changes: 12 additions & 0 deletions explorer/solscan/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package solscan

// AddressSummaryData The Data field within the Response structure of
// Fundamental blockchain data -> Address Data -> Get basic address details

type AddressSummaryData struct {
TokenAccount string `json:"token_account"`
TokenAddress string `json:"token_address"`
Amount int64 `json:"amount"`
TokenDecimals int `json:"token_decimals"`
Owner string `json:"owner"`
}

0 comments on commit efa7cfa

Please sign in to comment.