-
Notifications
You must be signed in to change notification settings - Fork 54
feat: asset-value #1206
base: main
Are you sure you want to change the base?
feat: asset-value #1206
Conversation
pastaghost
commented
Feb 22, 2023
•
edited
Loading
edited
- adds new asset-value package
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/pastaghost/asset-value" |
There was a problem hiding this comment.
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)) { |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to follow bignumberjs shorthand
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) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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
assetId: params.asset ? params.asset.assetId : params.assetId, | |
assetId: params.assetId ?? params.asset.assetId, |
PRECISION = 2, | ||
} | ||
|
||
export type AssetValueParams = { |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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') |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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...