@@ -42,23 +42,23 @@ type XRPClient struct {
4242 Headers http.Header
4343}
4444
45- type LedgerRequest struct {
46- Method string `json:"method"`
47- Params []XRPParamas `json:"params"`
45+ type XRPRequest struct {
46+ Method string `json:"method"`
47+ Params []interface {} `json:"params"`
4848}
4949
50- type XRPParamas struct {
50+ type LedgerParams struct {
5151 LedgerIndex string `json:"ledger_index"`
5252 Transactions bool `json:"transactions"`
5353 Expand bool `json:"expand"`
5454 OwnerFunds bool `json:"owner_funds"`
5555}
5656
5757type LedgerResponse struct {
58- Result XRPResult `json:"result"`
58+ Result LedgerResult `json:"result"`
5959}
6060
61- type XRPResult struct {
61+ type LedgerResult struct {
6262 LedgerIndex uint64 `json:"ledger_index"`
6363 LedgerHash string `json:"ledger_hash"`
6464 Validated bool `json:"validated"`
@@ -101,21 +101,39 @@ type XRPFields struct {
101101 Balance json.RawMessage `json:"Balance"`
102102}
103103
104- var getLatestStruct LedgerRequest
104+ type ServerInfoRequest struct {
105+ Method string `json:"method"`
106+ }
107+
108+ type ServerInfoResponse struct {
109+ Result ServerInfoResult `json:"result"`
110+ }
111+
112+ type ServerInfoResult struct {
113+ Info ServerInfo `json:"info"`
114+ }
115+
116+ type ServerInfo struct {
117+ BuildVersion string `json:"build_version"`
118+ ServerState string `json:"server_state"`
119+ }
120+
121+ var getLatestParams LedgerParams
122+ var getServerState XRPRequest
105123
106124func init () {
107- getLatestStruct = LedgerRequest {
108- Method : "ledger " ,
109- Params : [] XRPParamas {{
110- LedgerIndex : "validated" ,
111- Transactions : false ,
112- Expand : false ,
113- OwnerFunds : false ,
114- }} ,
125+ getLatestParams = LedgerParams {
126+ LedgerIndex : "validated " ,
127+ Transactions : false ,
128+ Expand : false ,
129+ OwnerFunds : false ,
130+ }
131+ getServerState = XRPRequest {
132+ Method : "server_info" ,
115133 }
116134}
117135
118- func (c XRPClient ) GetLedgerResponse (ctx context.Context , request LedgerRequest ) (* LedgerResponse , error ) {
136+ func (c XRPClient ) GetResponse (ctx context.Context , request XRPRequest ) ([] byte , error ) {
119137 getReq , err := json .Marshal (request )
120138 if err != nil {
121139 return nil , err
@@ -144,6 +162,20 @@ func (c XRPClient) GetLedgerResponse(ctx context.Context, request LedgerRequest)
144162 return nil , err
145163 }
146164
165+ return resBody , nil
166+ }
167+
168+ func (c XRPClient ) GetLedgerResponse (ctx context.Context , params LedgerParams ) (* LedgerResponse , error ) {
169+ request := XRPRequest {
170+ Method : "ledger" ,
171+ Params : []interface {}{params },
172+ }
173+
174+ resBody , err := c .GetResponse (ctx , request )
175+ if err != nil {
176+ return nil , err
177+ }
178+
147179 var respStruct LedgerResponse
148180 err = json .Unmarshal (resBody , & respStruct )
149181 if err != nil {
@@ -154,7 +186,7 @@ func (c XRPClient) GetLedgerResponse(ctx context.Context, request LedgerRequest)
154186}
155187
156188func (c XRPClient ) GetLatestBlockInfo (ctx context.Context ) (* indexer.BlockInfo , error ) {
157- respStruct , err := c .GetLedgerResponse (ctx , getLatestStruct )
189+ respStruct , err := c .GetLedgerResponse (ctx , getLatestParams )
158190 if err != nil {
159191 return nil , err
160192 }
@@ -166,16 +198,13 @@ func (c XRPClient) GetLatestBlockInfo(ctx context.Context) (*indexer.BlockInfo,
166198}
167199
168200func (c XRPClient ) GetBlockTimestamp (ctx context.Context , blockNum uint64 ) (uint64 , error ) {
169- getBlockStruct := LedgerRequest {
170- Method : "ledger" ,
171- Params : []XRPParamas {{
172- LedgerIndex : strconv .Itoa (int (blockNum )),
173- Transactions : false ,
174- Expand : false ,
175- OwnerFunds : false ,
176- }},
201+ getBlockParams := LedgerParams {
202+ LedgerIndex : strconv .Itoa (int (blockNum )),
203+ Transactions : false ,
204+ Expand : false ,
205+ OwnerFunds : false ,
177206 }
178- respStruct , err := c .GetLedgerResponse (ctx , getBlockStruct )
207+ respStruct , err := c .GetLedgerResponse (ctx , getBlockParams )
179208 if err != nil {
180209 return 0 , err
181210 }
@@ -185,16 +214,13 @@ func (c XRPClient) GetBlockTimestamp(ctx context.Context, blockNum uint64) (uint
185214
186215func (c XRPClient ) GetBlockResult (ctx context.Context , blockNum uint64 ,
187216) (* indexer.BlockResult [Block , Transaction ], error ) {
188- getBlockStruct := LedgerRequest {
189- Method : "ledger" ,
190- Params : []XRPParamas {{
191- LedgerIndex : strconv .Itoa (int (blockNum )),
192- Transactions : true ,
193- Expand : true ,
194- OwnerFunds : false ,
195- }},
217+ getBlockParams := LedgerParams {
218+ LedgerIndex : strconv .Itoa (int (blockNum )),
219+ Transactions : true ,
220+ Expand : true ,
221+ OwnerFunds : false ,
196222 }
197- respStruct , err := c .GetLedgerResponse (ctx , getBlockStruct )
223+ respStruct , err := c .GetLedgerResponse (ctx , getBlockParams )
198224 if err != nil {
199225 return nil , err
200226 }
@@ -329,3 +355,17 @@ func sourceAddressesRoot(tx XRPTransaction) (string, error) {
329355
330356 return "" , nil
331357}
358+
359+ func (c XRPClient ) GetServerInfo (ctx context.Context ) (string , error ) {
360+ resBody , err := c .GetResponse (ctx , getServerState )
361+ if err != nil {
362+ return "" , err
363+ }
364+ var respStruct ServerInfoResponse
365+ err = json .Unmarshal (resBody , & respStruct )
366+ if err != nil {
367+ return "" , err
368+ }
369+
370+ return respStruct .Result .Info .BuildVersion + "_" + respStruct .Result .Info .ServerState , nil
371+ }
0 commit comments