@xyo-network/xl1-protocol 1.4.1 → 1.4.3

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.
@@ -1,9 +1,7 @@
1
1
  import type { Hex } from '@xylabs/hex'
2
2
 
3
- import {
4
- AttoXL1,
5
- xl1ConvertFactor,
6
- } from '../xl1/index.ts'
3
+ import type { AttoXL1 } from '../xl1/index.ts'
4
+ import { MicroXL1 } from '../xl1/index.ts'
7
5
 
8
6
  export interface TransactionFeesBigInt {
9
7
  base: AttoXL1
@@ -21,15 +19,15 @@ export interface TransactionFeesFields {
21
19
  }
22
20
 
23
21
  export const minTransactionFees: TransactionFeesBigInt = {
24
- base: AttoXL1(1000n * xl1ConvertFactor('micro')),
25
- gasPrice: AttoXL1(1n * xl1ConvertFactor('micro')),
26
- gasLimit: AttoXL1(1000n * xl1ConvertFactor('micro')),
27
- priority: AttoXL1(0n * xl1ConvertFactor('micro')),
22
+ base: MicroXL1(1000n).toAtto(),
23
+ gasPrice: MicroXL1(1n).toAtto(),
24
+ gasLimit: MicroXL1(1000n).toAtto(),
25
+ priority: MicroXL1(0n).toAtto(),
28
26
  } as const
29
27
 
30
28
  export const defaultTransactionFees: TransactionFeesBigInt = {
31
29
  base: minTransactionFees.base,
32
- gasPrice: AttoXL1(10n * xl1ConvertFactor('micro')),
33
- gasLimit: AttoXL1(1_000_000n * xl1ConvertFactor('micro')),
34
- priority: AttoXL1(0n * xl1ConvertFactor('micro')),
30
+ gasPrice: MicroXL1(10n).toAtto(),
31
+ gasLimit: MicroXL1(1_000_000n).toAtto(),
32
+ priority: minTransactionFees.priority,
35
33
  } as const
package/src/xl1/XL1.ts CHANGED
@@ -6,17 +6,103 @@ export type PicoXL1 = bigint & { readonly _tag: 'PicoXL1' } // 1e-6 [XL1 * 1e12
6
6
  export type FemtoXL1 = bigint & { readonly _tag: 'FemtoXL1' } // 1e-3 [XL1 * 1e15 = femtoXL1] [femtoXL1 / 1e15 = XL1]
7
7
  export type AttoXL1 = bigint & { readonly _tag: 'AttoXL1' } // 1e-0 [XL1 * 1e18 = attoXL1] [attoXL1 / 1e18 = XL1]
8
8
 
9
- export type TypingFunc<T extends bigint> = (value: bigint) => T
9
+ type XL1Units = 'xl1' | 'milli' | 'micro' | 'nano' | 'pico' | 'femto' | 'atto'
10
10
 
11
- export const XL1: TypingFunc<XL1> = (value: bigint): XL1 => value as XL1
12
- export const MilliXL1: TypingFunc<MilliXL1> = (value: bigint): MilliXL1 => value as MilliXL1
13
- export const MicroXL1: TypingFunc<MicroXL1> = (value: bigint): MicroXL1 => value as MicroXL1
14
- export const NanoXL1: TypingFunc<NanoXL1> = (value: bigint): NanoXL1 => value as NanoXL1
15
- export const PicoXL1: TypingFunc<PicoXL1> = (value: bigint): PicoXL1 => value as PicoXL1
16
- export const FemtoXL1: TypingFunc<FemtoXL1> = (value: bigint): FemtoXL1 => value as FemtoXL1
17
- export const AttoXL1: TypingFunc<AttoXL1> = (value: bigint): AttoXL1 => value as AttoXL1
11
+ export type TypingFunc<T extends bigint> = {
12
+ (value: bigint): T
13
+ toAtto: () => AttoXL1
14
+ }
18
15
 
19
- type XL1Units = 'xl1' | 'milli' | 'micro' | 'nano' | 'pico' | 'femto' | 'atto'
16
+ export function XL1(value: bigint): XL1 & { toAtto(): AttoXL1 } {
17
+ const xl1Value = value as XL1
18
+
19
+ return new Proxy(xl1Value, {
20
+ get(target, prop) {
21
+ if (prop === 'toAtto') {
22
+ return () => AttoXL1(target * AttoXL1ConvertFactor.xl1)
23
+ }
24
+ return Reflect.get(target, prop)
25
+ },
26
+ }) as XL1 & { toAtto(): AttoXL1 }
27
+ }
28
+
29
+ export function MilliXL1(value: bigint): MilliXL1 & { toAtto(): AttoXL1 } {
30
+ const xl1Value = value as MilliXL1
31
+
32
+ return new Proxy(xl1Value, {
33
+ get(target, prop) {
34
+ if (prop === 'toAtto') {
35
+ return () => AttoXL1(target * AttoXL1ConvertFactor.milli)
36
+ }
37
+ return Reflect.get(target, prop)
38
+ },
39
+ }) as MilliXL1 & { toAtto(): AttoXL1 }
40
+ }
41
+
42
+ export function MicroXL1(value: bigint): MicroXL1 & { toAtto(): AttoXL1 } {
43
+ const xl1Value = value as MicroXL1
44
+
45
+ return new Proxy(xl1Value, {
46
+ get(target, prop) {
47
+ if (prop === 'toAtto') {
48
+ return () => AttoXL1(target * AttoXL1ConvertFactor.micro)
49
+ }
50
+ return Reflect.get(target, prop)
51
+ },
52
+ }) as MicroXL1 & { toAtto(): AttoXL1 }
53
+ }
54
+
55
+ export function NanoXL1(value: bigint): NanoXL1 & { toAtto(): AttoXL1 } {
56
+ const xl1Value = value as NanoXL1
57
+
58
+ return new Proxy(xl1Value, {
59
+ get(target, prop) {
60
+ if (prop === 'toAtto') {
61
+ return () => AttoXL1(target * AttoXL1ConvertFactor.nano)
62
+ }
63
+ return Reflect.get(target, prop)
64
+ },
65
+ }) as NanoXL1 & { toAtto(): AttoXL1 }
66
+ }
67
+
68
+ export function PicoXL1(value: bigint): PicoXL1 & { toAtto(): AttoXL1 } {
69
+ const xl1Value = value as PicoXL1
70
+
71
+ return new Proxy(xl1Value, {
72
+ get(target, prop) {
73
+ if (prop === 'toAtto') {
74
+ return () => AttoXL1(target * AttoXL1ConvertFactor.pico)
75
+ }
76
+ return Reflect.get(target, prop)
77
+ },
78
+ }) as PicoXL1 & { toAtto(): AttoXL1 }
79
+ }
80
+
81
+ export function FemtoXL1(value: bigint): FemtoXL1 & { toAtto(): AttoXL1 } {
82
+ const xl1Value = value as FemtoXL1
83
+
84
+ return new Proxy(xl1Value, {
85
+ get(target, prop) {
86
+ if (prop === 'toAtto') {
87
+ return () => AttoXL1(target * AttoXL1ConvertFactor.femto)
88
+ }
89
+ return Reflect.get(target, prop)
90
+ },
91
+ }) as FemtoXL1 & { toAtto(): AttoXL1 }
92
+ }
93
+
94
+ export function AttoXL1(value: bigint): AttoXL1 & { toAtto(): AttoXL1 } {
95
+ const xl1Value = value as AttoXL1
96
+
97
+ return new Proxy(xl1Value, {
98
+ get(target, prop) {
99
+ if (prop === 'toAtto') {
100
+ return () => AttoXL1(target * AttoXL1ConvertFactor.atto)
101
+ }
102
+ return Reflect.get(target, prop)
103
+ },
104
+ }) as AttoXL1 & { toAtto(): AttoXL1 }
105
+ }
20
106
 
21
107
  /** @deprecated use XL1Places and xl1ConvertFactor(unit) instead */
22
108
  export const XL1ConvertDict: Record<XL1Units, number> = {
@@ -39,6 +125,21 @@ export const XL1Places: Record<XL1Units, bigint> = {
39
125
  atto: 0n,
40
126
  } as const
41
127
 
128
+ /**
129
+ * Convert factor by which a respective unit is multiplied to convert it to AttoXL1 or
130
+ * by which AttoXL1 is divided to convert it to respective unit is multiplied.
131
+ */
132
+ export const AttoXL1ConvertFactor: Record<XL1Units, bigint> = {
133
+ xl1: 10n ** XL1Places.xl1,
134
+ milli: 10n ** XL1Places.milli,
135
+ micro: 10n ** XL1Places.micro,
136
+ nano: 10n ** XL1Places.nano,
137
+ pico: 10n ** XL1Places.pico,
138
+ femto: 10n ** XL1Places.femto,
139
+ atto: 10n ** XL1Places.atto,
140
+ } as const
141
+
142
+ /** @deprecated use AttoXL1ConvertFactor instead */
42
143
  export function xl1ConvertFactor(unit: XL1Units) {
43
144
  return 10n ** XL1Places[unit]
44
145
  }