[英]Converting Go gRPC call to Node.js
有一個 function 使用 grpc 調用從 grpc 節點獲取某些數據。
func GetVotesByAddr(r *http.Request, cli iotexapi.APIServiceClient) (proto.Message, error) {
method := &iotexapi.ReadStakingDataMethod{
Method: iotexapi.ReadStakingDataMethod_BUCKETS_BY_VOTER,
}
methodData, err := proto.Marshal(method)
if err != nil {
return nil, err
}
vars := mux.Vars(r)
readStakingdataRequest := &iotexapi.ReadStakingDataRequest{
Request: &iotexapi.ReadStakingDataRequest_BucketsByVoter{
BucketsByVoter: &iotexapi.ReadStakingDataRequest_VoteBucketsByVoter{
VoterAddress: vars["addr"],
Pagination: &iotexapi.PaginationParam{
Offset: uint32(0),
Limit: uint32(1000),
},
},
},
}
requestData, err := proto.Marshal(readStakingdataRequest)
if err != nil {
return nil, err
}
request := &iotexapi.ReadStateRequest{
ProtocolID: []byte("staking"),
MethodName: methodData,
Arguments: [][]byte{requestData},
}
response, err := cli.ReadState(context.Background(), request)
if err != nil {
return nil, err
}
bucketlist := &iotextypes.VoteBucketList{}
if err := proto.Unmarshal(response.Data, bucketlist); err != nil {
return nil, err
}
return bucketlist, nil
}
代碼取自https://github.com/iotexproject/pharos/blob/master/handler/handler_votes.go
我需要將其轉換為 js,我正在使用這個庫https://docs.iotex.io/native-development/reference-code/call-any-rpc-method ,它支持使用 js 進行 ioTex 網絡的 rpc 調用。
const state = await antenna.iotx.readState({
protocolID: "",
methodName: "",
arguments: "",
});
RPC調用文檔https://docs.iotex.io/reference/node-core-api-grpc#readstate
關於如何從 GO 到 Node.js 重建此調用的任何幫助都會有所幫助。
好的,所以在玩了一下之后,我自己解決了這個問題,下面是供將來參考的代碼
const state = await antenna.iotx.readState({
protocolID: Buffer.from("staking"),
methodName: IReadStakingDataMethodToBuffer({
method: IReadStakingDataMethodName.BUCKETS_BY_VOTER,
}),
arguments: [
IReadStakingDataRequestToBuffer({
bucketsByVoter: {
voterAddress: ioAddress,
pagination: {
offset: 0,
limit: 1000,
},
},
}),
],
height: undefined,
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.