Skip to content

Commit

Permalink
Merge pull request #16 from astroband/chore/prepare-for-v12
Browse files Browse the repository at this point in the history
Handle PathPaymentStrictSend operation
  • Loading branch information
nebolsin authored Oct 22, 2019
2 parents f719d7c + 6b7ceb1 commit 24d4e11
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 29 deletions.
23 changes: 20 additions & 3 deletions es/operation_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ func (f *operationFactory) assignSpecifics() {
f.assignCreateAccount(body.MustCreateAccountOp())
case xdr.OperationTypePayment:
f.assignPayment(body.MustPaymentOp())
case xdr.OperationTypePathPayment:
f.assignPathPayment(body.MustPathPaymentOp())
case xdr.OperationTypePathPaymentStrictReceive:
f.assignPathPaymentStrictReceive(body.MustPathPaymentStrictReceiveOp())
case xdr.OperationTypePathPaymentStrictSend:
f.assignPathPaymentStrictSend(body.MustPathPaymentStrictSendOp())
case xdr.OperationTypeManageSellOffer:
f.assignManageSellOffer(body.MustManageSellOfferOp())
case xdr.OperationTypeManageBuyOffer:
Expand Down Expand Up @@ -120,7 +122,7 @@ func (f *operationFactory) assignPayment(o xdr.PaymentOp) {
f.operation.SourceAsset = NewAsset(&o.Asset)
}

func (f *operationFactory) assignPathPayment(o xdr.PathPaymentOp) {
func (f *operationFactory) assignPathPaymentStrictReceive(o xdr.PathPaymentStrictReceiveOp) {
f.operation.DestinationAccountID = o.Destination.Address()
f.operation.DestinationAmount = amount.String(o.DestAmount)
f.operation.DestinationAsset = NewAsset(&o.DestAsset)
Expand All @@ -135,6 +137,21 @@ func (f *operationFactory) assignPathPayment(o xdr.PathPaymentOp) {
}
}

func (f *operationFactory) assignPathPaymentStrictSend(o xdr.PathPaymentStrictSendOp) {

f.operation.DestinationAccountID = o.Destination.Address()
f.operation.DestinationAmount = amount.String(o.DestMin)
f.operation.DestinationAsset = NewAsset(&o.DestAsset)

f.operation.SourceAmount = amount.String(o.SendAmount)
f.operation.SourceAsset = NewAsset(&o.SendAsset)

f.operation.Path = make([]*Asset, len(o.Path))
for i, a := range o.Path {
f.operation.Path[i] = NewAsset(&a)
}
}

func (f *operationFactory) assignManageSellOffer(o xdr.ManageSellOfferOp) {
f.operation.SourceAmount = amount.String(o.Amount)
f.operation.SourceAsset = NewAsset(&o.Buying)
Expand Down
30 changes: 26 additions & 4 deletions es/operation_factory_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ func (f *operationFactory) assignResult() {
f.assignCreateAccountResult(r.Tr.MustCreateAccountResult())
case xdr.OperationTypePayment:
f.assignPaymentResult(r.Tr.MustPaymentResult())
case xdr.OperationTypePathPayment:
f.assignPathPaymentResult(r.Tr.MustPathPaymentResult())
case xdr.OperationTypePathPaymentStrictReceive:
f.assignPathPaymentStrictReceiveResult(r.Tr.MustPathPaymentStrictReceiveResult())
case xdr.OperationTypePathPaymentStrictSend:
f.assignPathPaymentStrictSendResult(r.Tr.MustPathPaymentStrictSendResult())
case xdr.OperationTypeManageSellOffer:
f.assignManageSellOfferResult(r.Tr.MustManageSellOfferResult())
case xdr.OperationTypeManageBuyOffer:
Expand Down Expand Up @@ -62,9 +64,9 @@ func (f *operationFactory) assignPaymentResult(r xdr.PaymentResult) {
f.operation.Succesful = r.Code == xdr.PaymentResultCodePaymentSuccess
}

func (f *operationFactory) assignPathPaymentResult(r xdr.PathPaymentResult) {
func (f *operationFactory) assignPathPaymentStrictReceiveResult(r xdr.PathPaymentStrictReceiveResult) {
f.operation.InnerResultCode = int(r.Code)
f.operation.Succesful = r.Code == xdr.PathPaymentResultCodePathPaymentSuccess
f.operation.Succesful = r.Code == xdr.PathPaymentStrictReceiveResultCodePathPaymentStrictReceiveSuccess

if s, ok := r.GetSuccess(); ok {
if len(s.Offers) > 0 {
Expand All @@ -82,6 +84,26 @@ func (f *operationFactory) assignPathPaymentResult(r xdr.PathPaymentResult) {
}
}

func (f *operationFactory) assignPathPaymentStrictSendResult(r xdr.PathPaymentStrictSendResult) {

f.operation.InnerResultCode = int(r.Code)
f.operation.Succesful = r.Code == xdr.PathPaymentStrictSendResultCodePathPaymentStrictSendSuccess

if s, ok := r.GetSuccess(); ok {
if len(s.Offers) > 0 {
f.operation.AmountSent = amount.String(s.Offers[0].AmountBought)
}
f.operation.ResultLastAmount = amount.String(s.Last.Amount)
f.operation.AmountReceived = f.operation.ResultLastAmount

f.operation.ResultLastAsset = NewAsset(&s.Last.Asset)
f.operation.ResultLastDestination = s.Last.Destination.Address()
}
if a, ok := r.GetNoIssuer(); ok {
f.operation.ResultNoIssuer = NewAsset(&a)
}
}

func (f *operationFactory) assignManageSellOfferResult(r xdr.ManageSellOfferResult) {
f.operation.InnerResultCode = int(r.Code)
f.operation.Succesful = r.Code == xdr.ManageSellOfferResultCodeManageSellOfferSuccess
Expand Down
27 changes: 23 additions & 4 deletions es/trade_extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ func (e *TradeExtractor) extract() (trades []Trade) {

if e.result.Code == xdr.OperationResultCodeOpInner {
switch t := e.result.Tr.Type; t {
case xdr.OperationTypePathPayment:
trades = e.fetchFromPathPayment(e.result.Tr.MustPathPaymentResult())
case xdr.OperationTypePathPaymentStrictReceive:
trades = e.fetchFromPathPaymentStrictReceive(e.result.Tr.MustPathPaymentStrictReceiveResult())
case xdr.OperationTypePathPaymentStrictSend:
trades = e.fetchFromPathPaymentStrictSend(e.result.Tr.MustPathPaymentStrictSendResult())
case xdr.OperationTypeManageSellOffer:
trades = e.fetchFromManageSellOffer(e.result.Tr.MustManageSellOfferResult())
case xdr.OperationTypeCreatePassiveSellOffer:
Expand Down Expand Up @@ -91,8 +93,8 @@ func (e *TradeExtractor) fetchFromManageBuyOffer(result xdr.ManageBuyOfferResult
return e.fetchClaims(claims, e.operation.SourceAccountID)
}

func (e *TradeExtractor) fetchFromPathPayment(result xdr.PathPaymentResult) (trades []Trade) {
if result.Code != xdr.PathPaymentResultCodePathPaymentSuccess {
func (e *TradeExtractor) fetchFromPathPaymentStrictReceive(result xdr.PathPaymentStrictReceiveResult) (trades []Trade) {
if result.Code != xdr.PathPaymentStrictReceiveResultCodePathPaymentStrictReceiveSuccess {
return trades
}

Expand All @@ -109,6 +111,23 @@ func (e *TradeExtractor) fetchFromPathPayment(result xdr.PathPaymentResult) (tra
return e.fetchClaims(claims, e.operation.SourceAccountID)
}

func (e *TradeExtractor) fetchFromPathPaymentStrictSend(result xdr.PathPaymentStrictSendResult) (trades []Trade) {
if result.Code != xdr.PathPaymentStrictSendResultCodePathPaymentStrictSendSuccess {
return trades
}

success, ok := result.GetSuccess()
if !ok {
return trades
}

claims := success.Offers
if len(claims) == 0 {
return trades
}
return e.fetchClaims(claims, e.operation.SourceAccountID)
}

func (e *TradeExtractor) fetchClaims(claims []xdr.ClaimOfferAtom, accountID string) (trades []Trade) {
for _, claim := range claims {
pagingTokenA := PagingToken{EffectGroup: TradeEffectPagingTokenGroup, EffectIndex: e.tokenIndex + 1}.Merge(e.pagingToken)
Expand Down
18 changes: 7 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@ module github.com/astroband/astrologer
go 1.12

require (
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc // indirect
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect
github.com/elastic/go-elasticsearch/v7 v7.2.0
github.com/gammazero/deque v0.0.0-20190515085511-50f5fc498a27
github.com/gammazero/workerpool v0.0.0-20190515092108-567ff334f003
github.com/guregu/null v3.4.0+incompatible
github.com/jmoiron/sqlx v0.0.0-20190319043955-cdf62fdf55f6
github.com/lib/pq v1.0.0
github.com/mattn/go-runewidth v0.0.4
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db
github.com/jmoiron/sqlx v1.2.0
github.com/lib/pq v1.2.0
github.com/mattn/go-runewidth v0.0.4 // indirect
github.com/olekukonko/tablewriter v0.0.1
github.com/pkg/errors v0.8.1
github.com/schollz/progressbar v1.0.0
github.com/pkg/errors v0.8.1 // indirect
github.com/schollz/progressbar/v2 v2.13.3-0.20190604152910-b6d7f92524db
github.com/stellar/go v0.0.0-20190604131926-3dd82e9a6213
github.com/stellar/go-xdr v0.0.0-20180917104419-0bc96f33a18e
github.com/stellar/go v0.0.0-20191016082916-2f64a5e02f78
gopkg.in/alecthomas/kingpin.v2 v2.2.6
)
Loading

0 comments on commit 24d4e11

Please sign in to comment.