-
Notifications
You must be signed in to change notification settings - Fork 3
/
confidential-erc20.test.ts
173 lines (134 loc) · 7.02 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
import hre from "hardhat"
import { expect } from "chai"
import { type ConfidentialAccount, decryptValue, prepareIT } from "@coti-io/coti-sdk-typescript"
import { setupAccounts } from "./util/onboard"
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.wallet)
.deploy(name, symbol, initialSupply, { gasLimit, from: owner.wallet.address })
const contract = await token.waitForDeployment()
// const contract = await hre.ethers.getContractAt("ERC20Example", "0x19c0bb1bf8b923855598405ab9cc88c4a8aa9540")
return { contract, contractAddress: await contract.getAddress(), owner, otherAccount }
}
async function expectBalance(
token: Awaited<ReturnType<typeof deploy>>["contract"],
amount: number,
user: ConfidentialAccount
) {
const ctBalance = await token.connect(user.wallet).balanceOf()
let balance = decryptValue(ctBalance, user.userKey)
expect(balance).to.equal(amount)
}
async function expectAllowance(
contract: Awaited<ReturnType<typeof deploy>>["contract"],
amount: number,
owner: ConfidentialAccount,
spenderAddress: string
) {
const ctAllowance = await contract.allowance(owner.wallet.address, spenderAddress)
let allowance = decryptValue(ctAllowance, owner.userKey)
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 = decryptValue(my_CTBalance, owner.userKey)
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 = decryptValue(await deployment.contract.balanceOf(), owner.userKey)
await (
await contract
.connect(owner.wallet)
["transfer(address,uint64,bool)"](otherAccount.wallet.address, transferAmount, true, { gasLimit })
).wait()
await expectBalance(contract, initialBalance - transferAmount, owner)
await (
await contract
.connect(owner.wallet)
["transfer(address,uint64,bool)"](otherAccount.wallet.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 = decryptValue(await deployment.contract.balanceOf(), owner.userKey)
const func = contract.connect(owner.wallet)["transfer(address,uint256,bytes,bool)"]
const selector = func.fragment.selector
const { ctInt, signature } = await prepareIT(BigInt(transferAmount), owner, contractAddress, selector)
await (await func(otherAccount.wallet.address, ctInt, 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 = decryptValue(await deployment.contract.balanceOf(), owner.userKey)
await (await contract.connect(owner.wallet).approveClear(otherAccount.wallet.address, 0, { gasLimit })).wait()
const func = contract.connect(owner.wallet)["transferFrom(address,address,uint64,bool)"]
await (await func(owner.wallet.address, otherAccount.wallet.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.wallet).approveClear(otherAccount.wallet.address, transferAmount, { gasLimit })
).wait()
const func = contract.connect(owner.wallet)["transferFrom(address,address,uint64,bool)"]
await (await func(owner.wallet.address, otherAccount.wallet.address, transferAmount, true, { gasLimit })).wait()
})
it("TransferFrom - Confidential", async function () {
const { contract, contractAddress, owner, otherAccount } = deployment
const initialBalance = decryptValue(await deployment.contract.balanceOf(), owner.userKey)
await (
await contract.connect(owner.wallet).approveClear(otherAccount.wallet.address, transferAmount, { gasLimit })
).wait()
const func = contract.connect(owner.wallet)["transferFrom(address,address,uint256,bytes,bool)"]
const selector = func.fragment.selector
let { ctInt, signature } = await prepareIT(BigInt(transferAmount), owner, contractAddress, selector)
await (
await func(owner.wallet.address, otherAccount.wallet.address, ctInt, 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.wallet).approveClear(otherAccount.wallet.address, 0, { gasLimit })).wait()
await expectAllowance(contract, 0, owner, otherAccount.wallet.address)
const func = contract.connect(owner.wallet)["approve(address,uint256,bytes)"]
const selector = func.fragment.selector
const { ctInt, signature } = await prepareIT(BigInt(transferAmount), owner, contractAddress, selector)
await (await func(otherAccount.wallet.address, ctInt, signature, { gasLimit })).wait()
await expectAllowance(contract, transferAmount, owner, otherAccount.wallet.address)
})
})
})