Skip to content

Commit 59f11bb

Browse files
committed
Initial commit
0 parents  commit 59f11bb

11 files changed

Lines changed: 1296 additions & 0 deletions

File tree

.dockerignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.git/
2+
.gitignore
3+
bin/
4+
README.md

Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM golang:alpine as builder
2+
WORKDIR /go/src/app
3+
COPY . .
4+
RUN CGO_ENABLED=0 go install -ldflags '-extldflags "-static"'
5+
6+
FROM scratch
7+
COPY --from=builder /go/bin/near-exporter /near-exporter
8+
COPY --from=alpine:latest /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
9+
ENTRYPOINT ["/near-exporter"]

api/client.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package api
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"errors"
7+
"fmt"
8+
"io/ioutil"
9+
"log"
10+
"net/http"
11+
"time"
12+
)
13+
14+
type Client struct {
15+
httpClient *http.Client
16+
Endpoint string
17+
}
18+
19+
type Payload struct {
20+
JsonRPC string `json:"jsonrpc"`
21+
Id string `json:"id"`
22+
Method string `json:"method"`
23+
Params interface{} `json:"params"`
24+
}
25+
26+
type Response struct {
27+
JsonRPC string `json:"jsonrpc"`
28+
Id string `json:"id"`
29+
Result json.RawMessage `json:"result"`
30+
Error struct {
31+
Name string `json:"name"`
32+
Code int `json:"code"`
33+
Message string `json:"message"`
34+
Data string `json:"data"`
35+
} `json:"error"`
36+
}
37+
38+
func NewClient(endpoint string) *Client {
39+
timeout := time.Duration(10 * time.Second)
40+
41+
httpClient := &http.Client{
42+
Timeout: timeout,
43+
}
44+
45+
return &Client{
46+
Endpoint: endpoint,
47+
httpClient: httpClient,
48+
}
49+
}
50+
51+
func (c *Client) Request(method string, params interface{}) (*Response, error) {
52+
payload, err := json.Marshal(map[string]string{
53+
"query": method,
54+
})
55+
56+
if params != "" {
57+
p := Payload{
58+
JsonRPC: "2.0",
59+
Id: "near_exporter",
60+
Method: method,
61+
Params: params,
62+
}
63+
64+
payload, err = json.Marshal(p)
65+
66+
if err != nil {
67+
log.Println(err)
68+
}
69+
}
70+
71+
req, err := http.NewRequest("POST", c.Endpoint, bytes.NewBuffer(payload))
72+
req.Header.Set("Content-Type", "application/json")
73+
if err != nil {
74+
return nil, err
75+
}
76+
77+
r, err := c.httpClient.Do(req)
78+
if err != nil {
79+
return nil, err
80+
}
81+
defer r.Body.Close()
82+
83+
body, err := ioutil.ReadAll(r.Body)
84+
if err != nil {
85+
return nil, err
86+
}
87+
// fmt.Println(string(body))
88+
89+
var resp *Response
90+
err = json.Unmarshal(body, &resp)
91+
92+
if err != nil {
93+
return nil, err
94+
}
95+
96+
return resp, nil
97+
}
98+
99+
func (c *Client) do(method string, params interface{}, result interface{}) error {
100+
resp, err := c.Request(method, params)
101+
if err != nil {
102+
return err
103+
}
104+
105+
if resp.Error.Name != "" {
106+
return errors.New(fmt.Sprintf(
107+
"jsonrpc error(%d): %s %s",
108+
resp.Error.Code,
109+
resp.Error.Name,
110+
resp.Error.Message,
111+
))
112+
}
113+
114+
err = json.Unmarshal(resp.Result, &result)
115+
if err != nil {
116+
return err
117+
}
118+
119+
return nil
120+
}

api/protocolconfig.go

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
package api
2+
3+
import (
4+
"time"
5+
)
6+
7+
const ProtocolConfigMethod = "EXPERIMENTAL_protocol_config"
8+
9+
type ProtocolConfigResult struct {
10+
ProtocolVersion int `json:"protocol_version"`
11+
GenesisTime time.Time `json:"genesis_time"`
12+
ChainID string `json:"chain_id"`
13+
GenesisHeight int `json:"genesis_height"`
14+
NumBlockProducerSeats int `json:"num_block_producer_seats"`
15+
NumBlockProducerSeatsPerShard []int `json:"num_block_producer_seats_per_shard"`
16+
AvgHiddenValidatorSeatsPerShard []int `json:"avg_hidden_validator_seats_per_shard"`
17+
DynamicResharding bool `json:"dynamic_resharding"`
18+
ProtocolUpgradeStakeThreshold []int `json:"protocol_upgrade_stake_threshold"`
19+
EpochLength int `json:"epoch_length"`
20+
GasLimit int64 `json:"gas_limit"`
21+
MinGasPrice string `json:"min_gas_price"`
22+
MaxGasPrice string `json:"max_gas_price"`
23+
BlockProducerKickoutThreshold int `json:"block_producer_kickout_threshold"`
24+
ChunkProducerKickoutThreshold int `json:"chunk_producer_kickout_threshold"`
25+
OnlineMinThreshold []int `json:"online_min_threshold"`
26+
OnlineMaxThreshold []int `json:"online_max_threshold"`
27+
GasPriceAdjustmentRate []int `json:"gas_price_adjustment_rate"`
28+
RuntimeConfig struct {
29+
StorageAmountPerByte string `json:"storage_amount_per_byte"`
30+
TransactionCosts struct {
31+
ActionReceiptCreationConfig struct {
32+
SendSir int64 `json:"send_sir"`
33+
SendNotSir int64 `json:"send_not_sir"`
34+
Execution int64 `json:"execution"`
35+
} `json:"action_receipt_creation_config"`
36+
DataReceiptCreationConfig struct {
37+
BaseCost struct {
38+
SendSir int64 `json:"send_sir"`
39+
SendNotSir int64 `json:"send_not_sir"`
40+
Execution int64 `json:"execution"`
41+
} `json:"base_cost"`
42+
CostPerByte struct {
43+
SendSir int `json:"send_sir"`
44+
SendNotSir int `json:"send_not_sir"`
45+
Execution int `json:"execution"`
46+
} `json:"cost_per_byte"`
47+
} `json:"data_receipt_creation_config"`
48+
ActionCreationConfig struct {
49+
CreateAccountCost struct {
50+
SendSir int64 `json:"send_sir"`
51+
SendNotSir int64 `json:"send_not_sir"`
52+
Execution int64 `json:"execution"`
53+
} `json:"create_account_cost"`
54+
DeployContractCost struct {
55+
SendSir int64 `json:"send_sir"`
56+
SendNotSir int64 `json:"send_not_sir"`
57+
Execution int64 `json:"execution"`
58+
} `json:"deploy_contract_cost"`
59+
DeployContractCostPerByte struct {
60+
SendSir int `json:"send_sir"`
61+
SendNotSir int `json:"send_not_sir"`
62+
Execution int `json:"execution"`
63+
} `json:"deploy_contract_cost_per_byte"`
64+
FunctionCallCost struct {
65+
SendSir int64 `json:"send_sir"`
66+
SendNotSir int64 `json:"send_not_sir"`
67+
Execution int64 `json:"execution"`
68+
} `json:"function_call_cost"`
69+
FunctionCallCostPerByte struct {
70+
SendSir int `json:"send_sir"`
71+
SendNotSir int `json:"send_not_sir"`
72+
Execution int `json:"execution"`
73+
} `json:"function_call_cost_per_byte"`
74+
TransferCost struct {
75+
SendSir int64 `json:"send_sir"`
76+
SendNotSir int64 `json:"send_not_sir"`
77+
Execution int64 `json:"execution"`
78+
} `json:"transfer_cost"`
79+
StakeCost struct {
80+
SendSir int64 `json:"send_sir"`
81+
SendNotSir int64 `json:"send_not_sir"`
82+
Execution int64 `json:"execution"`
83+
} `json:"stake_cost"`
84+
AddKeyCost struct {
85+
FullAccessCost struct {
86+
SendSir int64 `json:"send_sir"`
87+
SendNotSir int64 `json:"send_not_sir"`
88+
Execution int64 `json:"execution"`
89+
} `json:"full_access_cost"`
90+
FunctionCallCost struct {
91+
SendSir int64 `json:"send_sir"`
92+
SendNotSir int64 `json:"send_not_sir"`
93+
Execution int64 `json:"execution"`
94+
} `json:"function_call_cost"`
95+
FunctionCallCostPerByte struct {
96+
SendSir int `json:"send_sir"`
97+
SendNotSir int `json:"send_not_sir"`
98+
Execution int `json:"execution"`
99+
} `json:"function_call_cost_per_byte"`
100+
} `json:"add_key_cost"`
101+
DeleteKeyCost struct {
102+
SendSir int64 `json:"send_sir"`
103+
SendNotSir int64 `json:"send_not_sir"`
104+
Execution int64 `json:"execution"`
105+
} `json:"delete_key_cost"`
106+
DeleteAccountCost struct {
107+
SendSir int64 `json:"send_sir"`
108+
SendNotSir int64 `json:"send_not_sir"`
109+
Execution int64 `json:"execution"`
110+
} `json:"delete_account_cost"`
111+
} `json:"action_creation_config"`
112+
StorageUsageConfig struct {
113+
NumBytesAccount int `json:"num_bytes_account"`
114+
NumExtraBytesRecord int `json:"num_extra_bytes_record"`
115+
} `json:"storage_usage_config"`
116+
BurntGasReward []int `json:"burnt_gas_reward"`
117+
PessimisticGasPriceInflationRatio []int `json:"pessimistic_gas_price_inflation_ratio"`
118+
} `json:"transaction_costs"`
119+
WasmConfig struct {
120+
ExtCosts struct {
121+
Base int `json:"base"`
122+
ContractCompileBase int `json:"contract_compile_base"`
123+
ContractCompileBytes int `json:"contract_compile_bytes"`
124+
ReadMemoryBase int64 `json:"read_memory_base"`
125+
ReadMemoryByte int `json:"read_memory_byte"`
126+
WriteMemoryBase int64 `json:"write_memory_base"`
127+
WriteMemoryByte int `json:"write_memory_byte"`
128+
ReadRegisterBase int64 `json:"read_register_base"`
129+
ReadRegisterByte int `json:"read_register_byte"`
130+
WriteRegisterBase int64 `json:"write_register_base"`
131+
WriteRegisterByte int `json:"write_register_byte"`
132+
Utf8DecodingBase int64 `json:"utf8_decoding_base"`
133+
Utf8DecodingByte int `json:"utf8_decoding_byte"`
134+
Utf16DecodingBase int64 `json:"utf16_decoding_base"`
135+
Utf16DecodingByte int `json:"utf16_decoding_byte"`
136+
Sha256Base int64 `json:"sha256_base"`
137+
Sha256Byte int `json:"sha256_byte"`
138+
Keccak256Base int64 `json:"keccak256_base"`
139+
Keccak256Byte int `json:"keccak256_byte"`
140+
Keccak512Base int64 `json:"keccak512_base"`
141+
Keccak512Byte int `json:"keccak512_byte"`
142+
Ripemd160Base int `json:"ripemd160_base"`
143+
Ripemd160Block int `json:"ripemd160_block"`
144+
EcrecoverBase int64 `json:"ecrecover_base"`
145+
LogBase int64 `json:"log_base"`
146+
LogByte int `json:"log_byte"`
147+
StorageWriteBase int64 `json:"storage_write_base"`
148+
StorageWriteKeyByte int `json:"storage_write_key_byte"`
149+
StorageWriteValueByte int `json:"storage_write_value_byte"`
150+
StorageWriteEvictedByte int `json:"storage_write_evicted_byte"`
151+
StorageReadBase int64 `json:"storage_read_base"`
152+
StorageReadKeyByte int `json:"storage_read_key_byte"`
153+
StorageReadValueByte int `json:"storage_read_value_byte"`
154+
StorageRemoveBase int64 `json:"storage_remove_base"`
155+
StorageRemoveKeyByte int `json:"storage_remove_key_byte"`
156+
StorageRemoveRetValueByte int `json:"storage_remove_ret_value_byte"`
157+
StorageHasKeyBase int64 `json:"storage_has_key_base"`
158+
StorageHasKeyByte int `json:"storage_has_key_byte"`
159+
StorageIterCreatePrefixBase int `json:"storage_iter_create_prefix_base"`
160+
StorageIterCreatePrefixByte int `json:"storage_iter_create_prefix_byte"`
161+
StorageIterCreateRangeBase int `json:"storage_iter_create_range_base"`
162+
StorageIterCreateFromByte int `json:"storage_iter_create_from_byte"`
163+
StorageIterCreateToByte int `json:"storage_iter_create_to_byte"`
164+
StorageIterNextBase int `json:"storage_iter_next_base"`
165+
StorageIterNextKeyByte int `json:"storage_iter_next_key_byte"`
166+
StorageIterNextValueByte int `json:"storage_iter_next_value_byte"`
167+
TouchingTrieNode int64 `json:"touching_trie_node"`
168+
PromiseAndBase int `json:"promise_and_base"`
169+
PromiseAndPerPromise int `json:"promise_and_per_promise"`
170+
PromiseReturn int `json:"promise_return"`
171+
ValidatorStakeBase int64 `json:"validator_stake_base"`
172+
ValidatorTotalStakeBase int64 `json:"validator_total_stake_base"`
173+
} `json:"ext_costs"`
174+
GrowMemCost int `json:"grow_mem_cost"`
175+
RegularOpCost int `json:"regular_op_cost"`
176+
LimitConfig struct {
177+
MaxGasBurnt int64 `json:"max_gas_burnt"`
178+
MaxStackHeight int `json:"max_stack_height"`
179+
StackLimiterVersion int `json:"stack_limiter_version"`
180+
InitialMemoryPages int `json:"initial_memory_pages"`
181+
MaxMemoryPages int `json:"max_memory_pages"`
182+
RegistersMemoryLimit int `json:"registers_memory_limit"`
183+
MaxRegisterSize int `json:"max_register_size"`
184+
MaxNumberRegisters int `json:"max_number_registers"`
185+
MaxNumberLogs int `json:"max_number_logs"`
186+
MaxTotalLogLength int `json:"max_total_log_length"`
187+
MaxTotalPrepaidGas int64 `json:"max_total_prepaid_gas"`
188+
MaxActionsPerReceipt int `json:"max_actions_per_receipt"`
189+
MaxNumberBytesMethodNames int `json:"max_number_bytes_method_names"`
190+
MaxLengthMethodName int `json:"max_length_method_name"`
191+
MaxArgumentsLength int `json:"max_arguments_length"`
192+
MaxLengthReturnedData int `json:"max_length_returned_data"`
193+
MaxContractSize int `json:"max_contract_size"`
194+
MaxTransactionSize int `json:"max_transaction_size"`
195+
MaxLengthStorageKey int `json:"max_length_storage_key"`
196+
MaxLengthStorageValue int `json:"max_length_storage_value"`
197+
MaxPromisesPerFunctionCallAction int `json:"max_promises_per_function_call_action"`
198+
MaxNumberInputDataDependencies int `json:"max_number_input_data_dependencies"`
199+
MaxFunctionsNumberPerContract int `json:"max_functions_number_per_contract"`
200+
} `json:"limit_config"`
201+
} `json:"wasm_config"`
202+
AccountCreationConfig struct {
203+
MinAllowedTopLevelAccountLength int `json:"min_allowed_top_level_account_length"`
204+
RegistrarAccountID string `json:"registrar_account_id"`
205+
} `json:"account_creation_config"`
206+
} `json:"runtime_config"`
207+
TransactionValidityPeriod int `json:"transaction_validity_period"`
208+
ProtocolRewardRate []int `json:"protocol_reward_rate"`
209+
MaxInflationRate []int `json:"max_inflation_rate"`
210+
NumBlocksPerYear int `json:"num_blocks_per_year"`
211+
ProtocolTreasuryAccount string `json:"protocol_treasury_account"`
212+
FishermenThreshold string `json:"fishermen_threshold"`
213+
MinimumStakeDivisor int `json:"minimum_stake_divisor"`
214+
}
215+
216+
func (c *Client) ProtocolConfig() (*ProtocolConfigResult, error) {
217+
var result *ProtocolConfigResult
218+
err := c.do(ProtocolConfigMethod, map[string]string{"finality": "final"}, &result)
219+
220+
return result, err
221+
}

api/status.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package api
2+
3+
const StatusMethod = "status"
4+
5+
type StatusResult struct {
6+
ChainId string `json:"chain_id"`
7+
LatestProtocolVersion int `json:"latest_protocol_version"`
8+
ProtocolVersion int `json:"protocol_version"`
9+
RpcAddr string `json:"rpc_addr"`
10+
SyncInfo struct {
11+
EarliestBlockHash string `json:"earliest_block_hash"`
12+
EarliestBlockHeight uint64 `json:"earliest_block_height"`
13+
EarliestBlockTime string `json:"earliest_block_time"`
14+
LatestBlockHash string `json:"latest_block_hash"`
15+
LatestBlockHeight uint64 `json:"latest_block_height"`
16+
LatestStateRoot string `json:"latest_state_root"`
17+
LatestBlockTime string `json:"latest_block_time"`
18+
Syncing bool `json:"syncing"`
19+
} `json:"sync_info"`
20+
//Validators []string `json:"validators"`
21+
Version struct {
22+
Version string `json:"version"`
23+
Build string `json:"build"`
24+
} `json:"version"`
25+
}
26+
27+
func (c *Client) Status() (*StatusResult, error) {
28+
var result *StatusResult
29+
err := c.do(StatusMethod, nil, &result)
30+
31+
return result, err
32+
}

0 commit comments

Comments
 (0)