@mimicprotocol/lib-ts 0.0.1-rc.31 → 0.0.1-rc.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/package.json +1 -1
- package/src/environment.ts +9 -14
- package/src/helpers/strings.ts +5 -3
- package/src/log.ts +3 -6
- package/src/tokens/ERC20Token.ts +2 -2
- package/src/tokens/SPLToken.ts +3 -3
- package/src/tokens/TokenAmount.ts +2 -2
- package/src/tokens/USD.ts +1 -1
- package/src/types/BigInt.ts +6 -6
- package/src/types/Result.ts +20 -8
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# @mimicprotocol/lib-ts
|
|
2
2
|
|
|
3
|
+
## 0.0.1-rc.32
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 8853afe: Fix clashing Result import
|
|
8
|
+
- e1277c7: Refactor Result to use unwrap instead of value
|
|
9
|
+
- 323663a: Change directory to positional argument in init and test command
|
|
10
|
+
- faac37c: Ensure early returns in favor of else blocks
|
|
11
|
+
|
|
3
12
|
## 0.0.1-rc.31
|
|
4
13
|
|
|
5
14
|
### Patch Changes
|
package/package.json
CHANGED
package/src/environment.ts
CHANGED
|
@@ -93,7 +93,7 @@ export namespace environment {
|
|
|
93
93
|
*/
|
|
94
94
|
export function rawTokenPriceQuery(token: Token, timestamp: Date | null = null): Result<USD[], string> {
|
|
95
95
|
if (token.isUSD()) return Result.ok<USD[], string>([USD.fromI32(1)])
|
|
96
|
-
|
|
96
|
+
if (!(token instanceof BlockchainToken)) return Result.err<USD[], string>('Price query not supported for token ' + token.toString())
|
|
97
97
|
|
|
98
98
|
const responseStr = _tokenPriceQuery(JSON.stringify(TokenPriceQuery.fromToken(changetype<BlockchainToken>(token), timestamp)))
|
|
99
99
|
const parsed = TokenPriceQueryResponse.fromJson<TokenPriceQueryResponse>(responseStr)
|
|
@@ -115,23 +115,18 @@ export namespace environment {
|
|
|
115
115
|
|
|
116
116
|
if (pricesResult.isError) return Result.err<USD, string>(pricesResult.error)
|
|
117
117
|
|
|
118
|
-
const prices = pricesResult.
|
|
118
|
+
const prices = pricesResult.unwrap()
|
|
119
119
|
if (prices.length === 0) return Result.err<USD, string>('Prices not found for token ' + token.toString())
|
|
120
120
|
|
|
121
121
|
const sortedPrices = prices.sort((a: USD, b: USD) => a.compare(b))
|
|
122
122
|
|
|
123
123
|
const length = sortedPrices.length
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
const sum = left.plus(right)
|
|
131
|
-
median = sum.div(BigInt.fromI32(2))
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
return Result.ok<USD, string>(median)
|
|
124
|
+
if (length % 2 === 1) return Result.ok<USD, string>(sortedPrices[length / 2])
|
|
125
|
+
|
|
126
|
+
const left = sortedPrices[length / 2 - 1]
|
|
127
|
+
const right = sortedPrices[length / 2]
|
|
128
|
+
const sum = left.plus(right)
|
|
129
|
+
return Result.ok<USD, string>(sum.div(BigInt.fromI32(2)))
|
|
135
130
|
}
|
|
136
131
|
|
|
137
132
|
/**
|
|
@@ -173,7 +168,7 @@ export namespace environment {
|
|
|
173
168
|
|
|
174
169
|
if (responseResult.isError) return Result.err<TokenAmount[], string>(responseResult.error)
|
|
175
170
|
|
|
176
|
-
const response = responseResult.
|
|
171
|
+
const response = responseResult.unwrap()
|
|
177
172
|
const resultMap: Map<string, TokenAmount> = new Map()
|
|
178
173
|
for (let i = 0; i < response.length; i++) {
|
|
179
174
|
for (let j = 0; j < response[i].length; j++) {
|
package/src/helpers/strings.ts
CHANGED
|
@@ -133,12 +133,14 @@ export function normalizeScientificNotation(input: string): string {
|
|
|
133
133
|
if (newDecimalPos <= 0) {
|
|
134
134
|
const zeros = '0'.repeat(-newDecimalPos)
|
|
135
135
|
return sign + '0.' + zeros + fullDigits
|
|
136
|
-
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (newDecimalPos >= fullDigits.length) {
|
|
137
139
|
const zeros = '0'.repeat(newDecimalPos - fullDigits.length)
|
|
138
140
|
return sign + fullDigits + zeros
|
|
139
|
-
} else {
|
|
140
|
-
return sign + fullDigits.substring(0, newDecimalPos) + '.' + fullDigits.substring(newDecimalPos)
|
|
141
141
|
}
|
|
142
|
+
|
|
143
|
+
return sign + fullDigits.substring(0, newDecimalPos) + '.' + fullDigits.substring(newDecimalPos)
|
|
142
144
|
}
|
|
143
145
|
|
|
144
146
|
export function isHex(str: string, strict: boolean = false): boolean {
|
package/src/log.ts
CHANGED
|
@@ -72,12 +72,9 @@ function format<T extends Stringable>(fmt: string, args: Array<T>): string {
|
|
|
72
72
|
const argsStr = args.map<string>(a => a.toString())
|
|
73
73
|
for (let i: i32 = 0, len: i32 = fmt.length; i < len; i++) {
|
|
74
74
|
if (i < len - 1 && fmt.charCodeAt(i) == 0x7b /* '{' */ && fmt.charCodeAt(i + 1) == 0x7d /* '}' */) {
|
|
75
|
-
if (argIndex >= argsStr.length)
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
out += argsStr[argIndex++]
|
|
79
|
-
i++
|
|
80
|
-
}
|
|
75
|
+
if (argIndex >= argsStr.length) throw new Error('Too few arguments for format string: ' + fmt)
|
|
76
|
+
out += argsStr[argIndex++]
|
|
77
|
+
i++
|
|
81
78
|
} else {
|
|
82
79
|
out += fmt.charAt(i)
|
|
83
80
|
}
|
package/src/tokens/ERC20Token.ts
CHANGED
|
@@ -164,7 +164,7 @@ export class ERC20Token extends BlockchainToken {
|
|
|
164
164
|
this._symbol = ERC20Token.EMPTY_SYMBOL
|
|
165
165
|
return
|
|
166
166
|
}
|
|
167
|
-
this._symbol = evm.decode(new EvmDecodeParam('string', response.
|
|
167
|
+
this._symbol = evm.decode(new EvmDecodeParam('string', response.unwrap()))
|
|
168
168
|
}
|
|
169
169
|
|
|
170
170
|
/**
|
|
@@ -181,6 +181,6 @@ export class ERC20Token extends BlockchainToken {
|
|
|
181
181
|
this._decimals = ERC20Token.EMPTY_DECIMALS
|
|
182
182
|
return
|
|
183
183
|
}
|
|
184
|
-
this._decimals = u8.parse(evm.decode(new EvmDecodeParam('uint8', result.
|
|
184
|
+
this._decimals = u8.parse(evm.decode(new EvmDecodeParam('uint8', result.unwrap())))
|
|
185
185
|
}
|
|
186
186
|
}
|
package/src/tokens/SPLToken.ts
CHANGED
|
@@ -102,7 +102,7 @@ export class SPLToken extends BlockchainToken {
|
|
|
102
102
|
this._decimals = SPLToken.EMPTY_DECIMALS
|
|
103
103
|
return
|
|
104
104
|
}
|
|
105
|
-
const decimals = SvmMint.fromHex(result.
|
|
105
|
+
const decimals = SvmMint.fromHex(result.unwrap().accountsInfo[0].data).decimals
|
|
106
106
|
this._decimals = decimals
|
|
107
107
|
}
|
|
108
108
|
|
|
@@ -120,12 +120,12 @@ export class SPLToken extends BlockchainToken {
|
|
|
120
120
|
this._symbol = SPLToken.EMPTY_SYMBOL
|
|
121
121
|
return
|
|
122
122
|
}
|
|
123
|
-
const data = result.
|
|
123
|
+
const data = result.unwrap().accountsInfo[0].data
|
|
124
124
|
// Return placeholder symbol from address if TokenMetadata standard is not used
|
|
125
125
|
this._symbol =
|
|
126
126
|
data === '0x'
|
|
127
127
|
? `${this.address.toString().slice(0, 5)}...${this.address.toString().slice(-5)}`
|
|
128
|
-
: SvmTokenMetadataData.fromTokenMetadataHex(result.
|
|
128
|
+
: SvmTokenMetadataData.fromTokenMetadataHex(result.unwrap().accountsInfo[0].data).symbol
|
|
129
129
|
}
|
|
130
130
|
|
|
131
131
|
/**
|
|
@@ -222,7 +222,7 @@ export class TokenAmount {
|
|
|
222
222
|
const tokenPriceResult = environment.tokenPriceQuery(this.token)
|
|
223
223
|
if (tokenPriceResult.isError) return Result.err<USD, string>(tokenPriceResult.error)
|
|
224
224
|
|
|
225
|
-
const tokenPrice = tokenPriceResult.
|
|
225
|
+
const tokenPrice = tokenPriceResult.unwrap()
|
|
226
226
|
const amountUsd = this.amount.times(tokenPrice.value).downscale(this.decimals)
|
|
227
227
|
return Result.ok<USD, string>(USD.fromBigInt(amountUsd))
|
|
228
228
|
}
|
|
@@ -237,7 +237,7 @@ export class TokenAmount {
|
|
|
237
237
|
const usdResult = this.toUsd()
|
|
238
238
|
if (usdResult.isError) return Result.err<TokenAmount, string>(usdResult.error)
|
|
239
239
|
|
|
240
|
-
return usdResult.
|
|
240
|
+
return usdResult.unwrap().toTokenAmount(other)
|
|
241
241
|
}
|
|
242
242
|
|
|
243
243
|
/**
|
package/src/tokens/USD.ts
CHANGED
|
@@ -197,7 +197,7 @@ export class USD {
|
|
|
197
197
|
const tokenPriceResult = environment.tokenPriceQuery(token)
|
|
198
198
|
if (tokenPriceResult.isError) return Result.err<TokenAmount, string>(tokenPriceResult.error)
|
|
199
199
|
|
|
200
|
-
const tokenPrice = tokenPriceResult.
|
|
200
|
+
const tokenPrice = tokenPriceResult.unwrap()
|
|
201
201
|
const tokenAmount = this.value.upscale(token.decimals).div(tokenPrice.value)
|
|
202
202
|
return Result.ok<TokenAmount, string>(TokenAmount.fromBigInt(token, tokenAmount))
|
|
203
203
|
}
|
package/src/types/BigInt.ts
CHANGED
|
@@ -404,15 +404,15 @@ export class BigInt extends Uint8Array {
|
|
|
404
404
|
return aIsNeg ? resultAbs.neg() : resultAbs
|
|
405
405
|
}
|
|
406
406
|
const cmp = BigInt.compare(this.abs(), other.abs())
|
|
407
|
-
if (cmp === 0)
|
|
408
|
-
|
|
409
|
-
|
|
407
|
+
if (cmp === 0) return BigInt.zero()
|
|
408
|
+
|
|
409
|
+
if (cmp > 0) {
|
|
410
410
|
const resultAbs = BigInt.subUnsigned(this.abs(), other.abs())
|
|
411
411
|
return this.isNegative() ? resultAbs.neg() : resultAbs
|
|
412
|
-
} else {
|
|
413
|
-
const resultAbs = BigInt.subUnsigned(other.abs(), this.abs())
|
|
414
|
-
return other.isNegative() ? resultAbs.neg() : resultAbs
|
|
415
412
|
}
|
|
413
|
+
|
|
414
|
+
const resultAbs = BigInt.subUnsigned(other.abs(), this.abs())
|
|
415
|
+
return other.isNegative() ? resultAbs.neg() : resultAbs
|
|
416
416
|
}
|
|
417
417
|
|
|
418
418
|
/**
|
package/src/types/Result.ts
CHANGED
|
@@ -4,20 +4,22 @@
|
|
|
4
4
|
// Copyright (c) 2018 Graph Protocol, Inc. and contributors.
|
|
5
5
|
// Modified by Mimic Protocol, 2025.
|
|
6
6
|
|
|
7
|
+
import { Stringable } from '../helpers'
|
|
8
|
+
|
|
7
9
|
/**
|
|
8
10
|
* The result of an operation, with a corresponding value and error type.
|
|
9
11
|
*/
|
|
10
|
-
export class Result<V, E> {
|
|
12
|
+
export class Result<V, E extends Stringable> {
|
|
11
13
|
constructor(
|
|
12
14
|
private _value: Wrapped<V> | null,
|
|
13
15
|
private _error: Wrapped<E> | null
|
|
14
16
|
) {}
|
|
15
17
|
|
|
16
|
-
static ok<V, E>(value: V): Result<V, E> {
|
|
18
|
+
static ok<V, E extends Stringable>(value: V): Result<V, E> {
|
|
17
19
|
return new Result<V, E>(new Wrapped<V>(value), null)
|
|
18
20
|
}
|
|
19
21
|
|
|
20
|
-
static err<V, E>(error: E): Result<V, E> {
|
|
22
|
+
static err<V, E extends Stringable>(error: E): Result<V, E> {
|
|
21
23
|
return new Result<V, E>(null, new Wrapped<E>(error))
|
|
22
24
|
}
|
|
23
25
|
|
|
@@ -29,15 +31,25 @@ export class Result<V, E> {
|
|
|
29
31
|
return this._error !== null
|
|
30
32
|
}
|
|
31
33
|
|
|
32
|
-
get value(): V {
|
|
33
|
-
if (this.isError) throw new Error('Trying to get a value from an error result')
|
|
34
|
-
return changetype<Wrapped<V>>(this._value).inner
|
|
35
|
-
}
|
|
36
|
-
|
|
37
34
|
get error(): E {
|
|
38
35
|
if (this.isOk) throw new Error('Trying to get an error from a successful result')
|
|
39
36
|
return changetype<Wrapped<E>>(this._error).inner
|
|
40
37
|
}
|
|
38
|
+
|
|
39
|
+
unwrap(): V {
|
|
40
|
+
if (this.isError) throw new Error(this.error.toString())
|
|
41
|
+
return changetype<Wrapped<V>>(this._value).inner
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
unwrapOr(defaultValue: V): V {
|
|
45
|
+
if (this.isError) return defaultValue
|
|
46
|
+
return this.unwrap()
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
unwrapOrElse(defaultValue: () => V): V {
|
|
50
|
+
if (this.isError) return defaultValue()
|
|
51
|
+
return this.unwrap()
|
|
52
|
+
}
|
|
41
53
|
}
|
|
42
54
|
|
|
43
55
|
// This is used to wrap a generic so that it can be unioned with `null`, working around limitations
|