Skip to content

Commit

Permalink
[TRA-169] keep existing vault orders if price and size don't need upd…
Browse files Browse the repository at this point in the history
  • Loading branch information
tqin7 committed Jul 11, 2024
1 parent dbedbd7 commit 8ff1dda
Show file tree
Hide file tree
Showing 18 changed files with 728 additions and 266 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export interface Vault {
/** The individual parameters of the vault. */

vaultParams?: VaultParams;
/** The client IDs of the most recently placed orders of the vault. */

mostRecentClientIds: number[];
}
/** Vault defines the total shares and owner shares of a vault. */

Expand All @@ -49,6 +52,9 @@ export interface VaultSDKType {
/** The individual parameters of the vault. */

vault_params?: VaultParamsSDKType;
/** The client IDs of the most recently placed orders of the vault. */

most_recent_client_ids: number[];
}

function createBaseGenesisState(): GenesisState {
Expand Down Expand Up @@ -111,7 +117,8 @@ function createBaseVault(): Vault {
vaultId: undefined,
totalShares: undefined,
ownerShares: [],
vaultParams: undefined
vaultParams: undefined,
mostRecentClientIds: []
};
}

Expand All @@ -133,6 +140,13 @@ export const Vault = {
VaultParams.encode(message.vaultParams, writer.uint32(34).fork()).ldelim();
}

writer.uint32(42).fork();

for (const v of message.mostRecentClientIds) {
writer.uint32(v);
}

writer.ldelim();
return writer;
},

Expand Down Expand Up @@ -161,6 +175,19 @@ export const Vault = {
message.vaultParams = VaultParams.decode(reader, reader.uint32());
break;

case 5:
if ((tag & 7) === 2) {
const end2 = reader.uint32() + reader.pos;

while (reader.pos < end2) {
message.mostRecentClientIds.push(reader.uint32());
}
} else {
message.mostRecentClientIds.push(reader.uint32());
}

break;

default:
reader.skipType(tag & 7);
break;
Expand All @@ -176,6 +203,7 @@ export const Vault = {
message.totalShares = object.totalShares !== undefined && object.totalShares !== null ? NumShares.fromPartial(object.totalShares) : undefined;
message.ownerShares = object.ownerShares?.map(e => OwnerShare.fromPartial(e)) || [];
message.vaultParams = object.vaultParams !== undefined && object.vaultParams !== null ? VaultParams.fromPartial(object.vaultParams) : undefined;
message.mostRecentClientIds = object.mostRecentClientIds?.map(e => e) || [];
return message;
}

Expand Down
2 changes: 2 additions & 0 deletions proto/dydxprotocol/vault/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ message Vault {
repeated OwnerShare owner_shares = 3;
// The individual parameters of the vault.
VaultParams vault_params = 4;
// The client IDs of the most recently placed orders of the vault.
repeated uint32 most_recent_client_ids = 5;
}
7 changes: 4 additions & 3 deletions protocol/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1288,16 +1288,17 @@ func New(
perpetualsmoduletypes.ModuleName,
statsmoduletypes.ModuleName,
satypes.ModuleName,
// should be before clob EndBlocker so that vault order cancels are
// processed before any vault order expirations (handled by clob)
vaultmoduletypes.ModuleName,
clobmoduletypes.ModuleName,
sendingmoduletypes.ModuleName,
vestmoduletypes.ModuleName,
rewardsmoduletypes.ModuleName,
epochsmoduletypes.ModuleName,
govplusmoduletypes.ModuleName,
delaymsgmoduletypes.ModuleName,
// Vault endblocker should be after clob endblocker. Otherwise,
// the block after the one where vault orders expire won't have
// any vault orders.
vaultmoduletypes.ModuleName,
listingmoduletypes.ModuleName,
revsharemoduletypes.ModuleName,
authz.ModuleName, // No-op.
Expand Down
2 changes: 1 addition & 1 deletion protocol/app/testdata/default_genesis_state.json
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@
"spread_buffer_ppm": 1500,
"skew_factor_ppm": 2000000,
"order_size_pct_ppm": 100000,
"order_expiration_seconds": 2,
"order_expiration_seconds": 60,
"activation_threshold_quote_quantums": "1000000000"
},
"vaults": []
Expand Down
19 changes: 19 additions & 0 deletions protocol/lib/bytes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lib

import (
"encoding/binary"
"strconv"
)

Expand All @@ -13,3 +14,21 @@ func IntToString[T int | int32 | int64](i T) string {
func UintToString[T uint | uint32 | uint64](i T) string {
return strconv.FormatUint(uint64(i), 10)
}

// Uint32ArrayToBytes converts a slice of uint32 to a byte slice.
func Uint32ArrayToBytes(arr []uint32) []byte {
buf := make([]byte, len(arr)*4)
for i, v := range arr {
binary.BigEndian.PutUint32(buf[i*4:], v)
}
return buf
}

// BytesToUint32Array converts a byte slice to a slice of uint32.
func BytesToUint32Array(b []byte) []uint32 {
arr := make([]uint32, len(b)/4)
for i := range arr {
arr[i] = binary.BigEndian.Uint32(b[i*4:])
}
return arr
}
62 changes: 62 additions & 0 deletions protocol/lib/bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,65 @@ func TestUintToString(t *testing.T) {
require.Equal(t, "15", UintToString(uint64(15)))
require.Equal(t, "18446744073709551615", UintToString(uint64(math.MaxUint64)))
}

func TestUint32ArrayToBytes(t *testing.T) {
require.Equal(
t,
[]byte{0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4},
Uint32ArrayToBytes([]uint32{1, 2, 3, 4}),
)
require.Equal(
t,
[]byte{0, 0, 0, 0, 255, 255, 255, 255},
Uint32ArrayToBytes([]uint32{0, 4294967295}),
)
require.Equal(
t,
[]byte{7, 91, 205, 21, 58, 222, 104, 177},
Uint32ArrayToBytes([]uint32{
123456789, // 111_01011011_11001101_00010101
987654321, // 111010_11011110_01101000_10110001
}),
)
require.Equal(
t,
[]byte{0, 0, 3, 233, 0, 0, 7, 210, 0, 0, 11, 187, 0, 0, 15, 164},
Uint32ArrayToBytes([]uint32{
1001, // 11_11101001
2002, // 111_11010010
3003, // 1011_10111011
4004, // 1111_10100100
}),
)
}

func TestBytesToUint32Array(t *testing.T) {
require.Equal(
t,
[]uint32{1, 2, 3, 4},
BytesToUint32Array([]byte{0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4}),
)
require.Equal(
t,
[]uint32{0, 4294967295},
BytesToUint32Array([]byte{0, 0, 0, 0, 255, 255, 255, 255}),
)
require.Equal(
t,
[]uint32{
123456789, // 111_01011011_11001101_00010101
987654321, // 111010_11011110_01101000_10110001
},
BytesToUint32Array([]byte{7, 91, 205, 21, 58, 222, 104, 177}),
)
require.Equal(
t,
[]uint32{
1001, // 11_11101001
2002, // 111_11010010
3003, // 1011_10111011
4004, // 1111_10100100
},
BytesToUint32Array([]byte{0, 0, 3, 233, 0, 0, 7, 210, 0, 0, 11, 187, 0, 0, 15, 164}),
)
}
2 changes: 1 addition & 1 deletion protocol/scripts/genesis/sample_pregenesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -1820,7 +1820,7 @@
"params": {
"activation_threshold_quote_quantums": "1000000000",
"layers": 2,
"order_expiration_seconds": 2,
"order_expiration_seconds": 60,
"order_size_pct_ppm": 100000,
"skew_factor_ppm": 2000000,
"spread_buffer_ppm": 1500,
Expand Down
2 changes: 1 addition & 1 deletion protocol/testutil/constants/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -1509,7 +1509,7 @@ const GenesisState = `{
"spread_buffer_ppm": 1500,
"skew_factor_ppm": 2000000,
"order_size_pct_ppm": 100000,
"order_expiration_seconds": 2,
"order_expiration_seconds": 60,
"activation_threshold_quote_quantums": "1000000000"
},
"vaults": []
Expand Down
1 change: 1 addition & 0 deletions protocol/x/vault/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
panic(err)
}
}
k.SetMostRecentClientIds(ctx, *vault.VaultId, vault.MostRecentClientIds)
k.AddVaultToAddressStore(ctx, *vault.VaultId)
}
}
Expand Down
Loading

0 comments on commit 8ff1dda

Please sign in to comment.