Skip to content

Commit

Permalink
API-63: Accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
asolovov committed Sep 1, 2023
1 parent 0e18216 commit a2e713f
Show file tree
Hide file tree
Showing 16 changed files with 1,135 additions and 4 deletions.
6 changes: 6 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ var (
ReadContractErr = fmt.Errorf("contract error read")
// RPCErr is used when error returned from RPC provider
RPCErr = fmt.Errorf("rpc provider error")
// EnumUnsupportedErr is used when given value is unsupported in enum
EnumUnsupportedErr = fmt.Errorf("unsupported status err")
)

func GetDialRPCErr(err error) error {
Expand All @@ -46,3 +48,7 @@ func GetReadContractErr(err error, contract string, method string) error {
func GetRPCProviderErr(err error, method string) error {
return fmt.Errorf("%w using %v: %w", RPCErr, method, err)
}

func GetUnsupportedErr(enum string) error {
return fmt.Errorf("%v enum %w", enum, EnumUnsupportedErr)
}
6 changes: 5 additions & 1 deletion events/marketUpdate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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"
perps_test "github.com/gateway-fm/perpsv3-Go/utils/tesing-contracts/perps-test"
perps_test "github.com/gateway-fm/perpsv3-Go/utils/testing-contracts/perps-test"
"github.com/stretchr/testify/require"
"log"
"os"
Expand All @@ -19,6 +19,10 @@ func TestEvents_ListenMarketUpdates_OnChain(t *testing.T) {
t.Skip("Skipping testing in CI environment")
}

if testing.Short() {
t.Skip("skipping test in short mode.")
}

rpc := os.Getenv("TEST_RPC_EVENTS")
if rpc == "" {
log.Fatal("no rpc in env vars")
Expand Down
6 changes: 5 additions & 1 deletion events/orders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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"
perps_test "github.com/gateway-fm/perpsv3-Go/utils/tesing-contracts/perps-test"
perps_test "github.com/gateway-fm/perpsv3-Go/utils/testing-contracts/perps-test"
"github.com/stretchr/testify/require"
)

Expand All @@ -20,6 +20,10 @@ func TestEvents_ListenOrders_OnChain(t *testing.T) {
t.Skip("Skipping testing in CI environment")
}

if testing.Short() {
t.Skip("skipping test in short mode.")
}

rpc := os.Getenv("TEST_RPC_EVENTS")
if rpc == "" {
log.Fatal("no rpc in env vars")
Expand Down
6 changes: 5 additions & 1 deletion events/trades_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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"
perps_test "github.com/gateway-fm/perpsv3-Go/utils/tesing-contracts/perps-test"
perps_test "github.com/gateway-fm/perpsv3-Go/utils/testing-contracts/perps-test"
"github.com/stretchr/testify/require"
)

Expand All @@ -20,6 +20,10 @@ func TestEvents_ListenTrades_OnChain(t *testing.T) {
t.Skip("Skipping testing in CI environment")
}

if testing.Short() {
t.Skip("skipping test in short mode.")
}

rpc := os.Getenv("TEST_RPC_EVENTS")
if rpc == "" {
log.Fatal("no rpc in env vars")
Expand Down
45 changes: 45 additions & 0 deletions mocks/service/mockService.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions models/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package models

import (
"github.com/ethereum/go-ethereum/common"
"github.com/gateway-fm/perpsv3-Go/contracts/perpsMarketGoerli"
"math/big"
)

// Account is a struct for account model
// - ID is an account NFT id
// - Permissions is a slice of UserPermissions struct with granted permissions data
// - Owner is a contract owner address
// - LastInteraction is a unix timestamp for last accounts contract interaction
type Account struct {
ID *big.Int
Permissions []*UserPermissions
Owner common.Address
LastInteraction uint64
}

// GetAccount is used to get account from given data
func GetAccount(
id *big.Int,
owner common.Address,
lastInteraction uint64,
permissions []perpsMarketGoerli.IAccountModuleAccountPermissions,
) *Account {
return &Account{
ID: id,
Permissions: getPermissions(permissions),
Owner: owner,
LastInteraction: lastInteraction,
}
}
178 changes: 178 additions & 0 deletions models/account_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package models

import (
"github.com/ethereum/go-ethereum/common"
"github.com/gateway-fm/perpsv3-Go/contracts/perpsMarketGoerli"
"github.com/stretchr/testify/require"
"math/big"
"testing"
"time"
)

func TestGetAccount(t *testing.T) {
timeNow := time.Now()

testCases := []struct {
name string
id *big.Int
owner common.Address
lastInteraction uint64
permissions []perpsMarketGoerli.IAccountModuleAccountPermissions
want *Account
}{
{
name: "blank account",
want: &Account{},
},
{
name: "no permissions",
id: big.NewInt(1),
owner: common.HexToAddress("0x76490713314fCEC173f44e99346F54c6e92a8E42"),
lastInteraction: uint64(timeNow.Unix()),
want: &Account{
ID: big.NewInt(1),
Owner: common.HexToAddress("0x76490713314fCEC173f44e99346F54c6e92a8E42"),
LastInteraction: uint64(timeNow.Unix()),
},
},
{
name: "one permission",
id: big.NewInt(1),
owner: common.HexToAddress("0x76490713314fCEC173f44e99346F54c6e92a8E42"),
lastInteraction: uint64(timeNow.Unix()),
permissions: []perpsMarketGoerli.IAccountModuleAccountPermissions{{
User: common.HexToAddress("0xf272382cB3BE898A8CdB1A23BE056fA2Fcf4513b"),
Permissions: [][32]byte{{65, 68, 77, 73, 78}},
}},
want: &Account{
ID: big.NewInt(1),
Owner: common.HexToAddress("0x76490713314fCEC173f44e99346F54c6e92a8E42"),
LastInteraction: uint64(timeNow.Unix()),
Permissions: []*UserPermissions{{
User: common.HexToAddress("0xf272382cB3BE898A8CdB1A23BE056fA2Fcf4513b"),
Permissions: []Permission{0},
}},
},
},
{
name: "several permissions",
id: big.NewInt(1),
owner: common.HexToAddress("0x76490713314fCEC173f44e99346F54c6e92a8E42"),
lastInteraction: uint64(timeNow.Unix()),
permissions: []perpsMarketGoerli.IAccountModuleAccountPermissions{{
User: common.HexToAddress("0xf272382cB3BE898A8CdB1A23BE056fA2Fcf4513b"),
Permissions: [][32]byte{{65, 68, 77, 73, 78}, {87, 73, 84, 72, 68, 82, 65, 87}, {68, 69, 76, 69, 71, 65, 84, 69}},
}},
want: &Account{
ID: big.NewInt(1),
Owner: common.HexToAddress("0x76490713314fCEC173f44e99346F54c6e92a8E42"),
LastInteraction: uint64(timeNow.Unix()),
Permissions: []*UserPermissions{{
User: common.HexToAddress("0xf272382cB3BE898A8CdB1A23BE056fA2Fcf4513b"),
Permissions: []Permission{0, 1, 2},
}},
},
},
{
name: "one permission several users",
id: big.NewInt(1),
owner: common.HexToAddress("0x76490713314fCEC173f44e99346F54c6e92a8E42"),
lastInteraction: uint64(timeNow.Unix()),
permissions: []perpsMarketGoerli.IAccountModuleAccountPermissions{
{
User: common.HexToAddress("0xf272382cB3BE898A8CdB1A23BE056fA2Fcf4513b"),
Permissions: [][32]byte{{65, 68, 77, 73, 78}},
},
{
User: common.HexToAddress("0x5FF4b3aacdeC86782d8c757FAa638d8790799E83"),
Permissions: [][32]byte{{87, 73, 84, 72, 68, 82, 65, 87}},
},
},
want: &Account{
ID: big.NewInt(1),
Owner: common.HexToAddress("0x76490713314fCEC173f44e99346F54c6e92a8E42"),
LastInteraction: uint64(timeNow.Unix()),
Permissions: []*UserPermissions{
{
User: common.HexToAddress("0xf272382cB3BE898A8CdB1A23BE056fA2Fcf4513b"),
Permissions: []Permission{0},
},
{
User: common.HexToAddress("0x5FF4b3aacdeC86782d8c757FAa638d8790799E83"),
Permissions: []Permission{1},
},
},
},
},
{
name: "several permissions several users",
id: big.NewInt(1),
owner: common.HexToAddress("0x76490713314fCEC173f44e99346F54c6e92a8E42"),
lastInteraction: uint64(timeNow.Unix()),
permissions: []perpsMarketGoerli.IAccountModuleAccountPermissions{
{
User: common.HexToAddress("0xf272382cB3BE898A8CdB1A23BE056fA2Fcf4513b"),
Permissions: [][32]byte{{65, 68, 77, 73, 78}, {87, 73, 84, 72, 68, 82, 65, 87}, {68, 69, 76, 69, 71, 65, 84, 69}},
},
{
User: common.HexToAddress("0x5FF4b3aacdeC86782d8c757FAa638d8790799E83"),
Permissions: [][32]byte{{65, 68, 77, 73, 78}, {87, 73, 84, 72, 68, 82, 65, 87}, {68, 69, 76, 69, 71, 65, 84, 69}},
},
},
want: &Account{
ID: big.NewInt(1),
Owner: common.HexToAddress("0x76490713314fCEC173f44e99346F54c6e92a8E42"),
LastInteraction: uint64(timeNow.Unix()),
Permissions: []*UserPermissions{
{
User: common.HexToAddress("0xf272382cB3BE898A8CdB1A23BE056fA2Fcf4513b"),
Permissions: []Permission{0, 1, 2},
},
{
User: common.HexToAddress("0x5FF4b3aacdeC86782d8c757FAa638d8790799E83"),
Permissions: []Permission{0, 1, 2},
},
},
},
},
{
name: "several permissions several users one bad",
id: big.NewInt(1),
owner: common.HexToAddress("0x76490713314fCEC173f44e99346F54c6e92a8E42"),
lastInteraction: uint64(timeNow.Unix()),
permissions: []perpsMarketGoerli.IAccountModuleAccountPermissions{
{
User: common.HexToAddress("0xf272382cB3BE898A8CdB1A23BE056fA2Fcf4513b"),
Permissions: [][32]byte{{65, 68, 77, 73, 78}, {87, 72, 84, 72, 68, 82, 65, 87}, {68, 69, 76, 69, 71, 65, 84, 69}},
},
{
User: common.HexToAddress("0x5FF4b3aacdeC86782d8c757FAa638d8790799E83"),
Permissions: [][32]byte{{65, 68, 77, 73, 78}, {87, 73, 84, 72, 68, 82, 65, 87}, {63, 69, 76, 69, 71, 65, 84, 69}},
},
},
want: &Account{
ID: big.NewInt(1),
Owner: common.HexToAddress("0x76490713314fCEC173f44e99346F54c6e92a8E42"),
LastInteraction: uint64(timeNow.Unix()),
Permissions: []*UserPermissions{
{
User: common.HexToAddress("0xf272382cB3BE898A8CdB1A23BE056fA2Fcf4513b"),
Permissions: []Permission{0, 2},
},
{
User: common.HexToAddress("0x5FF4b3aacdeC86782d8c757FAa638d8790799E83"),
Permissions: []Permission{0, 1},
},
},
},
},
}

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
res := GetAccount(tt.id, tt.owner, tt.lastInteraction, tt.permissions)

require.Equal(t, tt.want, res)
})
}
}
Loading

0 comments on commit a2e713f

Please sign in to comment.