1- import { Provider , Wallet } from "ethers"
2- import { buildInputText , type ConfidentialAccount , decryptUint } from "@coti-io/coti-sdk-typescript"
1+ import { itUint , Provider , Wallet } from "@coti-io/coti-ethers"
32import { getContract } from "../util/contracts"
43import { assert } from "../util/assert"
54
65const gasLimit = 12000000
76
8- async function assertBalance ( token : ReturnType < typeof getTokenContract > , amount : number , user : ConfidentialAccount ) {
7+ async function assertBalance ( token : ReturnType < typeof getTokenContract > , amount : number , user : Wallet ) {
98 const ctBalance = await token . balanceOf ( )
10- let balance = Number ( decryptUint ( ctBalance , user . userKey ) )
9+ let balance = Number ( await user . decryptValue ( ctBalance ) )
1110 assert ( balance === amount , `Expected balance to be ${ amount } , but got ${ balance } ` )
1211 return balance
1312}
1413
1514async function assertAllowance (
1615 token : ReturnType < typeof getTokenContract > ,
1716 amount : number ,
18- owner : ConfidentialAccount ,
17+ owner : Wallet ,
1918 spenderAddress : string
2019) {
21- const ctAllowance = await token . allowance ( owner . wallet . address , spenderAddress )
22- let allowance = Number ( decryptUint ( ctAllowance , owner . userKey ) )
20+ const ctAllowance = await token . allowance ( owner . address , spenderAddress )
21+ let allowance = Number ( await owner . decryptValue ( ctAllowance ) )
2322 assert ( allowance === amount , `Expected allowance to be ${ amount } , but got ${ allowance } ` )
2423}
2524
26- function getTokenContract ( user : ConfidentialAccount ) {
27- return getContract ( "ERC20Example" , user . wallet )
25+ function getTokenContract ( user : Wallet ) {
26+ return getContract ( "ERC20Example" , user )
2827}
2928
30- export async function erc20Example ( provider : Provider , user : ConfidentialAccount ) {
29+ export async function erc20Example ( provider : Provider , user : Wallet ) {
3130 const token = getTokenContract ( user )
3231 const otherWallet = new Wallet ( Wallet . createRandom ( provider ) . privateKey )
3332
3433 const transferAmount = 5
3534
36- let balance = Number ( decryptUint ( await token . balanceOf ( ) , user . userKey ) )
35+ let balance = Number ( await user . decryptValue ( await token . balanceOf ( ) ) )
3736 if ( balance === 0 ) {
3837 await ( await token . setBalance ( 100000000 , { gasLimit} ) ) . wait ( )
3938 balance = await assertBalance ( token , 100000000 , user )
@@ -53,7 +52,7 @@ export async function erc20Example(provider: Provider, user: ConfidentialAccount
5352async function clearTransfer (
5453 token : ReturnType < typeof getTokenContract > ,
5554 initlalBalance : number ,
56- owner : ConfidentialAccount ,
55+ owner : Wallet ,
5756 alice : Wallet ,
5857 transferAmount : number
5958) {
@@ -71,15 +70,15 @@ async function clearTransfer(
7170async function confidentialTransfer (
7271 token : ReturnType < typeof getTokenContract > ,
7372 initlalBalance : number ,
74- owner : ConfidentialAccount ,
73+ owner : Wallet ,
7574 alice : Wallet ,
7675 transferAmount : number
7776) {
7877 console . log ( "************* Transfer confidential " , transferAmount , " from my account to Alice *************" )
7978
8079 const func = token [ "transfer(address,uint256,bytes,bool)" ]
8180 const selector = func . fragment . selector
82- const { ciphertext, signature} = await buildInputText ( BigInt ( transferAmount ) , owner , await token . getAddress ( ) , selector )
81+ const { ciphertext, signature} = await owner . encryptValue ( BigInt ( transferAmount ) , await token . getAddress ( ) , selector ) as itUint
8382
8483 await ( await func ( alice . address , ciphertext , signature , false , { gasLimit} ) ) . wait ( )
8584 return await assertBalance ( token , initlalBalance - transferAmount , owner )
@@ -88,7 +87,7 @@ async function confidentialTransfer(
8887async function clearTransferFromWithoutAllowance (
8988 token : ReturnType < typeof getTokenContract > ,
9089 initlalBalance : number ,
91- owner : ConfidentialAccount ,
90+ owner : Wallet ,
9291 alice : Wallet ,
9392 transferAmount : number
9493) {
@@ -101,14 +100,14 @@ async function clearTransferFromWithoutAllowance(
101100 await ( await token . approveClear ( alice . address , 0 , { gasLimit} ) ) . wait ( )
102101
103102 const func = token [ "transferFrom(address,address,uint64,bool)" ]
104- await ( await func ( owner . wallet . address , alice . address , transferAmount , true , { gasLimit} ) ) . wait ( )
103+ await ( await func ( owner . address , alice . address , transferAmount , true , { gasLimit} ) ) . wait ( )
105104
106105 return await assertBalance ( token , initlalBalance , owner )
107106}
108107
109108async function clearApprove (
110109 token : ReturnType < typeof getTokenContract > ,
111- owner : ConfidentialAccount ,
110+ owner : Wallet ,
112111 alice : Wallet ,
113112 approveAmount : number
114113) {
@@ -119,7 +118,7 @@ async function clearApprove(
119118
120119async function confidentialApprove (
121120 token : ReturnType < typeof getTokenContract > ,
122- owner : ConfidentialAccount ,
121+ owner : Wallet ,
123122 alice : Wallet ,
124123 approveAmount : number
125124) {
@@ -129,7 +128,7 @@ async function confidentialApprove(
129128
130129 const func = token [ "approve(address,uint256,bytes)" ]
131130 const selector = func . fragment . selector
132- const { ciphertext, signature} = await buildInputText ( BigInt ( approveAmount ) , owner , await token . getAddress ( ) , selector )
131+ const { ciphertext, signature} = await await owner . encryptValue ( BigInt ( approveAmount ) , await token . getAddress ( ) , selector ) as itUint
133132 await ( await func ( alice . address , ciphertext , signature , { gasLimit} ) ) . wait ( )
134133
135134 await assertAllowance ( token , approveAmount , owner , alice . address )
@@ -138,31 +137,31 @@ async function confidentialApprove(
138137async function clearTransferFrom (
139138 token : ReturnType < typeof getTokenContract > ,
140139 initlalBalance : number ,
141- owner : ConfidentialAccount ,
140+ owner : Wallet ,
142141 alice : Wallet ,
143142 transferAmount : number
144143) {
145144 console . log ( "************* TransferFrom clear " , transferAmount , " from my account to Alice *************" )
146145
147146 const func = token [ "transferFrom(address,address,uint64,bool)" ]
148- await ( await func ( owner . wallet . address , alice . address , transferAmount , true , { gasLimit} ) ) . wait ( )
147+ await ( await func ( owner . address , alice . address , transferAmount , true , { gasLimit} ) ) . wait ( )
149148
150149 return await assertBalance ( token , initlalBalance - transferAmount , owner )
151150}
152151
153152async function confidentialTransferFrom (
154153 token : ReturnType < typeof getTokenContract > ,
155154 initlalBalance : number ,
156- owner : ConfidentialAccount ,
155+ owner : Wallet ,
157156 alice : Wallet ,
158157 transferAmount : number
159158) {
160159 console . log ( "************* TransferFrom confidential " , transferAmount , " from my account to Alice *************" )
161160
162161 const func = token [ "transferFrom(address,address,uint256,bytes,bool)" ]
163162 const selector = func . fragment . selector
164- let { ciphertext, signature} = await buildInputText ( BigInt ( transferAmount ) , owner , await token . getAddress ( ) , selector )
165- await ( await func ( owner . wallet . address , alice . address , ciphertext , signature , false , { gasLimit} ) ) . wait ( )
163+ let { ciphertext, signature} = await owner . encryptValue ( BigInt ( transferAmount ) , await token . getAddress ( ) , selector ) as itUint
164+ await ( await func ( owner . address , alice . address , ciphertext , signature , false , { gasLimit} ) ) . wait ( )
166165
167166 return await assertBalance ( token , initlalBalance - transferAmount , owner )
168167}
0 commit comments