-
Notifications
You must be signed in to change notification settings - Fork 502
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
Karthik/5614/generic offers parsing #5531
base: master
Are you sure you want to change the base?
Changes from all commits
7d2844b
c95cc41
62c5e4d
ce975a1
410b29e
9a54e6d
4ac02e6
2864e8d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package offers | ||
|
||
import ( | ||
"github.com/guregu/null" | ||
"github.com/stellar/go/ingest" | ||
"github.com/stellar/go/ingest/utils" | ||
"github.com/stellar/go/xdr" | ||
) | ||
|
||
// Constants for event types | ||
const ( | ||
EventTypeOfferCreated = "OfferCreated" | ||
EventTypeOfferFill = "OfferUpdated" | ||
EventTypeOfferClosed = "OfferClosed" | ||
) | ||
|
||
// Base struct with common fields for all offer events. | ||
type OfferEventData struct { | ||
SellerId string | ||
OfferID int64 | ||
BuyingAsset xdr.Asset | ||
SellingAsset xdr.Asset | ||
RemainingAmount int64 // Remaining amount that still needs to be filled for this offer | ||
PriceN int32 | ||
PriceD int32 | ||
Flags int32 | ||
IsPassive bool | ||
LastModifiedLedger uint32 | ||
Sponsor null.String | ||
} | ||
|
||
type OfferEvent interface { | ||
OfferEventType() string | ||
} | ||
|
||
type OfferCreatedEvent struct { | ||
OfferEventData | ||
} | ||
|
||
// Method to get common event data | ||
func (e OfferEventData) GetOfferData() OfferEventData { | ||
return e | ||
} | ||
|
||
func (e OfferCreatedEvent) OfferEventType() string { return EventTypeOfferCreated } | ||
|
||
type OfferFillEvent struct { | ||
OfferEventData | ||
FillAmount int64 // How much amount of the order was filled from last time | ||
} | ||
|
||
func (e OfferFillEvent) OfferEventType() string { return EventTypeOfferFill } | ||
|
||
type OfferClosedEvent struct { | ||
OfferEventData | ||
CloseReason string | ||
} | ||
|
||
func (e OfferClosedEvent) OfferEventType() string { return EventTypeOfferClosed } | ||
|
||
func populateOfferData(e *xdr.LedgerEntry) OfferEventData { | ||
offer := e.Data.MustOffer() | ||
return OfferEventData{ | ||
SellerId: offer.SellerId.Address(), | ||
OfferID: int64(offer.OfferId), | ||
|
||
BuyingAsset: offer.Buying, | ||
SellingAsset: offer.Selling, | ||
RemainingAmount: int64(offer.Amount), | ||
PriceN: int32(offer.Price.N), | ||
PriceD: int32(offer.Price.D), | ||
Flags: int32(offer.Flags), | ||
IsPassive: int32(offer.Flags) == int32(xdr.OfferEntryFlagsPassiveFlag), | ||
LastModifiedLedger: uint32(e.LastModifiedLedgerSeq), | ||
Sponsor: utils.LedgerEntrySponsorToNullString(*e), | ||
} | ||
} | ||
|
||
func ProcessOffer(change ingest.Change) OfferEvent { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This ProcessOffer has been pulled out into the top level Further down in this PR in |
||
if change.Type != xdr.LedgerEntryTypeOffer { | ||
return nil | ||
} | ||
var o OfferEventData | ||
var event OfferEvent | ||
|
||
switch { | ||
case change.Pre == nil && change.Post != nil: | ||
// New offer | ||
o = populateOfferData(change.Post) | ||
event = OfferCreatedEvent{OfferEventData: o} | ||
|
||
case change.Pre != nil && change.Post != nil: | ||
// Order Fill | ||
o = populateOfferData(change.Post) | ||
fillAmt := int64(change.Pre.Data.MustOffer().Amount - change.Post.Data.MustOffer().Amount) | ||
event = OfferFillEvent{OfferEventData: o, FillAmount: fillAmt} | ||
//TODO: populate MatchingOrders field in OfferFillEvent | ||
|
||
// Offer Fill | ||
case change.Pre != nil && change.Post == nil: | ||
// Offer Removed | ||
o = populateOfferData(change.Pre) | ||
event = OfferClosedEvent{OfferEventData: o} | ||
//TODO: populate CloseReason field in OfferClosedEvent | ||
} | ||
|
||
return event | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package utils | ||
|
||
import ( | ||
"github.com/guregu/null" | ||
"github.com/stellar/go/xdr" | ||
) | ||
|
||
func LedgerEntrySponsorToNullString(entry xdr.LedgerEntry) null.String { | ||
sponsoringID := entry.SponsoringID() | ||
|
||
var sponsor null.String | ||
if sponsoringID != nil { | ||
sponsor.SetValid((*sponsoringID).Address()) | ||
} | ||
|
||
return sponsor | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better to just use xdr.OfferEntry instead of this because it contains the same data so there is no need to introduce another struct