Skip to content

Commit

Permalink
Merge pull request #45 from wuyachi/main-v2-pr
Browse files Browse the repository at this point in the history
Merge from 1.0 main and update sdk
  • Loading branch information
wuyachi authored Nov 16, 2022
2 parents f3644b6 + c6ed2c3 commit 3971474
Show file tree
Hide file tree
Showing 16 changed files with 10,776 additions and 62 deletions.
3,750 changes: 3,750 additions & 0 deletions abi/mintable_erc20_abi/mintable_erc20_abi.go

Large diffs are not rendered by default.

6,696 changes: 6,696 additions & 0 deletions abi/nft_mapping_abi/nft_mapping_abi.go

Large diffs are not rendered by default.

21 changes: 19 additions & 2 deletions base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func GetChainName(id uint64) string {
case POLY:
return "Poly"
case ETH:
return "Ethereum-Ropsten"
return "Ethereum"
case RINKEBY:
return "Ethereum-Rinkeby"
case ONT:
Expand Down Expand Up @@ -136,8 +136,23 @@ func GetChainName(id uint64) string {
return "Cube"
case KAVA:
return "Kava"
case ZKSYNC:
return "zkSync"
case CELO:
return "Celo"
case CLOVER:
return "CLV P-Chain"
case CONFLUX:
return "Conflux"
case ASTAR:
return "Astar"
case APTOS:
return "Aptos"
case BRISE:
return "Bitgert"
case GOERLI:
return "Goerli"

default:
return fmt.Sprintf("Unknown(%d)", id)
}
Expand All @@ -155,6 +170,8 @@ func BlocksToSkip(chainId uint64) uint64 {
return 8
case PLT, BCSPALETTE:
return 5
case ONT:
return 0
case PIXIE:
return 2
case STARCOIN:
Expand All @@ -172,7 +189,7 @@ func BlocksToWait(chainId uint64) uint64 {
return 21
case ZION, ONT, NEO, NEO3, OK, SWITCHEO:
return 1
case HARMONY:
case HARMONY, ASTAR:
return 2
case PLT, BCSPALETTE:
return 4
Expand Down
13 changes: 10 additions & 3 deletions base/devnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ const (
HARMONY uint64 = 801
BCSPALETTE uint64 = 1001
STARCOIN uint64 = 318
ZKSYNC uint64 = 940
CELO uint64 = 960
CLOVER uint64 = 970
CONFLUX uint64 = 980
ASTAR uint64 = 990
APTOS uint64 = 998
BRISE uint64 = 1010

// Invalid chain ids
HEIMDALL uint64 = 9000001
Expand All @@ -67,10 +74,10 @@ const (
)

var CHAINS = []uint64{
ZION, ETH, ONT, NEO, BSC, HECO, O3, OK, MATIC, METIS, RINKEBY, PIXIE, HSC, HARMONY, BYTOM, STARCOIN, ONTEVM, CUBE,
KAVA, FLOW, GOERLI,
ZION, POLY, ETH, ONT, NEO, BSC, HECO, O3, OK, MATIC, METIS, RINKEBY, PIXIE, HSC, HARMONY, BYTOM, STARCOIN, ONTEVM, CUBE,
KAVA, FLOW, ZKSYNC, CELO, CLOVER, CONFLUX, ASTAR, APTOS, BRISE,
}

var ETH_CHAINS = []uint64{
ETH, BSC, HECO, OK, MATIC, O3, METIS, RINKEBY, PIXIE, HSC, HARMONY, BYTOM, KCC, ONTEVM, CUBE, KAVA, GOERLI,
ZION, ETH, BSC, HECO, OK, MATIC, O3, METIS, RINKEBY, PIXIE, HSC, HARMONY, BYTOM, KCC, ONTEVM, CUBE, KAVA, ZKSYNC, CELO, CLOVER, CONFLUX, ASTAR, BRISE,
}
16 changes: 12 additions & 4 deletions base/mainnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ const (
KAVA uint64 = 32
MILKO uint64 = 34
CUBE uint64 = 35
ZKSYNC uint64 = 100940
CELO uint64 = 36
CLOVER uint64 = 37
CONFLUX uint64 = 38
ASTAR uint64 = 40
APTOS uint64 = 41
BRISE uint64 = 42

// Invalid chain IDs
RINKEBY uint64 = 1000000
Expand All @@ -65,11 +72,12 @@ const (
)

var CHAINS = []uint64{
POLY, ETH, BSC, HECO, OK, ONT, NEO, NEO3, HEIMDALL, MATIC, SWITCHEO, O3, PLT, ARBITRUM, XDAI, OPTIMISM, FANTOM, AVA,
METIS, BOBA, PIXIE, OASIS, HSC, HARMONY, BYTOM, BCSPALETTE, STARCOIN, ONTEVM, KCC, MILKO, CUBE, KAVA, FLOW, GOERLI,
ZION, POLY, ETH, BSC, HECO, OK, ONT, NEO, NEO3, HEIMDALL, MATIC, SWITCHEO, O3, PLT, ARBITRUM, XDAI, OPTIMISM, FANTOM, AVA,
METIS, BOBA, PIXIE, OASIS, HSC, HARMONY, BYTOM, BCSPALETTE, STARCOIN, ONTEVM, KCC, MILKO, CUBE, KAVA, FLOW, ZKSYNC,
CELO, CLOVER, CONFLUX, ASTAR, APTOS, BRISE,
}

var ETH_CHAINS = []uint64{
ETH, BSC, HECO, OK, MATIC, O3, PLT, ARBITRUM, XDAI, OPTIMISM, FANTOM, AVA, METIS, BOBA, PIXIE, OASIS, HSC, HARMONY,
BYTOM, BCSPALETTE, KCC, ONTEVM, MILKO, CUBE, KAVA, GOERLI,
ZION, ETH, BSC, HECO, OK, MATIC, O3, PLT, ARBITRUM, XDAI, OPTIMISM, FANTOM, AVA, METIS, BOBA, PIXIE, OASIS, HSC, HARMONY,
BYTOM, BCSPALETTE, KCC, ONTEVM, MILKO, CUBE, KAVA, ZKSYNC, CELO, CLOVER, CONFLUX, ASTAR, BRISE,
}
19 changes: 13 additions & 6 deletions base/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ package base
const (
POLY uint64 = 0
ZION uint64 = POLY
ETH uint64 = 10002
ETH uint64 = 2
ONT uint64 = 3
NEO uint64 = 5
HECO uint64 = 7
Expand All @@ -35,7 +35,7 @@ const (
OK uint64 = 200
HEIMDALL uint64 = 201
MATIC uint64 = 202
ARBITRUM uint64 = 205
ARBITRUM uint64 = 215
XDAI uint64 = 206
FANTOM uint64 = 208
AVA uint64 = 209
Expand All @@ -54,6 +54,13 @@ const (
FLOW uint64 = 910
KAVA uint64 = 920
CUBE uint64 = 930
ZKSYNC uint64 = 940
CELO uint64 = 960
CLOVER uint64 = 970
CONFLUX uint64 = 980
ASTAR uint64 = 990
APTOS uint64 = 998
BRISE uint64 = 1010
GOERLI uint64 = 502

SWITCHEO uint64 = 1000
Expand All @@ -67,12 +74,12 @@ const (
)

var CHAINS = []uint64{
POLY, ETH, BSC, HECO, OK, ONT, NEO, NEO3, HEIMDALL, MATIC, SWITCHEO, O3, PLT, ARBITRUM, XDAI, OPTIMISM, FANTOM, AVA,
ZION, POLY, ETH, BSC, HECO, OK, ONT, NEO, NEO3, HEIMDALL, MATIC, SWITCHEO, O3, PLT, ARBITRUM, XDAI, OPTIMISM, FANTOM, AVA,
METIS, RINKEBY, BOBA, PIXIE, OASIS, HSC, HARMONY, BYTOM, BCSPALETTE, STARCOIN, ONTEVM, KCC, MILKO, CUBE, KAVA, FLOW,
GOERLI,
ZKSYNC, CELO, CLOVER, CONFLUX, ASTAR, APTOS, BRISE,
}

var ETH_CHAINS = []uint64{
ETH, BSC, HECO, OK, MATIC, O3, PLT, ARBITRUM, XDAI, OPTIMISM, FANTOM, AVA, METIS, RINKEBY, BOBA, PIXIE, OASIS, HSC,
HARMONY, HARMONY, BYTOM, BCSPALETTE, KCC, ONTEVM, MILKO, CUBE, KAVA, GOERLI,
ZION, ETH, BSC, HECO, OK, MATIC, O3, PLT, ARBITRUM, XDAI, OPTIMISM, FANTOM, AVA, METIS, RINKEBY, BOBA, PIXIE, OASIS, HSC,
HARMONY, HARMONY, BYTOM, BCSPALETTE, KCC, ONTEVM, MILKO, CUBE, KAVA, ZKSYNC, CELO, CLOVER, CONFLUX, ASTAR, BRISE,
}
103 changes: 103 additions & 0 deletions chains/aptos/aptos.go
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")
}
}
33 changes: 31 additions & 2 deletions chains/eth/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"

erc20 "github.com/polynetwork/bridge-common/abi/mintable_erc20_abi"
nftmapping "github.com/polynetwork/bridge-common/abi/nft_mapping_abi"
"github.com/polynetwork/bridge-common/chains"
"github.com/polynetwork/bridge-common/log"
"github.com/polynetwork/bridge-common/util"
Expand All @@ -54,13 +56,15 @@ func (c *Client) Address() string {
return c.address
}

func (c *Client) GetHeader(height uint64) (header Header, err error){
func (c *Client) GetHeader(height uint64) (header Header, err error) {
head := make(json.RawMessage, 0)
err = c.Rpc.CallContext(context.Background(), &head, "eth_getBlockByNumber", util.BlockNumArg(height), false)
if err == nil && len(head) == 0 {
err = ethereum.NotFound
}
if err != nil { return }
if err != nil {
return
}
header = Header(head)
if height == 0 {
_, err = header.GetHeight()
Expand Down Expand Up @@ -148,6 +152,31 @@ func (c *Client) Confirm(hash common.Hash, blocks uint64, count int) (height, co
return
}

func (c *Client) GetBalance(token, owner string) (balance *big.Int, err error) {
tokenAddress := common.HexToAddress(token)
ownerAddr := common.HexToAddress(owner)
if token == "0000000000000000000000000000000000000000" {
var result hexutil.Big
err = c.Rpc.CallContext(context.Background(), &result, "eth_getBalance", "0x"+owner, "latest")
balance = (*big.Int)(&result)
} else {
var contract *erc20.ERC20Extended
contract, err = erc20.NewERC20Mintable(tokenAddress, c)
if err == nil {
balance, err = contract.BalanceOf(nil, ownerAddr)
}
}
return
}

func (c *Client) GetNFTOwner(asset string, tokenId int64) (owner common.Address, err error) {
cm, err := nftmapping.NewCrossChainNFTMapping(common.HexToAddress(asset), c)
if err != nil {
return common.Address{}, err
}
return cm.OwnerOf(nil, big.NewInt(tokenId))
}

type SDK struct {
*chains.ChainSDK
nodes []*Client
Expand Down
16 changes: 16 additions & 0 deletions chains/neo/neo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package neo

import (
"fmt"
"github.com/joeqian10/neo-gogogo/nep5"
"math/big"
"time"

"github.com/joeqian10/neo-gogogo/helper"
Expand Down Expand Up @@ -80,6 +82,20 @@ func (c *Client) GetPolyEpochHeight(ccm string, chainId uint64) (height uint64,
return
}

func (c *Client) GetBalance(token, owner string) (balance *big.Int, err error) {
scriptHash, err := helper.UInt160FromString(token)
if err != nil {
return nil, err
}
nep5Helper := nep5.NewNep5Helper(scriptHash, c.address)
addrHash, err := helper.UInt160FromString(owner)
if err != nil {
return nil, err
}
amount, err := nep5Helper.BalanceOf(addrHash)
return new(big.Int).SetUint64(amount), nil
}

type SDK struct {
*chains.ChainSDK
nodes []*Client
Expand Down
16 changes: 16 additions & 0 deletions chains/neo3/neo3.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ package neo3

import (
"fmt"
"github.com/joeqian10/neo3-gogogo/helper"
"github.com/joeqian10/neo3-gogogo/nep17"
"math/big"
"time"

"github.com/joeqian10/neo3-gogogo/rpc"
Expand Down Expand Up @@ -53,6 +56,19 @@ func (c *Client) GetLatestHeight() (uint64, error) {
return uint64(res.Result), nil
}

func (c *Client) GetBalance(token, owner string) (balance *big.Int, err error) {
scriptHash, err := helper.UInt160FromString(token)
if err != nil {
return nil, err
}
nep17Helper := nep17.NewNep17Helper(scriptHash, c)
addrHash, err := helper.UInt160FromString(owner)
if err != nil {
return nil, err
}
return nep17Helper.BalanceOf(addrHash)
}

type SDK struct {
*chains.ChainSDK
nodes []*Client
Expand Down
Loading

0 comments on commit 3971474

Please sign in to comment.