github.com/stellar/stellar-etl@v1.0.1-0.20240312145900-4874b6bf2b89/internal/transform/offer_test.go (about) 1 package transform 2 3 import ( 4 "fmt" 5 "testing" 6 "time" 7 8 "github.com/guregu/null" 9 "github.com/stretchr/testify/assert" 10 11 "github.com/stellar/go/ingest" 12 "github.com/stellar/go/xdr" 13 ) 14 15 func TestTransformOffer(t *testing.T) { 16 type inputStruct struct { 17 ingest ingest.Change 18 } 19 type transformTest struct { 20 input inputStruct 21 wantOutput OfferOutput 22 wantErr error 23 } 24 25 hardCodedInput, err := makeOfferTestInput() 26 assert.NoError(t, err) 27 hardCodedOutput := makeOfferTestOutput() 28 29 tests := []transformTest{ 30 { 31 inputStruct{ingest.Change{ 32 Type: xdr.LedgerEntryTypeAccount, 33 Post: &xdr.LedgerEntry{ 34 Data: xdr.LedgerEntryData{ 35 Type: xdr.LedgerEntryTypeAccount, 36 }, 37 }, 38 }, 39 }, 40 OfferOutput{}, fmt.Errorf("Could not extract offer data from ledger entry; actual type is LedgerEntryTypeAccount"), 41 }, 42 { 43 inputStruct{wrapOfferEntry(xdr.OfferEntry{ 44 SellerId: genericAccountID, 45 OfferId: -1, 46 }, 0), 47 }, 48 OfferOutput{}, fmt.Errorf("OfferID is negative (-1) for offer from account: %s", genericAccountAddress), 49 }, 50 { 51 inputStruct{wrapOfferEntry(xdr.OfferEntry{ 52 SellerId: genericAccountID, 53 Amount: -2, 54 }, 0), 55 }, 56 OfferOutput{}, fmt.Errorf("Amount is negative (-2) for offer 0"), 57 }, 58 { 59 inputStruct{wrapOfferEntry(xdr.OfferEntry{ 60 SellerId: genericAccountID, 61 Price: xdr.Price{ 62 N: -3, 63 D: 10, 64 }, 65 }, 0), 66 }, 67 OfferOutput{}, fmt.Errorf("Price numerator is negative (-3) for offer 0"), 68 }, 69 { 70 inputStruct{wrapOfferEntry(xdr.OfferEntry{ 71 SellerId: genericAccountID, 72 Price: xdr.Price{ 73 N: 5, 74 D: -4, 75 }, 76 }, 0), 77 }, 78 OfferOutput{}, fmt.Errorf("Price denominator is negative (-4) for offer 0"), 79 }, 80 { 81 inputStruct{wrapOfferEntry(xdr.OfferEntry{ 82 SellerId: genericAccountID, 83 Price: xdr.Price{ 84 N: 5, 85 D: 0, 86 }, 87 }, 0), 88 }, 89 OfferOutput{}, fmt.Errorf("Price denominator is 0 for offer 0"), 90 }, 91 { 92 inputStruct{ 93 hardCodedInput, 94 }, 95 hardCodedOutput, nil, 96 }, 97 } 98 99 for _, test := range tests { 100 header := xdr.LedgerHeaderHistoryEntry{ 101 Header: xdr.LedgerHeader{ 102 ScpValue: xdr.StellarValue{ 103 CloseTime: 1000, 104 }, 105 LedgerSeq: 10, 106 }, 107 } 108 actualOutput, actualError := TransformOffer(test.input.ingest, header) 109 assert.Equal(t, test.wantErr, actualError) 110 assert.Equal(t, test.wantOutput, actualOutput) 111 } 112 } 113 114 func wrapOfferEntry(offerEntry xdr.OfferEntry, lastModified int) ingest.Change { 115 return ingest.Change{ 116 Type: xdr.LedgerEntryTypeOffer, 117 Pre: nil, 118 Post: &xdr.LedgerEntry{ 119 LastModifiedLedgerSeq: xdr.Uint32(lastModified), 120 Data: xdr.LedgerEntryData{ 121 Type: xdr.LedgerEntryTypeOffer, 122 Offer: &offerEntry, 123 }, 124 }, 125 } 126 } 127 128 func makeOfferTestInput() (ledgerChange ingest.Change, err error) { 129 ledgerChange = ingest.Change{ 130 Type: xdr.LedgerEntryTypeOffer, 131 Pre: &xdr.LedgerEntry{ 132 LastModifiedLedgerSeq: xdr.Uint32(30715263), 133 Data: xdr.LedgerEntryData{ 134 Type: xdr.LedgerEntryTypeOffer, 135 Offer: &xdr.OfferEntry{ 136 SellerId: testAccount1ID, 137 OfferId: 260678439, 138 Selling: nativeAsset, 139 Buying: ethAsset, 140 Amount: 2628450327, 141 Price: xdr.Price{ 142 N: 920936891, 143 D: 1790879058, 144 }, 145 Flags: 2, 146 }, 147 }, 148 Ext: xdr.LedgerEntryExt{ 149 V: 1, 150 V1: &xdr.LedgerEntryExtensionV1{ 151 SponsoringId: &testAccount3ID, 152 }, 153 }, 154 }, 155 Post: nil, 156 } 157 return 158 } 159 160 func makeOfferTestOutput() OfferOutput { 161 return OfferOutput{ 162 SellerID: testAccount1Address, 163 OfferID: 260678439, 164 SellingAssetType: "native", 165 SellingAssetCode: "", 166 SellingAssetIssuer: "", 167 SellingAssetID: -5706705804583548011, 168 BuyingAssetType: "credit_alphanum4", 169 BuyingAssetCode: "ETH", 170 BuyingAssetIssuer: testAccount3Address, 171 BuyingAssetID: 4476940172956910889, 172 Amount: 262.8450327, 173 PriceN: 920936891, 174 PriceD: 1790879058, 175 Price: 0.5142373444404865, 176 Flags: 2, 177 LastModifiedLedger: 30715263, 178 LedgerEntryChange: 2, 179 Deleted: true, 180 Sponsor: null.StringFrom(testAccount3Address), 181 LedgerSequence: 10, 182 ClosedAt: time.Date(1970, time.January, 1, 0, 16, 40, 0, time.UTC), 183 } 184 }