Skip to content

feat: usdc price #623

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

Open
wants to merge 12 commits into
base: main
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
6 changes: 3 additions & 3 deletions apps/indexer-admin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
"@patternfly/react-log-viewer": "^4.43.19",
"@rainbow-me/rainbowkit": "^1.2.0",
"@subql/components": "3.0.1-3",
"@subql/contract-sdk": "1.3.0",
"@subql/contract-sdk": "1.7.0",
"@subql/network-clients": "1.1.0",
"@subql/network-config": "1.1.1",
"@subql/network-config": "1.1.2-3",
"@subql/network-query": "1.1.0",
"@subql/react-hooks": "1.1.0",
"@subql/react-hooks": "1.1.2-19",
"ahooks": "^3.7.7",
"animate.css": "^4.1.1",
"antd": "^5.3.2",
Expand Down
Binary file added apps/indexer-admin/public/images/sqt.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 5 additions & 4 deletions apps/indexer-admin/src/hooks/paygHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { BigNumber } from 'ethers';
import { useContractSDK } from 'containers/contractSdk';
import { useCoordinatorIndexer } from 'containers/coordinatorIndexer';
import { PAYG_PRICE } from 'utils/queries';
import { network } from 'utils/web3';
import { network, SUPPORTED_NETWORK } from 'utils/web3';

import { useProjectDetails } from './projectHook';

Expand Down Expand Up @@ -42,17 +42,18 @@ export function usePAYGConfig(deploymentId: string) {
useDefault: true,
};
}
const usdcDecimal = STABLE_COIN_DECIMAL[SUPPORTED_NETWORK];

return {
paygPrice:
payg.token === sdk?.sqToken.address
? formatEther(BigNumber.from(payg.price).mul(1000))
: formatUnits(BigNumber.from(payg.price).mul(1000), +STABLE_COIN_DECIMAL),
: formatUnits(BigNumber.from(payg.price).mul(1000), usdcDecimal),
paygRatio: payg.priceRatio || 80,
paygMinPrice:
payg.token === sdk?.sqToken.address
? formatEther(BigNumber.from(payg.minPrice).mul(1000))
: formatUnits(BigNumber.from(payg.minPrice).mul(1000), +STABLE_COIN_DECIMAL),
: formatUnits(BigNumber.from(payg.minPrice).mul(1000), usdcDecimal),
paygExpiration: (payg.expiration ?? 0) / daySeconds,
token: payg.token,
useDefault: payg.useDefault,
Expand All @@ -73,7 +74,7 @@ export function usePAYGConfig(deploymentId: string) {
const paygPrice =
token === sdk?.sqToken.address
? parseEther(minPrice)
: parseUnits(minPrice, +import.meta.env.VITE_STABLE_TOKEN_DECIMAL);
: parseUnits(minPrice, STABLE_COIN_DECIMAL[SUPPORTED_NETWORK]);

await paygPriceRequest({
variables: {
Expand Down
86 changes: 61 additions & 25 deletions apps/indexer-admin/src/pages/global-config/GlobalConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import { BsBoxArrowInUpRight } from 'react-icons/bs';
import InfoCircleOutlined from '@ant-design/icons/InfoCircleOutlined';
import { useMutation, useQuery } from '@apollo/client';
import { openNotification, Spinner, Tag, Typography } from '@subql/components';
import { formatSQT, useAsyncMemo } from '@subql/react-hooks';
import { SQT_DECIMAL, STABLE_COIN_DECIMAL } from '@subql/network-config';
import { formatSQT, useAsyncMemo, useStableCoin } from '@subql/react-hooks';
import { useUpdate } from 'ahooks';
import { Button, Form, Input, Select, Switch, Tooltip } from 'antd';
import { useForm } from 'antd/es/form/Form';
import BigNumberJs from 'bignumber.js';
import { parseEther } from 'ethers/lib/utils';
import { STABLE_COIN_ADDRESS } from 'conf/stableCoin';
import { formatUnits, parseEther, parseUnits } from 'ethers/lib/utils';
import styled from 'styled-components';
import { SubqlInput } from 'styles/input';

Expand All @@ -23,7 +26,7 @@ import {
getIndexerSocialCredibility,
SET_CONFIG,
} from 'utils/queries';
import { TOKEN_SYMBOL } from 'utils/web3';
import { SUPPORTED_NETWORK, TOKEN_SYMBOL } from 'utils/web3';

import NodeOperatorInformation from './components/NodeOperatorInforamtion';

Expand All @@ -41,11 +44,13 @@ const Wrapper = styled.div`
const GlobalConfig: FC = () => {
const { indexer: account, loading } = useCoordinatorIndexer();

const sdk = useContractSDK();
const { transPrice } = useStableCoin(sdk, SUPPORTED_NETWORK);
const update = useUpdate();
const [config, setConfig] = useState({
autoReduceOverAllocation: false,
flexEnabled: false,
});
const sdk = useContractSDK();

const configQueryData = useQuery<AllConfig>(GET_ALL_CONFIG);
const [setConfigMutation] = useMutation(SET_CONFIG);
Expand Down Expand Up @@ -79,13 +84,26 @@ const GlobalConfig: FC = () => {
const validPeriod = configQueryData.data.allConfig.find(
(i) => i.key === ConfigKey.FlexValidPeriod
);
const tokenAddress = configQueryData.data.allConfig.find(
(i) => i.key === ConfigKey.FlexTokenAddress
);

const isSQT = tokenAddress?.value === sdk?.sqToken.address || !tokenAddress?.value;

form.setFieldValue('priceRatio', BigNumberJs(priceRatio?.value || '0'));
form.setFieldValue('price', BigNumberJs(formatSQT(price?.value || '0')).multipliedBy(1000));
form.setFieldValue('flexTokenAddress', tokenAddress?.value || sdk?.sqToken.address);
form.setFieldValue(
'price',
BigNumberJs(
isSQT
? formatSQT(price?.value || '0')
: formatUnits(price?.value || '0', STABLE_COIN_DECIMAL[SUPPORTED_NETWORK])
).multipliedBy(1000)
);
form.setFieldValue('validPeriod', BigNumberJs(validPeriod?.value || '0').dividedBy(86400));
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [configQueryData.data, form]);
}, [configQueryData.data, form, sdk]);

return (
<div
Expand Down Expand Up @@ -231,6 +249,7 @@ const GlobalConfig: FC = () => {
/>
</Tooltip>
</SubqlInput>

<SubqlInput style={{ display: 'flex', gap: 4 }}>
<Form.Item
label={
Expand Down Expand Up @@ -268,29 +287,38 @@ const GlobalConfig: FC = () => {
type="number"
addonAfter={
<Select
value={sdk?.sqToken?.address}
// onChange={(e) => {}}
value={form.getFieldValue('flexTokenAddress')}
onChange={(e) => {
form.setFieldValue('flexTokenAddress', e);
update();
}}
options={[
// {
// value: STABLE_COIN_ADDRESS,
// label: (
// <div style={{ display: 'flex', alignItems: 'center' }}>
// <img
// style={{ width: 24, height: 24, marginRight: 8 }}
// src="/images/usdc.png"
// alt=""
// />
// <Typography>USDC</Typography>
// </div>
// ),
// },
{
value: sdk?.sqToken?.address,
value: STABLE_COIN_ADDRESS,
label: (
<div style={{ display: 'flex', alignItems: 'center' }}>
<img
style={{ width: 24, height: 24, marginRight: 8 }}
src="/images/ksqt.svg"
src="/images/usdc.png"
alt=""
/>
<Typography>USDC</Typography>
</div>
),
},
{
value: sdk?.sqToken?.address,
label: (
<div style={{ display: 'flex', alignItems: 'center' }}>
<img
style={{
width: 24,
height: 24,
marginRight: 8,
borderRadius: '50%',
overflow: 'hidden',
}}
src="/images/sqt.png"
alt=""
/>
<Typography>{TOKEN_SYMBOL}</Typography>
Expand Down Expand Up @@ -366,13 +394,15 @@ const GlobalConfig: FC = () => {
onClick={async () => {
await form.validateFields();
try {
const isSQT = form.getFieldValue('flexTokenAddress') === sdk?.sqToken.address;
await setConfigMutation({
variables: {
key: ConfigKey.FlexPrice,
value: parseEther(
value: parseUnits(
BigNumberJs(form.getFieldValue('price') || 1)
.dividedBy(1000)
.toString()
.toString(),
isSQT ? SQT_DECIMAL : STABLE_COIN_DECIMAL[SUPPORTED_NETWORK]
).toString(),
},
});
Expand All @@ -390,6 +420,12 @@ const GlobalConfig: FC = () => {
value: BigNumberJs(form.getFieldValue('priceRatio') || 80),
},
});
await setConfigMutation({
variables: {
key: ConfigKey.FlexTokenAddress,
value: form.getFieldValue('flexTokenAddress'),
},
});
openNotification({
type: 'success',
title: 'Config Updated',
Expand Down
Loading
Loading