diff --git a/src/packages/gasSponsor.ts b/src/packages/gasSponsor.ts index 7ff52d4..4780034 100644 --- a/src/packages/gasSponsor.ts +++ b/src/packages/gasSponsor.ts @@ -1,22 +1,23 @@ import { ethers } from "ethers"; import { CONTRACT_NAME } from "../constants"; -import { ClaimRewardParams, ClaimRewardRespone, CreateGiftRespone, GasSponsorCreateGiftsParams, WithdrawGiftRespone, WithdrawRewardParams } from "../types"; +import { ClaimRewardParams, ClaimRewardRespone, CreateGiftRespone, GasSponsorCreateGiftsParams, RawData, WithdrawGiftRespone, WithdrawRewardParams } from "../types"; import { GiftCore } from "./giftCore"; export class GasSponsor extends GiftCore { constructor(privateKey?: string, isDev?: boolean){ - super(CONTRACT_NAME.GAS_SPONSOR_CONTRACT_ADDRESS, privateKey, isDev) + super(CONTRACT_NAME.GAS_SPONSOR_CONTRACT_ADDRESS, privateKey, isDev) } async createGifts(params: GasSponsorCreateGiftsParams): Promise{ - const { inputConfig, feeToken, giftContractAddress, signer} = params + const { inputConfig, feeToken, giftContractAddress, signer, gasPrice, gasLimit} = params const isNative = inputConfig.rewardToken === ethers.constants.AddressZero try { const nonce = await this.getNonceAccount(signer.address) const response = await this.contract.connect(signer).createGift(giftContractAddress, inputConfig, feeToken,{ - gasLimit: 650000, + gasLimit, + gasPrice, nonce: nonce, value: isNative ? BigInt(inputConfig.totalReward) : BigInt(0) }); @@ -44,13 +45,14 @@ export class GasSponsor extends GiftCore { async claimReward(params: ClaimRewardParams): Promise{ - const{ wallet, giftContractAddress } = params + const{ wallet, giftContractAddress, gasLimit, gasPrice } = params try { const nonce = await this.getNonceAccount(wallet.address) const owner = this.createSigner(wallet) const response = await this.contract.connect(owner).claimReward(giftContractAddress,{ - gasLimit: 650000, + gasLimit, + gasPrice, nonce }) @@ -63,13 +65,15 @@ export class GasSponsor extends GiftCore { } async withdrawRemainingReward(params: WithdrawRewardParams): Promise{ - const { wallet, giftContractAddress } = params + const { wallet, giftContractAddress, gasLimit, gasPrice } = params + try { const nonce = await this.getNonceAccount(wallet.address) const owner = this.createSigner(wallet) const response = await this.contract.connect(owner).withdrawRemainingReward(giftContractAddress,{ - gasLimit: 650000, + gasLimit, + gasPrice, nonce, }) @@ -81,5 +85,63 @@ export class GasSponsor extends GiftCore { throw new Error(error as unknown as string) } } + + getRawDataCreateGift(params: GasSponsorCreateGiftsParams): RawData { + const { inputConfig, feeToken, giftContractAddress, signer} = params + const isNative = inputConfig.rewardToken === ethers.constants.AddressZero + + try { + //@ts-expect-error + let iface = new ethers.utils.Interface(this.abi); + + const data = iface.encodeFunctionData("createGift", [giftContractAddress, Object.values(inputConfig), feeToken]) + + return { + from: signer.publicKey, + to: giftContractAddress, + data, + value: isNative ? BigInt(inputConfig.totalReward).toString() : BigInt(0).toString() + } + } catch (error) { + throw new Error(error as unknown as string) + } + } + + getRawDataClaimGift(params: ClaimRewardParams): RawData{ + const{ giftContractAddress, wallet} = params + try { + //@ts-expect-error + let iface = new ethers.utils.Interface(this.abi); + + const data = iface.encodeFunctionData("claimReward", [giftContractAddress]) + console.log("🚀 ~ GasSponsor ~ getRawDataClaimGift ~ data:", data) + + return { + from: wallet?.address, + to: giftContractAddress, + data, + } + } catch (error) { + throw new Error(error as unknown as string) + } + } + + getRawDataWithdrawReward(params: WithdrawRewardParams): RawData{ + const { wallet, giftContractAddress } = params + try { + //@ts-expect-error + let iface = new ethers.utils.Interface(this.abi); + + const data = iface.encodeFunctionData("withdrawRemainingReward", [giftContractAddress]) + + return { + from: wallet?.address, + to: giftContractAddress, + data, + } + } catch (error) { + throw new Error(error as unknown as string) + } + } } \ No newline at end of file diff --git a/src/packages/giftCore.ts b/src/packages/giftCore.ts index d893fec..710a9ef 100644 --- a/src/packages/giftCore.ts +++ b/src/packages/giftCore.ts @@ -8,12 +8,14 @@ export class GiftCore{ protected contract: Contract protected admin: ethers.Wallet protected contractAddress: string + protected abi: ethers.ContractInterface constructor(contractName: CONTRACT_NAME, privateKey?: string, isDev?: boolean){ this.contractAddress = GIFT_CONTRACT(contractName,isDev) + this.abi = GIFT_ABI[contractName] this.provider = new providers.JsonRpcProvider(RPC_URL, CHAIN_ID); this.admin =new EthersWallet(privateKey as string || PRIVATE_KEY, this.provider) - this.contract = new Contract(GIFT_CONTRACT(contractName,isDev), GIFT_ABI[contractName], this.admin) + this.contract = new Contract(this.contractAddress, this.abi, this.admin) } async getNonceAccount(address: string): Promise{ @@ -31,4 +33,8 @@ export class GiftCore{ return signer } + + getContractAddress(): string { + return this.contractAddress + } } \ No newline at end of file diff --git a/src/packages/giftFactoy.ts b/src/packages/giftFactoy.ts index f9c0482..03f43c2 100644 --- a/src/packages/giftFactoy.ts +++ b/src/packages/giftFactoy.ts @@ -1,17 +1,16 @@ import { Contract, ethers } from "ethers"; import { getGiftReward, getInsertedSlotReward } from "../api"; import { CONTRACT_NAME, ERC20ABI, GIFT_ABI } from '../constants' -import { ClaimRewardParams, ClaimRewardRespone, CreateGiftRespone, CreateGiftsParams, GetInsertedSlotParams, GiftConfigResponse, GiftFactoryEngine, InsertedSlotRepsonse, SetFee, WithdrawGiftRespone, WithdrawRewardParams } from "../types"; +import { ClaimRewardParams, ClaimRewardRespone, CreateGiftRespone, CreateGiftsParams, GetInsertedSlotParams, GiftConfigResponse, GiftFactoryEngine, InsertedSlotRepsonse, RawData, SetFee, WithdrawGiftRespone, WithdrawRewardParams } from "../types"; import { convertBalanceToWei } from "../utils"; import { GasSponsor } from "./gasSponsor"; import { GiftCore } from "./giftCore"; export class GiftFactory extends GiftCore{ static instance: GiftFactory - private sponsorGasContract?: GasSponsor + sponsorGasContract?: GasSponsor constructor(params: GiftFactoryEngine){ - if(GiftFactory.instance){ return GiftFactory.instance } @@ -22,9 +21,9 @@ export class GiftFactory extends GiftCore{ GiftFactory.instance = this } - + async createGifts(params: CreateGiftsParams): Promise{ - const {wallet, rewardToken, totalReward, totalSlots, randomPercent, endTimestamp , baseMultiplier = 1} = params + const {wallet, rewardToken, totalReward, totalSlots, randomPercent, endTimestamp , baseMultiplier = 1, gasLimit, gasPrice} = params try { const inputConfig = { rewardToken: rewardToken.address as string, @@ -44,7 +43,9 @@ export class GiftFactory extends GiftCore{ ...inputConfig, rewardToken: ethers.constants.AddressZero, }, - feeToken: ethers.constants.AddressZero + feeToken: ethers.constants.AddressZero, + gasPrice, + gasLimit }) return responseGift as CreateGiftRespone @@ -61,7 +62,9 @@ export class GiftFactory extends GiftCore{ signer, giftContractAddress: this.contractAddress, inputConfig, - feeToken: ethers.constants.AddressZero + feeToken: ethers.constants.AddressZero, + gasLimit, + gasPrice }) return responseGift as CreateGiftRespone @@ -70,6 +73,49 @@ export class GiftFactory extends GiftCore{ } } + getRawDataCreateGift(params: CreateGiftsParams): RawData{ + const {wallet, rewardToken, totalReward, totalSlots, randomPercent, endTimestamp , baseMultiplier = 1} = params + + try { + const inputConfig = { + rewardToken: rewardToken.address as string, + totalReward: BigInt(convertBalanceToWei(totalReward.toString(), rewardToken.decimal as number)), + totalSlots: BigInt(totalSlots), + randomPercent: BigInt(randomPercent), + baseMultiplier: BigInt(baseMultiplier), + endTimestamp + } + + const signer = this.createSigner(wallet) + + + if(!rewardToken.address){ + const responseGiftRawData = this.sponsorGasContract?.getRawDataCreateGift({ + signer, + giftContractAddress: this.contractAddress, + inputConfig: { + ...inputConfig, + rewardToken: ethers.constants.AddressZero, + }, + feeToken: ethers.constants.AddressZero, + }) + + return responseGiftRawData as RawData + } + + const responseGiftRawData = this.sponsorGasContract?.getRawDataCreateGift({ + signer, + giftContractAddress: this.contractAddress, + inputConfig, + feeToken: ethers.constants.AddressZero, + }) + + return responseGiftRawData as RawData + } catch (error) { + throw new Error(error as unknown as string) + } + } + async claimGift(params: ClaimRewardParams): Promise{ const { wallet, giftContractAddress } = params @@ -84,6 +130,12 @@ export class GiftFactory extends GiftCore{ return {...response, amount: reward} as ClaimRewardRespone } + getRawDataClaimGift(params: ClaimRewardParams): RawData{ + const response = this.sponsorGasContract?.getRawDataClaimGift(params) + + return response as RawData + } + async withdrawRemainingReward(params: WithdrawRewardParams): Promise{ const giftReward = await getGiftReward(params.giftContractAddress) @@ -92,6 +144,12 @@ export class GiftFactory extends GiftCore{ return {...response, amount: giftReward} as WithdrawGiftRespone } + getRawDataWithdrawReward(params: ClaimRewardParams): RawData{ + const response = this.sponsorGasContract?.getRawDataWithdrawReward(params) + + return response as RawData + } + async submitRewardRecipient(recipcient: string, giftContractAddress: string): Promise{ try { const giftContract = new ethers.Contract(giftContractAddress , GIFT_ABI['COIN98_GIFT_CONTRACT_ADDRESS'], this.admin ) diff --git a/src/types/gifts.ts b/src/types/gifts.ts index 41dbc59..31415cc 100644 --- a/src/types/gifts.ts +++ b/src/types/gifts.ts @@ -8,11 +8,13 @@ export interface CreateGiftsParams { totalSlots: number | bigint, randomPercent: number| bigint, endTimestamp: number | bigint, - baseMultiplier?: | bigint + baseMultiplier?: number | bigint + gasPrice?: number, + gasLimit?: number } -export interface InputConfig extends Omit{ +export interface InputConfig extends Omit{ rewardToken: string } export interface GasSponsorCreateGiftsParams { @@ -21,6 +23,8 @@ export interface GasSponsorCreateGiftsParams { inputConfig: InputConfig, feeToken: string, nonce?: number | string, + gasPrice?: number, + gasLimit?: number } export interface SetFee { @@ -31,6 +35,8 @@ export interface SetFee { } interface BaseRewardParams{ + gasPrice: number, + gasLimit: number wallet: Wallet, giftContractAddress: string } @@ -78,4 +84,11 @@ export interface InsertedSlotRepsonse{ isInserted: boolean, reward: number, slotNumber: number +} + +export interface RawData{ + from: string, + to: string, + data: string, + value?: string } \ No newline at end of file