diff --git a/models/position_test.go b/models/position_test.go new file mode 100644 index 0000000..dd37aa1 --- /dev/null +++ b/models/position_test.go @@ -0,0 +1,51 @@ +package models + +import ( + "math/big" + "testing" + "time" + + "github.com/stretchr/testify/require" +) + +func TestGetPositionFromContract(t *testing.T) { + timeNow := time.Now() + + testCases := []struct { + name string + obj positionContract + blockN uint64 + time uint64 + want *Position + }{ + { + name: "blank object", + want: &Position{}, + }, + { + name: "full object", + obj: positionContract{ + TotalPnl: big.NewInt(1), + AccruedFunding: big.NewInt(2), + PositionSize: big.NewInt(3), + }, + time: uint64(timeNow.Unix()), + blockN: uint64(4), + want: &Position{ + TotalPnl: big.NewInt(1), + AccruedFunding: big.NewInt(2), + PositionSize: big.NewInt(3), + BlockTimestamp: uint64(timeNow.Unix()), + BlockNumber: uint64(4), + }, + }, + } + + for _, tt := range testCases { + t.Run(tt.name, func(t *testing.T) { + res := GetPositionFromContract(tt.obj, tt.blockN, tt.time) + + require.Equal(t, tt.want, res) + }) + } +} diff --git a/models/model_test.go b/models/trade_test.go similarity index 75% rename from models/model_test.go rename to models/trade_test.go index 3766f6f..5584626 100644 --- a/models/model_test.go +++ b/models/trade_test.go @@ -94,45 +94,3 @@ func TestGetTradeFromEvent(t *testing.T) { }) } } - -func TestGetPositionFromContract(t *testing.T) { - timeNow := time.Now() - - testCases := []struct { - name string - obj positionContract - blockN uint64 - time uint64 - want *Position - }{ - { - name: "blank object", - want: &Position{}, - }, - { - name: "full object", - obj: positionContract{ - TotalPnl: big.NewInt(1), - AccruedFunding: big.NewInt(2), - PositionSize: big.NewInt(3), - }, - time: uint64(timeNow.Unix()), - blockN: uint64(4), - want: &Position{ - TotalPnl: big.NewInt(1), - AccruedFunding: big.NewInt(2), - PositionSize: big.NewInt(3), - BlockTimestamp: uint64(timeNow.Unix()), - BlockNumber: uint64(4), - }, - }, - } - - for _, tt := range testCases { - t.Run(tt.name, func(t *testing.T) { - res := GetPositionFromContract(tt.obj, tt.blockN, tt.time) - - require.Equal(t, tt.want, res) - }) - } -} diff --git a/services/positions_test.go b/services/positions_test.go new file mode 100644 index 0000000..38a9a17 --- /dev/null +++ b/services/positions_test.go @@ -0,0 +1,72 @@ +package services + +import ( + "fmt" + "log" + "math/big" + "os" + "testing" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/ethclient" + "github.com/gateway-fm/perpsv3-Go/contracts/coreGoerli" + "github.com/gateway-fm/perpsv3-Go/contracts/perpsMarketGoerli" + "github.com/gateway-fm/perpsv3-Go/contracts/spotMarketGoerli" + "github.com/gateway-fm/perpsv3-Go/errors" + "github.com/stretchr/testify/require" +) + +func TestService_GetPosition_OnChain(t *testing.T) { + rpc := os.Getenv("TEST_RPC") + if rpc == "" { + log.Fatal("no rpc in env vars") + } + + rpcClient, _ := ethclient.Dial(rpc) + + coreC, _ := coreGoerli.NewCoreGoerli(common.HexToAddress("0x76490713314fCEC173f44e99346F54c6e92a8E42"), rpcClient) + spot, _ := spotMarketGoerli.NewSpotMarketGoerli(common.HexToAddress("0x5FF4b3aacdeC86782d8c757FAa638d8790799E83"), rpcClient) + perps, _ := perpsMarketGoerli.NewPerpsMarketGoerli(common.HexToAddress("0xf272382cB3BE898A8CdB1A23BE056fA2Fcf4513b"), rpcClient) + + testCases := []struct { + name string + accountID *big.Int + marketID *big.Int + wantErr error + }{ + { + name: "good data", + accountID: big.NewInt(28006), + marketID: big.NewInt(100), + }, + { + name: "0 account ID no errors", + accountID: big.NewInt(0), + marketID: big.NewInt(100), + }, + { + name: "bad market id", + accountID: big.NewInt(28006), + marketID: big.NewInt(0), + wantErr: errors.GetReadContractErr(fmt.Errorf("execution reverted"), "PerpsMarket", "getOpenPosition"), + }, + } + for _, tt := range testCases { + t.Run(tt.name, func(t *testing.T) { + s := NewService(rpcClient, coreC, 11664658, spot, 10875051, perps, 0) + + res, err := s.GetPosition(tt.accountID, tt.marketID) + + if tt.wantErr == nil { + require.NotNil(t, res) + require.NotNil(t, res.PositionSize) + require.NotNil(t, res.AccruedFunding) + require.NotNil(t, res.TotalPnl) + require.NotEqual(t, res.BlockTimestamp, 0) + require.NotEqual(t, res.BlockNumber, 0) + } else { + require.EqualError(t, tt.wantErr, err.Error()) + } + }) + } +} diff --git a/services/service_test.go b/services/trades_test.go similarity index 70% rename from services/service_test.go rename to services/trades_test.go index 9557765..b55943c 100644 --- a/services/service_test.go +++ b/services/trades_test.go @@ -1,7 +1,6 @@ package services import ( - "fmt" "log" "math/big" "os" @@ -12,7 +11,6 @@ import ( "github.com/gateway-fm/perpsv3-Go/contracts/coreGoerli" "github.com/gateway-fm/perpsv3-Go/contracts/perpsMarketGoerli" "github.com/gateway-fm/perpsv3-Go/contracts/spotMarketGoerli" - "github.com/gateway-fm/perpsv3-Go/errors" "github.com/gateway-fm/perpsv3-Go/models" "github.com/stretchr/testify/require" ) @@ -120,58 +118,3 @@ func TestService_RetrieveTrades_OnChain(t *testing.T) { }) } } - -func TestService_GetPosition_OnChain(t *testing.T) { - rpc := os.Getenv("TEST_RPC") - if rpc == "" { - log.Fatal("no rpc in env vars") - } - - rpcClient, _ := ethclient.Dial(rpc) - - coreC, _ := coreGoerli.NewCoreGoerli(common.HexToAddress("0x76490713314fCEC173f44e99346F54c6e92a8E42"), rpcClient) - spot, _ := spotMarketGoerli.NewSpotMarketGoerli(common.HexToAddress("0x5FF4b3aacdeC86782d8c757FAa638d8790799E83"), rpcClient) - perps, _ := perpsMarketGoerli.NewPerpsMarketGoerli(common.HexToAddress("0xf272382cB3BE898A8CdB1A23BE056fA2Fcf4513b"), rpcClient) - - testCases := []struct { - name string - accountID *big.Int - marketID *big.Int - wantErr error - }{ - { - name: "good data", - accountID: big.NewInt(28006), - marketID: big.NewInt(100), - }, - { - name: "0 account ID no errors", - accountID: big.NewInt(0), - marketID: big.NewInt(100), - }, - { - name: "bad market id", - accountID: big.NewInt(28006), - marketID: big.NewInt(0), - wantErr: errors.GetReadContractErr(fmt.Errorf("execution reverted"), "PerpsMarket", "getOpenPosition"), - }, - } - for _, tt := range testCases { - t.Run(tt.name, func(t *testing.T) { - s := NewService(rpcClient, coreC, 11664658, spot, 10875051, perps, 0) - - res, err := s.GetPosition(tt.accountID, tt.marketID) - - if tt.wantErr == nil { - require.NotNil(t, res) - require.NotNil(t, res.PositionSize) - require.NotNil(t, res.AccruedFunding) - require.NotNil(t, res.TotalPnl) - require.NotEqual(t, res.BlockTimestamp, 0) - require.NotEqual(t, res.BlockNumber, 0) - } else { - require.EqualError(t, tt.wantErr, err.Error()) - } - }) - } -}