Skip to content

Commit 6ca9aac

Browse files
authored
Merge branch 'dev' into intract-mint-branch
2 parents 10788b7 + b186053 commit 6ca9aac

File tree

9 files changed

+233
-16
lines changed

9 files changed

+233
-16
lines changed

libs/red-envelope/src/abis/RedPacket.json

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,68 @@
142142
"name": "RedPacketCreated",
143143
"type": "event"
144144
},
145+
{
146+
"anonymous": false,
147+
"inputs": [
148+
{
149+
"indexed": true,
150+
"internalType": "uint256",
151+
"name": "id",
152+
"type": "uint256"
153+
},
154+
{
155+
"indexed": true,
156+
"internalType": "address",
157+
"name": "recipient",
158+
"type": "address"
159+
},
160+
{
161+
"indexed": true,
162+
"internalType": "address",
163+
"name": "token",
164+
"type": "address"
165+
},
166+
{
167+
"indexed": false,
168+
"internalType": "uint256",
169+
"name": "amount",
170+
"type": "uint256"
171+
}
172+
],
173+
"name": "RedPacketGasCollected",
174+
"type": "event"
175+
},
176+
{
177+
"anonymous": false,
178+
"inputs": [
179+
{
180+
"indexed": true,
181+
"internalType": "uint256",
182+
"name": "id",
183+
"type": "uint256"
184+
},
185+
{
186+
"indexed": true,
187+
"internalType": "address",
188+
"name": "recipient",
189+
"type": "address"
190+
},
191+
{
192+
"indexed": true,
193+
"internalType": "address",
194+
"name": "token",
195+
"type": "address"
196+
},
197+
{
198+
"indexed": false,
199+
"internalType": "uint256",
200+
"name": "amount",
201+
"type": "uint256"
202+
}
203+
],
204+
"name": "RedPacketWithdraw",
205+
"type": "event"
206+
},
145207
{
146208
"anonymous": false,
147209
"inputs": [
@@ -289,6 +351,11 @@
289351
"name": "id",
290352
"type": "uint256"
291353
},
354+
{
355+
"internalType": "bytes32",
356+
"name": "password",
357+
"type": "bytes32"
358+
},
292359
{
293360
"internalType": "uint256",
294361
"name": "expiry",
@@ -365,6 +432,11 @@
365432
"name": "isGasfree",
366433
"type": "bool"
367434
},
435+
{
436+
"internalType": "bytes32",
437+
"name": "password",
438+
"type": "bytes32"
439+
},
368440
{
369441
"internalType": "uint256",
370442
"name": "expiry",
@@ -378,7 +450,7 @@
378450
],
379451
"name": "createRedPacket",
380452
"outputs": [],
381-
"stateMutability": "nonpayable",
453+
"stateMutability": "payable",
382454
"type": "function"
383455
},
384456
{
@@ -531,6 +603,11 @@
531603
"internalType": "bool",
532604
"name": "isGasfree",
533605
"type": "bool"
606+
},
607+
{
608+
"internalType": "bytes32",
609+
"name": "password",
610+
"type": "bytes32"
534611
}
535612
],
536613
"internalType": "struct RedPacket.RedPacketInfo",
@@ -675,6 +752,11 @@
675752
"name": "recipient",
676753
"type": "address"
677754
},
755+
{
756+
"internalType": "bytes32",
757+
"name": "password",
758+
"type": "bytes32"
759+
},
678760
{
679761
"internalType": "uint256",
680762
"name": "expiry",
@@ -768,6 +850,11 @@
768850
"name": "isGasfree",
769851
"type": "bool"
770852
},
853+
{
854+
"internalType": "bytes32",
855+
"name": "password",
856+
"type": "bytes32"
857+
},
771858
{
772859
"internalType": "uint256",
773860
"name": "expiry",
@@ -933,6 +1020,11 @@
9331020
"internalType": "bool",
9341021
"name": "isGasfree",
9351022
"type": "bool"
1023+
},
1024+
{
1025+
"internalType": "bytes32",
1026+
"name": "password",
1027+
"type": "bytes32"
9361028
}
9371029
],
9381030
"stateMutability": "view",
@@ -1049,5 +1141,9 @@
10491141
"outputs": [],
10501142
"stateMutability": "nonpayable",
10511143
"type": "function"
1144+
},
1145+
{
1146+
"stateMutability": "payable",
1147+
"type": "receive"
10521148
}
10531149
]

libs/red-envelope/src/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ export const configuration = {
77
chainId: Chains.ZkLinkNovaSepolia,
88
wethAddress: '0x8280a4e7D5B3B658ec4580d3Bc30f5e50454F169',
99
quoterContractAddress: '0x86Fc6ab84CFc6a506d51FC722D3aDe959599A98A',
10-
redPacketContractAddress: '0x2f7821161C6c11a473A6DC03896ea6BB5ac5Cb2F',
11-
paymasterContractAddress: '0x0a7f97fbF3cf238aAD21119CE70aB157CE11ce01',
10+
redPacketContractAddress: '0x2897AB1D7DEbc60feAA1535F3Cb623f1D3579dB1',
11+
paymasterContractAddress: '0x8b030151d249fD6Df6cc31a7aB63B912130b84f4',
1212
tokens: [
1313
{
1414
label: 'ETH',

libs/red-envelope/src/red-envelope.service.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ export class RedEnvelopeService extends ActionDto<FieldTypes> {
145145
regex: '^[1-9]\\d*$',
146146
regexDesc: 'It should be a positive integer.',
147147
},
148+
{
149+
name: 'password',
150+
label: 'Red Packet Password',
151+
desc: 'Input your red packets password, like: i love btc',
152+
type: 'password',
153+
},
148154
{
149155
name: 'gasToken',
150156
label: 'Who should pay for the claiming gas fee',
@@ -185,6 +191,7 @@ export class RedEnvelopeService extends ActionDto<FieldTypes> {
185191
{ name: 'packetHash', type: 'bytes32' },
186192
{ name: 'isRandom', type: 'bool' },
187193
{ name: 'isGasfree', type: 'bool' },
194+
{ name: 'password', type: 'bytes32' },
188195
{ name: 'expiry', type: 'uint256' },
189196
],
190197
};
@@ -200,6 +207,7 @@ export class RedEnvelopeService extends ActionDto<FieldTypes> {
200207
isRandom: params.isRandom,
201208
isGasfree: params.isGasfree,
202209
expiry: params.expiry,
210+
password: params.password,
203211
};
204212
const signature = await this.wallet.signTypedData(
205213
domain,
@@ -222,13 +230,15 @@ export class RedEnvelopeService extends ActionDto<FieldTypes> {
222230
RedPacketClaim: [
223231
{ name: 'id', type: 'uint256' },
224232
{ name: 'recipient', type: 'address' },
233+
{ name: 'password', type: 'bytes32' },
225234
{ name: 'expiry', type: 'uint256' },
226235
],
227236
};
228237

229238
const signMessage = {
230239
id: params.id,
231240
recipient: params.recipient,
241+
password: params.password,
232242
expiry: params.expiry,
233243
};
234244

@@ -251,11 +261,13 @@ export class RedEnvelopeService extends ActionDto<FieldTypes> {
251261
const signature = await this.genClaimSignature({
252262
id,
253263
expiry,
264+
password: keccak256(ethers.toUtf8Bytes('1')),
254265
recipient: this.wallet.address,
255266
});
256267

257268
const gasEstimate = await this.envelopContract.claimRedPacket.estimateGas(
258269
id,
270+
ethers.keccak256(ethers.toUtf8Bytes('1')),
259271
expiry,
260272
signature,
261273
);
@@ -334,14 +346,16 @@ export class RedEnvelopeService extends ActionDto<FieldTypes> {
334346
distributionToken,
335347
amountOfRedEnvelopes,
336348
gasToken,
349+
password: rawPassword,
337350
} = formData;
338-
351+
const password = ethers.keccak256(ethers.toUtf8Bytes(rawPassword));
339352
const totalShare = this.genTotalShare(parseInt(amountOfRedEnvelopes));
340353
const packetHash = PACKET_HASH;
341354
const isRandom =
342355
distributionMode === DistributionModeValue.RandomAmountPerAddress;
343356
const isGasfree = gasToken === GasTokenValue.DistributedToken;
344-
const payForGas = await this.claimRedEnvelopeMinGas(formData);
357+
// const payForGas = await this.claimRedEnvelopeMinGas(formData); //
358+
const payForGas = 0n;
345359

346360
const decimals = await this.getDecimals(distributionToken);
347361
const totalDistributionAmountBn = parseUnits(
@@ -361,6 +375,7 @@ export class RedEnvelopeService extends ActionDto<FieldTypes> {
361375
isRandom: isRandom,
362376
isGasfree: isGasfree,
363377
expiry: expiry,
378+
password,
364379
});
365380

366381
const totalValue = totalDistributionAmountBn + payForGas;
@@ -376,6 +391,7 @@ export class RedEnvelopeService extends ActionDto<FieldTypes> {
376391
packetHash,
377392
isRandom,
378393
isGasfree,
394+
password,
379395
expiry,
380396
signature,
381397
);
@@ -424,16 +440,24 @@ export class RedEnvelopeService extends ActionDto<FieldTypes> {
424440
public async preCheckTransaction(
425441
data: GenerateTransactionParams<FieldTypes>,
426442
) {
427-
const { additionalData } = data;
443+
const { additionalData, formData } = data;
444+
const { password } = formData;
428445
const { code, account } = additionalData;
429446
if (!code) {
430447
throw new Error('missing code');
431448
}
432449
const packetId = this.getPacketIDByCode(code);
450+
const packetInfo = await this.envelopContract.redPackets(packetId);
451+
const passwordHash = ethers.keccak256(ethers.toUtf8Bytes(password));
433452
const hasClaimed = await this.envelopContract.isClaimed(packetId, account);
434453
if (hasClaimed) {
435454
return 'User has already received';
436455
}
456+
457+
if ([...packetInfo].pop() !== passwordHash) {
458+
return 'Wrong Password';
459+
}
460+
437461
const hasUnclaimedPacket =
438462
await this.envelopContract.getRedPacketBalance(packetId);
439463
if (hasUnclaimedPacket.unClaimedCount === 0n) {
@@ -519,7 +543,7 @@ export class RedEnvelopeService extends ActionDto<FieldTypes> {
519543
data: GenerateTransactionParams<FieldTypes>,
520544
): Promise<GenerateTransactionResponse> {
521545
const { additionalData, formData } = data;
522-
const { gasToken } = formData;
546+
const { gasToken, password } = formData;
523547
const isGasfree = gasToken === GasTokenValue.DistributedToken;
524548
const { code, account } = additionalData;
525549
if (!code) {
@@ -542,10 +566,12 @@ export class RedEnvelopeService extends ActionDto<FieldTypes> {
542566
const signature = await this.genClaimSignature({
543567
id: packetId,
544568
expiry,
569+
password: ethers.keccak256(ethers.toUtf8Bytes(password)),
545570
recipient: account,
546571
});
547572
const tx = await this.envelopContract.claimRedPacket.populateTransaction(
548573
packetId,
574+
ethers.keccak256(ethers.toUtf8Bytes(password)),
549575
expiry,
550576
signature,
551577
);

libs/red-envelope/src/type.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ export interface CreateRedPacketParams {
2929
isRandom: boolean;
3030
isGasfree: boolean;
3131
expiry: number;
32+
password: string;
3233
}
3334

3435
export interface ClaimRedPacketParams {
3536
id: bigint;
3637
recipient: string;
38+
password: string;
3739
expiry: number;
3840
}
3941

@@ -43,4 +45,5 @@ export type FieldTypes = {
4345
distributionToken: string;
4446
amountOfRedEnvelopes: string;
4547
gasToken: string;
48+
password: string;
4649
};

0 commit comments

Comments
 (0)