Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

txnbuild: add max limit to change trust #1269

Merged
merged 3 commits into from
May 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be a const?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope you will get an error because the value is returned from a method


// 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you confirm by looking e.g. in Stellar lab that the limit was set as expected in the XDR? If so this is fine - just include the link to the lab.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep.. will add link

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")
}