Skip to content

Commit

Permalink
chore: rawdata + gas params
Browse files Browse the repository at this point in the history
  • Loading branch information
ohdcthang committed Aug 13, 2024
1 parent f3cb58c commit e5e1ff3
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 18 deletions.
78 changes: 70 additions & 8 deletions src/packages/gasSponsor.ts
Original file line number Diff line number Diff line change
@@ -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<CreateGiftRespone>{
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)
});
Expand Down Expand Up @@ -44,13 +45,14 @@ export class GasSponsor extends GiftCore {


async claimReward(params: ClaimRewardParams): Promise<ClaimRewardRespone>{
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
})

Expand All @@ -63,13 +65,15 @@ export class GasSponsor extends GiftCore {
}

async withdrawRemainingReward(params: WithdrawRewardParams): Promise<WithdrawGiftRespone>{
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,
})

Expand All @@ -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)
}
}
}
8 changes: 7 additions & 1 deletion src/packages/giftCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>{
Expand All @@ -31,4 +33,8 @@ export class GiftCore{

return signer
}

getContractAddress(): string {
return this.contractAddress
}
}
72 changes: 65 additions & 7 deletions src/packages/giftFactoy.ts
Original file line number Diff line number Diff line change
@@ -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
}
Expand All @@ -22,9 +21,9 @@ export class GiftFactory extends GiftCore{

GiftFactory.instance = this
}

async createGifts(params: CreateGiftsParams): Promise<CreateGiftRespone>{
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,
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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<ClaimRewardRespone>{
const { wallet, giftContractAddress } = params

Expand All @@ -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<WithdrawGiftRespone>{
const giftReward = await getGiftReward(params.giftContractAddress)

Expand All @@ -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<string>{
try {
const giftContract = new ethers.Contract(giftContractAddress , GIFT_ABI['COIN98_GIFT_CONTRACT_ADDRESS'], this.admin )
Expand Down
17 changes: 15 additions & 2 deletions src/types/gifts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CreateGiftsParams, 'wallet' | 'rewardToken' >{
export interface InputConfig extends Omit<CreateGiftsParams, 'wallet' | 'rewardToken' | 'gasPrice' | 'gasLimit' >{
rewardToken: string
}
export interface GasSponsorCreateGiftsParams {
Expand All @@ -21,6 +23,8 @@ export interface GasSponsorCreateGiftsParams {
inputConfig: InputConfig,
feeToken: string,
nonce?: number | string,
gasPrice?: number,
gasLimit?: number
}

export interface SetFee {
Expand All @@ -31,6 +35,8 @@ export interface SetFee {
}

interface BaseRewardParams{
gasPrice: number,
gasLimit: number
wallet: Wallet,
giftContractAddress: string
}
Expand Down Expand Up @@ -78,4 +84,11 @@ export interface InsertedSlotRepsonse{
isInserted: boolean,
reward: number,
slotNumber: number
}

export interface RawData{
from: string,
to: string,
data: string,
value?: string
}

0 comments on commit e5e1ff3

Please sign in to comment.