Skip to content

Commit

Permalink
Merge pull request #9550 from hicommonwealth/9449-updating-prize-amou…
Browse files Browse the repository at this point in the history
…nts-on-contests-faster

Client side prize amounts on contests
  • Loading branch information
masvelio authored Oct 18, 2024
2 parents e834086 + fc28182 commit 8805d8c
Show file tree
Hide file tree
Showing 15 changed files with 317 additions and 129 deletions.
58 changes: 10 additions & 48 deletions libs/model/src/services/commonProtocol/contestHelper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AppError } from '@hicommonwealth/core';
import { ZERO_ADDRESS } from '@hicommonwealth/shared';
import { commonProtocol } from '@hicommonwealth/shared';
import { Mutex } from 'async-mutex';
import Web3, { PayableCallOptions } from 'web3';
import { AbiItem } from 'web3-utils';
Expand Down Expand Up @@ -259,59 +259,21 @@ export const getContestBalance = async (
oneOff?: boolean,
): Promise<number> => {
const web3 = new Web3(rpcNodeUrl);

const contestInstance = new web3.eth.Contract(
contestABI as AbiItem[],
contest,
);

const promises = [contestInstance.methods.contestToken().call()];

if (!oneOff) {
promises.push(contestInstance.methods.FeeMangerAddress().call());
}

const results = await Promise.all(promises);

const balancePromises: Promise<number>[] = [];

if (!oneOff) {
const feeManager = new web3.eth.Contract(
feeManagerABI as AbiItem[],
String(results[1]),
);
balancePromises.push(
feeManager.methods.getBeneficiaryBalance(contest, results[0]).call(),
);
}
if (String(results[0]) === ZERO_ADDRESS) {
balancePromises.push(
web3.eth.getBalance(contest).then((v) => {
return Number(v);
}),
);
} else {
const calldata =
'0x70a08231' +
web3.eth.abi.encodeParameters(['address'], [contest]).substring(2);
balancePromises.push(
web3.eth
.call({
to: String(results[0]),
data: calldata,
})
.then((v) => {
return Number(web3.eth.abi.decodeParameter('uint256', v));
}),
);
}

const balanceResults = await Promise.all(balancePromises);

return Number(
balanceResults.length === 2
? BigInt(balanceResults[0]) + BigInt(balanceResults[1])
: BigInt(balanceResults[0]),
const balance = await commonProtocol.getTotalContestBalance(
contestInstance,
contest,
web3,
feeManagerABI,
oneOff,
);

return balance;
};

export const addContentBatch = async (
Expand Down
60 changes: 60 additions & 0 deletions libs/shared/src/commonProtocol/contractHelpers/Contest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { ZERO_ADDRESS } from '@hicommonwealth/shared';

export const getTotalContestBalance = async (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
contestContract: any,
contestAddress: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
web3: any,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
feeManagerAbi: any[],
oneOff?: boolean,
): Promise<number> => {
const promises = [contestContract.methods.contestToken().call()];

if (!oneOff) {
promises.push(contestContract.methods.FeeMangerAddress().call());
}

const results = await Promise.all(promises);

const balancePromises: Promise<number>[] = [];

if (!oneOff) {
const feeManager = new web3.eth.Contract(feeManagerAbi, String(results[1]));
balancePromises.push(
feeManager.methods
.getBeneficiaryBalance(contestAddress, results[0])
.call(),
);
}
if (String(results[0]) === ZERO_ADDRESS) {
balancePromises.push(
web3.eth.getBalance(contestAddress).then((v: bigint) => {
return Number(v);
}),
);
} else {
const calldata =
'0x70a08231' +
web3.eth.abi.encodeParameters(['address'], [contestAddress]).substring(2);
balancePromises.push(
web3.eth
.call({
to: String(results[0]),
data: calldata,
})
.then((v: string) => {
return Number(web3.eth.abi.decodeParameter('uint256', v));
}),
);
}

const balanceResults = await Promise.all(balancePromises);

return Number(
balanceResults.length === 2
? BigInt(balanceResults[0]) + BigInt(balanceResults[1])
: BigInt(balanceResults[0]),
);
};
1 change: 1 addition & 0 deletions libs/shared/src/commonProtocol/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './chainConfig';
export * from './contractHelpers/Contest';
export * from './contractHelpers/Launchpad';
export * from './utils';
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,162 @@ export const ContestAbi = [
stateMutability: 'nonpayable',
type: 'function',
},
{
inputs: [],
stateMutability: 'view',
type: 'function',
name: 'endTime',
outputs: [
{
internalType: 'uint256',
name: '',
type: 'uint256',
},
],
},
{
inputs: [],
stateMutability: 'view',
type: 'function',
name: 'startTime',
outputs: [
{
internalType: 'uint256',
name: '',
type: 'uint256',
},
],
},
{
inputs: [],
name: 'contestInterval',
outputs: [
{
internalType: 'uint256',
name: '',
type: 'uint256',
},
],
stateMutability: 'view',
type: 'function',
},
{
inputs: [],
name: 'currentContentId',
outputs: [
{
internalType: 'uint256',
name: '',
type: 'uint256',
},
],
stateMutability: 'view',
type: 'function',
},
{
inputs: [],
name: 'getWinnerIds',
outputs: [
{
internalType: 'uint256[]',
name: '',
type: 'uint256[]',
},
],
stateMutability: 'nonpayable',
type: 'function',
},
{
inputs: [
{
internalType: 'uint256',
name: 'contestId',
type: 'uint256',
},
],
name: 'getPastWinners',
outputs: [
{
internalType: 'uint256[]',
name: '',
type: 'uint256[]',
},
],
stateMutability: 'nonpayable',
type: 'function',
},
{
inputs: [
{
internalType: 'uint256',
name: '',
type: 'uint256',
},
],
stateMutability: 'view',
type: 'function',
name: 'content',
outputs: [
{
internalType: 'string',
name: 'url',
type: 'string',
},
{
internalType: 'uint256',
name: 'cumulativeVotes',
type: 'uint256',
},
{
internalType: 'address',
name: 'creator',
type: 'address',
},
{
internalType: 'bool',
name: 'completed',
type: 'bool',
},
],
},
{
inputs: [],
name: 'FeeMangerAddress',
outputs: [
{
internalType: 'address',
name: '',
type: 'address',
},
],
stateMutability: 'view',
type: 'function',
},
{
inputs: [],
name: 'contestLength',
outputs: [
{
internalType: 'uint256',
name: '',
type: 'uint256',
},
],
stateMutability: 'view',
type: 'function',
},
{
inputs: [],
name: 'newContest',
outputs: [],
stateMutability: 'nonpayable',
type: 'function',
},
{
inputs: [],
name: 'endContest',
outputs: [],
stateMutability: 'nonpayable',
type: 'function',
},
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export const feeManagerABI = [
{
inputs: [
{
internalType: 'address',
name: 'beneficiary',
type: 'address',
},
{
internalType: 'address',
name: 'token',
type: 'address',
},
],
name: 'getBeneficiaryBalance',
outputs: [
{
internalType: 'uint256',
name: '',
type: 'uint256',
},
],
stateMutability: 'view',
type: 'function',
},
];
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ZERO_ADDRESS } from '@hicommonwealth/shared';
import { ZERO_ADDRESS, commonProtocol } from '@hicommonwealth/shared';
import { AbiItem, TransactionReceipt } from 'web3';
import { ContestAbi } from './Abi/ContestAbi';
import { Erc20Abi } from './Abi/ERC20Abi';
import { feeManagerABI } from './Abi/feeManagerAbi';
import ContractBase from './ContractBase';
import NamespaceFactory from './NamespaceFactory';

Expand Down Expand Up @@ -230,27 +231,20 @@ class Contest extends ContractBase {
}
}

async getContestBalance(): Promise<number> {
//Indicate if contest is not recurring
async getContestBalance(oneOff: boolean): Promise<number> {
if (!this.initialized || !this.walletEnabled) {
await this.initialize(false);
}
this.reInitContract();
const tokenAddress = await this.contract.methods.contestToken().call();
if (tokenAddress === ZERO_ADDRESS) {
const balance = await this.web3.eth.getBalance(this.contractAddress);
return parseFloat(this.web3.utils.fromWei(balance, 'ether'));
} else {
const calldata =
'0x70a08231' +
this.web3.eth.abi
.encodeParameters(['address'], [this.contractAddress])
.substring(2);
const returnData = await this.web3.eth.call({
to: tokenAddress,
data: calldata,
});
return Number(this.web3.eth.abi.decodeParameter('uint256', returnData));
}
const contestBalance = await commonProtocol.getTotalContestBalance(
this.contract,
this.contractAddress,
this.web3,
feeManagerABI,
oneOff,
);
return contestBalance;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ const useBuyStakeMutation = ({
queryKey: [ContractMethods.GET_FEE_MANAGER_BALANCE],
});

await queryClient.invalidateQueries({
queryKey: [ContractMethods.GET_CONTEST_BALANCE],
});

await queryClient.invalidateQueries({
queryKey: [ContractMethods.GET_CONTEST_BALANCE],
});

if (shouldUpdateActiveAddress) {
await setActiveAccountOnTransactionSuccess(variables.walletAddress);
}
Expand Down
Loading

0 comments on commit 8805d8c

Please sign in to comment.