From 009a375ccb7cad8f1507531637dd981203e1f59f Mon Sep 17 00:00:00 2001 From: Timur Ramazanov Date: Thu, 30 May 2019 15:55:00 +0300 Subject: [PATCH] Update to v11 protocol --- es/operation.go | 23 ++++++++++++----- es/operation_result.go | 57 ++++++++++++++++++++++++++---------------- 2 files changed, 53 insertions(+), 27 deletions(-) diff --git a/es/operation.go b/es/operation.go index 5d9f04a..0ad6e6e 100644 --- a/es/operation.go +++ b/es/operation.go @@ -141,10 +141,12 @@ func NewOperation(t *Transaction, o *xdr.Operation, n byte) *Operation { newPayment(o.Body.MustPaymentOp(), op) case xdr.OperationTypePathPayment: newPathPayment(o.Body.MustPathPaymentOp(), op) - case xdr.OperationTypeManageOffer: - newManageOffer(o.Body.MustManageOfferOp(), op) - case xdr.OperationTypeCreatePassiveOffer: - newCreatePassiveOffer(o.Body.MustCreatePassiveOfferOp(), op) + case xdr.OperationTypeManageSellOffer: + newManageSellOffer(o.Body.MustManageSellOfferOp(), op) + case xdr.OperationTypeManageBuyOffer: + newManageBuyOffer(o.Body.MustManageBuyOfferOp(), op) + case xdr.OperationTypeCreatePassiveSellOffer: + newCreatePassiveSellOffer(o.Body.MustCreatePassiveSellOfferOp(), op) case xdr.OperationTypeSetOptions: newSetOptions(o.Body.MustSetOptionsOp(), op) case xdr.OperationTypeChangeTrust: @@ -188,7 +190,7 @@ func newPathPayment(o xdr.PathPaymentOp, op *Operation) { } } -func newManageOffer(o xdr.ManageOfferOp, op *Operation) { +func newManageSellOffer(o xdr.ManageSellOfferOp, op *Operation) { op.SourceAmount = amount.String(o.Amount) op.SourceAsset = NewAsset(&o.Buying) op.OfferID = int(o.OfferId) @@ -197,7 +199,16 @@ func newManageOffer(o xdr.ManageOfferOp, op *Operation) { op.DestinationAsset = NewAsset(&o.Selling) } -func newCreatePassiveOffer(o xdr.CreatePassiveOfferOp, op *Operation) { +func newManageBuyOffer(o xdr.ManageBuyOfferOp, op *Operation) { + op.SourceAmount = amount.String(o.BuyAmount) + op.SourceAsset = NewAsset(&o.Selling) + op.DestinationAsset = NewAsset(&o.Buying) + op.OfferID = int(o.OfferId) + op.OfferPrice, _ = big.NewRat(int64(o.Price.N), int64(o.Price.D)).Float64() + op.OfferPriceND = &Price{int(o.Price.N), int(o.Price.D)} +} + +func newCreatePassiveSellOffer(o xdr.CreatePassiveSellOfferOp, op *Operation) { op.SourceAmount = amount.String(o.Amount) op.SourceAsset = NewAsset(&o.Buying) op.OfferPrice, _ = big.NewRat(int64(o.Price.N), int64(o.Price.D)).Float64() diff --git a/es/operation_result.go b/es/operation_result.go index 328ff48..e7be11d 100644 --- a/es/operation_result.go +++ b/es/operation_result.go @@ -19,10 +19,12 @@ func AppendResult(op *Operation, r *xdr.OperationResult) { newPaymentResult(r.Tr.MustPaymentResult(), op) case xdr.OperationTypePathPayment: newPathPaymentResult(r.Tr.MustPathPaymentResult(), op) - case xdr.OperationTypeManageOffer: - newManageOfferResult(r.Tr.MustManageOfferResult(), op) - case xdr.OperationTypeCreatePassiveOffer: - newManageOfferResult(r.Tr.MustCreatePassiveOfferResult(), op) + case xdr.OperationTypeManageSellOffer: + newManageSellOfferResult(r.Tr.MustManageSellOfferResult(), op) + case xdr.OperationTypeManageBuyOffer: + newManageBuyOfferResult(r.Tr.MustManageBuyOfferResult(), op) + case xdr.OperationTypeCreatePassiveSellOffer: + newManageSellOfferResult(r.Tr.MustCreatePassiveSellOfferResult(), op) case xdr.OperationTypeSetOptions: newSetOptionsResult(r.Tr.MustSetOptionsResult(), op) case xdr.OperationTypeChangeTrust: @@ -73,28 +75,41 @@ func newPathPaymentResult(r xdr.PathPaymentResult, op *Operation) { } } -func newManageOfferResult(r xdr.ManageOfferResult, op *Operation) { +func newManageSellOfferResult(r xdr.ManageSellOfferResult, op *Operation) { op.InnerResultCode = int(r.Code) - op.Succesful = r.Code == xdr.ManageOfferResultCodeManageOfferSuccess + op.Succesful = r.Code == xdr.ManageSellOfferResultCodeManageSellOfferSuccess if s, ok := r.GetSuccess(); ok { - op.ResultOffersClaimed = appendOffersClaimed(s.OffersClaimed) - - if o, ok := s.Offer.GetOffer(); ok { - p, _ := big.NewRat(int64(o.Price.N), int64(o.Price.D)).Float64() - - op.ResultOffer = &Offer{ - Amount: amount.String(o.Amount), - Price: p, - PriceND: Price{int(o.Price.N), int(o.Price.D)}, - Buying: *NewAsset(&o.Buying), - Selling: *NewAsset(&o.Selling), - OfferID: int64(o.OfferId), - SellerID: o.SellerId.Address(), - } + newManageOfferResult(s, op) + } +} + +func newManageBuyOfferResult(r xdr.ManageBuyOfferResult, op *Operation) { + op.InnerResultCode = int(r.Code) + op.Succesful = r.Code == xdr.ManageBuyOfferResultCodeManageBuyOfferSuccess + + if s, ok := r.GetSuccess(); ok { + newManageOfferResult(s, op) + } +} - op.ResultOfferEffect = s.Offer.Effect.String() +func newManageOfferResult(s xdr.ManageOfferSuccessResult, op *Operation) { + op.ResultOffersClaimed = appendOffersClaimed(s.OffersClaimed) + + if o, ok := s.Offer.GetOffer(); ok { + p, _ := big.NewRat(int64(o.Price.N), int64(o.Price.D)).Float64() + + op.ResultOffer = &Offer{ + Amount: amount.String(o.Amount), + Price: p, + PriceND: Price{int(o.Price.N), int(o.Price.D)}, + Buying: *NewAsset(&o.Buying), + Selling: *NewAsset(&o.Selling), + OfferID: int64(o.OfferId), + SellerID: o.SellerId.Address(), } + + op.ResultOfferEffect = s.Offer.Effect.String() } }