将Go的gRPC调用转换为Node.js

huangapple go评论135阅读模式
英文:

Converting Go gRPC call to Node.js

问题

有一个函数使用gRPC调用从gRPC节点获取特定数据。

  1. func GetVotesByAddr(r *http.Request, cli iotexapi.APIServiceClient) (proto.Message, error) {
  2. method := &iotexapi.ReadStakingDataMethod{
  3. Method: iotexapi.ReadStakingDataMethod_BUCKETS_BY_VOTER,
  4. }
  5. methodData, err := proto.Marshal(method)
  6. if err != nil {
  7. return nil, err
  8. }
  9. vars := mux.Vars(r)
  10. readStakingdataRequest := &iotexapi.ReadStakingDataRequest{
  11. Request: &iotexapi.ReadStakingDataRequest_BucketsByVoter{
  12. BucketsByVoter: &iotexapi.ReadStakingDataRequest_VoteBucketsByVoter{
  13. VoterAddress: vars["addr"],
  14. Pagination: &iotexapi.PaginationParam{
  15. Offset: uint32(0),
  16. Limit: uint32(1000),
  17. },
  18. },
  19. },
  20. }
  21. requestData, err := proto.Marshal(readStakingdataRequest)
  22. if err != nil {
  23. return nil, err
  24. }
  25. request := &iotexapi.ReadStateRequest{
  26. ProtocolID: []byte("staking"),
  27. MethodName: methodData,
  28. Arguments: [][]byte{requestData},
  29. }
  30. response, err := cli.ReadState(context.Background(), request)
  31. if err != nil {
  32. return nil, err
  33. }
  34. bucketlist := &iotextypes.VoteBucketList{}
  35. if err := proto.Unmarshal(response.Data, bucketlist); err != nil {
  36. return nil, err
  37. }
  38. return bucketlist, nil
  39. }

代码来源:https://github.com/iotexproject/pharos/blob/master/handler/handler_votes.go

我需要将其转换为JavaScript,我正在使用支持使用JavaScript进行ioTex网络的RPC调用的库。

  1. const state = await antenna.iotx.readState({
  2. protocolID: "",
  3. methodName: "",
  4. arguments: "",
  5. });

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.

  1. func GetVotesByAddr(r *http.Request, cli iotexapi.APIServiceClient) (proto.Message, error) {
  2. method := &iotexapi.ReadStakingDataMethod{
  3. Method: iotexapi.ReadStakingDataMethod_BUCKETS_BY_VOTER,
  4. }
  5. methodData, err := proto.Marshal(method)
  6. if err != nil {
  7. return nil, err
  8. }
  9. vars := mux.Vars(r)
  10. readStakingdataRequest := &iotexapi.ReadStakingDataRequest{
  11. Request: &iotexapi.ReadStakingDataRequest_BucketsByVoter{
  12. BucketsByVoter: &iotexapi.ReadStakingDataRequest_VoteBucketsByVoter{
  13. VoterAddress: vars["addr"],
  14. Pagination: &iotexapi.PaginationParam{
  15. Offset: uint32(0),
  16. Limit: uint32(1000),
  17. },
  18. },
  19. },
  20. }
  21. requestData, err := proto.Marshal(readStakingdataRequest)
  22. if err != nil {
  23. return nil, err
  24. }
  25. request := &iotexapi.ReadStateRequest{
  26. ProtocolID: []byte("staking"),
  27. MethodName: methodData,
  28. Arguments: [][]byte{requestData},
  29. }
  30. response, err := cli.ReadState(context.Background(), request)
  31. if err != nil {
  32. return nil, err
  33. }
  34. bucketlist := &iotextypes.VoteBucketList{}
  35. if err := proto.Unmarshal(response.Data, bucketlist); err != nil {
  36. return nil, err
  37. }
  38. return bucketlist, nil
  39. }

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.

  1. const state = await antenna.iotx.readState({
  2. protocolID: "",
  3. methodName: "",
  4. arguments: "",
  5. });

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

好的,以下是代码的中文翻译:

  1. const state = await antenna.iotx.readState({
  2. protocolID: Buffer.from("staking"),
  3. methodName: IReadStakingDataMethodToBuffer({
  4. method: IReadStakingDataMethodName.BUCKETS_BY_VOTER,
  5. }),
  6. arguments: [
  7. IReadStakingDataRequestToBuffer({
  8. bucketsByVoter: {
  9. voterAddress: ioAddress,
  10. pagination: {
  11. offset: 0,
  12. limit: 1000,
  13. },
  14. },
  15. }),
  16. ],
  17. height: undefined,
  18. });
英文:

Ok so after playing a bit I was able to solve this myself below is code for future references

  1. const state = await antenna.iotx.readState({
  2. protocolID: Buffer.from("staking"),
  3. methodName: IReadStakingDataMethodToBuffer({
  4. method: IReadStakingDataMethodName.BUCKETS_BY_VOTER,
  5. }),
  6. arguments: [
  7. IReadStakingDataRequestToBuffer({
  8. bucketsByVoter: {
  9. voterAddress: ioAddress,
  10. pagination: {
  11. offset: 0,
  12. limit: 1000,
  13. },
  14. },
  15. }),
  16. ],
  17. height: undefined,
  18. });

huangapple
  • 本文由 发表于 2021年12月22日 18:07:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/70447321.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定