Skip to content

Commit

Permalink
fix: token native
Browse files Browse the repository at this point in the history
  • Loading branch information
ohdcthang committed Aug 10, 2024
1 parent 2b46515 commit 4ae5d29
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.1.1",
"version": "1.0.0",
"license": "MIT",
"main": "src/index.ts",
"types": "src/index.ts",
Expand Down
5 changes: 2 additions & 3 deletions src/constants/chains.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const isDev = process.env.NODE_ENV === 'development'

export const RPC_URL = isDev ? "https://rpc-testnet.viction.xyz" : "https://rpc.viction.xyz"
export const CHAIN_ID = isDev ? 89 : 88
export const RPC_URL = "https://rpc.viction.xyz"
export const CHAIN_ID = 88
12 changes: 5 additions & 7 deletions src/constants/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ export enum CONTRACT_NAME {
GAS_SPONSOR_CONTRACT_ADDRESS = "GAS_SPONSOR_CONTRACT_ADDRESS"
}

const isDev = process.env.NODE_ENV === 'development'

export const GIFT_CONTRACT: Record<CONTRACT_NAME, string> = {
"BROADCASTER_CONTRACT_ADDRESS" : isDev ? '0xa19269Cde8b6B8A52640c5bE0566D54805D04253' : '0x9fAE0D50e105f0D3E9815f38b8255F72a679dEf2',
"RANDOMIZER_CONTRACT_ADDRESS" : isDev ? '0x7c3c41E7c76b14d7f03c3fF875f9eCb2F69CB607' : '0x8261384E6076B535c50dE75BDc391Ca6D630F7a6',
"COIN98_GIFT_CONTRACT_ADDRESS": isDev ? '0x495bFeE6Bf9e2e6bBEDa1E4F8B5E249a22EEA167' : '0x00800E8816DE5c42da2746F3e438EB3bDC1ef151',
"COIN98_GIFT_FACTORY_CONTRACT_ADDRESS": isDev ? '0x080A34F16d545b0F084Ea7202f391cfa5b9914Cd' : '0x563F0e812a896Db7dC4877bf5a2554F59d4f7228',
"GAS_SPONSOR_CONTRACT_ADDRESS": isDev ? '0xDD133F8CDe28e353fEE74C187ce1A433B1185229' : '0x726cf8d916b39B848F9a989BE5567FBb4Fb46F5a',
"BROADCASTER_CONTRACT_ADDRESS" : '0x9fAE0D50e105f0D3E9815f38b8255F72a679dEf2',
"RANDOMIZER_CONTRACT_ADDRESS" : '0x8261384E6076B535c50dE75BDc391Ca6D630F7a6',
"COIN98_GIFT_CONTRACT_ADDRESS": '0x00800E8816DE5c42da2746F3e438EB3bDC1ef151',
"COIN98_GIFT_FACTORY_CONTRACT_ADDRESS": '0x563F0e812a896Db7dC4877bf5a2554F59d4f7228',
"GAS_SPONSOR_CONTRACT_ADDRESS": '0x726cf8d916b39B848F9a989BE5567FBb4Fb46F5a'
}
27 changes: 16 additions & 11 deletions src/packages/gasSponsor.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ethers } from "ethers";
import { CONTRACT_NAME } from "../constants";
import { ClaimRewardParams, ClaimRewardRespone, CreateGiftRespone, GasSponsorCreateGiftsParams, WithdrawGiftRespone, WithdrawRewardParams } from "../types";
import { GiftCore } from "./giftCore";
Expand All @@ -9,16 +10,25 @@ export class GasSponsor extends GiftCore {

async createGifts(params: GasSponsorCreateGiftsParams): Promise<CreateGiftRespone>{
const { inputConfig, feeToken, giftContractAddress, signer} = 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,
nonce: nonce
nonce: nonce,
value: isNative ? BigInt(inputConfig.totalReward) : BigInt(0)
});

const { transactionHash ,events } = await response.wait()

if(isNative){
return {
contractAddress: events[0]?.address,
transactionHash
}
}

const transferEvent = events?.find((e: { event: string }) => e.event === "Transfer")
const contractAddress = transferEvent?.args['to']

Expand All @@ -43,12 +53,9 @@ export class GasSponsor extends GiftCore {
nonce
})

const { transactionHash , events } = await response.wait()

const transferEvent = events?.find((e: { event: string }) => e.event === "Transfer")
const amount = Number(transferEvent?.args['amount'])
const { transactionHash } = await response.wait()

return {transactionHash, amount}
return {transactionHash, amount: 0}
} catch (error) {
throw new Error(error as unknown as string)
}
Expand All @@ -62,14 +69,12 @@ export class GasSponsor extends GiftCore {

const response = await this.contract.connect(owner).withdrawRemainingReward(giftContractAddress,{
gasLimit: 650000,
nonce
nonce,
})

const { transactionHash , events } = await response.wait()
const transferEvent = events?.find((e: { event: string }) => e.event === "Transfer")
const amount = Number(transferEvent?.args['amount'])
const { transactionHash } = await response.wait()

return {transactionHash, amount}
return {transactionHash, amount: 0}

} catch (error) {
throw new Error(error as unknown as string)
Expand Down
30 changes: 25 additions & 5 deletions src/packages/giftFactoy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,24 @@ export class GiftFactory extends GiftCore{
endTimestamp
}

const signer = this.createSigner(wallet)
if(!rewardToken.address){
const responseGift = await this.sponsorGasContract.createGifts({
signer,
giftContractAddress: this.contractAddress,
inputConfig: {
...inputConfig,
rewardToken: ethers.constants.AddressZero,
},
feeToken: ethers.constants.AddressZero
})

return responseGift
}

// const feeConfig = await this.contract.getFee(ethers.constants.AddressZero);
// const feeAmount = (BigInt(inputConfig.totalReward.toString()) * BigInt(feeConfig.percentAmount)) / BigInt(10000);
// const totalRewards = BigInt(inputConfig.totalReward.toString());

const signer = this.createSigner(wallet)

const tokenContract = new Contract(rewardToken.address as string, ERC20ABI, signer)
const nonce = await this.getNonceAccount(signer.address)
const response = await tokenContract.approve(this.contractAddress,String(convertBalanceToWei(totalReward.toString(), rewardToken.decimal)),{
Expand All @@ -52,15 +64,23 @@ export class GiftFactory extends GiftCore{
}

async claimGift(params: ClaimRewardParams): Promise<ClaimRewardRespone>{
const { wallet, giftContractAddress } = params

const slotInfo = await this.getInsertedSlot({
giftContractAddress: giftContractAddress,
recipientAddress: wallet?.address
})
const response = await this.sponsorGasContract.claimReward(params)

return response
return {...response, amount: slotInfo.reward}
}

async withdrawRemainingReward(params: WithdrawRewardParams): Promise<WithdrawGiftRespone>{
const giftInfo = await this.getGiftConfig(params.giftContractAddress)

const response = await this.sponsorGasContract.withdrawRemainingReward(params)

return response
return {...response, amount: giftInfo.remainingReward}
}

async submitRewardRecipient(recipcient: string, giftContractAddress: string): Promise<string>{
Expand Down
4 changes: 2 additions & 2 deletions src/types/gifts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface CreateGiftsParams {
rewardToken: Partial<TokenInfo>,
totalReward: number | bigint,
totalSlots: number | bigint,
randomPercent: | bigint,
randomPercent: number| bigint,
endTimestamp: number | bigint,
baseMultiplier?: | bigint
}
Expand Down Expand Up @@ -38,7 +38,7 @@ interface BaseRewardParams{
export interface ClaimRewardParams extends BaseRewardParams{ }


export interface WithdrawRewardParams extends BaseRewardParams{}
export interface WithdrawRewardParams extends BaseRewardParams{ }

interface BaseRewardRespone{
transactionHash: string
Expand Down

0 comments on commit 4ae5d29

Please sign in to comment.