github.com/stellar/stellar-etl@v1.0.1-0.20240312145900-4874b6bf2b89/internal/transform/trustline_test.go (about)

     1  package transform
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	"github.com/stellar/go/ingest"
    11  	"github.com/stellar/go/xdr"
    12  )
    13  
    14  func TestTransformTrustline(t *testing.T) {
    15  	type inputStruct struct {
    16  		ingest ingest.Change
    17  	}
    18  	type transformTest struct {
    19  		input      inputStruct
    20  		wantOutput TrustlineOutput
    21  		wantErr    error
    22  	}
    23  
    24  	hardCodedInput := makeTrustlineTestInput()
    25  	hardCodedOutput := makeTrustlineTestOutput()
    26  
    27  	tests := []transformTest{
    28  		{
    29  			inputStruct{
    30  				ingest.Change{
    31  					Type: xdr.LedgerEntryTypeOffer,
    32  					Pre:  nil,
    33  					Post: &xdr.LedgerEntry{
    34  						Data: xdr.LedgerEntryData{
    35  							Type: xdr.LedgerEntryTypeOffer,
    36  						},
    37  					},
    38  				},
    39  			},
    40  			TrustlineOutput{}, fmt.Errorf("Could not extract trustline data from ledger entry; actual type is LedgerEntryTypeOffer"),
    41  		},
    42  	}
    43  
    44  	for i := range hardCodedInput {
    45  		tests = append(tests, transformTest{
    46  			input:      inputStruct{hardCodedInput[i]},
    47  			wantOutput: hardCodedOutput[i],
    48  			wantErr:    nil,
    49  		})
    50  	}
    51  
    52  	for _, test := range tests {
    53  		header := xdr.LedgerHeaderHistoryEntry{
    54  			Header: xdr.LedgerHeader{
    55  				ScpValue: xdr.StellarValue{
    56  					CloseTime: 1000,
    57  				},
    58  				LedgerSeq: 10,
    59  			},
    60  		}
    61  		actualOutput, actualError := TransformTrustline(test.input.ingest, header)
    62  		assert.Equal(t, test.wantErr, actualError)
    63  		assert.Equal(t, test.wantOutput, actualOutput)
    64  	}
    65  }
    66  
    67  func makeTrustlineTestInput() []ingest.Change {
    68  	assetLedgerEntry := xdr.LedgerEntry{
    69  		LastModifiedLedgerSeq: 24229503,
    70  		Data: xdr.LedgerEntryData{
    71  			Type: xdr.LedgerEntryTypeTrustline,
    72  			TrustLine: &xdr.TrustLineEntry{
    73  				AccountId: testAccount1ID,
    74  				Asset:     ethTrustLineAsset,
    75  				Balance:   6203000,
    76  				Limit:     9000000000000000000,
    77  				Flags:     xdr.Uint32(xdr.TrustLineFlagsAuthorizedFlag),
    78  				Ext: xdr.TrustLineEntryExt{
    79  					V: 1,
    80  					V1: &xdr.TrustLineEntryV1{
    81  						Liabilities: xdr.Liabilities{
    82  							Buying:  1000,
    83  							Selling: 2000,
    84  						},
    85  					},
    86  				},
    87  			},
    88  		},
    89  	}
    90  	lpLedgerEntry := xdr.LedgerEntry{
    91  		LastModifiedLedgerSeq: 123456789,
    92  		Data: xdr.LedgerEntryData{
    93  			Type: xdr.LedgerEntryTypeTrustline,
    94  			TrustLine: &xdr.TrustLineEntry{
    95  				AccountId: testAccount2ID,
    96  				Asset:     liquidityPoolAsset,
    97  				Balance:   5000000,
    98  				Limit:     1111111111111111111,
    99  				Flags:     xdr.Uint32(xdr.TrustLineFlagsAuthorizedFlag),
   100  				Ext: xdr.TrustLineEntryExt{
   101  					V: 1,
   102  					V1: &xdr.TrustLineEntryV1{
   103  						Liabilities: xdr.Liabilities{
   104  							Buying:  15000,
   105  							Selling: 5000,
   106  						},
   107  					},
   108  				},
   109  			},
   110  		},
   111  	}
   112  	return []ingest.Change{
   113  		{
   114  			Type: xdr.LedgerEntryTypeTrustline,
   115  			Pre:  &xdr.LedgerEntry{},
   116  			Post: &assetLedgerEntry,
   117  		},
   118  		{
   119  			Type: xdr.LedgerEntryTypeTrustline,
   120  			Pre:  &xdr.LedgerEntry{},
   121  			Post: &lpLedgerEntry,
   122  		},
   123  	}
   124  }
   125  
   126  func makeTrustlineTestOutput() []TrustlineOutput {
   127  	return []TrustlineOutput{
   128  		{
   129  			LedgerKey:          "AAAAAQAAAACI4aa0pXFSj6qfJuIObLw/5zyugLRGYwxb7wFSr3B9eAAAAAFFVEgAAAAAAGfMAIZMO4kWjGqv4Lw0cJ7QIcUFcuL5iGE0IggsIily",
   130  			AccountID:          testAccount1Address,
   131  			AssetType:          1,
   132  			AssetIssuer:        testAccount3Address,
   133  			AssetCode:          "ETH",
   134  			AssetID:            -2311386320395871674,
   135  			Balance:            0.6203,
   136  			TrustlineLimit:     9000000000000000000,
   137  			Flags:              1,
   138  			BuyingLiabilities:  0.0001,
   139  			SellingLiabilities: 0.0002,
   140  			LastModifiedLedger: 24229503,
   141  			LedgerEntryChange:  1,
   142  			Deleted:            false,
   143  			LedgerSequence:     10,
   144  			ClosedAt:           time.Date(1970, time.January, 1, 0, 16, 40, 0, time.UTC),
   145  		},
   146  		{
   147  			LedgerKey:          "AAAAAQAAAAAcR0GXGO76pFs4y38vJVAanjnLg4emNun7zAx0pHcDGAAAAAMBAwQFBwkAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==",
   148  			AccountID:          testAccount2Address,
   149  			AssetType:          3,
   150  			AssetID:            -1967220342708457407,
   151  			Balance:            0.5,
   152  			TrustlineLimit:     1111111111111111111,
   153  			LiquidityPoolID:    "0103040507090000000000000000000000000000000000000000000000000000",
   154  			Flags:              1,
   155  			BuyingLiabilities:  0.0015,
   156  			SellingLiabilities: 0.0005,
   157  			LastModifiedLedger: 123456789,
   158  			LedgerEntryChange:  1,
   159  			Deleted:            false,
   160  			LedgerSequence:     10,
   161  			ClosedAt:           time.Date(1970, time.January, 1, 0, 16, 40, 0, time.UTC),
   162  		},
   163  	}
   164  }