This repository has been archived by the owner on Jan 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfidential-erc20.test.ts
174 lines (133 loc) · 6.8 KB
/
confidential-erc20.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import hre from "hardhat"
import { expect } from "chai"
import { setupAccounts } from "./util/onboard"
import { itUint, Wallet } from "@coti-io/coti-ethers"
export const deploymentInfo = { name: "My Confidential Token", symbol: "CTOK", decimals: 5, initialSupply: 500000000 } as const
const gasLimit = 12000000
async function deploy() {
const [owner, otherAccount] = await setupAccounts()
const tokenContract = await hre.ethers.getContractFactory("ERC20Example")
const { name, symbol, initialSupply } = deploymentInfo
const token = await tokenContract
.connect(owner as any)
.deploy(name, symbol, initialSupply, { gasLimit, from: owner.address })
const contract = await token.waitForDeployment()
return { contract, contractAddress: await contract.getAddress(), owner, otherAccount }
}
async function expectBalance(
token: Awaited<ReturnType<typeof deploy>>["contract"],
amount: number,
wallet: Wallet
) {
const ctBalance = await token.connect(wallet as any).balanceOf()
let balance = await wallet.decryptValue(ctBalance)
expect(balance).to.equal(amount)
}
async function expectAllowance(
contract: Awaited<ReturnType<typeof deploy>>["contract"],
amount: number,
owner: Wallet,
spenderAddress: string
) {
const ctAllowance = await contract.allowance(owner.address, spenderAddress)
let allowance = await owner.decryptValue(ctAllowance)
expect(allowance).to.equal(amount)
}
describe("Confidential ERC20", function () {
let deployment: Awaited<ReturnType<typeof deploy>>
before(async function () {
deployment = await deploy()
})
describe("Deployment", function () {
it("Deployed address should not be undefined", async function () {
const { contractAddress } = deployment
expect(contractAddress).to.not.equal(undefined)
})
it("Owner initial balance", async function () {
const { contract, owner } = deployment
const my_CTBalance = await contract.balanceOf()
let my_balance = await owner.decryptValue(my_CTBalance)
expect(my_balance).to.equal(deploymentInfo.initialSupply)
})
it("Function 'name' should be correct", async function () {
expect(await deployment.contract.name()).to.equal(deploymentInfo.name)
})
it("Function 'symbol' should be correct", async function () {
expect(await deployment.contract.symbol()).to.equal(deploymentInfo.symbol)
})
it("Function 'decimals' should be correct", async function () {
expect(await deployment.contract.decimals()).to.equal(deploymentInfo.decimals)
})
it("Function 'totalSupply' should be correct", async function () {
expect(await deployment.contract.totalSupply()).to.equal(deploymentInfo.initialSupply)
})
})
const transferAmount = 5
describe(`Transfer ${transferAmount}`, function () {
it("Transfer - clear", async function () {
const { contract, owner, otherAccount } = deployment
const initialBalance = Number(await owner.decryptValue(await deployment.contract.balanceOf()))
await (
await contract
.connect(owner as any)
["transfer(address,uint64,bool)"](otherAccount.address, transferAmount, true, { gasLimit })
).wait()
await expectBalance(contract, initialBalance - transferAmount, owner)
await (
await contract
.connect(owner as any)
["transfer(address,uint64,bool)"](otherAccount.address, transferAmount, true, { gasLimit })
).wait()
await expectBalance(contract, initialBalance - 2 * transferAmount, owner)
})
it("Transfer - Confidential", async function () {
const { contract, contractAddress, owner, otherAccount } = deployment
const initialBalance = Number(await owner.decryptValue(await deployment.contract.balanceOf()))
const func = contract.connect(owner as any)["transfer(address,uint256,bytes,bool)"]
const selector = func.fragment.selector
const { ciphertext, signature } = await owner.encryptValue(BigInt(transferAmount), contractAddress, selector) as itUint
await (await func(otherAccount.address, ciphertext, signature, false, { gasLimit })).wait()
await expectBalance(contract, initialBalance - transferAmount, owner)
})
it("TransferFrom - clear without giving allowance should fail", async function () {
const { contract, owner, otherAccount } = deployment
const initialBalance = Number(await owner.decryptValue(await deployment.contract.balanceOf()))
await (await contract.connect(owner as any).approveClear(otherAccount.address, 0, { gasLimit })).wait()
const func = contract.connect(owner as any)["transferFrom(address,address,uint64,bool)"]
await (await func(owner.address, otherAccount.address, transferAmount, true, { gasLimit })).wait()
await expectBalance(contract, initialBalance, owner)
})
it("TransferFrom - clear", async function () {
const { contract, owner, otherAccount } = deployment
await (
await contract.connect(owner as any).approveClear(otherAccount.address, transferAmount, { gasLimit })
).wait()
const func = contract.connect(owner as any)["transferFrom(address,address,uint64,bool)"]
await (await func(owner.address, otherAccount.address, transferAmount, true, { gasLimit })).wait()
})
it("TransferFrom - Confidential", async function () {
const { contract, contractAddress, owner, otherAccount } = deployment
const initialBalance = Number(await owner.decryptValue(await deployment.contract.balanceOf()))
await (
await contract.connect(owner as any).approveClear(otherAccount.address, transferAmount, { gasLimit })
).wait()
const func = contract.connect(owner as any)["transferFrom(address,address,uint256,bytes,bool)"]
const selector = func.fragment.selector
let { ciphertext, signature } = await owner.encryptValue(BigInt(transferAmount), contractAddress, selector) as itUint
await (
await func(owner.address, otherAccount.address, ciphertext, signature, false, { gasLimit })
).wait()
await expectBalance(contract, initialBalance - transferAmount, owner)
})
it("Approve/Allowance - Confidential", async function () {
const { contract, contractAddress, owner, otherAccount } = deployment
await (await contract.connect(owner as any).approveClear(otherAccount.address, 0, { gasLimit })).wait()
await expectAllowance(contract, 0, owner, otherAccount.address)
const func = contract.connect(owner as any)["approve(address,uint256,bytes)"]
const selector = func.fragment.selector
const { ciphertext, signature } = await owner.encryptValue(BigInt(transferAmount), contractAddress, selector) as itUint
await (await func(otherAccount.address, ciphertext, signature, { gasLimit })).wait()
await expectAllowance(contract, transferAmount, owner, otherAccount.address)
})
})
})