Skip to content

Commit

Permalink
txnbuild: add max limit to change trust (#1269)
Browse files Browse the repository at this point in the history
* add max limit to change trust

* update changelog

* review changes
  • Loading branch information
poliha authored May 13, 2019
1 parent 4a5fd62 commit 38839a0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
5 changes: 5 additions & 0 deletions txnbuild/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
All notable changes to this project will be documented in this
file. This project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased [v1.2.0]

* Added `MaxTrustlineLimit` which represents the maximum value for a trustline limit.
* ChangeTrust operation with no `Limit` field set now defaults to `MaxTrustlineLimit`.

## [v1.1.0](https://github.com/stellar/go/releases/tag/horizonclient-v1.1.0) - 2019-02-02

* Support for multiple signatures ([#1198](https://github.com/stellar/go/pull/1198))
Expand Down
12 changes: 11 additions & 1 deletion txnbuild/change_trust.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package txnbuild

import (
"math"

"github.com/stellar/go/amount"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/xdr"
)

// ChangeTrust represents the Stellar change trust operation. See
// https://www.stellar.org/developers/guides/concepts/list-of-operations.html
// https://www.stellar.org/developers/guides/concepts/list-of-operations.html.
// If Limit is omitted, it defaults to txnbuild.MaxTrustlineLimit.
type ChangeTrust struct {
Line Asset
Limit string
SourceAccount Account
}

// MaxTrustlineLimit represents the maximum value that can be set as a trustline limit.
var MaxTrustlineLimit = amount.StringFromInt64(math.MaxInt64)

// RemoveTrustlineOp returns a ChangeTrust operation to remove the trustline of the described asset,
// by setting the limit to "0".
func RemoveTrustlineOp(issuedAsset Asset) ChangeTrust {
Expand All @@ -33,6 +39,10 @@ func (ct *ChangeTrust) BuildXDR() (xdr.Operation, error) {
return xdr.Operation{}, errors.Wrap(err, "can't convert trustline asset to XDR")
}

if ct.Limit == "" {
ct.Limit = MaxTrustlineLimit
}

xdrLimit, err := amount.Parse(ct.Limit)
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to parse limit amount")
Expand Down
29 changes: 29 additions & 0 deletions txnbuild/change_trust_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package txnbuild

import (
"testing"

"github.com/stellar/go/network"
"github.com/stretchr/testify/assert"
)

func TestChangeTrustMaxLimit(t *testing.T) {
kp0 := newKeypair0()
txSourceAccount := makeTestAccount(kp0, "9605939170639898")

changeTrust := ChangeTrust{
Line: CreditAsset{"ABCD", kp0.Address()},
}

tx := Transaction{
SourceAccount: &txSourceAccount,
Operations: []Operation{&changeTrust},
Timebounds: NewInfiniteTimeout(),
Network: network.TestNetworkPassphrase,
}
received := buildSignEncode(t, tx, kp0)

// https://www.stellar.org/laboratory/#xdr-viewer?input=AAAAAODcbeFyXKxmUWK1L6znNbKKIkPkHRJNbLktcKPqLnLFAAAAZAAiII0AAAAbAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAGAAAAAUFCQ0QAAAAA4Nxt4XJcrGZRYrUvrOc1sooiQ%2BQdEk1suS1wo%2BoucsV%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwAAAAAAAAAB6i5yxQAAAEBen%2BAa821qqfb%2BASHhCg074ulPcCbIsUNvzUg2x92R%2FvRRKIGF5RztvPGBkktWkXLarDe5yqlBA%2BL3zpcVs%2BwB&type=TransactionEnvelope&network=test
expected := "AAAAAODcbeFyXKxmUWK1L6znNbKKIkPkHRJNbLktcKPqLnLFAAAAZAAiII0AAAAbAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAGAAAAAUFCQ0QAAAAA4Nxt4XJcrGZRYrUvrOc1sooiQ+QdEk1suS1wo+oucsV//////////wAAAAAAAAAB6i5yxQAAAEBen+Aa821qqfb+ASHhCg074ulPcCbIsUNvzUg2x92R/vRRKIGF5RztvPGBkktWkXLarDe5yqlBA+L3zpcVs+wB"
assert.Equal(t, expected, received, "Base 64 XDR should match")
}

0 comments on commit 38839a0

Please sign in to comment.