3
3
type AccountWallet ,
4
4
type AccountWalletWithSecretKey ,
5
5
type AztecAddress ,
6
- type CompleteAddress ,
7
6
ExtendedNote ,
8
7
Fr ,
9
8
Note ,
@@ -17,7 +16,7 @@ import { TokenContract } from '@aztec/noir-contracts.js';
17
16
18
17
import { jest } from '@jest/globals' ;
19
18
20
- import { addAccounts , publicDeployAccounts } from '../fixtures/snapshot_manager.js' ;
19
+ import { addAccounts } from '../fixtures/snapshot_manager.js' ;
21
20
22
21
const { PXE_URL } = process . env ;
23
22
if ( ! PXE_URL ) {
@@ -65,52 +64,93 @@ describe('token transfer test', () => {
65
64
const TOKEN_SYMBOL = 'USD' ;
66
65
const TOKEN_DECIMALS = 18n ;
67
66
const MINT_AMOUNT = 20n ;
67
+
68
+ const WALLET_COUNT = 1 ;
69
+ const ROUNDS = 5n ;
70
+
68
71
let pxe : PXE ;
69
72
let wallets : AccountWalletWithSecretKey [ ] ;
70
- let completeAddresses : CompleteAddress [ ] ;
73
+ let recipientWallet : AccountWalletWithSecretKey ;
71
74
let tokenAddress : AztecAddress ;
72
75
let tokenAtWallet0 : TokenContract ;
76
+
73
77
beforeAll ( async ( ) => {
78
+ expect ( ROUNDS ) . toBeLessThanOrEqual ( MINT_AMOUNT ) ;
79
+
80
+ // My guess is that is never proven so if we are waiting for it to prove we wait forever?
74
81
pxe = await createCompatibleClient ( PXE_URL , logger ) ;
75
- const { accountKeys } = await addAccounts ( 3 , logger ) ( { pxe } ) ;
82
+ {
83
+ const { accountKeys } = await addAccounts ( 1 , logger , false ) ( { pxe } ) ;
84
+ const accountManagers = accountKeys . map ( ak => getSchnorrAccount ( pxe , ak [ 0 ] , ak [ 1 ] , 1 ) ) ;
85
+
86
+ const partialAddress = accountManagers [ 0 ] . getCompleteAddress ( ) . partialAddress ;
87
+ await pxe . registerAccount ( accountKeys [ 0 ] [ 0 ] , partialAddress ) ;
88
+ recipientWallet = await accountManagers [ 0 ] . getWallet ( ) ;
89
+ logger . verbose ( `Recipient Wallet address: ${ recipientWallet . getAddress ( ) } registered` ) ;
90
+ }
91
+
92
+ const { accountKeys } = await addAccounts ( Number ( WALLET_COUNT ) , logger , false ) ( { pxe } ) ;
76
93
const accountManagers = accountKeys . map ( ak => getSchnorrAccount ( pxe , ak [ 0 ] , ak [ 1 ] , 1 ) ) ;
77
- wallets = await Promise . all ( accountManagers . map ( a => a . getWallet ( ) ) ) ;
78
- completeAddresses = await pxe . getRegisteredAccounts ( ) ;
79
- wallets . forEach ( ( w , i ) => logger . verbose ( `Wallet ${ i } address: ${ w . getAddress ( ) } ` ) ) ;
80
- await publicDeployAccounts ( wallets [ 0 ] , completeAddresses . slice ( 0 , 2 ) ) ;
94
+
95
+ wallets = await Promise . all (
96
+ accountManagers . map ( async ( a , i ) => {
97
+ const partialAddress = a . getCompleteAddress ( ) . partialAddress ;
98
+ await pxe . registerAccount ( accountKeys [ i ] [ 0 ] , partialAddress ) ;
99
+ const wallet = await a . getWallet ( ) ;
100
+ logger . verbose ( `Wallet ${ i } address: ${ wallet . getAddress ( ) } registered` ) ;
101
+ return wallet ;
102
+ } ) ,
103
+ ) ;
81
104
82
105
logger . verbose ( `Deploying TokenContract...` ) ;
83
106
const tokenContract = await TokenContract . deploy (
84
107
wallets [ 0 ] ,
85
- completeAddresses [ 0 ] ,
108
+ wallets [ 0 ] . getAddress ( ) ,
86
109
TOKEN_NAME ,
87
110
TOKEN_SYMBOL ,
88
111
TOKEN_DECIMALS ,
89
112
)
90
113
. send ( )
91
- . deployed ( ) ;
114
+ . deployed ( { timeout : 600 } ) ;
92
115
93
116
tokenAddress = tokenContract . address ;
94
117
tokenAtWallet0 = await TokenContract . at ( tokenAddress , wallets [ 0 ] ) ;
95
118
96
- logger . verbose ( `Minting ${ MINT_AMOUNT } publicly...` ) ;
97
- await tokenAtWallet0 . methods . mint_public ( completeAddresses [ 0 ] . address , MINT_AMOUNT ) . send ( ) . wait ( ) ;
98
-
99
- logger . verbose ( `Minting ${ MINT_AMOUNT } privately...` ) ;
100
- const secret = Fr . random ( ) ;
101
- const secretHash = computeSecretHash ( secret ) ;
102
- const receipt = await tokenAtWallet0 . methods . mint_private ( MINT_AMOUNT , secretHash ) . send ( ) . wait ( ) ;
103
-
104
- await addPendingShieldNoteToPXE ( {
105
- amount : MINT_AMOUNT ,
106
- secretHash,
107
- txHash : receipt . txHash ,
108
- accountAddress : completeAddresses [ 0 ] . address ,
109
- assetAddress : tokenAddress ,
110
- wallet : wallets [ 0 ] ,
119
+ logger . verbose ( `Minting ${ MINT_AMOUNT } public assets to the ${ wallets . length } wallets...` ) ;
120
+
121
+ await Promise . all (
122
+ wallets . map ( w => tokenAtWallet0 . methods . mint_public ( w . getAddress ( ) , MINT_AMOUNT ) . send ( ) . wait ( { timeout : 600 } ) ) ,
123
+ ) ;
124
+
125
+ logger . verbose ( `Minting ${ MINT_AMOUNT } private assets to the ${ wallets . length } wallets...` ) ;
126
+ const secrets : Fr [ ] = wallets . map ( ( ) => Fr . random ( ) ) ;
127
+
128
+ const txs = await Promise . all (
129
+ wallets . map ( ( w , i ) =>
130
+ tokenAtWallet0 . methods . mint_private ( MINT_AMOUNT , computeSecretHash ( secrets [ i ] ) ) . send ( ) . wait ( { timeout : 600 } ) ,
131
+ ) ,
132
+ ) ;
133
+
134
+ wallets . forEach ( async ( wallet , i ) => {
135
+ await addPendingShieldNoteToPXE ( {
136
+ amount : MINT_AMOUNT ,
137
+ secretHash : computeSecretHash ( secrets [ i ] ) ,
138
+ txHash : txs [ i ] . txHash ,
139
+ accountAddress : wallet . getAddress ( ) ,
140
+ assetAddress : tokenAddress ,
141
+ wallet : wallet ,
142
+ } ) ;
111
143
} ) ;
112
- const txClaim = tokenAtWallet0 . methods . redeem_shield ( completeAddresses [ 0 ] . address , MINT_AMOUNT , secret ) . send ( ) ;
113
- await txClaim . wait ( { debug : true } ) ;
144
+
145
+ await Promise . all (
146
+ wallets . map ( async ( w , i ) =>
147
+ ( await TokenContract . at ( tokenAddress , w ) ) . methods
148
+ . redeem_shield ( w . getAddress ( ) , MINT_AMOUNT , secrets [ i ] )
149
+ . send ( )
150
+ . wait ( { timeout : 600 } ) ,
151
+ ) ,
152
+ ) ;
153
+
114
154
logger . verbose ( `Minting complete.` ) ;
115
155
} ) ;
116
156
@@ -119,34 +159,54 @@ describe('token transfer test', () => {
119
159
expect ( name ) . toBe ( TOKEN_NAME ) ;
120
160
} ) ;
121
161
122
- it ( 'can transfer 1 publicly' , async ( ) => {
162
+ it ( 'can transfer 1 token privately and publicly' , async ( ) => {
163
+ const recipient = recipientWallet . getAddress ( ) ;
123
164
const transferAmount = 1n ;
124
- const numTransfers = MINT_AMOUNT / transferAmount ;
125
- const initialBalance = await tokenAtWallet0 . methods . balance_of_public ( completeAddresses [ 0 ] . address ) . simulate ( ) ;
126
- expect ( initialBalance ) . toBeGreaterThanOrEqual ( transferAmount ) ;
127
- for ( let i = 1n ; i <= numTransfers ; i ++ ) {
128
- await tokenAtWallet0 . methods
129
- . transfer_public ( completeAddresses [ 0 ] . address , completeAddresses [ 1 ] . address , transferAmount , 0 )
130
- . send ( )
131
- . wait ( ) ;
132
- }
133
- const finalBalance0 = await tokenAtWallet0 . methods . balance_of_public ( completeAddresses [ 0 ] . address ) . simulate ( ) ;
134
- expect ( finalBalance0 ) . toBe ( 0n ) ;
135
- const finalBalance1 = await tokenAtWallet0 . methods . balance_of_public ( completeAddresses [ 1 ] . address ) . simulate ( ) ;
136
- expect ( finalBalance1 ) . toBe ( MINT_AMOUNT ) ;
137
- } ) ;
138
165
139
- it ( 'can transfer 1 privately' , async ( ) => {
140
- const transferAmount = 1n ;
141
- const numTransfers = MINT_AMOUNT / transferAmount ;
142
- const initialBalance = await tokenAtWallet0 . methods . balance_of_private ( completeAddresses [ 0 ] . address ) . simulate ( ) ;
143
- expect ( initialBalance ) . toBeGreaterThanOrEqual ( transferAmount ) ;
144
- for ( let i = 1n ; i <= numTransfers ; i ++ ) {
145
- await tokenAtWallet0 . methods . transfer ( completeAddresses [ 1 ] . address , transferAmount ) . send ( ) . wait ( ) ;
166
+ wallets . forEach ( async w => {
167
+ expect ( MINT_AMOUNT ) . toBe (
168
+ await ( await TokenContract . at ( tokenAddress , w ) ) . methods . balance_of_private ( w . getAddress ( ) ) . simulate ( ) ,
169
+ ) ;
170
+ expect ( MINT_AMOUNT ) . toBe ( await tokenAtWallet0 . methods . balance_of_public ( w . getAddress ( ) ) . simulate ( ) ) ;
171
+ } ) ;
172
+
173
+ expect ( 0n ) . toBe (
174
+ await ( await TokenContract . at ( tokenAddress , recipientWallet ) ) . methods . balance_of_private ( recipient ) . simulate ( ) ,
175
+ ) ;
176
+ expect ( 0n ) . toBe ( await tokenAtWallet0 . methods . balance_of_public ( recipient ) . simulate ( ) ) ;
177
+
178
+ // For each round, make both private and public transfers
179
+ for ( let i = 1n ; i <= ROUNDS ; i ++ ) {
180
+ const txs = await Promise . all ( [
181
+ ...wallets . map ( async w =>
182
+ ( await TokenContract . at ( tokenAddress , w ) ) . methods . transfer ( recipient , transferAmount ) ,
183
+ ) ,
184
+ ...wallets . map ( async w =>
185
+ (
186
+ await TokenContract . at ( tokenAddress , w )
187
+ ) . methods . transfer_public ( w . getAddress ( ) , recipient , transferAmount , 0 ) ,
188
+ ) ,
189
+ ] ) ;
190
+
191
+ txs . forEach ( async t => await t . prove ( ) ) ;
192
+
193
+ await Promise . all ( txs . map ( t => t . send ( ) . wait ( { timeout : 600 } ) ) ) ;
146
194
}
147
- const finalBalance0 = await tokenAtWallet0 . methods . balance_of_private ( completeAddresses [ 0 ] . address ) . simulate ( ) ;
148
- expect ( finalBalance0 ) . toBe ( 0n ) ;
149
- const finalBalance1 = await tokenAtWallet0 . methods . balance_of_private ( completeAddresses [ 1 ] . address ) . simulate ( ) ;
150
- expect ( finalBalance1 ) . toBe ( MINT_AMOUNT ) ;
195
+
196
+ wallets . forEach ( async w => {
197
+ expect ( MINT_AMOUNT - ROUNDS * transferAmount ) . toBe (
198
+ await ( await TokenContract . at ( tokenAddress , w ) ) . methods . balance_of_private ( w . getAddress ( ) ) . simulate ( ) ,
199
+ ) ;
200
+ expect ( MINT_AMOUNT - ROUNDS * transferAmount ) . toBe (
201
+ await tokenAtWallet0 . methods . balance_of_public ( w . getAddress ( ) ) . simulate ( ) ,
202
+ ) ;
203
+ } ) ;
204
+
205
+ expect ( ROUNDS * transferAmount * BigInt ( wallets . length ) ) . toBe (
206
+ await ( await TokenContract . at ( tokenAddress , recipientWallet ) ) . methods . balance_of_private ( recipient ) . simulate ( ) ,
207
+ ) ;
208
+ expect ( ROUNDS * transferAmount * BigInt ( wallets . length ) ) . toBe (
209
+ await tokenAtWallet0 . methods . balance_of_public ( recipient ) . simulate ( ) ,
210
+ ) ;
151
211
} ) ;
152
212
} ) ;
0 commit comments