-
Notifications
You must be signed in to change notification settings - Fork 10
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 #45 from wuyachi/main-v2-pr
Merge from 1.0 main and update sdk
- Loading branch information
Showing
16 changed files
with
10,776 additions
and
62 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,103 @@ | ||
package aptos | ||
|
||
import ( | ||
"github.com/polynetwork/bridge-common/chains" | ||
"github.com/polynetwork/bridge-common/util" | ||
"github.com/portto/aptos-go-sdk/client" | ||
"net/http" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
type Rpc = client.AptosClient | ||
|
||
type Client struct { | ||
Rpc | ||
address string | ||
} | ||
|
||
func New(url string) *Client { | ||
c := client.NewAptosClient(url) | ||
return &Client{ | ||
Rpc: c, | ||
address: url, | ||
} | ||
} | ||
|
||
func (c *Client) Address() string { | ||
return c.address | ||
} | ||
|
||
func (c *Client) GetLatestHeight() (uint64, error) { | ||
url := c.address + "/v1/-/healthy" | ||
httpClient := &http.Client{} | ||
req, err := http.NewRequest("GET", url, nil) | ||
if err != nil { | ||
return 0, err | ||
} | ||
res, err := httpClient.Do(req) | ||
if err != nil { | ||
return 0, err | ||
} | ||
heightStr := res.Header.Get("x-aptos-block-height") | ||
return strconv.ParseUint(heightStr, 10, 64) | ||
} | ||
|
||
type SDK struct { | ||
*chains.ChainSDK | ||
nodes []*Client | ||
options *chains.Options | ||
} | ||
|
||
func NewSDK(chainID uint64, urls []string, interval time.Duration, maxGap uint64) (*SDK, error) { | ||
|
||
clients := make([]*Client, len(urls)) | ||
nodes := make([]chains.SDK, len(urls)) | ||
for i, url := range urls { | ||
c := New(url) | ||
nodes[i] = c | ||
clients[i] = c | ||
} | ||
sdk, err := chains.NewChainSDK(chainID, nodes, interval, maxGap) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &SDK{ChainSDK: sdk, nodes: clients}, nil | ||
} | ||
|
||
func WithOptions(chainID uint64, urls []string, interval time.Duration, maxGap uint64) (*SDK, error) { | ||
sdk, err := util.Single(&SDK{ | ||
options: &chains.Options{ | ||
ChainID: chainID, | ||
Nodes: urls, | ||
Interval: interval, | ||
MaxGap: maxGap, | ||
}, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return sdk.(*SDK), nil | ||
} | ||
|
||
func (s *SDK) Node() *Client { | ||
return s.nodes[s.ChainSDK.Index()] | ||
} | ||
|
||
func (s *SDK) Select() *Client { | ||
return s.nodes[s.ChainSDK.Select()] | ||
} | ||
|
||
func (s *SDK) Create() (interface{}, error) { | ||
return NewSDK(s.options.ChainID, s.options.Nodes, s.options.Interval, s.options.MaxGap) | ||
} | ||
|
||
func (s *SDK) Key() string { | ||
if s.ChainSDK != nil { | ||
return s.ChainSDK.Key() | ||
} else if s.options != nil { | ||
return s.options.Key() | ||
} else { | ||
panic("Unable to identify the sdk") | ||
} | ||
} |
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
Oops, something went wrong.