Skip to content

Commit

Permalink
fix: pass admin
Browse files Browse the repository at this point in the history
  • Loading branch information
ohdcthang committed Aug 12, 2024
1 parent 0d2ea97 commit 0ea5588
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
7 changes: 4 additions & 3 deletions src/packages/gasSponsor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { ClaimRewardParams, ClaimRewardRespone, CreateGiftRespone, GasSponsorCre
import { GiftCore } from "./giftCore";

export class GasSponsor extends GiftCore {
constructor(){
super(CONTRACT_NAME.GAS_SPONSOR_CONTRACT_ADDRESS)
constructor(privateKey?: string){
super(CONTRACT_NAME.GAS_SPONSOR_CONTRACT_ADDRESS, privateKey)

}

async createGifts(params: GasSponsorCreateGiftsParams): Promise<CreateGiftRespone>{
Expand All @@ -20,7 +21,7 @@ export class GasSponsor extends GiftCore {
value: isNative ? BigInt(inputConfig.totalReward) : BigInt(0)
});

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

if(isNative){
return {
Expand Down
11 changes: 6 additions & 5 deletions src/packages/giftCore.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import type { Wallet } from "@wallet/core"
import { Contract, ethers, providers, Wallet as EthersWallet } from "ethers"
import { CHAIN_ID, CONTRACT_NAME, GIFT_ABI, GIFT_CONTRACT, PRIVATE_KEY, RPC_URL } from "../constants"
import { CHAIN_ID, CONTRACT_NAME, GIFT_ABI, GIFT_CONTRACT, RPC_URL } from "../constants"

export class GiftCore{
provider: ethers.providers.JsonRpcProvider
contract: Contract
signer: ethers.Wallet
admin: ethers.Wallet
contractAddress: string
static instance: GiftCore;

constructor(contractName: CONTRACT_NAME){
constructor(contractName: CONTRACT_NAME, privateKey?: string){
this.contractAddress = GIFT_CONTRACT[contractName]
this.provider = new providers.JsonRpcProvider(RPC_URL, CHAIN_ID);
this.signer =new EthersWallet(PRIVATE_KEY, this.provider)
this.contract = new Contract(GIFT_CONTRACT[contractName], GIFT_ABI[contractName], this.signer)
this.admin =new EthersWallet(privateKey as string, this.provider)
this.contract = new Contract(GIFT_CONTRACT[contractName], GIFT_ABI[contractName], this.admin)
}

async getNonceAccount(address: string): Promise<number>{
Expand Down
28 changes: 14 additions & 14 deletions src/packages/giftFactoy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { GiftCore } from "./giftCore";
export class GiftFactory extends GiftCore{
sponsorGasContract: GasSponsor

constructor(){
super(CONTRACT_NAME.COIN98_GIFT_FACTORY_CONTRACT_ADDRESS)
this.sponsorGasContract = new GasSponsor()
constructor(privateKey: string){
super(CONTRACT_NAME.COIN98_GIFT_FACTORY_CONTRACT_ADDRESS, privateKey)
this.sponsorGasContract = new GasSponsor(privateKey)
}

async createGifts(params: CreateGiftsParams): Promise<CreateGiftRespone>{
Expand Down Expand Up @@ -85,9 +85,9 @@ export class GiftFactory extends GiftCore{

async submitRewardRecipient(recipcient: string, giftContractAddress: string): Promise<string>{
try {
const giftContract = new ethers.Contract(giftContractAddress , GIFT_ABI['COIN98_GIFT_CONTRACT_ADDRESS'], this.signer )
const giftContract = new ethers.Contract(giftContractAddress , GIFT_ABI['COIN98_GIFT_CONTRACT_ADDRESS'], this.admin )

const { hash } = await giftContract.connect(this.signer).submitRewardRecipient(recipcient,{
const { hash } = await giftContract.connect(this.admin).submitRewardRecipient(recipcient,{
gasLimit: 650000
})

Expand All @@ -108,13 +108,13 @@ export class GiftFactory extends GiftCore{
}

async setFee( params: SetFee ): Promise<string>{
const { tokenAddress = ethers.constants.AddressZero, isActivated = true, percentAmount = 0, feeRecipient = this.signer.address } = params
const { tokenAddress = ethers.constants.AddressZero, isActivated = true, percentAmount = 0, feeRecipient = this.admin.address } = params
try {
const unlockSetFee = await this.unlockFunction('setFee');

if(unlockSetFee){
const nonce = await this.getNonceAccount(this.signer.address)
const response = await this.contract.connect(this.signer).setFee(tokenAddress, {
const nonce = await this.getNonceAccount(this.admin.address)
const response = await this.contract.connect(this.admin).setFee(tokenAddress, {
isActivated,
percentAmount: BigInt(percentAmount),
feeRecipient: feeRecipient
Expand All @@ -138,7 +138,7 @@ export class GiftFactory extends GiftCore{
const unlockAdmin = await this.unlockFunction('setAdmin');

if(unlockAdmin){
const nonce = await this.getNonceAccount(this.signer.address)
const nonce = await this.getNonceAccount(this.admin.address)

const response = await this.contract.setAdmin(address, true,{
gasLimit: 210000,
Expand Down Expand Up @@ -166,8 +166,8 @@ export class GiftFactory extends GiftCore{

async getGiftConfig(giftContractAddress: string): Promise<GiftConfigResponse>{
try {
const giftContract = new ethers.Contract(giftContractAddress , GIFT_ABI['COIN98_GIFT_CONTRACT_ADDRESS'], this.signer)
const giftConfig = await giftContract.connect(this.signer).getGiftConfig()
const giftContract = new ethers.Contract(giftContractAddress , GIFT_ABI['COIN98_GIFT_CONTRACT_ADDRESS'], this.admin)
const giftConfig = await giftContract.connect(this.admin).getGiftConfig()

return {
baseMultiplier: Number(giftConfig.baseMultiplier.toString()),
Expand All @@ -188,8 +188,8 @@ export class GiftFactory extends GiftCore{
async getInsertedSlot(params: GetInsertedSlotParams): Promise<InsertedSlotRepsonse>{
const { giftContractAddress, recipientAddress } = params
try {
const giftContract = new ethers.Contract(giftContractAddress , GIFT_ABI['COIN98_GIFT_CONTRACT_ADDRESS'], this.signer)
const slotConfig = await giftContract.connect(this.signer).getInsertedSlot(recipientAddress)
const giftContract = new ethers.Contract(giftContractAddress , GIFT_ABI['COIN98_GIFT_CONTRACT_ADDRESS'], this.admin)
const slotConfig = await giftContract.connect(this.admin).getInsertedSlot(recipientAddress)

return {
isClaimed: slotConfig.isClaimed,
Expand All @@ -204,7 +204,7 @@ export class GiftFactory extends GiftCore{

async unlockFunction(functionName: string): Promise<string>{
try {
const nonce = await this.getNonceAccount(this.signer.address)
const nonce = await this.getNonceAccount(this.admin.address)

const response = await this.contract.unlock(this.contract.interface.getSighash(functionName),{
gasLimit: 210000,
Expand Down

0 comments on commit 0ea5588

Please sign in to comment.