Skip to content

Commit

Permalink
Merge branch 'dev' into intract-mint-branch
Browse files Browse the repository at this point in the history
  • Loading branch information
xsteadybcgo authored Dec 23, 2024
2 parents 10788b7 + b186053 commit 6ca9aac
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 16 deletions.
98 changes: 97 additions & 1 deletion libs/red-envelope/src/abis/RedPacket.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,68 @@
"name": "RedPacketCreated",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "uint256",
"name": "id",
"type": "uint256"
},
{
"indexed": true,
"internalType": "address",
"name": "recipient",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "token",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "RedPacketGasCollected",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "uint256",
"name": "id",
"type": "uint256"
},
{
"indexed": true,
"internalType": "address",
"name": "recipient",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "token",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "RedPacketWithdraw",
"type": "event"
},
{
"anonymous": false,
"inputs": [
Expand Down Expand Up @@ -289,6 +351,11 @@
"name": "id",
"type": "uint256"
},
{
"internalType": "bytes32",
"name": "password",
"type": "bytes32"
},
{
"internalType": "uint256",
"name": "expiry",
Expand Down Expand Up @@ -365,6 +432,11 @@
"name": "isGasfree",
"type": "bool"
},
{
"internalType": "bytes32",
"name": "password",
"type": "bytes32"
},
{
"internalType": "uint256",
"name": "expiry",
Expand All @@ -378,7 +450,7 @@
],
"name": "createRedPacket",
"outputs": [],
"stateMutability": "nonpayable",
"stateMutability": "payable",
"type": "function"
},
{
Expand Down Expand Up @@ -531,6 +603,11 @@
"internalType": "bool",
"name": "isGasfree",
"type": "bool"
},
{
"internalType": "bytes32",
"name": "password",
"type": "bytes32"
}
],
"internalType": "struct RedPacket.RedPacketInfo",
Expand Down Expand Up @@ -675,6 +752,11 @@
"name": "recipient",
"type": "address"
},
{
"internalType": "bytes32",
"name": "password",
"type": "bytes32"
},
{
"internalType": "uint256",
"name": "expiry",
Expand Down Expand Up @@ -768,6 +850,11 @@
"name": "isGasfree",
"type": "bool"
},
{
"internalType": "bytes32",
"name": "password",
"type": "bytes32"
},
{
"internalType": "uint256",
"name": "expiry",
Expand Down Expand Up @@ -933,6 +1020,11 @@
"internalType": "bool",
"name": "isGasfree",
"type": "bool"
},
{
"internalType": "bytes32",
"name": "password",
"type": "bytes32"
}
],
"stateMutability": "view",
Expand Down Expand Up @@ -1049,5 +1141,9 @@
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"stateMutability": "payable",
"type": "receive"
}
]
4 changes: 2 additions & 2 deletions libs/red-envelope/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export const configuration = {
chainId: Chains.ZkLinkNovaSepolia,
wethAddress: '0x8280a4e7D5B3B658ec4580d3Bc30f5e50454F169',
quoterContractAddress: '0x86Fc6ab84CFc6a506d51FC722D3aDe959599A98A',
redPacketContractAddress: '0x2f7821161C6c11a473A6DC03896ea6BB5ac5Cb2F',
paymasterContractAddress: '0x0a7f97fbF3cf238aAD21119CE70aB157CE11ce01',
redPacketContractAddress: '0x2897AB1D7DEbc60feAA1535F3Cb623f1D3579dB1',
paymasterContractAddress: '0x8b030151d249fD6Df6cc31a7aB63B912130b84f4',
tokens: [
{
label: 'ETH',
Expand Down
34 changes: 30 additions & 4 deletions libs/red-envelope/src/red-envelope.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ export class RedEnvelopeService extends ActionDto<FieldTypes> {
regex: '^[1-9]\\d*$',
regexDesc: 'It should be a positive integer.',
},
{
name: 'password',
label: 'Red Packet Password',
desc: 'Input your red packets password, like: i love btc',
type: 'password',
},
{
name: 'gasToken',
label: 'Who should pay for the claiming gas fee',
Expand Down Expand Up @@ -185,6 +191,7 @@ export class RedEnvelopeService extends ActionDto<FieldTypes> {
{ name: 'packetHash', type: 'bytes32' },
{ name: 'isRandom', type: 'bool' },
{ name: 'isGasfree', type: 'bool' },
{ name: 'password', type: 'bytes32' },
{ name: 'expiry', type: 'uint256' },
],
};
Expand All @@ -200,6 +207,7 @@ export class RedEnvelopeService extends ActionDto<FieldTypes> {
isRandom: params.isRandom,
isGasfree: params.isGasfree,
expiry: params.expiry,
password: params.password,
};
const signature = await this.wallet.signTypedData(
domain,
Expand All @@ -222,13 +230,15 @@ export class RedEnvelopeService extends ActionDto<FieldTypes> {
RedPacketClaim: [
{ name: 'id', type: 'uint256' },
{ name: 'recipient', type: 'address' },
{ name: 'password', type: 'bytes32' },
{ name: 'expiry', type: 'uint256' },
],
};

const signMessage = {
id: params.id,
recipient: params.recipient,
password: params.password,
expiry: params.expiry,
};

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

const gasEstimate = await this.envelopContract.claimRedPacket.estimateGas(
id,
ethers.keccak256(ethers.toUtf8Bytes('1')),
expiry,
signature,
);
Expand Down Expand Up @@ -334,14 +346,16 @@ export class RedEnvelopeService extends ActionDto<FieldTypes> {
distributionToken,
amountOfRedEnvelopes,
gasToken,
password: rawPassword,
} = formData;

const password = ethers.keccak256(ethers.toUtf8Bytes(rawPassword));
const totalShare = this.genTotalShare(parseInt(amountOfRedEnvelopes));
const packetHash = PACKET_HASH;
const isRandom =
distributionMode === DistributionModeValue.RandomAmountPerAddress;
const isGasfree = gasToken === GasTokenValue.DistributedToken;
const payForGas = await this.claimRedEnvelopeMinGas(formData);
// const payForGas = await this.claimRedEnvelopeMinGas(formData); //
const payForGas = 0n;

const decimals = await this.getDecimals(distributionToken);
const totalDistributionAmountBn = parseUnits(
Expand All @@ -361,6 +375,7 @@ export class RedEnvelopeService extends ActionDto<FieldTypes> {
isRandom: isRandom,
isGasfree: isGasfree,
expiry: expiry,
password,
});

const totalValue = totalDistributionAmountBn + payForGas;
Expand All @@ -376,6 +391,7 @@ export class RedEnvelopeService extends ActionDto<FieldTypes> {
packetHash,
isRandom,
isGasfree,
password,
expiry,
signature,
);
Expand Down Expand Up @@ -424,16 +440,24 @@ export class RedEnvelopeService extends ActionDto<FieldTypes> {
public async preCheckTransaction(
data: GenerateTransactionParams<FieldTypes>,
) {
const { additionalData } = data;
const { additionalData, formData } = data;
const { password } = formData;
const { code, account } = additionalData;
if (!code) {
throw new Error('missing code');
}
const packetId = this.getPacketIDByCode(code);
const packetInfo = await this.envelopContract.redPackets(packetId);
const passwordHash = ethers.keccak256(ethers.toUtf8Bytes(password));
const hasClaimed = await this.envelopContract.isClaimed(packetId, account);
if (hasClaimed) {
return 'User has already received';
}

if ([...packetInfo].pop() !== passwordHash) {
return 'Wrong Password';
}

const hasUnclaimedPacket =
await this.envelopContract.getRedPacketBalance(packetId);
if (hasUnclaimedPacket.unClaimedCount === 0n) {
Expand Down Expand Up @@ -519,7 +543,7 @@ export class RedEnvelopeService extends ActionDto<FieldTypes> {
data: GenerateTransactionParams<FieldTypes>,
): Promise<GenerateTransactionResponse> {
const { additionalData, formData } = data;
const { gasToken } = formData;
const { gasToken, password } = formData;
const isGasfree = gasToken === GasTokenValue.DistributedToken;
const { code, account } = additionalData;
if (!code) {
Expand All @@ -542,10 +566,12 @@ export class RedEnvelopeService extends ActionDto<FieldTypes> {
const signature = await this.genClaimSignature({
id: packetId,
expiry,
password: ethers.keccak256(ethers.toUtf8Bytes(password)),
recipient: account,
});
const tx = await this.envelopContract.claimRedPacket.populateTransaction(
packetId,
ethers.keccak256(ethers.toUtf8Bytes(password)),
expiry,
signature,
);
Expand Down
3 changes: 3 additions & 0 deletions libs/red-envelope/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ export interface CreateRedPacketParams {
isRandom: boolean;
isGasfree: boolean;
expiry: number;
password: string;
}

export interface ClaimRedPacketParams {
id: bigint;
recipient: string;
password: string;
expiry: number;
}

Expand All @@ -43,4 +45,5 @@ export type FieldTypes = {
distributionToken: string;
amountOfRedEnvelopes: string;
gasToken: string;
password: string;
};
Loading

0 comments on commit 6ca9aac

Please sign in to comment.