Skip to content
This repository has been archived by the owner on Apr 11, 2023. It is now read-only.

feat: asset-value #1206

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

feat: asset-value #1206

wants to merge 5 commits into from

Conversation

pastaghost
Copy link
Collaborator

@pastaghost pastaghost commented Feb 22, 2023

  • adds new asset-value package

@pastaghost pastaghost requested a review from a team as a code owner February 22, 2023 08:44
@pastaghost pastaghost changed the title feat: asset value feat: asset-value Feb 22, 2023
},
"repository": {
"type": "git",
"url": "https://github.com/pastaghost/asset-value"
Copy link
Contributor

Choose a reason for hiding this comment

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

update with shapeshift repo

state: AssetValueState

public constructor(params: AssetValueParams | SerializedAssetValue) {
if (isAssetValueParams(params)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

wouldn't an if (typeof params === 'string') { doSerializedStuff } be more straight forward?

return `${serializedState}|${hash}` as SerializedAssetValue
}

public mult = this.multipliedBy.bind(this)
Copy link
Contributor

Choose a reason for hiding this comment

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

to follow bignumberjs shorthand

Suggested change
public mult = this.multipliedBy.bind(this)
public times = this.multipliedBy.bind(this)


public mult = this.multipliedBy.bind(this)
public div = this.dividedBy.bind(this)
public gte = this.isGreaterThanOrEqualTo.bind(this)
Copy link
Contributor

Choose a reason for hiding this comment

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

missing gt


//TODO(pastaghost): Add JSDoc documentation
export class AssetValue {
state: AssetValueState
Copy link
Contributor

Choose a reason for hiding this comment

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

reason for object over standard class vars? also should probably make private to avoid mutation outside of class

break
}
this.state = {
assetId: params.asset ? params.asset.assetId : params.assetId,
Copy link
Contributor

@kaladinlight kaladinlight Feb 23, 2023

Choose a reason for hiding this comment

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

shorthand option available for setting these values

Suggested change
assetId: params.asset ? params.asset.assetId : params.assetId,
assetId: params.assetId ?? params.asset.assetId,

PRECISION = 2,
}

export type AssetValueParams = {
Copy link
Contributor

Choose a reason for hiding this comment

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

personally would just add these types along side asset value class for easier readability in such a small package (could just reduce to single index.ts really imo)

@@ -0,0 +1,2 @@
export const CHECKSUM_LENGTH = 8
export const DEFAULT_FORMAT_DECIMALS = 6
Copy link
Contributor

Choose a reason for hiding this comment

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

could be useful to support configurable DEFAULT_FORMAT_DECIMALS possibly?

if (!(serializedState && hash)) {
throw new Error('Cannot initialize AssetValue from improperly-formatted SerializedAssetValue')
}
const parsedState = JSON.parse(serializedState)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't recall the thrown error from JSON.parse, but it can throw if you want to throw a custom error

}
const parsedState = JSON.parse(serializedState)
if (!(parsedState && parsedState.a && parsedState.p && parsedState.v)) {
throw new Error('Cannot initialize AssetValue from underspecified SerializedAssetValue')
Copy link
Contributor

Choose a reason for hiding this comment

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

could be extra nice and keep track of missing keys and provide as detail for the error

v: this.state.value,
})
const hash = Md5.hashStr(serializedState).slice(0, CHECKSUM_LENGTH)
return `${serializedState}|${hash}` as SerializedAssetValue
Copy link
Contributor

Choose a reason for hiding this comment

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

don't think as cast should be necessary

}

//TODO(pastaghost): Add JSDoc documentation
export class AssetValue {
Copy link
Contributor

Choose a reason for hiding this comment

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

my only other thought is around the use of bnOrZero throughout in relation to being a big number wrapped asset. kind of makes it unpure in a sense, though it is for our use case and we use that for all of our bignumber math (I still have mixed feeling about this in general but it is what it is haha)

}

public dividedBy(factor: string | number): AssetValue {
const value = bnOrZero(this.state.value).dividedBy(bnOrZero(factor)).toFixed()
Copy link
Contributor

Choose a reason for hiding this comment

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

I have mentioned staying away from bnOrZero specifically for dividedBy. Result is Infinity vs NaN with toFixed. Honestly not sure how we handle either value in the client throughout though...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants