Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename token to assetHash for spot assets #263

Merged
merged 6 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions packages/backend/src/core/SpotValidiumUpdater.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ForcedAction, OraclePrice, SpotCairoOutput } from '@explorer/encoding'
import { InMemoryMerkleStorage, MerkleTree, VaultLeaf } from '@explorer/state'
import { Hash256, PedersenHash, StarkKey } from '@explorer/types'
import { AssetHash, Hash256, PedersenHash, StarkKey } from '@explorer/types'
import { expect, mockFn } from 'earljs'

import type { MerkleTreeRepository } from '../peripherals/database/MerkleTreeRepository'
Expand Down Expand Up @@ -43,13 +43,13 @@ describe(SpotValidiumUpdater.name, () => {
const newVaultA = {
vaultId: 5n,
starkKey: StarkKey.fake('5'),
token: PedersenHash.fake('678'),
assetHash: AssetHash.fake('678'),
balance: 555n,
}
const newVaultB = {
vaultId: 2n,
starkKey: StarkKey.fake('88'),
token: PedersenHash.fake('54321'),
assetHash: AssetHash.fake('54321'),
balance: 10999n,
}
const mockSpotBatch = mock<SpotBatch>({
Expand All @@ -63,15 +63,15 @@ describe(SpotValidiumUpdater.name, () => {
value: new VaultLeaf(
newVaultA.starkKey,
newVaultA.balance,
newVaultA.token
newVaultA.assetHash
),
},
{
index: newVaultB.vaultId,
value: new VaultLeaf(
newVaultB.starkKey,
newVaultB.balance,
newVaultB.token
newVaultB.assetHash
),
},
])
Expand Down Expand Up @@ -122,7 +122,7 @@ describe(SpotValidiumUpdater.name, () => {
const newVault = {
vaultId: 5n,
starkKey: StarkKey.fake('5'),
token: PedersenHash.fake('678'),
assetHash: AssetHash.fake('678'),
balance: 555n,
}
const mockSpotBatch = mock<SpotBatch>({
Expand All @@ -149,7 +149,7 @@ describe(SpotValidiumUpdater.name, () => {
value: new VaultLeaf(
newVault.starkKey,
newVault.balance,
newVault.token
newVault.assetHash
),
},
],
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/core/SpotValidiumUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class SpotValidiumUpdater extends StateUpdater<VaultLeaf> {
buildNewVaultLeaves(batch: SpotBatch): { index: bigint; value: VaultLeaf }[] {
return batch.vaults.map((vault) => ({
index: vault.vaultId,
value: new VaultLeaf(vault.starkKey, vault.balance, vault.token),
value: new VaultLeaf(vault.starkKey, vault.balance, vault.assetHash),
}))
}
}
2 changes: 1 addition & 1 deletion packages/backend/src/core/StateUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class StateUpdater<T extends PositionLeaf | VaultLeaf> {
return newVaultLeaves.map(({ value, index }) => ({
vaultId: index,
starkKey: value.starkKey,
token: value.token,
assetHash: value.assetHash,
balance: value.balance,
}))
}
Expand Down
8 changes: 4 additions & 4 deletions packages/backend/src/peripherals/database/VaultRepository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EthereumAddress, PedersenHash, StarkKey } from '@explorer/types'
import { AssetHash, EthereumAddress, StarkKey } from '@explorer/types'
import { VaultRow } from 'knex/types/tables'

import { Logger } from '../../tools/Logger'
Expand All @@ -8,7 +8,7 @@ import { Database } from './shared/Database'
export interface VaultRecord {
vaultId: bigint
starkKey: StarkKey
token: PedersenHash
assetHash: AssetHash
balance: bigint
}

Expand Down Expand Up @@ -103,7 +103,7 @@ export function toVaultRecord(
return {
stateUpdateId: row.state_update_id,
vaultId: BigInt(row.vault_id),
token: PedersenHash(row.token),
assetHash: AssetHash(row.asset_hash),
starkKey: StarkKey(row.stark_key),
balance: BigInt(row.balance),
}
Expand All @@ -117,7 +117,7 @@ export function toVaultRow(
state_update_id: stateUpdateId,
vault_id: record.vaultId,
stark_key: record.starkKey.toString(),
token: record.token.toString(),
asset_hash: record.assetHash.toString(),
balance: record.balance,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
====== IMPORTANT NOTICE ======

DO NOT EDIT OR RENAME THIS FILE

This is a migration file. Once created the file should not be renamed or edited,
because migrations are only run once on the production server.

If you find that something was incorrectly set up in the `up` function you
should create a new migration file that fixes the issue.

*/

import { Knex } from 'knex'

export async function up(knex: Knex) {
await knex.schema.alterTable('vaults', (t) => {
t.renameColumn('token', 'asset_hash')
})
}

export async function down(knex: Knex) {
await knex.schema.alterTable('vaults', (t) => {
t.renameColumn('asset_hash', 'token')
})
}
2 changes: 1 addition & 1 deletion packages/backend/src/peripherals/database/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ declare module 'knex/types/tables' {
state_update_id: number
vault_id: bigint
stark_key: string
token: string
asset_hash: string
balance: bigint
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PedersenHash, StarkKey } from '@explorer/types'
import { AssetHash, PedersenHash, StarkKey } from '@explorer/types'
import { expect } from 'earljs'

import { EXAMPLE_SPOT_BATCH } from '../../test/starkwareData'
Expand Down Expand Up @@ -26,7 +26,7 @@ describe(toSpotBatch.name, () => {
starkKey: StarkKey(
'0x061b721a3f7b04524dc8919840b83094e080ad41de44caee78ccc36b61b07fea'
),
token: PedersenHash(
assetHash: AssetHash(
'0400d54c002305c535c5cc0cfb43b675ecb0776391be0b5a6231523df643f361'
),
vaultId: 272051n,
Expand All @@ -36,7 +36,7 @@ describe(toSpotBatch.name, () => {
starkKey: StarkKey(
'0x061b721a3f7b04524dc8919840b83094e080ad41de44caee78ccc36b61b07fea'
),
token: PedersenHash(
assetHash: AssetHash(
'0400771e0ad43c760b0b49d9f9ce5b85e9f8a7745079ced44af6bdb6edc69936'
),
vaultId: 272052n,
Expand All @@ -46,7 +46,7 @@ describe(toSpotBatch.name, () => {
starkKey: StarkKey(
'0x061b721a3f7b04524dc8919840b83094e080ad41de44caee78ccc36b61b07fea'
),
token: PedersenHash(
assetHash: AssetHash(
'04004ccd8ff8dbf262ea16708f8fe51b7169a605b15086b437aacbb07462fb7e'
),
vaultId: 272053n,
Expand Down
6 changes: 3 additions & 3 deletions packages/backend/src/peripherals/starkware/toSpotBatch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PedersenHash, StarkKey } from '@explorer/types'
import { AssetHash, PedersenHash, StarkKey } from '@explorer/types'

import { SpotBatchResponse } from './schema'

Expand All @@ -13,7 +13,7 @@ export interface SpotBatch {
export interface SpotBatchVault {
vaultId: bigint
starkKey: StarkKey
token: PedersenHash
assetHash: AssetHash
balance: bigint
}

Expand All @@ -36,7 +36,7 @@ export function toSpotBatch(
vaults: Object.entries(response.update.vaults).map(([vaultId, vault]) => ({
vaultId: BigInt(vaultId),
starkKey: StarkKey.from(BigInt(vault.stark_key)),
token: PedersenHash(vault.token),
assetHash: AssetHash(vault.token),
balance: BigInt(vault.balance),
})),
orders: Object.entries(response.update.orders).map(([orderId, order]) => ({
Expand Down
4 changes: 2 additions & 2 deletions packages/state/src/VaultLeaf.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { PedersenHash, StarkKey } from '@explorer/types'
import { AssetHash, PedersenHash, StarkKey } from '@explorer/types'
import { expect } from 'earljs'

import { VaultLeaf } from './VaultLeaf'

const tokenA = PedersenHash(
const tokenA = AssetHash(
'0xd5b742d29ab21fdb06ac5c7c460550131c0b30cbc4c911985174c0ea4a92ec'
)

Expand Down
16 changes: 8 additions & 8 deletions packages/state/src/VaultLeaf.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { pedersen } from '@explorer/crypto'
import { json, PedersenHash, StarkKey } from '@explorer/types'
import { AssetHash, json, PedersenHash, StarkKey } from '@explorer/types'

import { MerkleValue } from './MerkleValue'
import { packBytes } from './packBytes'

export class VaultLeaf extends MerkleValue {
static EMPTY = new VaultLeaf(StarkKey.ZERO, 0n, PedersenHash.ZERO)
static EMPTY = new VaultLeaf(StarkKey.ZERO, 0n, AssetHash.ZERO)

constructor(
public readonly starkKey: StarkKey,
public readonly balance: bigint,
public readonly token: PedersenHash,
public readonly assetHash: AssetHash,
protected knownHash?: PedersenHash
) {
super()
Expand All @@ -22,8 +22,8 @@ export class VaultLeaf extends MerkleValue {
// https://github.com/starkware-libs/starkex-resources/blob/master/stark_ex_objects/starkware/objects/state.py#L76
async calculateHash() {
const key_token_hash = await pedersen(
PedersenHash(this.starkKey.substring(2)),
this.token
PedersenHash(this.starkKey.toString()),
PedersenHash(this.assetHash.toString())
adamiak marked this conversation as resolved.
Show resolved Hide resolved
)
const hash = await pedersen(
key_token_hash,
Expand All @@ -36,7 +36,7 @@ export class VaultLeaf extends MerkleValue {
return {
starkKey: this.starkKey,
balance: this.balance,
token: this.token,
token: this.assetHash,
}
}

Expand All @@ -47,7 +47,7 @@ export class VaultLeaf extends MerkleValue {
return new VaultLeaf(
StarkKey(cast.starkKey),
BigInt(cast.balance),
PedersenHash(cast.token),
AssetHash(cast.assetHash),
knownHash
)
}
Expand All @@ -56,7 +56,7 @@ export class VaultLeaf extends MerkleValue {
return {
starkKey: this.starkKey.toString(),
balance: this.balance.toString(),
token: this.token.toString(),
assetHash: this.assetHash.toString(),
}
}
}
12 changes: 10 additions & 2 deletions packages/types/src/AssetHash.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,16 @@ describe(AssetHash.name, () => {
expect(() => AssetHash('foo')).toThrow(TypeError)
})

it('throws for short hex strings', () => {
expect(() => AssetHash('0x123abc')).toThrow(TypeError)
it('fixes short non-prefixed strings', () => {
expect(AssetHash('123abc').toString()).toEqual(
'0x0000000000000000000000000000000000000000000000000000000000123abc'
)
})

it('fixes short prefixed strings', () => {
expect(AssetHash('0x123abc').toString()).toEqual(
'0x0000000000000000000000000000000000000000000000000000000000123abc'
)
})

it('throws for long hex strings', () => {
Expand Down
14 changes: 10 additions & 4 deletions packages/types/src/AssetHash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@ export interface AssetHash extends String {
}

export function AssetHash(value: string) {
if (!value.startsWith('0x')) {
value = '0x' + value
if (value.startsWith('0x')) {
value = value.slice(2)
Comment on lines +10 to +11
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is a better approach. We are slicing the 0x prefix if it's there and then we are adding it anyway.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is done to pad the length to 64. So that 0x000ab and 0xab end up being equal.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, makes sense then

}
if (!/^0x[a-f\d]{64}$/i.test(value)) {
throw new TypeError('Invalid AssetHash')
if (!/^[\da-fA-F]+$/.test(value)) {
throw new TypeError('AssetHash must be a hex string')
}
if (value.length > 64) {
throw new TypeError('AssetHash too large')
}
value = '0x' + value.padStart(64, '0')
return value.toLowerCase() as unknown as AssetHash
}

AssetHash.ZERO = AssetHash('0'.repeat(64))

AssetHash.from = function from(value: BigNumber | bigint) {
if (typeof value !== 'bigint') {
value = value.toBigInt()
Expand Down