From 38839a0815683ba58e7879bfc0fc2c02ed971168 Mon Sep 17 00:00:00 2001 From: poliha Date: Mon, 13 May 2019 17:02:33 +0100 Subject: [PATCH] txnbuild: add max limit to change trust (#1269) * add max limit to change trust * update changelog * review changes --- txnbuild/CHANGELOG.md | 5 +++++ txnbuild/change_trust.go | 12 +++++++++++- txnbuild/change_trust_test.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 txnbuild/change_trust_test.go diff --git a/txnbuild/CHANGELOG.md b/txnbuild/CHANGELOG.md index 0f1022183d..252aaed860 100644 --- a/txnbuild/CHANGELOG.md +++ b/txnbuild/CHANGELOG.md @@ -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)) diff --git a/txnbuild/change_trust.go b/txnbuild/change_trust.go index 13ef94e554..9168ca3487 100644 --- a/txnbuild/change_trust.go +++ b/txnbuild/change_trust.go @@ -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 { @@ -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") diff --git a/txnbuild/change_trust_test.go b/txnbuild/change_trust_test.go new file mode 100644 index 0000000000..da5f11cce7 --- /dev/null +++ b/txnbuild/change_trust_test.go @@ -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") +}