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

Error Parsing Price for ManageSellOffer Operation #2579

Closed
qiwu7 opened this issue May 11, 2020 · 4 comments · Fixed by #2588
Closed

Error Parsing Price for ManageSellOffer Operation #2579

qiwu7 opened this issue May 11, 2020 · 4 comments · Fixed by #2588
Assignees
Labels
bug txnbuild 2nd-generation transaction build library for Go SDK

Comments

@qiwu7
Copy link

qiwu7 commented May 11, 2020

What version are you using?

5b5b4d6

What did you do?

use the following to retrieve envelope xdr

curl --location --request GET 'https://horizon-testnet.stellar.org/ledgers/142220/transactions' | jq '._embedded.records[1].envelope_xdr'
func main() {
	envelopeXDR := "AAAAAgAAAABDEP4VQWm3jomKahN5UCW4Bmxfym0cgn12vT4ThV81SgAAAGQAAiuJAAAAAgAAAAAAAAAAAAAAAQAAAAEAAAAAQxD+FUFpt46JimoTeVAluAZsX8ptHIJ9dr0+E4VfNUoAAAADAAAAAVNFQwAAAAAA4oSfAJtLwUw3su8iSHJB8refwQ45OL55+YxCq7vFHJEAAAAAAAAAADuaygAAAAABO5rKAAAAAAAAAAAAAAAAAAAAAAGFXzVKAAAAQLSJWk7CPp1N29pETu9uKYDJqqsHoH4LtsJV9aL6/nI5UNzcRGe0L4C0x47xMacezObrIw4N1Y6tbQ3LPla/sg4="
	genericTx, err := txnbuild.TransactionFromXDR(envelopeXDR)
	if err != nil {
		fmt.Println("error building generic transaction from envelope xdr")
	}
	// Unpacks the GenericTransaction instance into a txnbuild.Transaction
	tx, ok := genericTx.Transaction()
	if !ok {
		fmt.Println("error unpacking generic tx")
	}

	for _, op := range tx.Operations() {
		xdrOp, err := op.BuildXDR()
		if err != nil {
			fmt.Printf("error building xdrOp from txnbuild.Operation: %v\n", err)
		} else {
			fmt.Printf("successfully building xdrOp from txnbuild.Operation: %v\n", xdrOp)
		}
	}
}

then saw the following error

error building xdrOp from txnbuild.Operation: failed to parse 'Price': Couldn't find approximation

What did you expect to see?

I expect to build the xdr.Operation from the txnbuild.Operation successfully

What did you see instead?

But I saw an error while building it

error building xdrOp from txnbuild.Operation: failed to parse 'Price': Couldn't find approximation

Possible location that causes the failure

go/txnbuild/manage_offer.go

Lines 102 to 105 in 8c55e6b

xdrPrice, err := price.Parse(mo.Price)
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to parse 'Price'")
}

@qiwu7 qiwu7 added the bug Something isn't working label May 11, 2020
@leighmcculloch leighmcculloch added txnbuild 2nd-generation transaction build library for Go SDK bug and removed bug Something isn't working labels May 11, 2020
@tamirms tamirms self-assigned this May 12, 2020
@ire-and-curses
Copy link
Member

ire-and-curses commented May 12, 2020

https://laboratory.stellar.org/#xdr-viewer?input=AAAAAgAAAABDEP4VQWm3jomKahN5UCW4Bmxfym0cgn12vT4ThV81SgAAAGQAAiuJAAAAAgAAAAAAAAAAAAAAAQAAAAEAAAAAQxD%2BFUFpt46JimoTeVAluAZsX8ptHIJ9dr0%2BE4VfNUoAAAADAAAAAVNFQwAAAAAA4oSfAJtLwUw3su8iSHJB8refwQ45OL55%2BYxCq7vFHJEAAAAAAAAAADuaygAAAAABO5rKAAAAAAAAAAAAAAAAAAAAAAGFXzVKAAAAQLSJWk7CPp1N29pETu9uKYDJqqsHoH4LtsJV9aL6%2FnI5UNzcRGe0L4C0x47xMacezObrIw4N1Y6tbQ3LPla%2Fsg4%3D&type=TransactionEnvelope&network=test

It seems to be a rounding problem in price.

Numerator: 1
Denominator: 1000000000
Result of price.StringFromFloat64(float64(result.Price.N) / float64(result.Price.D)): 0.0000000

//StringFromFloat64 will format a float64 to decimal representation with 7 digits after the decimal point
func StringFromFloat64(v float64) string {
	return strconv.FormatFloat(v, 'f', 7, 64)
}

@ire-and-curses
Copy link
Member

Underlying issues that also need fixing: #2514, #2515

@bartekn
Copy link
Contributor

bartekn commented May 12, 2020

Yes, looks like #2514 should fix this one. The code in

go/txnbuild/manage_offer.go

Lines 126 to 150 in 5b5b4d6

func (mo *ManageSellOffer) FromXDR(xdrOp xdr.Operation) error {
result, ok := xdrOp.Body.GetManageSellOfferOp()
if !ok {
return errors.New("error parsing manage_sell_offer operation from xdr")
}
mo.SourceAccount = accountFromXDR(xdrOp.SourceAccount)
mo.OfferID = int64(result.OfferId)
mo.Amount = amount.String(result.Amount)
if result.Price != (xdr.Price{}) {
mo.Price = price.StringFromFloat64(float64(result.Price.N) / float64(result.Price.D))
}
buyingAsset, err := assetFromXDR(result.Buying)
if err != nil {
return errors.Wrap(err, "error parsing buying_asset in manage_sell_offer operation")
}
mo.Buying = buyingAsset
sellingAsset, err := assetFromXDR(result.Selling)
if err != nil {
return errors.Wrap(err, "error parsing selling_asset in manage_sell_offer operation")
}
mo.Selling = sellingAsset
return nil
}
tries to find a string/decimal representation of the price because manage offer builder accepts strings only. When #2514 is done we can simply pass N and D.

@qiwu7
Copy link
Author

qiwu7 commented May 13, 2020

do we have an eta for this issue? Appreciated 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug txnbuild 2nd-generation transaction build library for Go SDK
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants