punkkit-sdk 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/.env +47 -0
  2. package/.gitmodules +3 -0
  3. package/README.md +158 -0
  4. package/config/auction.config.ts +40 -0
  5. package/config/env.config.ts +204 -0
  6. package/config/uniswap.config.ts +107 -0
  7. package/config/voucher.config.ts +10 -0
  8. package/contracts/MutiVoucher.sol +78 -0
  9. package/contracts/auction/ChainXAuction.sol +177 -0
  10. package/contracts/auction/ChainXAuctionV2.sol +672 -0
  11. package/contracts/auction/ChainXWrappedETH.sol +80 -0
  12. package/contracts/auction/ChainYLiquidityManager.sol +57 -0
  13. package/contracts/auction/ChainYShadowETH.sol +148 -0
  14. package/contracts/auction/ChainYVault.sol +195 -0
  15. package/contracts/auction/ChainYVaultCoinbase.sol +276 -0
  16. package/contracts/auction/ChainYVaultV2.sol +318 -0
  17. package/contracts/auction/coinbase-and-stake/README.md +55 -0
  18. package/contracts/auction/coinbase-and-stake/coinbase.sol +142 -0
  19. package/contracts/auction/coinbase-and-stake/invokeCoinbase.sol +159 -0
  20. package/contracts/auction/coinbase-and-stake/invokeStake.sol +82 -0
  21. package/contracts/auction/coinbase-and-stake/stake.sol +92 -0
  22. package/contracts/auction/interfaces/IUniswapV2Factory.sol +15 -0
  23. package/contracts/auction/interfaces/IUniswapV2Pair.sol +53 -0
  24. package/contracts/auction/interfaces/IUniswapV2Router02.sol +25 -0
  25. package/contracts/auction/interfaces/IUnlockStrategy.sol +18 -0
  26. package/contracts/auction/libraries/EventParser.sol +32 -0
  27. package/contracts/auction/libraries/TransactionParser.sol +70 -0
  28. package/contracts/auction/strategies/MatchResultWithdrawnStrategy.sol +33 -0
  29. package/contracts/auction/utils/BytesLib.sol +180 -0
  30. package/contracts/auction/utils/RLPReader.sol +355 -0
  31. package/contracts/uniswap/Create2.sol +80 -0
  32. package/contracts/uniswap/DynamicFee.sol +100 -0
  33. package/contracts/uniswap/Example.sol +35 -0
  34. package/contracts/uniswap/HookMiner.sol +52 -0
  35. package/contracts/uniswap/LimitOrder.sol +486 -0
  36. package/contracts/uniswap/LiquidPool.sol +179 -0
  37. package/contracts/uniswap/MockERC20.sol +20 -0
  38. package/hardhat.config.ts +35 -0
  39. package/ignition/modules/LimitOrder.ts +33 -0
  40. package/package.json +32 -0
  41. package/scripts/auction/deploy.ts +23 -0
  42. package/scripts/auction/deployCoinbase.ts +21 -0
  43. package/scripts/auction/deployXAuction.ts +23 -0
  44. package/scripts/auction/deployYVault.ts +22 -0
  45. package/scripts/deploy_voucher.ts +20 -0
  46. package/scripts/uniswap/deploy/deploy.ts +65 -0
  47. package/scripts/uniswap/deploy/deploy_create2.ts +11 -0
  48. package/scripts/uniswap/deploy/deploy_example.ts +35 -0
  49. package/scripts/uniswap/deploy/deploy_hooks.ts +74 -0
  50. package/scripts/uniswap/deploy/deploy_mockERC20.ts +42 -0
  51. package/scripts/uniswap/deploy/help.ts +96 -0
  52. package/scripts/uniswap/deploy/init.ts +70 -0
  53. package/src/auction/chainXAuction.ts +209 -0
  54. package/src/auction/chainYVault.ts +153 -0
  55. package/src/auction/event.ts +19 -0
  56. package/src/auction/serialize.ts +162 -0
  57. package/src/auction/type.ts +71 -0
  58. package/src/lib/signer.ts +20 -0
  59. package/src/lib/unlock.ts +14 -0
  60. package/src/uniswap/1-marketprice/addLiquidity.ts +80 -0
  61. package/src/uniswap/1-marketprice/removeLiquidity.ts +63 -0
  62. package/src/uniswap/1-marketprice/swap.ts +100 -0
  63. package/src/uniswap/2-limitorder/kill.ts +70 -0
  64. package/src/uniswap/2-limitorder/place.ts +93 -0
  65. package/src/uniswap/2-limitorder/withdraw.ts +78 -0
  66. package/src/uniswap/3-dynamicfee/dynamicfee.ts +321 -0
  67. package/src/uniswap/lib/ERC20.ts +49 -0
  68. package/src/uniswap/lib/contract.ts +18 -0
  69. package/src/uniswap/lib/limitOrder.ts +40 -0
  70. package/src/uniswap/lib/liqCalculation.ts +152 -0
  71. package/src/uniswap/lib/listen.ts +57 -0
  72. package/src/uniswap/lib/pool.ts +62 -0
  73. package/src/uniswap/lib/swap.ts +8 -0
  74. package/src/uniswap/lib/types.ts +21 -0
  75. package/src/uniswap/lib/utils.ts +26 -0
  76. package/src/uniswap/playgroud/abiencode.ts +21 -0
  77. package/src/uniswap/playgroud/amount0.ts +47 -0
  78. package/src/uniswap/playgroud/errordecode.ts +54 -0
  79. package/src/uniswap/playgroud/errorsigs.ts +86 -0
  80. package/src/voucher.ts +122 -0
  81. package/test/auction/ChainXAuctionV2.test.ts +265 -0
  82. package/test/auction/ChainYVaultV2.test.js +163 -0
  83. package/test/auction/ChainYVaultV2.test.ts +183 -0
  84. package/test/auction/auction.test.ts +106 -0
  85. package/test/connect_punk.test.ts +26 -0
  86. package/test/create2.test.ts +44 -0
  87. package/test/normal.ts +43 -0
  88. package/test/test-config.ts +18 -0
  89. package/test/uniswap/example.test.ts +62 -0
  90. package/test/uniswap/limitOrder.test.ts +184 -0
  91. package/test/uniswap/mockERC20.test.ts +142 -0
  92. package/test/voucher_hardhat.test.ts +120 -0
  93. package/test/voucher_punk.test.ts +83 -0
  94. package/tsconfig.json +11 -0
@@ -0,0 +1,120 @@
1
+ import { expect } from "chai";
2
+ import { ethers } from "hardhat";
3
+ import type { Contract } from "ethers";
4
+
5
+ describe("MutiVoucher", function () {
6
+ let mutiVoucher: Contract;
7
+ let conversionRate = BigInt(1); // conversion rate for the voucher
8
+ let addr1: any;
9
+ let addr2: any;
10
+ let voucherNameString="BitCoin";
11
+ let voucherNameBytes32=ethers.utils.formatBytes32String(voucherNameString);
12
+
13
+ // before关键字只会执行一次,beforeEach会执行多次
14
+ beforeEach(async function () {
15
+ const MutiVoucher = await ethers.getContractFactory("MutiVoucher");
16
+ mutiVoucher = await MutiVoucher.deploy();
17
+ [addr1, addr2] = await ethers.getSigners();
18
+ await mutiVoucher.deployed();
19
+ console.log("voucher address:",mutiVoucher.address);
20
+ await mutiVoucher.createVoucher(voucherNameBytes32, conversionRate);
21
+ });
22
+
23
+
24
+ it("get info of voucher", async function () {
25
+ // 查询代金券的汇率信息
26
+ const rate = await mutiVoucher.getVoucherInfo(voucherNameBytes32);
27
+ console.log("rate:", rate);
28
+ expect(rate).to.equal(conversionRate);
29
+ });
30
+
31
+ it("Should allow user to buy voucher", async function () {
32
+ // 用户 addr1 使用 ETH 购买代金券
33
+ const buyAmount = ethers.utils.parseEther("1.0"); // 1 ETH
34
+ await mutiVoucher.connect(addr1).buy(voucherNameBytes32, { value: buyAmount });
35
+
36
+ // 检查用户余额是否正确增加
37
+ const balance = await mutiVoucher.balanceOf(voucherNameBytes32, addr1.address);
38
+
39
+ console.log("balance:", balance)
40
+
41
+ });
42
+
43
+ it("Should not allow user to buy voucher with zero ETH", async function () {
44
+ await expect(
45
+ mutiVoucher.connect(addr1).buy(voucherNameBytes32, { value: 0 })
46
+ ).to.be.revertedWith("Message value must be greater than zero");
47
+ });
48
+
49
+ it("Should allow user to use voucher", async function () {
50
+ // 用户 addr1 使用 ETH 购买代金券
51
+ const buyAmount = ethers.utils.parseEther("1.0"); // 1 ETH
52
+ await mutiVoucher.connect(addr1).buy(voucherNameBytes32, { value: buyAmount });
53
+ // 使用代金券
54
+ const useAmount = buyAmount.mul(conversionRate);
55
+ await mutiVoucher.connect(addr1).use(voucherNameBytes32, useAmount);
56
+ // 检查用户余额
57
+ const balance = await mutiVoucher.balanceOf(voucherNameBytes32, addr1.address);
58
+ expect(balance).to.equal(0);
59
+ });
60
+
61
+ it("Should not allow user to use more vouchers than they have", async function () {
62
+ // 用户 addr1 使用 ETH 购买代金券
63
+ const buyAmount = ethers.utils.parseEther("1.0"); // 1 ETH
64
+ await mutiVoucher.connect(addr1).buy(voucherNameBytes32, { value: buyAmount });
65
+ // 用户试图使用超过拥有的代金券,应当失败
66
+ const excessiveAmount = buyAmount.mul(conversionRate).add(1);
67
+ await expect(
68
+ mutiVoucher.connect(addr1).use(voucherNameBytes32, excessiveAmount)
69
+ ).to.be.revertedWith("Insufficient balance");
70
+ });
71
+
72
+ it("Should not create same voucher twice", async function () {
73
+ await expect(
74
+ mutiVoucher.createVoucher(voucherNameBytes32, 200)
75
+ ).to.be.revertedWith("Voucher already exist");
76
+ });
77
+
78
+ it("Should get all voucher", async function () {
79
+ await mutiVoucher.createVoucher(ethers.utils.formatBytes32String("V1"), 100);
80
+ await mutiVoucher.createVoucher(ethers.utils.formatBytes32String("V2"), 200);
81
+ await mutiVoucher.createVoucher(ethers.utils.formatBytes32String("V3"), 300);
82
+
83
+ const vouchersBytes = await mutiVoucher.getAllVouchers();
84
+ console.log("vouchersBytes:", vouchersBytes);
85
+ // 从Bytes32转化为string类型
86
+ const vouchersStrings :string[]=vouchersBytes.map((v:any)=>{
87
+ try {
88
+ return ethers.utils.parseBytes32String(v);
89
+ } catch (err) {
90
+ console.error("解析失败:", v, err);
91
+ return ""; // 或者其他默认值
92
+ }
93
+ });
94
+
95
+
96
+ console.log("vouchersStrings:", vouchersStrings);
97
+
98
+ expect(vouchersStrings).to.have.members([voucherNameString, "V1", "V2", "V3"]);
99
+ expect(vouchersStrings.length).to.equal(4);
100
+ });
101
+
102
+ it("Should return correct balance for multiple users", async function () {
103
+ // 用户 addr1 和 addr2 使用 ETH 购买代金券
104
+ const buyAmount1 = ethers.utils.parseEther("1.0"); // 1 ETH
105
+ const buyAmount2 = ethers.utils.parseEther("0.5"); // 0.5 ETH
106
+
107
+ await mutiVoucher.connect(addr1).buy(voucherNameBytes32, { value: buyAmount1 });
108
+ await mutiVoucher.connect(addr2).buy(voucherNameBytes32, { value: buyAmount2 });
109
+
110
+ // 检查用户的余额
111
+ const balance1 = await mutiVoucher.balanceOf(voucherNameBytes32, addr1.address);
112
+ const balance2 = await mutiVoucher.balanceOf(voucherNameBytes32, addr2.address);
113
+
114
+ const expectedAmount1 = buyAmount1.mul(conversionRate);
115
+ const expectedAmount2 = buyAmount2.mul(conversionRate);
116
+
117
+ expect(balance1).to.equal(expectedAmount1);
118
+ expect(balance2).to.equal(expectedAmount2);
119
+ });
120
+ });
@@ -0,0 +1,83 @@
1
+ import { ethers } from "hardhat";
2
+ import { Contract, Wallet } from "ethers";
3
+ import { MUTI_VOUCHER_ADDR } from "../config/voucher.config";
4
+ import { BuildUseVoucherTx } from "../src/voucher";
5
+ import { RPC_URL, PRIVATE_KEY, ACCOUNT_ADDR } from "../config/env.config";
6
+ import { getSigner } from "../src/lib/signer";
7
+ import { expect } from "chai";
8
+
9
+ describe("punk链测试", async function () {
10
+ let mutiVoucher: Contract;
11
+ let conversionRate = BigInt(1); // conversion rate for the voucher
12
+ let account1: Wallet;
13
+ let account1Address:string;
14
+ let voucherName: string;
15
+ let voucherBytes32:string;
16
+
17
+ before(async function () {
18
+ // 直接连接到已部署的合约
19
+ const MutiVoucher = await ethers.getContractFactory("MutiVoucher");
20
+ mutiVoucher = await MutiVoucher.attach(MUTI_VOUCHER_ADDR);
21
+ account1 = getSigner(RPC_URL, PRIVATE_KEY);
22
+ account1Address = ACCOUNT_ADDR;
23
+
24
+ voucherName = "ETH";
25
+ voucherBytes32=ethers.utils.formatBytes32String(voucherName);
26
+ // await mutiVoucher.createVoucher(voucherBytes32, conversionRate);
27
+ });
28
+ it("should show all voucher",async function(){
29
+
30
+ const vouchersBytes = await mutiVoucher.getAllVouchers();
31
+ console.log("vouchersBytes:", vouchersBytes);
32
+ // 从Bytes32转化为string类型
33
+ const vouchersStrings :string[]=vouchersBytes.map((v:any)=>{
34
+ try {
35
+ return ethers.utils.parseBytes32String(v);
36
+ } catch (err) {
37
+ console.error("解析失败:", v, err);
38
+ return ""; // 或者其他默认值
39
+ }
40
+ });
41
+
42
+
43
+ console.log("vouchersStrings:", vouchersStrings);
44
+ });
45
+
46
+ it("Should allow user to buy voucher", async function () {
47
+ // 用户 addr1 使用 ETH 购买代金券
48
+ const buyAmount = ethers.utils.parseEther("1.0"); // 1 ETH
49
+ await mutiVoucher.connect(account1).buy(voucherBytes32, { value: buyAmount });
50
+ // 检查用户余额是否正确增加
51
+ const balance = await mutiVoucher.balanceOf(voucherBytes32, account1Address);
52
+ console.log("balance:", balance)
53
+
54
+ });
55
+
56
+ it("Should allow user to use voucher", async function () {
57
+ // 用户 addr1 使用 ETH 购买代金券
58
+ const buyAmount = ethers.utils.parseEther("1.0"); // 1 ETH
59
+ await mutiVoucher.connect(account1).buy(voucherBytes32, { value: buyAmount });
60
+
61
+ const useAmount = buyAmount.mul(conversionRate);
62
+ // populateTransaction 为可发送的交易请求对象
63
+ // const txRequest = await mutiVoucher.populateTransaction.use(
64
+ // voucherName,
65
+ // useAmount
66
+ // );
67
+
68
+ // 普通转账类型
69
+ const txRequest = {
70
+ to: account1Address,
71
+ value: ethers.utils.parseEther("0.1"),
72
+ data: "0x", // 普通转账可省略
73
+ };
74
+
75
+ const customTx = BuildUseVoucherTx(txRequest, voucherBytes32);
76
+ const Oldbalance = await mutiVoucher.balanceOf(voucherBytes32, account1Address);
77
+ const sent = await account1.sendTransaction(customTx);
78
+ await sent.wait();
79
+ const balance = await mutiVoucher.balanceOf(voucherBytes32, account1Address);
80
+ console.log("Oldbalance:", Oldbalance);
81
+ console.log("balance:", balance)
82
+ });
83
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es2020",
4
+ "module": "commonjs",
5
+ "esModuleInterop": true,
6
+ "forceConsistentCasingInFileNames": true,
7
+ "strict": true,
8
+ "skipLibCheck": true,
9
+ "resolveJsonModule": true
10
+ }
11
+ }