Skip to content

Commit 9ab5248

Browse files
committed
🐞 fix: bytes32 not enough
1 parent b9e104c commit 9ab5248

9 files changed

+51
-20
lines changed

contracts/Campaign.sol

+8-8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ contract Campaign is ICampaign, Ownable, ERC721 {
2323
uint256 public immutable requiredAmount;
2424
Consts.CampaignStatus public status;
2525

26-
bytes32 public campaignUri;
26+
string public campaignUri;
2727
uint256 public immutable startTime;
2828
uint256 public immutable totalEpochsCount;
2929
uint256 public immutable period;
@@ -60,7 +60,7 @@ contract Campaign is ICampaign, Ownable, ERC721 {
6060
}
6161

6262
struct Record {
63-
bytes32 contentUri;
63+
string contentUri;
6464
}
6565

6666
constructor(
@@ -71,7 +71,7 @@ contract Campaign is ICampaign, Ownable, ERC721 {
7171
uint256 startTime_,
7272
uint256 totalPeriod_,
7373
uint256 periodLength_,
74-
bytes32 campaignUri_
74+
string memory campaignUri_
7575
) ERC721(name_, symbol_) {
7676
require(address(token_) != address(0), 'Campaign: invalid token');
7777
require(amount_ != 0, 'Campaign: invalid amount');
@@ -84,7 +84,7 @@ contract Campaign is ICampaign, Ownable, ERC721 {
8484
campaignUri = campaignUri_;
8585
}
8686

87-
function setCampaignUri(bytes32 newUri) external override onlyOwner {
87+
function setCampaignUri(string calldata newUri) external override onlyOwner {
8888
campaignUri = newUri;
8989
emit EvCampaignUriSet(campaignUri);
9090
}
@@ -156,8 +156,8 @@ contract Campaign is ICampaign, Ownable, ERC721 {
156156
successTokensCount = _idx;
157157
for (uint256 tokenId = 0; tokenId < _idx; tokenId++) {
158158
for (uint256 j = 0; j < totalEpochsCount; j++) {
159-
bytes32 content = records[j][tokenId].contentUri;
160-
if (content == bytes32(0)) {
159+
string memory content = records[j][tokenId].contentUri;
160+
if (bytes(content).length == 0) {
161161
uint256 penalty = properties[tokenId].pendingReward;
162162
hostReward += (penalty * Consts.HOST_REWARD) / Consts.DECIMAL;
163163
protocolFee += (penalty * Consts.PROTOCOL_FEE) / Consts.DECIMAL;
@@ -182,9 +182,9 @@ contract Campaign is ICampaign, Ownable, ERC721 {
182182

183183
/**
184184
* @dev user check in
185-
* @param contentUri bytes32 of ipfs uri or other decentralize storage
185+
* @param contentUri string of ipfs uri or other decentralize storage
186186
*/
187-
function checkIn(bytes32 contentUri, uint256 tokenId)
187+
function checkIn(string calldata contentUri, uint256 tokenId)
188188
external
189189
override
190190
onlyTokenHolder(tokenId)

contracts/CampaignFactoryUpgradable.sol

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ contract CampaignFactoryUpgradable is ICampaignFactory, UUPSUpgradeable, Ownable
4343
uint256 startTime,
4444
uint256 totalPeriod,
4545
uint256 periodLength,
46-
bytes32 campaignUri
46+
string calldata campaignUri
4747
) public override onlyWhiteUser onlyWhiteToken(token) {
4848
require(amount <= whiteTokens[token], 'CampaignF: amount exceed cap');
4949
require(block.timestamp + 600 < startTime, 'CampaignF: start too soon');

contracts/interface/ICampaign.sol

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pragma solidity 0.8.15;
33

44
interface ICampaign {
55
// owner update content uri
6-
function setCampaignUri(bytes32 newUri) external;
6+
function setCampaignUri(string calldata newUri) external;
77

88
// user sign up the campaign
99
function signUp() external;
@@ -12,21 +12,21 @@ interface ICampaign {
1212
function admit(uint256[] calldata) external;
1313

1414
// user check at a fixed frequency
15-
function checkIn(bytes32, uint256) external;
15+
function checkIn(string calldata, uint256) external;
1616

1717
// settle the reward
1818
function settle() external;
1919

2020
// user claim reward after campaign ended
2121
function claim(uint256 tokenId) external;
2222

23-
event EvCampaignUriSet(bytes32 newUri);
23+
event EvCampaignUriSet(string newUri);
2424

2525
event EvSignUp(uint256 tokenId);
2626

2727
event EvRegisterSuccessfully(uint256 tokenId);
2828

29-
event EvCheckIn(uint256 epoch, uint256 tokenId, bytes32 contentUri);
29+
event EvCheckIn(uint256 epoch, uint256 tokenId, string contentUri);
3030

3131
event EvModifyRegistry(uint256[] tokenList, bool[] status);
3232

contracts/interface/ICampaignFactory.sol

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ interface ICampaignFactory {
1313
uint256 startTime,
1414
uint256 totalPeriod,
1515
uint256 periodLength,
16-
bytes32 campaignUri
16+
string calldata campaignUri
1717
) external;
1818

1919
event EvCampaignCreated(address indexed host, address indexed campaignAddress);

cspell.json

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
"Hola",
5757
"ipfs",
5858
"Qmxxxxx",
59+
"Qmxxxx",
60+
"multiformats",
5961
"Merkle",
6062
"vitalik",
6163
"georli",

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
"dependencies": {
5656
"@openzeppelin/contracts": "^4.4.1",
5757
"@openzeppelin/contracts-upgradeable": "^4.4.2",
58-
"hardhat-deploy": "^0.11.12"
58+
"hardhat-deploy": "^0.11.12",
59+
"multiformats": "^9.7.1"
5960
},
6061
"files": []
6162
}

scripts/cid.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// import { base64 } from 'multiformats/bases/base64';
2+
import { ethers } from 'hardhat';
3+
import { CID } from 'multiformats/cid';
4+
import * as json from 'multiformats/codecs/json';
5+
import { sha256 } from 'multiformats/hashes/sha2';
6+
7+
async function main() {
8+
const bytes = json.encode({ hello: 'world' });
9+
10+
const hash = await sha256.digest(bytes);
11+
const cid = CID.create(1, json.code, hash);
12+
13+
/* cspell:disable-next-line */
14+
const c = CID.parse('bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi');
15+
16+
console.log(cid, c.toV0());
17+
18+
console.log(ethers.utils.formatBytes32String('ipfs://1111111111111111111111111'));
19+
}
20+
21+
main().catch((e) => {
22+
console.error(e);
23+
});

test/CampaignFactoryUpgradable.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('CampaignFactoryUpgradable', () => {
3535
(await getCurrentTime()) + 86400 / 2,
3636
2,
3737
86400,
38-
ethers.utils.formatBytes32String('ipfs://Qmxxxxx'),
38+
'ipfs://Qmxxxx',
3939
);
4040

4141
const receipt: ContractReceipt = await tx.wait();
@@ -82,7 +82,7 @@ describe('CampaignFactoryUpgradable', () => {
8282
(await getCurrentTime()) + 86400,
8383
3,
8484
86400,
85-
ethers.utils.formatBytes32String('ipfs://Qmxxxxx'),
85+
'ipfs://Qmxxxx',
8686
);
8787

8888
const receipt: ContractReceipt = await tx.wait();
@@ -119,15 +119,15 @@ describe('CampaignFactoryUpgradable', () => {
119119
await TimeGo(86400);
120120

121121
for (const user of users) {
122-
await campaign.connect(user).checkIn(ethers.utils.formatBytes32String('ipfs://Qmxxxxx'), holderInfo[user.address]);
122+
await campaign.connect(user).checkIn('ipfs://Qmxxxx', holderInfo[user.address]);
123123
}
124124

125125
expect(await campaign.currentEpoch()).to.be.equal(0);
126126

127127
await TimeGo(86400);
128128

129129
for (const user of users) {
130-
await campaign.connect(user).checkIn(ethers.utils.formatBytes32String('ipfs://Qmxxxxx'), holderInfo[user.address]);
130+
await campaign.connect(user).checkIn('ipfs://Qmxxxx', holderInfo[user.address]);
131131
}
132132

133133
expect(await campaign.currentEpoch()).to.be.equal(1);
@@ -136,7 +136,7 @@ describe('CampaignFactoryUpgradable', () => {
136136

137137
// first one forget to checkIn
138138
for (const user of users.slice(1)) {
139-
await campaign.connect(user).checkIn(ethers.utils.formatBytes32String('ipfs://Qmxxxxx'), holderInfo[user.address]);
139+
await campaign.connect(user).checkIn('ipfs://Qmxxxx', holderInfo[user.address]);
140140
}
141141
expect(await campaign.currentEpoch()).to.be.equal(2);
142142

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -7439,6 +7439,11 @@ multicodec@^1.0.0:
74397439
buffer "^5.6.0"
74407440
varint "^5.0.0"
74417441

7442+
multiformats@^9.7.1:
7443+
version "9.7.1"
7444+
resolved "https://registry.yarnpkg.com/multiformats/-/multiformats-9.7.1.tgz#ab348e5fd6f8e7fb3fd56033211bda48854e2173"
7445+
integrity sha512-TaVmGEBt0fhxiNJMGphBfB+oGvUxFs8KgGvgl8d3C+GWtrFcvXdJ2196eg+dYhmSFClmgFfSfJEklo+SZzdNuw==
7446+
74427447
multihashes@^0.4.15, multihashes@~0.4.15:
74437448
version "0.4.21"
74447449
resolved "https://registry.yarnpkg.com/multihashes/-/multihashes-0.4.21.tgz#dc02d525579f334a7909ade8a122dabb58ccfcb5"

0 commit comments

Comments
 (0)