Skip to content

Commit f9c7de3

Browse files
committed
Revamped UInts to work properly
1 parent 2e1bab0 commit f9c7de3

File tree

13 files changed

+564
-489
lines changed

13 files changed

+564
-489
lines changed

packages/api/src/graphql/modules/QueryGraphqlModule.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ import {
4747
BaseModuleType,
4848
log,
4949
ModuleContainer,
50-
ModulesRecord,
51-
range,
50+
ModulesRecord, NonMethods,
51+
range
5252
} from "@proto-kit/common";
5353
import { ObjMap } from "graphql/jsutils/ObjMap";
5454

packages/common/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,4 @@ export function expectDefined<T>(value: T | undefined): asserts value is T {
102102
type NonMethodKeys<Type> = {
103103
[Key in keyof Type]: Type[Key] extends Function ? never : Key;
104104
}[keyof Type];
105-
type NonMethods<Type> = Pick<Type, NonMethodKeys<Type>>;
105+
export type NonMethods<Type> = Pick<Type, NonMethodKeys<Type>>;

packages/library/src/hooks/TransactionFeeHook.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export class TransactionFeeHook extends ProvableTransactionHook<TransactionFeeHo
102102
new TokenId(this.config.tokenId),
103103
from.value,
104104
PublicKey.fromBase58(this.config.feeRecipient),
105-
Balance.from(fee.value)
105+
Balance.Unsafe.fromField(fee.value)
106106
);
107107
}
108108

@@ -136,12 +136,12 @@ export class TransactionFeeHook extends ProvableTransactionHook<TransactionFeeHo
136136
errors.invalidFeeConfigMethodId()
137137
);
138138

139-
const fee = UInt64.from(feeConfig.baseFee.value).add(
140-
UInt64.from(feeConfig.weight.value).mul(
141-
UInt64.from(feeConfig.perWeightUnitFee.value)
139+
const fee = UInt64.Unsafe.fromField(feeConfig.baseFee.value).add(
140+
UInt64.Unsafe.fromField(feeConfig.weight.value).mul(
141+
UInt64.Unsafe.fromField(feeConfig.perWeightUnitFee.value)
142142
)
143143
);
144144

145-
this.transferFee(executionData.transaction.sender, UInt64.from(fee.value));
145+
this.transferFee(executionData.transaction.sender, UInt64.Unsafe.fromField(fee.value));
146146
}
147147
}

packages/library/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export * from "./math/UInt";
22
export * from "./math/UInt32";
33
export * from "./math/UInt64";
44
export * from "./math/UInt112";
5-
export * from "./math/UInt224";
5+
// export * from "./math/UInt224";
66
export * from "./math/PrecisionHelper";
77
export * from "./protocol/VanillaProtocolModules";
88
export * from "./runtime/Balances";
Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
import { UInt112 } from "./UInt112";
22
import { UInt224 } from "./UInt224";
33

4-
export class PrecisionUInt112 extends UInt112 {
5-
public static fromUInt112(uint: UInt112){
6-
return new PrecisionUInt112(uint.value)
7-
}
8-
9-
public static get precision(): PrecisionUInt112 {
10-
return new PrecisionUInt112(UInt112.MAXINT().value);
11-
}
12-
13-
public mulPrecision(y: UInt112): PrecisionUInt224 {
14-
return PrecisionUInt224.fromUInt224(
15-
UInt224.from(this.value).mul(UInt224.from(y.value))
16-
)
17-
}
18-
}
19-
20-
export class PrecisionUInt224 extends UInt224 {
21-
public static get precision(): PrecisionUInt224 {
22-
return new PrecisionUInt224(UInt224.MAXINT().value);
23-
}
24-
25-
public static fromUInt224(uint: UInt224){
26-
return new PrecisionUInt224(uint.value)
27-
}
4+
// export class PrecisionUInt112 extends UInt112 {
5+
// public static fromUInt112(uint: UInt112){
6+
// return new PrecisionUInt112({ value: uint.value })
7+
// }
8+
//
9+
// public static get precision(): PrecisionUInt112 {
10+
// return new PrecisionUInt112({ value: UInt112.MAXINT().value });
11+
// }
12+
//
13+
// public mulPrecision(y: UInt112): PrecisionUInt224 {
14+
// return PrecisionUInt224.fromUInt224(
15+
// UInt224.Unsafe.fromField(this.value).mul(UInt224.Unsafe.fromField(y.value))
16+
// )
17+
// }
18+
// }
19+
//
20+
// export class PrecisionUInt224 extends UInt224 {
21+
// public static get precision(): PrecisionUInt224 {
22+
// return new PrecisionUInt224({ value: UInt224.MAXINT().value });
23+
// }
24+
//
25+
// public static fromUInt224(uint: UInt224){
26+
// return new PrecisionUInt224({ value: uint.value })
27+
// }
2828

2929
// public divPrecision(y: UInt112) : PrecisionUInt112 {
3030
// return new PrecisionUInt112(
3131
// this.div(UInt224.from(y.value)).toUInt112().value
3232
// )
3333
// }
34-
}
34+
// }

packages/library/src/math/Types.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { UIntX } from "./UInt";
2+
3+
type AvailableBitLengths = 32 | 64 | 112 | 224;
4+
5+
type NextLowest<Input extends AvailableBitLengths> = Input extends 224
6+
? 112
7+
: Input extends 112
8+
? 64
9+
: Input extends 64
10+
? 32
11+
: never;
12+
13+
type RecursiveSmaller<Input extends AvailableBitLengths> =
14+
| Input
15+
| (NextLowest<Input> extends never
16+
? never
17+
: RecursiveSmaller<NextLowest<Input>>);
18+
19+
/**
20+
* Type to determine all possible bitlengths of UInts that would fit into
21+
* a given bitlength without doing additional rangechecks.
22+
* I.e. FittingUInt<112> = UIntX<32 | 64 | 112>
23+
*/
24+
export type FittingUInt<Input extends number> =
25+
Input extends AvailableBitLengths
26+
? UIntX<RecursiveSmaller<Input>>
27+
: UIntX<Input>;

0 commit comments

Comments
 (0)