Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sonic validator #1594

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions src/api/treasury/getTreasury.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@ import { ApiChain, ApiChains } from '../../utils/chain';
import { getTokensForChain, isTokenNative } from '../tokens/tokens';
import { getAmmPrice } from '../stats/getAmmPrices';
import { keysToObject } from '../../utils/array';
import {
getChainConcentratedLiquidityAssets,
hasChainConcentratedLiquidityAssets,
} from './nftAssets';
import { getChainConcentratedLiquidityAssets, hasChainConcentratedLiquidityAssets } from './nftAssets';
import { ZERO_ADDRESS } from '../../utils/address';

const REFRESH_INTERVAL = 60000 * 10;
const REFRESH_INTERVAL = 6000 * 10;

// treasury addresses that should be queried for balances
let treasuryAddressesByChain: TreasuryWalletRegistry;
Expand Down Expand Up @@ -297,11 +294,7 @@ async function buildTreasuryReportForChain(chain: ApiChain): Promise<TreasuryRep
return balanceReport;
}

function findUsdValueForBalance(
tokenInfo: TreasuryAsset,
tokenPrice: number,
balance: BigNumber
): BigNumber {
function findUsdValueForBalance(tokenInfo: TreasuryAsset, tokenPrice: number, balance: BigNumber): BigNumber {
if (isVaultAsset(tokenInfo)) {
return balance
.multipliedBy(tokenPrice)
Expand Down
35 changes: 4 additions & 31 deletions src/api/treasury/multicallUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,7 @@ import { fetchContract } from '../rpc/client';
import ERC20Abi from '../../abis/ERC20Abi';
import { MULTICALL_V3 } from '../../utils/web3Helpers';
import MulticallAbi from '../../abis/common/Multicall/MulticallAbi';

const validatorContractAbi = [
{
constant: true,
inputs: [],
name: 'balance',
outputs: [
{
name: '',
type: 'uint256',
},
],
payable: false,
stateMutability: 'view',
type: 'function',
},
] as const;
import { fetchAPIBalance, fetchSonicValidatorBalance, isSonicValidator } from './validatorHelpers';

export const mapAssetToCall = (
asset: TreasuryAsset,
Expand All @@ -53,25 +37,14 @@ export const mapAssetToCall = (
} else if (isConcLiquidityAsset(asset)) {
return [getLpBreakdownForOracle(asset.oracleId)];
} else if (isValidatorAsset(asset)) {
if (asset.method === 'contract') {
const contract = fetchContract(asset.methodPath, validatorContractAbi, chainId);
return [contract.read.balance()];
if (isSonicValidator(asset)) {
return [fetchSonicValidatorBalance(asset, chainId)];
} else {
return [fetchAPIBalance(asset as ValidatorAsset)];
}
}
};

export const fetchAPIBalance = async (apiAsset: ValidatorAsset): Promise<TreasuryApiResult> => {
let balance: number = await fetch(apiAsset.methodPath)
.then(res => res.json())
.then((res: any) => ((res.data?.length ?? 0) > 0 ? res.data[0].balance : res.data.balance));
return {
apiAsset,
balance: new BigNumber(balance).shiftedBy(9),
};
};

export const extractBalancesFromTreasuryCallResults = (
apiAssets: TreasuryAsset[],
treasuryAddresses: string[],
Expand All @@ -94,7 +67,7 @@ export const extractBalancesFromTreasuryCallResults = (
});
allBalances.push(bal);
} else if (isValidatorAsset(asset)) {
if (asset.method === 'contract') {
if (asset.method === 'sonic-contract') {
const value = callResult.value as bigint[];
allBalances.push({
address: asset.id,
Expand Down
3 changes: 2 additions & 1 deletion src/api/treasury/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ export type NativeAsset = Asset & {
export type ValidatorAsset = Asset & {
id: string;
assetType: 'validator';
method: 'api' | 'contract';
method: 'api' | 'sonic-contract';
methodPath: string;
numberId?: number;
helper?: string;
};

export type ConcLiquidityAsset = Asset & {
Expand Down
69 changes: 61 additions & 8 deletions src/api/treasury/validatorHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { ValidatorAsset } from './types';
import { TreasuryApiResult, ValidatorAsset } from './types';
import { ApiChain } from '../../utils/chain';
import { fetchContract } from '../rpc/client';
import { ChainId } from '../../../packages/address-book/src/address-book';
import BigNumber from 'bignumber.js';

const validatorsByChain: Partial<Record<ApiChain, ValidatorAsset[]>> = {
ethereum: [
Expand Down Expand Up @@ -134,22 +137,72 @@ const validatorsByChain: Partial<Record<ApiChain, ValidatorAsset[]>> = {
assetType: 'validator',
},
],
fuse: [
sonic: [
{
id: 'fuse-validator',
name: 'Fuse Validator',
id: 'sonic-validator',
numberId: 31,
name: 'SONIC Validator',
address: 'native',
oracleId: 'FUSE',
oracleId: 'wS',
oracleType: 'tokens',
decimals: 18,
symbol: 'FUSE',
method: 'contract',
methodPath: '0xa852A119a29d44e13A4B939B482D522808437BAe',
symbol: 'S',
method: 'sonic-contract',
methodPath: '0x10E13f11419165beB0F456eC8a230899E4013BBD',
assetType: 'validator',
helper: '0xFC00FACE00000000000000000000000000000000',
},
],
};

export const hasChainValidator = (chain: ApiChain): boolean => !!validatorsByChain[chain];

export const getChainValidators = (chain: ApiChain): ValidatorAsset[] => validatorsByChain[chain];

type SonicValidator = Required<ValidatorAsset>;

export function isSonicValidator(asset: ValidatorAsset): asset is SonicValidator {
return asset.method === 'sonic-contract';
}

const sonicValidatorContractAbi = [
{
inputs: [{ internalType: 'uint256', name: 'validatorID', type: 'uint256' }],
name: 'getSelfStake',
outputs: [{ name: '', type: 'uint256' }],
payable: false,
stateMutability: 'view',
type: 'function',
},
{
inputs: [
{ internalType: 'address', name: 'delegator', type: 'address' },
{ internalType: 'uint256', name: 'toValidatorID', type: 'uint256' },
],
name: 'pendingRewards',
outputs: [{ name: '', type: 'uint256' }],
payable: false,
stateMutability: 'view',
type: 'function',
},
] as const;

export const fetchSonicValidatorBalance = async (asset: SonicValidator, chainId: number): Promise<bigint> => {
const contract = fetchContract(asset.helper, sonicValidatorContractAbi, chainId);
const [selfStaked, pending] = await Promise.all([
contract.read.getSelfStake([BigInt(asset.numberId)]),
contract.read.pendingRewards([asset.methodPath as `0x${string}`, BigInt(asset.numberId)]),
]);

return selfStaked + pending;
};

export const fetchAPIBalance = async (apiAsset: ValidatorAsset): Promise<TreasuryApiResult> => {
let balance: number = await fetch(apiAsset.methodPath)
.then(res => res.json())
.then((res: any) => ((res.data?.length ?? 0) > 0 ? res.data[0].balance : res.data.balance));
return {
apiAsset,
balance: new BigNumber(balance).shiftedBy(9),
};
};
20 changes: 0 additions & 20 deletions src/api/validators/fetchFuseValidatorData.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/api/validators/validatorStructure.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import { fetchEthValidatorTotalPerformance } from './fetchEthValidatorData';
import { fetchFtmValidatorTotalPerformance } from './fetchFtmValidatorData';
import { fetchFuseValidatorTotalPerformance } from './fetchFuseValidatorData';

export const validatorStructure = {
eth: {
validatorFunctionName: fetchEthValidatorTotalPerformance,
},
ftm: {
validatorFunctionName: fetchFtmValidatorTotalPerformance,
},
fuse: {
validatorFunctionName: fetchFuseValidatorTotalPerformance,
},
};