@@ -13,7 +13,8 @@ import {
13
13
runtimeModule ,
14
14
} from "@proto-kit/module" ;
15
15
import { StateMap , assert } from "@proto-kit/protocol" ;
16
- import { Field , Provable , PublicKey , Struct , UInt64 } from "o1js" ;
16
+ import { Field , PublicKey , Struct } from "o1js" ;
17
+ import { UInt64 } from "../math/UInt64" ;
17
18
18
19
export const errors = {
19
20
senderNotFrom : ( ) => "Sender does not match 'from'" ,
@@ -25,8 +26,9 @@ export class BalancesKey extends Struct({
25
26
tokenId : TokenId ,
26
27
address : PublicKey ,
27
28
} ) {
28
- // eslint-disable-next-line @typescript-eslint/no-empty-function
29
- public test ( ) { }
29
+ public static from ( tokenId : TokenId , address : PublicKey ) {
30
+ return new BalancesKey ( { tokenId, address } ) ;
31
+ }
30
32
}
31
33
32
34
export class Balance extends UInt64 { }
@@ -58,13 +60,8 @@ export class Balances<Config = NoConfig>
58
60
public getBalance ( tokenId : TokenId , address : PublicKey ) : Balance {
59
61
const key = new BalancesKey ( { tokenId, address } ) ;
60
62
const balanceOption = this . balances . get ( key ) ;
61
-
62
- return Provable . if < Balance > (
63
- balanceOption . isSome ,
64
- Balance ,
65
- balanceOption . value ,
66
- Balance . from ( 0 )
67
- ) ;
63
+ const balance = Balance . from ( balanceOption . value . value ) ;
64
+ return balance ;
68
65
}
69
66
70
67
public setBalance ( tokenId : TokenId , address : PublicKey , amount : Balance ) {
@@ -85,16 +82,7 @@ export class Balances<Config = NoConfig>
85
82
86
83
assert ( fromBalanceIsSufficient , errors . fromBalanceInsufficient ( ) ) ;
87
84
88
- // used to prevent field underflow during subtraction
89
- const paddedFrombalance = fromBalance . add ( amount ) ;
90
- const safeFromBalance = Provable . if < Balance > (
91
- fromBalanceIsSufficient ,
92
- Balance ,
93
- fromBalance ,
94
- paddedFrombalance
95
- ) ;
96
-
97
- const newFromBalance = safeFromBalance . sub ( amount ) ;
85
+ const newFromBalance = fromBalance . sub ( amount ) ;
98
86
const newToBalance = toBalance . add ( amount ) ;
99
87
100
88
this . setBalance ( tokenId , from , newFromBalance ) ;
@@ -109,14 +97,7 @@ export class Balances<Config = NoConfig>
109
97
110
98
public burn ( tokenId : TokenId , address : PublicKey , amount : Balance ) {
111
99
const balance = this . getBalance ( tokenId , address ) ;
112
- // replace with library/uint64
113
- const paddedBalance = Provable . if < Balance > (
114
- balance . greaterThanOrEqual ( amount ) ,
115
- Balance ,
116
- balance ,
117
- balance . add ( amount )
118
- ) ;
119
- const newBalance = paddedBalance . sub ( amount ) ;
100
+ const newBalance = balance . sub ( amount ) ;
120
101
this . setBalance ( tokenId , address , newBalance ) ;
121
102
}
122
103
0 commit comments