Skip to content

Latest commit

 

History

History

txnbuild

txnbuild

txnbuild is a Stellar SDK, implemented in Go. It provides a reference implementation of the complete set of operations that compose transactions for the Stellar distributed ledger.

This project is maintained by the Stellar Development Foundation.

  import (
	"log"
	
	"github.com/stellar/go/clients/horizonclient"
	"github.com/stellar/go/keypair"
	"github.com/stellar/go/network"
	"github.com/stellar/go/txnbuild"
	)

	// Make a keypair for a known account from a secret seed
	kp, _ := keypair.Parse("SBPQUZ6G4FZNWFHKUWC5BEYWF6R52E3SEP7R3GWYSM2XTKGF5LNTWW4R")

	// Get the current state of the account from the network
	client := horizonclient.DefaultTestNetClient
	ar := horizonclient.AccountRequest{AccountID: kp.Address()}
	sourceAccount, err := client.AccountDetail(ar)
  	if err != nil {
    		log.Println(err)
  	}

	// Build an operation to create and fund a new account
	op := txnbuild.CreateAccount{
		Destination: "GCCOBXW2XQNUSL467IEILE6MMCNRR66SSVL4YQADUNYYNUVREF3FIV2Z",
		Amount:      "10",
	}

	// Construct the transaction that holds the operations to execute on the network
	tx := txnbuild.Transaction{
		SourceAccount: &sourceAccount,
		Operations:    []txnbuild.Operation{&op},
		Timebounds:    txnbuild.NewTimebounds(0, 300),
		Network:       network.TestNetworkPassphrase,
	}

	// Serialise, sign and encode the transaction
	txe, err := tx.BuildSignEncode(kp.(*keypair.Full))
  	if err != nil {
    		log.Println(err)
  	}

	// Send the transaction to the network
	resp, err := client.SubmitTransactionXDR(txeBase64)
  	if err != nil {
    		log.Println(err)
  	}

Getting Started

This library is aimed at developers building Go applications on top of the Stellar network. Transactions constructed by this library may be submitted to any Horizon instance for processing onto the ledger, using any Stellar SDK client. The recommended client for Go programmers is horizonclient. Together, these two libraries provide a complete Stellar SDK.

An easy-to-follow demonstration that exercises this SDK on the TestNet with actual accounts is also included! See the Demo section below.

Prerequisites

  • Go 1.10 or greater

Installing

  • Download the Stellar Go monorepo: git clone git@github.com:stellar/go.git
  • Enter the source directory: cd $GOPATH/src/github.com/stellar/go
  • Download external dependencies: dep ensure -v

Running the tests

Run the unit tests from the package directory: go test

Demo

To see the SDK in action, build and run the demo:

  • Enter the demo directory: cd $GOPATH/src/github.com/stellar/go/txnbuild/cmd/demo
  • Build the demo: go build
  • Run the demo: ./demo init

Contributing

Please read Code of Conduct to understand this project's communication rules.

To submit improvements and fixes to this library, please see CONTRIBUTING.

License

This project is licensed under the Apache License - see the LICENSE file for details.