英文:
Converting Go gRPC call to Node.js
问题
有一个函数使用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
我需要将其转换为JavaScript,我正在使用支持使用JavaScript进行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的任何帮助,将会很有帮助。
英文:
There is a function that uses grpc call to get certain data from a grpc node.
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
}
code taken from https://github.com/iotexproject/pharos/blob/master/handler/handler_votes.go
I need to convert this to js, I am using this library https://docs.iotex.io/native-development/reference-code/call-any-rpc-method which supports rpc calls using js for ioTex network.
const state = await antenna.iotx.readState({
protocolID: "",
methodName: "",
arguments: "",
});
RPC call document https://docs.iotex.io/reference/node-core-api-grpc#readstate
Any help on how we can rebuild this call from GO to Node.js would be helpful.
答案1
得分: 1
好的,以下是代码的中文翻译:
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,
});
英文:
Ok so after playing a bit I was able to solve this myself below is code for future references
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,
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论