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

     1  package transform
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/stellar/go/ingest"
    10  	"github.com/stellar/go/xdr"
    11  )
    12  
    13  func TestTransformPool(t *testing.T) {
    14  	type inputStruct struct {
    15  		ingest ingest.Change
    16  	}
    17  	type transformTest struct {
    18  		input      inputStruct
    19  		wantOutput PoolOutput
    20  		wantErr    error
    21  	}
    22  
    23  	hardCodedInput := makePoolTestInput()
    24  	hardCodedOutput := makePoolTestOutput()
    25  
    26  	tests := []transformTest{
    27  		{
    28  			inputStruct{
    29  				ingest.Change{
    30  					Type: xdr.LedgerEntryTypeOffer,
    31  					Pre:  nil,
    32  					Post: &xdr.LedgerEntry{
    33  						Data: xdr.LedgerEntryData{
    34  							Type: xdr.LedgerEntryTypeOffer,
    35  						},
    36  					},
    37  				},
    38  			},
    39  			PoolOutput{}, nil,
    40  		},
    41  		{
    42  			inputStruct{
    43  				hardCodedInput,
    44  			},
    45  			hardCodedOutput, nil,
    46  		},
    47  	}
    48  
    49  	for _, test := range tests {
    50  		header := xdr.LedgerHeaderHistoryEntry{
    51  			Header: xdr.LedgerHeader{
    52  				ScpValue: xdr.StellarValue{
    53  					CloseTime: 1000,
    54  				},
    55  				LedgerSeq: 10,
    56  			},
    57  		}
    58  		actualOutput, actualError := TransformPool(test.input.ingest, header)
    59  		assert.Equal(t, test.wantErr, actualError)
    60  		assert.Equal(t, test.wantOutput, actualOutput)
    61  	}
    62  }
    63  
    64  func wrapPoolEntry(poolEntry xdr.LiquidityPoolEntry, lastModified int) ingest.Change {
    65  	return ingest.Change{
    66  		Type: xdr.LedgerEntryTypeLiquidityPool,
    67  		Pre: &xdr.LedgerEntry{
    68  			LastModifiedLedgerSeq: xdr.Uint32(lastModified),
    69  			Data: xdr.LedgerEntryData{
    70  				Type:          xdr.LedgerEntryTypeLiquidityPool,
    71  				LiquidityPool: &poolEntry,
    72  			},
    73  		},
    74  	}
    75  }
    76  
    77  func makePoolTestInput() ingest.Change {
    78  	ledgerEntry := xdr.LedgerEntry{
    79  		LastModifiedLedgerSeq: 30705278,
    80  		Data: xdr.LedgerEntryData{
    81  			Type: xdr.LedgerEntryTypeLiquidityPool,
    82  			LiquidityPool: &xdr.LiquidityPoolEntry{
    83  				LiquidityPoolId: xdr.PoolId{23, 45, 67},
    84  				Body: xdr.LiquidityPoolEntryBody{
    85  					Type: xdr.LiquidityPoolTypeLiquidityPoolConstantProduct,
    86  					ConstantProduct: &xdr.LiquidityPoolEntryConstantProduct{
    87  						Params: xdr.LiquidityPoolConstantProductParameters{
    88  							AssetA: lpAssetA,
    89  							AssetB: lpAssetB,
    90  							Fee:    30,
    91  						},
    92  						ReserveA:                 105,
    93  						ReserveB:                 10,
    94  						TotalPoolShares:          35,
    95  						PoolSharesTrustLineCount: 5,
    96  					},
    97  				},
    98  			},
    99  		},
   100  	}
   101  	return ingest.Change{
   102  		Type: xdr.LedgerEntryTypeLiquidityPool,
   103  		Pre:  &ledgerEntry,
   104  		Post: nil,
   105  	}
   106  }
   107  
   108  func makePoolTestOutput() PoolOutput {
   109  	return PoolOutput{
   110  		PoolID:             "172d430000000000000000000000000000000000000000000000000000000000",
   111  		PoolType:           "constant_product",
   112  		PoolFee:            30,
   113  		TrustlineCount:     5,
   114  		PoolShareCount:     0.0000035,
   115  		AssetAType:         "native",
   116  		AssetACode:         lpAssetA.GetCode(),
   117  		AssetAIssuer:       lpAssetA.GetIssuer(),
   118  		AssetAID:           -5706705804583548011,
   119  		AssetAReserve:      0.0000105,
   120  		AssetBType:         "credit_alphanum4",
   121  		AssetBCode:         lpAssetB.GetCode(),
   122  		AssetBID:           6690054458235693884,
   123  		AssetBIssuer:       lpAssetB.GetIssuer(),
   124  		AssetBReserve:      0.0000010,
   125  		LastModifiedLedger: 30705278,
   126  		LedgerEntryChange:  2,
   127  		Deleted:            true,
   128  		LedgerSequence:     10,
   129  		ClosedAt:           time.Date(1970, time.January, 1, 0, 16, 40, 0, time.UTC),
   130  	}
   131  }