terra

package module
Version: v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 7, 2021 License: MIT Imports: 40 Imported by: 0

README

terra.go

Prerequisites

  • >= go 1.15

Install

$ go get github.com/frostornge/terra-go

Packages

  • bind
    • contract binding helper ref
  • httpclient
    • http middleware to process codec encoded respones
  • interface
    • main contract binding (WIP)
  • service
    • LCD biding
  • types

How to use

import (
  "log"
  
  terra "github.com/frostornge/terra-go"
  "github.com/frostornge/terra-go/httpclient"
  "github.com/frostornge/terra-go/types"
  
  "github.com/cosmos/cosmos-sdk/crypto/keys"
  cosmostypes "github.com/cosmos/cosmos-sdk/types"
  terraapp "github.com/terra-project/core/app"
  terratypes "github.com/terra-project/core/types"
)

func must(err error) {
  if err != nil {
    panic(err)
  }
}

func main() {
  lcdClient := terra.NewClient(
    httpclient.New(
      terraapp.MakeCodec(), 
      "https://tequila-lcd.terra.dev",
    )
  )
 
  var (
    acc terra.Account
    key terra.Key
    err error
  )
 
  // with hexed ECDSA private key
  {
    key = terra.NewRawKey("{private_key}")
    acc, err = terra.NewAccount(context.Background(), lcdClient, key)
    must(err)
  }
  
  // with KeyBase
  {
    keyBase := keys.NewInMemory()
    _, err = keyBase.CreateAccount(
      "example", "{mnemonic}", 
      keys.DefaultBIP39Passphrase, "{passphrase}", 
      terratypes.FullFundraiserPath, keys.Secp256k1,
    )
    must(err)
    
    key, err = terra.NewWalletKey("example", "{passphrase}", keyBase)
    must(err)
    
    acc, err = terra.NewAccount(context.Background(), lcdClient, key)
    must(err)
  }


  // example#1 - bank send
  {
    var (
      to     cosmostypes.AccAddress
      amount cosmostypes.Int
    )
  
    tx, msg, err := acc.CreateAndSignTx(context.Background(), terra.CreateTxOptions{
      Msgs: []cosmostypes.Msg{
        bank.MsgSend{
          FromAddress: acc.GetAddress(),
          ToAddress: to,
          Amount: cosmostypes.Coins{{
            Denom: "uusd",
            Amount: amount,
          }},
        }
      },
    })
    must(err)
    
    txResp, err := lcdClient.Transaction().BroadcastTx(ctx, tx, types.ModeBlock)
    must(err)
    
    log.Println(txResp.TxHash)
  }
  
}

LICENSE

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultGasAdjustment = 1.2
	DefaultGasPrice      = cosmostypes.DecCoins{{
		Denom:  terraassets.MicroLunaDenom,
		Amount: cosmostypes.NewDecWithPrec(150000, 6),
	}}
)

Functions

func MakeCodec added in v0.0.3

func MakeCodec() *codec.Codec

Types

type Account

type Account interface {
	GetClient() Client
	GetChainId() string
	Update(ctx context.Context) error
	CreateTx(ctx context.Context, opts CreateTxOptions) (terraauth.StdSignMsg, error)
	CreateAndSignTx(ctx context.Context, opts CreateTxOptions) (terraauth.StdTx, terraauth.StdSignMsg, error)

	cosmosauth.Account
}

func NewAccount

func NewAccount(ctx context.Context, client Client, key Key) (Account, error)

type Client

type Client interface {
	Auth() service.AuthService
	Bank() service.BankService
	Contract() service.ContractService
	Treasury() service.TreasuryService
	Tendermint() service.TendermintService
	Transaction() service.TransactionService
}

func NewClient

func NewClient(client httpclient.Client) Client

type CreateTxOptions

type CreateTxOptions struct {
	Msgs          []cosmostypes.Msg
	Fee           *terraauth.StdFee
	GasAdjustment float64
	GasPrices     cosmostypes.DecCoins
	Sequence      *uint64
	Memo          string
}

type Key

type Key interface {
	AccAddress() cosmostypes.AccAddress
	ValAddress() cosmostypes.ValAddress
	PubKey() crypto.PubKey

	SignTx(msg terraauth.StdSignMsg) (terraauth.StdTx, error)
	MakeSignature(msg terraauth.StdSignMsg) (terraauth.StdSignature, error)
}

func NewRawKey

func NewRawKey(hexed string) Key

func NewWalletKey

func NewWalletKey(name, passphrase string, wallet keys.Keybase) (Key, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL