forked from textileio/near-api-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
899 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package types | ||
package config | ||
|
||
import ( | ||
"github.com/ethereum/go-ethereum/rpc" | ||
|
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,121 @@ | ||
package models | ||
|
||
import ( | ||
"encoding/json" | ||
"time" | ||
|
||
"github.com/gateway-fm/near-api-go/types" | ||
"github.com/gateway-fm/near-api-go/types/hash" | ||
"github.com/gateway-fm/near-api-go/types/key" | ||
"github.com/gateway-fm/near-api-go/types/signature" | ||
) | ||
|
||
// NetworkInfo holds network information | ||
type NetworkInfo struct { | ||
ActivePeers []FullPeerInfo `json:"active_peers"` | ||
NumActivePeers uint `json:"num_active_peers"` | ||
PeerMaxCount uint32 `json:"peer_max_count"` | ||
HighestHeightPeers []FullPeerInfo `json:"highest_height_peers"` | ||
SentBytesPerSec uint64 `json:"sent_bytes_per_sec"` | ||
ReceivedBytesPerSec uint64 `json:"received_bytes_per_sec"` | ||
KnownProducers []KnownProducer `json:"known_producers"` | ||
MetricRecorder MetricRecorder `json:"metric_recorder"` | ||
PeerCounter uint `json:"peer_counter"` | ||
} | ||
|
||
type FullPeerInfo struct { | ||
PeerInfo PeerInfo `json:"peer_info"` | ||
ChainInfo PeerChainInfo `json:"chain_info"` | ||
EdgeInfo EdgeInfo `json:"edge_info"` | ||
} | ||
|
||
// PeerInfo holds peer information | ||
type PeerInfo struct { | ||
ID key.PeerID `json:"id"` | ||
Addr *string `json:"addr"` | ||
AccountID *types.AccountID `json:"account_id"` | ||
} | ||
|
||
// PeerChainInfo contains peer chain information. This is derived from PeerCHainInfoV2 in nearcore | ||
type PeerChainInfo struct { | ||
// Chain Id and hash of genesis block. | ||
GenesisID GenesisID `json:"genesis_id"` | ||
// Last known chain height of the peer. | ||
Height types.BlockHeight `json:"height"` | ||
// Shards that the peer is tracking. | ||
TrackedShards []types.ShardID `json:"tracked_shards"` | ||
// Denote if a node is running in archival mode or not. | ||
Archival bool `json:"archival"` | ||
} | ||
|
||
// EdgeInfo contains information that will be ultimately used to create a new edge. It contains nonce proposed for the edge with signature from peer. | ||
type EdgeInfo struct { | ||
Nonce types.Nonce `json:"nonce"` | ||
Signature signature.Signature `json:"signature"` | ||
} | ||
|
||
// KnownProducer is basically PeerInfo, but AccountID is known | ||
type KnownProducer struct { | ||
AccountID types.AccountID `json:"account_id"` | ||
Addr *string `json:"addr"` | ||
PeerID key.PeerID `json:"peer_id"` | ||
} | ||
|
||
// TODO: chain/network/src/recorder.rs | ||
type MetricRecorder = json.RawMessage | ||
|
||
type GenesisID struct { | ||
// Chain Id | ||
ChainID string `json:"chain_id"` | ||
// Hash of genesis block | ||
Hash hash.CryptoHash `json:"hash"` | ||
} | ||
|
||
type StatusResponse struct { | ||
// Binary version | ||
Version NodeVersion `json:"version"` | ||
// Unique chain id. | ||
ChainID string `json:"chain_id"` | ||
// Currently active protocol version. | ||
ProtocolVersion uint32 `json:"protocol_version"` | ||
// Latest protocol version that this client supports. | ||
LatestProtocolVersion uint32 `json:"latest_protocol_version"` | ||
// Address for RPC server. | ||
RPCAddr string `json:"rpc_addr"` | ||
// Current epoch validators. | ||
Validators []ValidatorInfo `json:"validators"` | ||
// Sync status of the node. | ||
SyncInfo StatusSyncInfo `json:"sync_info"` | ||
// Validator id of the node | ||
ValidatorAccountID *types.AccountID `json:"validator_account_id"` | ||
} | ||
|
||
type NodeVersion struct { | ||
Version string `json:"version"` | ||
Build string `json:"build"` | ||
} | ||
|
||
type ValidatorInfo struct { | ||
AccountID types.AccountID `json:"account_id"` | ||
Slashed bool `json:"is_slashed"` | ||
} | ||
|
||
type StatusSyncInfo struct { | ||
LatestBlockHash hash.CryptoHash `json:"latest_block_hash"` | ||
LatestBlockHeight types.BlockHeight `json:"latest_block_height"` | ||
LatestBlockTime time.Time `json:"latest_block_time"` | ||
Syncing bool `json:"syncing"` | ||
} | ||
|
||
type ValidatorsResponse struct { | ||
CurrentValidators []CurrentEpochValidatorInfo `json:"current_validator"` | ||
} | ||
|
||
type CurrentEpochValidatorInfo struct { | ||
ValidatorInfo | ||
PublicKey key.PublicKey `json:"public_key"` | ||
Stake types.Balance `json:"stake"` | ||
Shards []types.ShardID `json:"shards"` | ||
NumProducedBlocks types.NumBlocks `json:"num_produced_blocks"` | ||
NumExpectedBlocks types.NumBlocks `json:"num_expected_blocks"` | ||
} |
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,98 @@ | ||
package types | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"math" | ||
"math/big" | ||
|
||
uint128 "lukechampine.com/uint128" | ||
) | ||
|
||
var ( | ||
tenPower24 = uint128.From64(uint64(math.Pow10(12))).Mul64(uint64(math.Pow10(12))) | ||
zeroNEAR = Balance(uint128.From64(0)) | ||
) | ||
|
||
// Balance holds amount of yoctoNEAR | ||
type Balance uint128.Uint128 | ||
|
||
func (bal *Balance) UnmarshalJSON(b []byte) error { | ||
var s string | ||
if err := json.Unmarshal(b, &s); err != nil { | ||
return err | ||
} | ||
|
||
val := big.Int{} | ||
if _, ok := val.SetString(s, 10); !ok { | ||
return fmt.Errorf("unable to parse '%s'", s) | ||
} | ||
|
||
*bal = Balance(uint128.FromBig(&val)) | ||
|
||
return nil | ||
} | ||
|
||
func (bal Balance) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(bal.String()) | ||
} | ||
|
||
func (bal Balance) String() string { | ||
return uint128.Uint128(bal).String() | ||
} | ||
|
||
// Convenience funcs | ||
func (bal Balance) Div64(div uint64) Balance { | ||
return Balance(uint128.Uint128(bal).Div64(div)) | ||
} | ||
|
||
// TODO | ||
func NEARToYocto(near uint64) Balance { | ||
if near == 0 { | ||
return zeroNEAR | ||
} | ||
|
||
return Balance(uint128.From64(near).Mul(tenPower24)) | ||
} | ||
|
||
// TODO | ||
func YoctoToNEAR(yocto Balance) uint64 { | ||
div := uint128.Uint128(yocto).Div(tenPower24) | ||
if h := div.Hi; h != 0 { | ||
panic(fmt.Errorf("yocto div failed, remaining: %d", h)) | ||
} | ||
|
||
return div.Lo | ||
} | ||
|
||
func scaleToYocto(f *big.Float) (r *big.Int) { | ||
// Convert reference 1 NEAR to big.Float | ||
base := new(big.Float).SetPrec(128).SetInt(uint128.Uint128(NEARToYocto(1)).Big()) | ||
|
||
// Multiply base using the supplied float | ||
// XXX: small precision issues here will haunt me forever | ||
bigf2 := new(big.Float).SetPrec(128).SetMode(big.ToZero).Mul(base, f) | ||
|
||
// Convert it to big.Int | ||
r, _ = bigf2.Int(nil) | ||
return | ||
} | ||
|
||
// TODO | ||
func BalanceFromFloat(f float64) (bal Balance) { | ||
bigf := big.NewFloat(f) | ||
bal = Balance(uint128.FromBig(scaleToYocto(bigf))) | ||
return | ||
} | ||
|
||
// TODO | ||
func BalanceFromString(s string) (bal Balance, err error) { | ||
var bigf *big.Float | ||
bigf, _, err = big.ParseFloat(s, 10, 128, big.ToZero) | ||
if err != nil { | ||
return | ||
} | ||
|
||
bal = Balance(uint128.FromBig(scaleToYocto(bigf))) | ||
return | ||
} |
Oops, something went wrong.