@mimicprotocol/lib-ts 0.0.1-rc.9 → 0.1.0
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 +144 -0
- package/README.md +7 -7
- package/constants.d.ts +1 -0
- package/constants.js +1 -0
- package/index.ts +3 -0
- package/package.json +18 -4
- package/src/chains/Arbitrum.ts +14 -0
- package/src/chains/Avalanche.ts +15 -0
- package/src/chains/BNB.ts +15 -0
- package/src/chains/Base.ts +14 -0
- package/src/chains/BaseSepolia.ts +7 -0
- package/src/chains/Ethereum.ts +7 -7
- package/src/chains/Gnosis.ts +14 -0
- package/src/chains/Optimism.ts +7 -7
- package/src/chains/Polygon.ts +10 -7
- package/src/chains/Sonic.ts +13 -0
- package/src/chains/index.ts +7 -0
- package/src/context/Context.ts +102 -7
- package/src/environment.ts +150 -71
- package/src/evm.ts +5 -4
- package/src/helpers/BorshDeserializer.ts +133 -0
- package/src/helpers/consensus.ts +35 -0
- package/src/helpers/constants.ts +7 -1
- package/src/helpers/index.ts +5 -0
- package/src/helpers/math.ts +20 -0
- package/src/helpers/serialize.ts +5 -125
- package/src/helpers/strings.ts +82 -5
- package/src/intents/Call/EvmCall.ts +199 -0
- package/src/intents/Call/EvmDynamicCall.ts +272 -0
- package/src/intents/Call/SvmCall.ts +204 -0
- package/src/intents/Call/index.ts +3 -0
- package/src/intents/Intent.ts +347 -35
- package/src/intents/Operation.ts +114 -0
- package/src/intents/Swap.ts +127 -114
- package/src/intents/Transfer.ts +72 -123
- package/src/intents/index.ts +1 -0
- package/src/log.ts +83 -0
- package/src/queries/EvmCallQuery.ts +43 -0
- package/src/queries/QueryResponse.ts +26 -0
- package/src/queries/RelevantTokensQuery.ts +82 -0
- package/src/queries/SubgraphQuery.ts +50 -0
- package/src/queries/SvmAccountsInfoQuery.ts +65 -0
- package/src/queries/TokenPriceQuery.ts +47 -0
- package/src/queries/index.ts +6 -1
- package/src/storage/index.ts +1 -0
- package/src/storage/storage.ts +40 -0
- package/src/svm.ts +27 -0
- package/src/tokens/BlockchainToken.ts +108 -0
- package/src/tokens/DenominationToken.ts +70 -0
- package/src/tokens/ERC20Token.ts +192 -0
- package/src/tokens/SPLToken.ts +162 -0
- package/src/tokens/Token.ts +55 -155
- package/src/tokens/TokenAmount.ts +72 -30
- package/src/tokens/TokenProvider.ts +54 -0
- package/src/tokens/Tokens.ts +186 -0
- package/src/tokens/USD.ts +9 -6
- package/src/tokens/index.ts +6 -0
- package/src/types/Address.ts +86 -14
- package/src/types/BigInt.ts +14 -22
- package/src/types/ByteArray.ts +41 -3
- package/src/types/Bytes.ts +7 -0
- package/src/types/ChainId.ts +9 -1
- package/src/types/Option.ts +35 -0
- package/src/types/Result.ts +68 -0
- package/src/types/TriggerType.ts +4 -0
- package/src/types/evm/EvmDecodeParam.ts +7 -0
- package/src/types/evm/EvmEncodeParam.ts +31 -0
- package/src/types/evm/index.ts +2 -0
- package/src/types/index.ts +8 -2
- package/src/types/svm/SvmAccountInfo.ts +32 -0
- package/src/types/svm/SvmAccountMeta.ts +28 -0
- package/src/types/svm/SvmFindProgramAddress.ts +32 -0
- package/src/types/svm/SvmMint.ts +44 -0
- package/src/types/svm/SvmPdaSeed.ts +19 -0
- package/src/types/svm/SvmTokenMetadataData.ts +29 -0
- package/src/types/svm/index.ts +5 -0
- package/src/intents/Call.ts +0 -238
- package/src/queries/Call.ts +0 -16
- package/src/types/EvmDecodeParam.ts +0 -30
- package/src/types/EvmEncodeParam.ts +0 -54
package/src/types/Address.ts
CHANGED
|
@@ -4,27 +4,54 @@
|
|
|
4
4
|
// Copyright (c) 2018 Graph Protocol, Inc. and contributors.
|
|
5
5
|
// Modified by Mimic Protocol, 2025.
|
|
6
6
|
|
|
7
|
+
import { EVM_NATIVE_ADDRESS, USD_ADDRESS } from '../helpers/constants'
|
|
8
|
+
import { isHex } from '../helpers/strings'
|
|
9
|
+
|
|
7
10
|
import { ByteArray } from './ByteArray'
|
|
8
11
|
import { Bytes } from './Bytes'
|
|
12
|
+
import { Option } from './Option'
|
|
9
13
|
|
|
10
14
|
/**
|
|
11
|
-
* Represents an
|
|
15
|
+
* Represents an EVM or SVM address, a fixed-length 20 or 32-byte value.
|
|
12
16
|
*/
|
|
13
17
|
export class Address extends Bytes {
|
|
14
18
|
/**
|
|
15
|
-
* Returns a zero address (20 bytes filled with zeroes).
|
|
19
|
+
* Returns a zero address (default 20 bytes filled with zeroes).
|
|
16
20
|
*/
|
|
17
|
-
static zero(): Address {
|
|
18
|
-
const self = new ByteArray(
|
|
21
|
+
static zero(length: i32 = 20): Address {
|
|
22
|
+
const self = new ByteArray(length)
|
|
19
23
|
return changetype<Address>(self)
|
|
20
24
|
}
|
|
21
25
|
|
|
26
|
+
/**
|
|
27
|
+
* Returns a None variant for the Option type, representing no address
|
|
28
|
+
* @param length 32 by default (SVM)
|
|
29
|
+
* @returns Option.none with empty bytes
|
|
30
|
+
*/
|
|
31
|
+
static none(length: u32 = 32): Option<Address> {
|
|
32
|
+
return Option.none<Address>(Address.fromBytes(new Bytes(length)))
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Returns the USD denomination address.
|
|
37
|
+
*/
|
|
38
|
+
static USD(): Address {
|
|
39
|
+
return Address.fromString(USD_ADDRESS)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Returns the native address.
|
|
44
|
+
*/
|
|
45
|
+
static native(): Address {
|
|
46
|
+
return Address.fromString(EVM_NATIVE_ADDRESS)
|
|
47
|
+
}
|
|
48
|
+
|
|
22
49
|
/**
|
|
23
50
|
* Converts a string representation of an address to an Address instance.
|
|
24
|
-
*
|
|
51
|
+
* If hex, EVM address is returned. Otherwise, base58 (SVM) is assumed.
|
|
25
52
|
*/
|
|
26
53
|
static fromString(str: string): Address {
|
|
27
|
-
return this.fromHexString(str)
|
|
54
|
+
return isHex(str) ? this.fromHexString(str) : this.fromBase58String(str)
|
|
28
55
|
}
|
|
29
56
|
|
|
30
57
|
/**
|
|
@@ -35,26 +62,71 @@ export class Address extends Bytes {
|
|
|
35
62
|
return this.fromBytes(bytes)
|
|
36
63
|
}
|
|
37
64
|
|
|
65
|
+
/**
|
|
66
|
+
* Converts a base58 string representation of an address to an Address instance.
|
|
67
|
+
*/
|
|
68
|
+
static fromBase58String(str: string): Address {
|
|
69
|
+
const bytes = Bytes.fromBase58String(str)
|
|
70
|
+
return this.fromBytes(bytes)
|
|
71
|
+
}
|
|
72
|
+
|
|
38
73
|
/**
|
|
39
74
|
* Converts a Bytes instance to an Address.
|
|
40
|
-
* Throws an error if the input is not exactly 20 bytes long.
|
|
75
|
+
* Throws an error if the input is not exactly 20 or 32 bytes long.
|
|
41
76
|
*/
|
|
42
77
|
static fromBytes(bytes: Bytes): Address {
|
|
43
|
-
if (bytes.length != 20
|
|
78
|
+
if (bytes.length != 20 && bytes.length != 32)
|
|
79
|
+
throw new Error(`Bytes of length ${bytes.length} can not be converted to 20 or 32 byte addresses`)
|
|
44
80
|
return changetype<Address>(bytes)
|
|
45
81
|
}
|
|
46
82
|
|
|
47
83
|
/**
|
|
48
|
-
* Returns
|
|
84
|
+
* Returns a copy of this address.
|
|
85
|
+
*/
|
|
86
|
+
clone(): Address {
|
|
87
|
+
return changetype<Address>(this.slice(0))
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Tells whether this address is an Ethereum address.
|
|
92
|
+
*/
|
|
93
|
+
isEVM(): bool {
|
|
94
|
+
return this.length == 20
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Tells whether this address is a Solana address.
|
|
99
|
+
*/
|
|
100
|
+
isSVM(): bool {
|
|
101
|
+
return this.length == 32
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Tells whether this address is the USD denomination address.
|
|
106
|
+
*/
|
|
107
|
+
isUsd(): boolean {
|
|
108
|
+
return this.equals(Address.USD())
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Tells whether this address is the native address.
|
|
113
|
+
*/
|
|
114
|
+
isNative(): boolean {
|
|
115
|
+
return this.equals(Address.native())
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Returns the address in hexadecimal or base58, accordingly. This method is overridden to avoid
|
|
49
120
|
* returning the UTF-8 encoded version of the address.
|
|
50
121
|
*/
|
|
51
122
|
toString(): string {
|
|
52
|
-
return super.toHexString()
|
|
123
|
+
return this.isEVM() ? super.toHexString() : super.toBase58String()
|
|
53
124
|
}
|
|
54
125
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
126
|
+
/**
|
|
127
|
+
* Returns the address as its underlying bytes
|
|
128
|
+
*/
|
|
129
|
+
toBytes(): Bytes {
|
|
130
|
+
return changetype<Bytes>(this.slice(0))
|
|
59
131
|
}
|
|
60
132
|
}
|
package/src/types/BigInt.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// Copyright (c) 2018 Graph Protocol, Inc. and contributors.
|
|
5
5
|
// Modified by Mimic Protocol, 2025.
|
|
6
6
|
|
|
7
|
-
import { areAllZeros, bytesToHexString, isHex,
|
|
7
|
+
import { areAllZeros, bytesToHexString, isHex, MAX_UINT256_HEX, normalizeScientificNotation } from '../helpers'
|
|
8
8
|
|
|
9
9
|
import { ByteArray } from './ByteArray'
|
|
10
10
|
import { Bytes } from './Bytes'
|
|
@@ -14,23 +14,19 @@ const ZERO_ASCII = '0'.charCodeAt(0)
|
|
|
14
14
|
/**
|
|
15
15
|
* Represents an arbitrary-precision integer stored as a byte array.
|
|
16
16
|
*/
|
|
17
|
-
export class BigInt extends Uint8Array
|
|
18
|
-
private static readonly SERIALIZED_PREFIX: string = 'BigInt'
|
|
19
|
-
|
|
17
|
+
export class BigInt extends Uint8Array {
|
|
20
18
|
/**
|
|
21
|
-
*
|
|
19
|
+
* Returns a BigInt initialized to zero.
|
|
22
20
|
*/
|
|
23
|
-
static
|
|
24
|
-
|
|
25
|
-
if (!isBigInt) throw new Error('Invalid serialized BigInt')
|
|
26
|
-
return BigInt.fromString(serialized.slice(BigInt.SERIALIZED_PREFIX.length + 1, -1))
|
|
21
|
+
static zero(): BigInt {
|
|
22
|
+
return BigInt.fromI32(0)
|
|
27
23
|
}
|
|
28
24
|
|
|
29
25
|
/**
|
|
30
|
-
* Returns a BigInt initialized to
|
|
26
|
+
* Returns a BigInt initialized to the maximum value for a 256-bit unsigned integer.
|
|
31
27
|
*/
|
|
32
|
-
static
|
|
33
|
-
return BigInt.
|
|
28
|
+
static maxUint256(): BigInt {
|
|
29
|
+
return BigInt.fromHexString(MAX_UINT256_HEX)
|
|
34
30
|
}
|
|
35
31
|
|
|
36
32
|
/**
|
|
@@ -408,15 +404,15 @@ export class BigInt extends Uint8Array implements Serializable {
|
|
|
408
404
|
return aIsNeg ? resultAbs.neg() : resultAbs
|
|
409
405
|
}
|
|
410
406
|
const cmp = BigInt.compare(this.abs(), other.abs())
|
|
411
|
-
if (cmp === 0)
|
|
412
|
-
|
|
413
|
-
|
|
407
|
+
if (cmp === 0) return BigInt.zero()
|
|
408
|
+
|
|
409
|
+
if (cmp > 0) {
|
|
414
410
|
const resultAbs = BigInt.subUnsigned(this.abs(), other.abs())
|
|
415
411
|
return this.isNegative() ? resultAbs.neg() : resultAbs
|
|
416
|
-
} else {
|
|
417
|
-
const resultAbs = BigInt.subUnsigned(other.abs(), this.abs())
|
|
418
|
-
return other.isNegative() ? resultAbs.neg() : resultAbs
|
|
419
412
|
}
|
|
413
|
+
|
|
414
|
+
const resultAbs = BigInt.subUnsigned(other.abs(), this.abs())
|
|
415
|
+
return other.isNegative() ? resultAbs.neg() : resultAbs
|
|
420
416
|
}
|
|
421
417
|
|
|
422
418
|
/**
|
|
@@ -789,8 +785,4 @@ export class BigInt extends Uint8Array implements Serializable {
|
|
|
789
785
|
? `${isNegative ? '-' : ''}${wholePart}.${decimalPart}`
|
|
790
786
|
: `${isNegative ? '-' : ''}${wholePart}`
|
|
791
787
|
}
|
|
792
|
-
|
|
793
|
-
serialize(): string {
|
|
794
|
-
return `${BigInt.SERIALIZED_PREFIX}(${this.toString()})`
|
|
795
|
-
}
|
|
796
788
|
}
|
package/src/types/ByteArray.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// Copyright (c) 2018 Graph Protocol, Inc. and contributors.
|
|
5
5
|
// Modified by Mimic Protocol, 2025.
|
|
6
6
|
|
|
7
|
-
import { bytesToHexString, bytesToString } from '../helpers'
|
|
7
|
+
import { bytesFromBase58String, bytesToBase58String, bytesToHexString, bytesToString, isHex } from '../helpers'
|
|
8
8
|
import { Serializable } from '../helpers'
|
|
9
9
|
|
|
10
10
|
import { BigInt } from './BigInt'
|
|
@@ -20,7 +20,7 @@ export class ByteArray extends Uint8Array implements Serializable {
|
|
|
20
20
|
* The resulting byte array is in little-endian order.
|
|
21
21
|
*/
|
|
22
22
|
static empty(): ByteArray {
|
|
23
|
-
return ByteArray
|
|
23
|
+
return new ByteArray(0)
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
/**
|
|
@@ -101,13 +101,21 @@ export class ByteArray extends Uint8Array implements Serializable {
|
|
|
101
101
|
* It may optionally start with '0x'.
|
|
102
102
|
*/
|
|
103
103
|
static fromHexString(hex: string): ByteArray {
|
|
104
|
-
assert(hex.length % 2 == 0,
|
|
104
|
+
assert(hex.length % 2 == 0, `input ${hex} has odd length`)
|
|
105
|
+
assert(isHex(hex), `input ${hex} is not valid hex`)
|
|
105
106
|
if (hex.length >= 2 && hex.charAt(0) == '0' && hex.charAt(1) == 'x') hex = hex.substring(2)
|
|
106
107
|
const output = new Bytes(hex.length / 2)
|
|
107
108
|
for (let i = 0; i < hex.length; i += 2) output[i / 2] = I8.parseInt(hex.substring(i, i + 2), 16)
|
|
108
109
|
return output
|
|
109
110
|
}
|
|
110
111
|
|
|
112
|
+
/**
|
|
113
|
+
* Converts a base58 string to a ByteArray.
|
|
114
|
+
*/
|
|
115
|
+
static fromBase58String(base58: string): ByteArray {
|
|
116
|
+
return bytesFromBase58String(base58)
|
|
117
|
+
}
|
|
118
|
+
|
|
111
119
|
/**
|
|
112
120
|
* Converts a UTF-8 string to a ByteArray.
|
|
113
121
|
*/
|
|
@@ -168,6 +176,29 @@ export class ByteArray extends Uint8Array implements Serializable {
|
|
|
168
176
|
return !(this == other)
|
|
169
177
|
}
|
|
170
178
|
|
|
179
|
+
/**
|
|
180
|
+
* Interprets the byte array as a little-endian U16.
|
|
181
|
+
* Throws in case of overflow.
|
|
182
|
+
*/
|
|
183
|
+
toU16(): u16 {
|
|
184
|
+
for (let i = 2; i < this.length; i++) {
|
|
185
|
+
if (this[i] != 0) {
|
|
186
|
+
assert(false, 'overflow converting ' + this.toHexString() + ' to u16')
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
const paddedBytes = new Bytes(2)
|
|
190
|
+
paddedBytes[0] = 0
|
|
191
|
+
paddedBytes[1] = 0
|
|
192
|
+
const minLen = paddedBytes.length < this.length ? paddedBytes.length : this.length
|
|
193
|
+
for (let i = 0; i < minLen; i++) {
|
|
194
|
+
paddedBytes[i] = this[i]
|
|
195
|
+
}
|
|
196
|
+
let x: u16 = 0
|
|
197
|
+
x = (x | paddedBytes[1]) << 8
|
|
198
|
+
x = x | paddedBytes[0]
|
|
199
|
+
return x
|
|
200
|
+
}
|
|
201
|
+
|
|
171
202
|
/**
|
|
172
203
|
* Interprets the byte array as a little-endian U32.
|
|
173
204
|
* Throws in case of overflow.
|
|
@@ -303,6 +334,13 @@ export class ByteArray extends Uint8Array implements Serializable {
|
|
|
303
334
|
return bytesToHexString(this)
|
|
304
335
|
}
|
|
305
336
|
|
|
337
|
+
/**
|
|
338
|
+
* Converts this ByteArray to a base58 string.
|
|
339
|
+
*/
|
|
340
|
+
toBase58String(): string {
|
|
341
|
+
return bytesToBase58String(this)
|
|
342
|
+
}
|
|
343
|
+
|
|
306
344
|
/**
|
|
307
345
|
* Converts this ByteArray to a string representation.
|
|
308
346
|
*/
|
package/src/types/Bytes.ts
CHANGED
|
@@ -34,6 +34,13 @@ export class Bytes extends ByteArray {
|
|
|
34
34
|
return changetype<Bytes>(ByteArray.fromHexString(hex))
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Converts a base58 string to a Bytes instance.
|
|
39
|
+
*/
|
|
40
|
+
static fromBase58String(hex: string): Bytes {
|
|
41
|
+
return changetype<Bytes>(ByteArray.fromBase58String(hex))
|
|
42
|
+
}
|
|
43
|
+
|
|
37
44
|
/**
|
|
38
45
|
* Converts a UTF-8 string to a Bytes instance.
|
|
39
46
|
*/
|
package/src/types/ChainId.ts
CHANGED
|
@@ -4,6 +4,14 @@
|
|
|
4
4
|
*/
|
|
5
5
|
export enum ChainId {
|
|
6
6
|
ETHEREUM = 1,
|
|
7
|
-
POLYGON = 137,
|
|
8
7
|
OPTIMISM = 10,
|
|
8
|
+
BNB = 56,
|
|
9
|
+
GNOSIS = 100,
|
|
10
|
+
POLYGON = 137,
|
|
11
|
+
SONIC = 146,
|
|
12
|
+
BASE = 8453,
|
|
13
|
+
ARBITRUM = 42161,
|
|
14
|
+
AVALANCHE = 43114,
|
|
15
|
+
BASE_SEPOLIA = 84532,
|
|
16
|
+
SOLANA_MAINNET = 507424,
|
|
9
17
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Simple wrapper for Option<T> Rust types
|
|
3
|
+
* To be used in tandem with BorshDeserializer
|
|
4
|
+
*/
|
|
5
|
+
export class Option<T> {
|
|
6
|
+
constructor(
|
|
7
|
+
private _value: T,
|
|
8
|
+
public readonly isSome: bool
|
|
9
|
+
) {}
|
|
10
|
+
|
|
11
|
+
static some<T>(value: T): Option<T> {
|
|
12
|
+
return new Option<T>(value, true)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
static none<T>(defaultValue: T = 0 as T): Option<T> {
|
|
16
|
+
return new Option<T>(defaultValue, false)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
unwrap(): T {
|
|
20
|
+
if (this.isSome) return this._value
|
|
21
|
+
throw new Error("Can't unwrap None variant")
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
eq(other: Option<T>): bool {
|
|
25
|
+
if (this.isSome != other.isSome) return false
|
|
26
|
+
if (!this.isSome && !other.isSome) return true
|
|
27
|
+
return this._value == other._value
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
toString(): string {
|
|
31
|
+
if (!this.isSome) return 'None'
|
|
32
|
+
// @ts-expect-error: AssemblyScript lacks runtime reflection, so we assume toString exists
|
|
33
|
+
return `Some(${this._value.toString ? this._value.toString() : '[Object]'})`
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// eslint-disable-next-line no-secrets/no-secrets
|
|
2
|
+
// This file is based on code from "The Graph Tooling" (https://github.com/graphprotocol/graph-tooling/tree/7faa3098b2e6c61f09fc81b8b2d333e66b0080d1).
|
|
3
|
+
// Licensed under the MIT License.
|
|
4
|
+
// Copyright (c) 2018 Graph Protocol, Inc. and contributors.
|
|
5
|
+
// Modified by Mimic Protocol, 2025.
|
|
6
|
+
|
|
7
|
+
import { Stringable } from '../helpers'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The result of an operation, with a corresponding value and error type.
|
|
11
|
+
*/
|
|
12
|
+
export class Result<V, E extends Stringable> {
|
|
13
|
+
constructor(
|
|
14
|
+
private _value: Wrapped<V> | null,
|
|
15
|
+
private _error: Wrapped<E> | null
|
|
16
|
+
) {}
|
|
17
|
+
|
|
18
|
+
static ok<V, E extends Stringable>(value: V): Result<V, E> {
|
|
19
|
+
return new Result<V, E>(new Wrapped<V>(value), null)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
static err<V, E extends Stringable>(error: E): Result<V, E> {
|
|
23
|
+
return new Result<V, E>(null, new Wrapped<E>(error))
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
get isOk(): boolean {
|
|
27
|
+
return this._value !== null
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
get isError(): boolean {
|
|
31
|
+
return this._error !== null
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
get error(): E {
|
|
35
|
+
if (this.isOk) throw new Error('Trying to get an error from a successful result')
|
|
36
|
+
return changetype<Wrapped<E>>(this._error).inner
|
|
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
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// This is used to wrap a generic so that it can be unioned with `null`, working around limitations
|
|
56
|
+
// with primitives.
|
|
57
|
+
export class Wrapped<T> {
|
|
58
|
+
inner: T
|
|
59
|
+
|
|
60
|
+
constructor(inner: T) {
|
|
61
|
+
this.inner = inner
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// This is used to represent a void return type
|
|
66
|
+
export class Void {
|
|
67
|
+
constructor() {}
|
|
68
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { JSON } from 'json-as'
|
|
2
|
+
|
|
3
|
+
import { serialize, Stringable } from '../../helpers'
|
|
4
|
+
|
|
5
|
+
@json
|
|
6
|
+
export class EvmEncodeParam {
|
|
7
|
+
constructor(
|
|
8
|
+
public readonly abiType: string,
|
|
9
|
+
public readonly value: string
|
|
10
|
+
) {}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Creates an EvmEncodeParam for a primitive value.
|
|
14
|
+
* @param type - The ABI type signature (e.g., "uint256", "address", "string")
|
|
15
|
+
* @param value - The value to encode, must implement Stringable interface
|
|
16
|
+
* @returns A new EvmEncodeParam instance representing the primitive value
|
|
17
|
+
*/
|
|
18
|
+
static fromValue<T extends Stringable>(type: string, value: T): EvmEncodeParam {
|
|
19
|
+
return new EvmEncodeParam(type, serialize(value))
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Creates an EvmEncodeParam for complex types like arrays or tuples.
|
|
24
|
+
* @param type - The ABI type signature (e.g., "address[]", "()", "()[]")
|
|
25
|
+
* @param values - Array of EvmEncodeParam instances representing the nested elements
|
|
26
|
+
* @returns A new EvmEncodeParam instance representing the complex type
|
|
27
|
+
*/
|
|
28
|
+
static fromValues(type: string, values: EvmEncodeParam[]): EvmEncodeParam {
|
|
29
|
+
return new EvmEncodeParam(type, JSON.stringify(values))
|
|
30
|
+
}
|
|
31
|
+
}
|
package/src/types/index.ts
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
|
+
import { JSON } from 'json-as'
|
|
2
|
+
|
|
1
3
|
export * from './Address'
|
|
2
4
|
export * from './BigInt'
|
|
3
5
|
export * from './ByteArray'
|
|
4
6
|
export * from './Bytes'
|
|
5
7
|
export * from './ChainId'
|
|
6
|
-
export * from './
|
|
7
|
-
export * from './
|
|
8
|
+
export * from './evm'
|
|
9
|
+
export * from './Option'
|
|
10
|
+
export * from './Result'
|
|
11
|
+
export * from './svm'
|
|
12
|
+
export * from './TriggerType'
|
|
13
|
+
export { JSON }
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { stringToBool } from '../../helpers'
|
|
2
|
+
|
|
3
|
+
export class SvmAccountInfo {
|
|
4
|
+
constructor(
|
|
5
|
+
public owner: string,
|
|
6
|
+
public lamports: string,
|
|
7
|
+
public data: string,
|
|
8
|
+
public rentEpoch: string,
|
|
9
|
+
public executable: bool
|
|
10
|
+
) {}
|
|
11
|
+
|
|
12
|
+
static fromSerializable(serializable: SerializableSvmAccountInfo): SvmAccountInfo {
|
|
13
|
+
return new SvmAccountInfo(
|
|
14
|
+
serializable.owner,
|
|
15
|
+
serializable.lamports,
|
|
16
|
+
serializable.data,
|
|
17
|
+
serializable.rentEpoch,
|
|
18
|
+
stringToBool(serializable.executable)
|
|
19
|
+
)
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
@json
|
|
24
|
+
export class SerializableSvmAccountInfo {
|
|
25
|
+
constructor(
|
|
26
|
+
public owner: string,
|
|
27
|
+
public lamports: string,
|
|
28
|
+
public data: string,
|
|
29
|
+
public rentEpoch: string,
|
|
30
|
+
public executable: string
|
|
31
|
+
) {}
|
|
32
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Address } from '../Address'
|
|
2
|
+
|
|
3
|
+
@json
|
|
4
|
+
export class SvmAccountMeta {
|
|
5
|
+
constructor(
|
|
6
|
+
public pubkey: string,
|
|
7
|
+
public isWritable: bool = false,
|
|
8
|
+
public isSigner: bool = false
|
|
9
|
+
) {}
|
|
10
|
+
|
|
11
|
+
writable(): SvmAccountMeta {
|
|
12
|
+
this.isWritable = true
|
|
13
|
+
return this
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
signer(): SvmAccountMeta {
|
|
17
|
+
this.isSigner = true
|
|
18
|
+
return this
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
static fromAddress(pubkey: Address): SvmAccountMeta {
|
|
22
|
+
return new SvmAccountMeta(pubkey.toString())
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
static fromString(pubkey: string): SvmAccountMeta {
|
|
26
|
+
return new SvmAccountMeta(pubkey)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Address } from './../Address'
|
|
2
|
+
import { SvmPdaSeed } from './SvmPdaSeed'
|
|
3
|
+
|
|
4
|
+
@json
|
|
5
|
+
export class SvmFindProgramAddressParams {
|
|
6
|
+
public readonly seeds: SvmPdaSeed[]
|
|
7
|
+
public readonly programId: string
|
|
8
|
+
|
|
9
|
+
constructor(seeds: SvmPdaSeed[], programId: string) {
|
|
10
|
+
this.seeds = seeds.slice()
|
|
11
|
+
this.programId = programId
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class SvmFindProgramAddressResult {
|
|
16
|
+
constructor(
|
|
17
|
+
public address: Address,
|
|
18
|
+
public bump: u8
|
|
19
|
+
) {}
|
|
20
|
+
|
|
21
|
+
static fromSerializable(serializable: SerializableSvmFindProgramAddressResult): SvmFindProgramAddressResult {
|
|
22
|
+
return new SvmFindProgramAddressResult(Address.fromString(serializable.address), serializable.bump)
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
@json
|
|
27
|
+
export class SerializableSvmFindProgramAddressResult {
|
|
28
|
+
constructor(
|
|
29
|
+
public address: string,
|
|
30
|
+
public bump: u8
|
|
31
|
+
) {}
|
|
32
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { BorshDeserializer } from '../../helpers'
|
|
2
|
+
|
|
3
|
+
import { Address } from './../Address'
|
|
4
|
+
import { Bytes } from './../Bytes'
|
|
5
|
+
import { Option } from './../Option'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* State layout for Mint TokenProgram PDAs
|
|
9
|
+
*/
|
|
10
|
+
export class SvmMint {
|
|
11
|
+
static DECIMALS_OFFSET: u32 = 44
|
|
12
|
+
|
|
13
|
+
constructor(
|
|
14
|
+
public mintAuthority: Option<Address>,
|
|
15
|
+
public supply: u64,
|
|
16
|
+
public decimals: u8,
|
|
17
|
+
public isInitialized: bool,
|
|
18
|
+
public freezeAuthority: Option<Address>
|
|
19
|
+
) {}
|
|
20
|
+
|
|
21
|
+
static fromHex(hex: string): SvmMint {
|
|
22
|
+
return this.fromBytes(Bytes.fromHexString(hex))
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
static fromBytes(bytes: Bytes): SvmMint {
|
|
26
|
+
const deserializer = BorshDeserializer.fromBytes(bytes)
|
|
27
|
+
|
|
28
|
+
const mintAuthorityFlag = deserializer.tryU32() === 1
|
|
29
|
+
const mintAuthority = deserializer.tryPubkey()
|
|
30
|
+
const supply = deserializer.tryU64()
|
|
31
|
+
const decimals = deserializer.tryU8()
|
|
32
|
+
const isInitialized = deserializer.tryBool()
|
|
33
|
+
const freezeAuthorityFlag = deserializer.tryU32() === 1
|
|
34
|
+
const freezeAuthority = deserializer.tryPubkey()
|
|
35
|
+
|
|
36
|
+
return new SvmMint(
|
|
37
|
+
mintAuthorityFlag ? Option.some(mintAuthority) : Address.none(),
|
|
38
|
+
supply,
|
|
39
|
+
decimals,
|
|
40
|
+
isInitialized,
|
|
41
|
+
freezeAuthorityFlag ? Option.some(freezeAuthority) : Address.none()
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Byteable, bytesToHexString } from '../../helpers'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Type for findProgramAddress seeds
|
|
5
|
+
* As we need all seeds to be decoded as bytes in the same way as in Rust,
|
|
6
|
+
* this class provides a simple interface for end-users to abstract all this
|
|
7
|
+
*/
|
|
8
|
+
@json
|
|
9
|
+
export class SvmPdaSeed {
|
|
10
|
+
constructor(public readonly hex: string) {}
|
|
11
|
+
|
|
12
|
+
static fromString(str: string): SvmPdaSeed {
|
|
13
|
+
return new SvmPdaSeed(bytesToHexString(Uint8Array.wrap(String.UTF8.encode(str))))
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
static from<T extends Byteable>(t: T): SvmPdaSeed {
|
|
17
|
+
return new SvmPdaSeed(bytesToHexString(t.toBytes()))
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { BorshDeserializer } from '../../helpers'
|
|
2
|
+
|
|
3
|
+
import { Bytes } from './../Bytes'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Partial state layout for Data Metaplex PDA (Metadata PDA "data" field)
|
|
7
|
+
*/
|
|
8
|
+
export class SvmTokenMetadataData {
|
|
9
|
+
static DATA_OFFSET: u32 = 65
|
|
10
|
+
// eslint-disable-next-line no-secrets/no-secrets
|
|
11
|
+
static METADATA_PROGRAM_ID: string = 'metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s'
|
|
12
|
+
|
|
13
|
+
constructor(
|
|
14
|
+
public name: string,
|
|
15
|
+
public symbol: string,
|
|
16
|
+
public uri: string
|
|
17
|
+
) {}
|
|
18
|
+
|
|
19
|
+
static fromTokenMetadataHex(hex: string): SvmTokenMetadataData {
|
|
20
|
+
return this.fromTokenMetadataBytes(Bytes.fromHexString(hex))
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
static fromTokenMetadataBytes(bytes: Bytes): SvmTokenMetadataData {
|
|
24
|
+
const deserializer = BorshDeserializer.fromBytes(bytes)
|
|
25
|
+
deserializer.setOffset(this.DATA_OFFSET)
|
|
26
|
+
|
|
27
|
+
return new SvmTokenMetadataData(deserializer.tryString(), deserializer.tryString(), deserializer.tryString())
|
|
28
|
+
}
|
|
29
|
+
}
|