Julian Strobl 5b25d4cefc
Improve linter setup (#186)
* [linter] Add `musttag`

Enforce field tags in (un)marshaled structs.

* [linter] Add `nestif`

Reports deeply nested if statements.

* [linter] Add `noctx`

Finds sending http request without context.Context.

* [linter] Add `paralleltest`

Paralleltest detects missing usage of t.Parallel() method in your Go
test.

* [linter] Add `tagalign`

Check that struct tags are well aligned.

* [linter] Add `tagliatelle`

Checks the struct tags.

* [linter] Add `whitespace`

Tool for detection of leading and trailing whitespace.

* [paralleltest] Exclude files bc of data race in tests

Signed-off-by: Julian Strobl <jmastr@mailbox.org>
2023-11-17 10:56:25 +01:00
..
2023-11-17 10:56:25 +01:00
2023-11-17 10:56:25 +01:00

Library for RPC requests to Planetmint

How to use it

In the example below we use the account addr0 for which we have the private key in our keyring. We configure the address prefix andd change the default RPC endpoint to a remote one. The only keyring backend currently supported is the test backend under keyring-test. After that we construct three messages to send 10plmnt each to three addresses addr1, addr2 and addr3. We then build and sign the transaction and eventually send this transaction via RPC. For debugging purposes we print the transaction that we send as JSON.

package main

import (
        "context"
        "fmt"
        "log"

        sdk "github.com/cosmos/cosmos-sdk/types"
        banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
        "github.com/planetmint/planetmint-go/app"
        "github.com/planetmint/planetmint-go/lib"
)

func main() {
        goCtx := context.Background()
        encodingConfig := app.MakeEncodingConfig()

        libConfig := lib.GetConfig()
        libConfig.SetEncodingConfig(encodingConfig)
        libConfig.SetGRPCEndpoint("testnet-grpc.rddl.io:443")
        libConfig.SetGRPCTLSCert("/etc/ssl/certs/ca-certificates.crt")

        addr0 := sdk.MustAccAddressFromBech32("plmnt168z8fyyzap0nw75d4atv9ucr2ye60d57dzlzaf")
        addr1 := sdk.MustAccAddressFromBech32("plmnt1vklujvmr9hsk9zwpquk4waecr2u5vcyjd8vgm8")
        addr2 := sdk.MustAccAddressFromBech32("plmnt1pwquxvqmmdry4gdel4g4rz0js7jy65453h92g7")
        addr3 := sdk.MustAccAddressFromBech32("plmnt1dyuhg8ldu3d6nvhrvzzemtc3893dys9v9lvdty")

        coin := sdk.NewCoins(sdk.NewInt64Coin("plmnt", 10))
        msg1 := banktypes.NewMsgSend(addr0, addr1, coin)
        msg2 := banktypes.NewMsgSend(addr0, addr2, coin)
        msg3 := banktypes.NewMsgSend(addr0, addr3, coin)

        txBytes, txJSON, err := lib.BuildAndSignTx(goCtx, addr0, msg1, msg2, msg3)
        if err != nil {
                log.Fatal(err)
        }
        fmt.Println(txJSON)

        // Optional
        _, err = lib.SimulateTx(goCtx, txBytes)
        if err != nil {
                log.Fatal(err)
        }

        _, err = lib.BroadcastTx(goCtx, txBytes)
        if err != nil {
                log.Fatal(err)
        }
}

Sample output:

$ go run main.go|jq
{
  "body": {
    "messages": [
      {
        "@type": "/cosmos.bank.v1beta1.MsgSend",
        "from_address": "plmnt168z8fyyzap0nw75d4atv9ucr2ye60d57dzlzaf",
        "to_address": "plmnt1vklujvmr9hsk9zwpquk4waecr2u5vcyjd8vgm8",
        "amount": [
          {
            "denom": "plmnt",
            "amount": "10"
          }
        ]
      },
      {
        "@type": "/cosmos.bank.v1beta1.MsgSend",
        "from_address": "plmnt168z8fyyzap0nw75d4atv9ucr2ye60d57dzlzaf",
        "to_address": "plmnt1pwquxvqmmdry4gdel4g4rz0js7jy65453h92g7",
        "amount": [
          {
            "denom": "plmnt",
            "amount": "10"
          }
        ]
      },
      {
        "@type": "/cosmos.bank.v1beta1.MsgSend",
        "from_address": "plmnt168z8fyyzap0nw75d4atv9ucr2ye60d57dzlzaf",
        "to_address": "plmnt1dyuhg8ldu3d6nvhrvzzemtc3893dys9v9lvdty",
        "amount": [
          {
            "denom": "plmnt",
            "amount": "10"
          }
        ]
      }
    ],
    "memo": "",
    "timeout_height": "0",
    "extension_options": [],
    "non_critical_extension_options": []
  },
  "auth_info": {
    "signer_infos": [
      {
        "public_key": {
          "@type": "/cosmos.crypto.secp256k1.PubKey",
          "key": "AzthjTLaH+NCBBRH4NImcxSa9Ma59TJ0ntQE9S5TJ/wb"
        },
        "mode_info": {
          "single": {
            "mode": "SIGN_MODE_DIRECT"
          }
        },
        "sequence": "6"
      }
    ],
    "fee": {
      "amount": [
        {
          "denom": "plmnt",
          "amount": "1"
        }
      ],
      "gas_limit": "200000",
      "payer": "",
      "granter": ""
    },
    "tip": null
  },
  "signatures": [
    "iolwpJtcFPKshQWMfgvYO+EMSavdq0auicWZCNI46AIBWH6aPEca7esfqdv2m6VE4hCHzxCNx58wnfVNnutEEQ=="
  ]
}