Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
use txnbuild
Browse files Browse the repository at this point in the history
  • Loading branch information
poliha committed Jul 25, 2019
1 parent 3594fef commit 3c7533a
Show file tree
Hide file tree
Showing 19 changed files with 351 additions and 266 deletions.
6 changes: 3 additions & 3 deletions api/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package api
import (
"fmt"

"github.com/stellar/go/build"
hProtocol "github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/txnbuild"
"github.com/stellar/kelp/model"
)

Expand Down Expand Up @@ -222,8 +222,8 @@ type Balance struct {

// ExchangeShim is the interface we use as a generic API for all crypto exchanges
type ExchangeShim interface {
SubmitOps(ops []build.TransactionMutator, asyncCallback func(hash string, e error)) error
SubmitOpsSynch(ops []build.TransactionMutator, asyncCallback func(hash string, e error)) error // forced synchronous version of SubmitOps
SubmitOps(ops []txnbuild.Operation, asyncCallback func(hash string, e error)) error
SubmitOpsSynch(ops []txnbuild.Operation, asyncCallback func(hash string, e error)) error // forced synchronous version of SubmitOps
GetBalanceHack(asset hProtocol.Asset) (*Balance, error)
LoadOffersHack() ([]hProtocol.Offer, error)
Constrainable
Expand Down
10 changes: 5 additions & 5 deletions api/strategy.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package api

import (
"github.com/stellar/go/build"
hProtocol "github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/txnbuild"
"github.com/stellar/kelp/model"
)

// Strategy represents some logic for a bot to follow while doing market making
type Strategy interface {
PruneExistingOffers(buyingAOffers []hProtocol.Offer, sellingAOffers []hProtocol.Offer) ([]build.TransactionMutator, []hProtocol.Offer, []hProtocol.Offer)
PruneExistingOffers(buyingAOffers []hProtocol.Offer, sellingAOffers []hProtocol.Offer) ([]txnbuild.Operation, []hProtocol.Offer, []hProtocol.Offer)
PreUpdate(maxAssetA float64, maxAssetB float64, trustA float64, trustB float64) error
UpdateWithOps(buyingAOffers []hProtocol.Offer, sellingAOffers []hProtocol.Offer) ([]build.TransactionMutator, error)
UpdateWithOps(buyingAOffers []hProtocol.Offer, sellingAOffers []hProtocol.Offer) ([]txnbuild.Operation, error)
PostUpdate() error
GetFillHandlers() ([]FillHandler, error)
}

// SideStrategy represents a strategy on a single side of the orderbook
type SideStrategy interface {
PruneExistingOffers(offers []hProtocol.Offer) ([]build.TransactionMutator, []hProtocol.Offer)
PruneExistingOffers(offers []hProtocol.Offer) ([]txnbuild.Operation, []hProtocol.Offer)
PreUpdate(maxAssetA float64, maxAssetB float64, trustA float64, trustB float64) error
UpdateWithOps(offers []hProtocol.Offer) (ops []build.TransactionMutator, newTopOffer *model.Number, e error)
UpdateWithOps(offers []hProtocol.Offer) (ops []txnbuild.Operation, newTopOffer *model.Number, e error)
PostUpdate() error
GetFillHandlers() ([]FillHandler, error)
}
5 changes: 2 additions & 3 deletions cmd/trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/nikhilsaraf/go-tools/multithreading"
"github.com/spf13/cobra"
"github.com/stellar/go/build"
"github.com/stellar/go/clients/horizonclient"
hProtocol "github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/support/config"
Expand Down Expand Up @@ -196,7 +195,7 @@ func makeExchangeShimSdex(
options inputs,
client *horizonclient.Client,
ieif *plugins.IEIF,
network build.Network,
network string,
threadTracker *multithreading.ThreadTracker,
tradingPair *model.TradingPair,
) (api.ExchangeShim, *plugins.SDEX) {
Expand Down Expand Up @@ -288,7 +287,7 @@ func makeExchangeShimSdex(

func makeStrategy(
l logger.Logger,
network build.Network,
network string,
botConfig trader.BotConfig,
client *horizonclient.Client,
sdex *plugins.SDEX,
Expand Down
61 changes: 41 additions & 20 deletions gui/backend/autogenerate_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"log"
"net/http"

"github.com/stellar/go/build"
"github.com/stellar/go/clients/horizon"
"github.com/stellar/go/clients/horizonclient"
"github.com/stellar/go/keypair"
"github.com/stellar/go/network"
hProtocol "github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/txnbuild"
"github.com/stellar/kelp/gui/model"
"github.com/stellar/kelp/plugins"
"github.com/stellar/kelp/support/kelpos"
Expand Down Expand Up @@ -99,38 +99,59 @@ func (s *APIServer) autogenerateBot(w http.ResponseWriter, r *http.Request) {
}

func (s *APIServer) setupAccount(address string, signer string, botName string) error {
_, e := s.checkFundAccount(address, botName)
fundedAccount, e := s.checkFundAccount(address, botName)
if e != nil {
return fmt.Errorf("error checking and funding account: %s\n", e)
}

client := horizon.DefaultTestNetClient
txn, e := build.Transaction(
build.SourceAccount{AddressOrSeed: address},
build.AutoSequence{SequenceProvider: client},
build.TestNetwork,
build.Trust("COUPON", "GBMMZMK2DC4FFP4CAI6KCVNCQ7WLO5A7DQU7EC7WGHRDQBZB763X4OQI"),
build.Payment(
build.Destination{AddressOrSeed: address},
build.CreditAmount{Code: "COUPON", Issuer: "GBMMZMK2DC4FFP4CAI6KCVNCQ7WLO5A7DQU7EC7WGHRDQBZB763X4OQI", Amount: "1000.0"},
build.SourceAccount{AddressOrSeed: "GBMMZMK2DC4FFP4CAI6KCVNCQ7WLO5A7DQU7EC7WGHRDQBZB763X4OQI"},
),
)
var txOps []txnbuild.Operation
trustOp := txnbuild.ChangeTrust{
Line: txnbuild.CreditAsset{Code: "COUPON",
Issuer: "GBMMZMK2DC4FFP4CAI6KCVNCQ7WLO5A7DQU7EC7WGHRDQBZB763X4OQI"},
}
txOps = append(txOps, &trustOp)

paymentOp := txnbuild.Payment{
Destination: address,
Amount: "1000.0",
Asset: txnbuild.CreditAsset{Code: "COUPON",
Issuer: "GBMMZMK2DC4FFP4CAI6KCVNCQ7WLO5A7DQU7EC7WGHRDQBZB763X4OQI"},
SourceAccount: &txnbuild.SimpleAccount{AccountID: "GBMMZMK2DC4FFP4CAI6KCVNCQ7WLO5A7DQU7EC7WGHRDQBZB763X4OQI"},
}
txOps = append(txOps, &paymentOp)

tx := txnbuild.Transaction{
SourceAccount: fundedAccount,
Operations: txOps,
Timebounds: txnbuild.NewInfiniteTimeout(),
Network: network.TestNetworkPassphrase,
BaseFee: 100,
}
e = tx.Build()

if e != nil {
return fmt.Errorf("cannot create trustline transaction for account %s for bot '%s': %s\n", address, botName, e)
}

txnS, e := txn.Sign(signer, issuerSeed)
if e != nil {
return fmt.Errorf("cannot sign trustline transaction for account %s for bot '%s': %s\n", address, botName, e)
for _, s := range []string{signer, issuerSeed} {
kp, e := keypair.Parse(s)
if e != nil {
return fmt.Errorf("cannot parse seed %s required for signing: %s\n", s, e)
}

e = tx.Sign(kp.(*keypair.Full))
if e != nil {
return fmt.Errorf("cannot sign trustline transaction for account %s for bot '%s': %s\n", address, botName, e)
}
}

txn64, e := txnS.Base64()
txn64, e := tx.Base64()
if e != nil {
return fmt.Errorf("cannot convert trustline transaction to base64 for account %s for bot '%s': %s\n", address, botName, e)
}

resp, e := client.SubmitTransaction(txn64)
client := horizonclient.DefaultTestNetClient
resp, e := client.SubmitTransactionXDR(txn64)
if e != nil {
return fmt.Errorf("error submitting change trust transaction for address %s for bot '%s': %s\n", address, botName, e)
}
Expand Down
49 changes: 35 additions & 14 deletions gui/backend/upsert_bot_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import (
"net/http"
"strings"

"github.com/stellar/go/build"
"github.com/stellar/go/clients/horizon"
"github.com/stellar/go/clients/horizonclient"
"github.com/stellar/go/keypair"
"github.com/stellar/go/network"
hProtocol "github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/strkey"
"github.com/stellar/go/txnbuild"
"github.com/stellar/kelp/gui/model"
"github.com/stellar/kelp/plugins"
"github.com/stellar/kelp/support/kelpos"
Expand Down Expand Up @@ -219,11 +221,11 @@ func (s *APIServer) reinitBotCheck(req upsertBotConfigRequest) {
}

func (s *APIServer) checkAddTrustline(account hProtocol.Account, kp keypair.KP, traderSeed string, botName string, isTestnet bool, assets []hProtocol.Asset) error {
network := build.PublicNetwork
client := horizon.DefaultPublicNetClient
activeNetwork := network.PublicNetworkPassphrase
client := horizonclient.DefaultPublicNetClient
if isTestnet {
network = build.TestNetwork
client = horizon.DefaultTestNetClient
activeNetwork = network.TestNetworkPassphrase
client = horizonclient.DefaultTestNetClient
}

// find trustlines to be added
Expand Down Expand Up @@ -255,31 +257,50 @@ func (s *APIServer) checkAddTrustline(account hProtocol.Account, kp keypair.KP,

// build txn
address := kp.Address()
muts := []build.TransactionMutator{
build.SourceAccount{AddressOrSeed: address},
build.AutoSequence{SequenceProvider: client},
network,
accountReq := horizonclient.AccountRequest{AccountID: address}
account, err := horizonclient.DefaultTestNetClient.AccountDetail(accountReq)
if err != nil {
return fmt.Errorf("Unable to load account for %s\n: %s", address, err)
}

var txOps []txnbuild.Operation
for _, a := range trustlines {
muts = append(muts, build.Trust(a.Code, a.Issuer))

trustOp := txnbuild.ChangeTrust{
Line: txnbuild.CreditAsset{Code: a.Code, Issuer: a.Issuer},
}
txOps = append(txOps, &trustOp)
log.Printf("added trust asset operation to transaction for asset: %+v\n", a)
}
tx, e := build.Transaction(muts...)

tx := txnbuild.Transaction{
SourceAccount: &account,
Operations: txOps,
Timebounds: txnbuild.NewInfiniteTimeout(),
Network: activeNetwork,
BaseFee: 100,
}
e := tx.Build()
if e != nil {
return fmt.Errorf("cannot create trustline transaction for account %s for bot '%s': %s\n", address, botName, e)
}

txnS, e := tx.Sign(traderSeed)
kpSigner, e := keypair.Parse(traderSeed)
if e != nil {
return fmt.Errorf("cannot parse seed %s required for signing: %s\n", traderSeed, e)
}

e = tx.Sign(kpSigner.(*keypair.Full))
if e != nil {
return fmt.Errorf("cannot sign trustline transaction for account %s for bot '%s': %s\n", address, botName, e)
}

txn64, e := txnS.Base64()
txn64, e := tx.Base64()
if e != nil {
return fmt.Errorf("cannot convert trustline transaction to base64 for account %s for bot '%s': %s\n", address, botName, e)
}

txSuccess, e := client.SubmitTransaction(txn64)
txSuccess, e := client.SubmitTransactionXDR(txn64)
if e != nil {
var herr *horizon.Error
switch t := e.(type) {
Expand Down
Loading

0 comments on commit 3c7533a

Please sign in to comment.