Skip to content

Commit

Permalink
handle min value in deployment deposit
Browse files Browse the repository at this point in the history
  • Loading branch information
baktun14 committed Mar 1, 2024
1 parent 4d964f4 commit d158ca6
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 17 deletions.
1 change: 1 addition & 0 deletions deploy-web/src/components/deployment/DeploymentListRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ export const DeploymentListRow: React.FunctionComponent<Props> = ({ deployment,
{isActive && isDepositingDeployment && (
<DeploymentDepositModal
denom={deployment.escrowAccount.balance.denom}
disableMin
handleCancel={() => setIsDepositingDeployment(false)}
onDeploymentDeposit={onDeploymentDeposit}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,22 @@ import { GranteeDepositMenuItem } from "./GranteeDepositMenuItem";

type Props = {
infoText?: string | ReactNode;
min?: number;
disableMin?: boolean;
denom: string;
onDeploymentDeposit: (deposit: number, depositorAddress: string) => void;
handleCancel: () => void;
children?: ReactNode;
};

export const DeploymentDepositModal: React.FunctionComponent<Props> = ({ handleCancel, onDeploymentDeposit, denom, min = 0, infoText = null }) => {
export const DeploymentDepositModal: React.FunctionComponent<Props> = ({ handleCancel, onDeploymentDeposit, disableMin, denom, infoText = null }) => {
const formRef = useRef(null);
const { settings } = useSettings();
const { enqueueSnackbar } = useSnackbar();
const [error, setError] = useState("");
const [isCheckingDepositor, setIsCheckingDepositor] = useState(false);
const { walletBalances, address } = useWallet();
const { data: granteeGrants } = useGranteeGrants(address);
const depositData = useDenomData(denom);
const {
handleSubmit,
control,
Expand All @@ -45,15 +46,20 @@ export const DeploymentDepositModal: React.FunctionComponent<Props> = ({ handleC
unregister
} = useForm({
defaultValues: {
amount: min,
amount: 0,
useDepositor: false,
depositorAddress: ""
}
});
const { amount, useDepositor, depositorAddress } = watch();
const depositData = useDenomData(denom);
const usdcIbcDenom = useUsdcDenom();

useEffect(() => {
if (depositData && amount === 0 && !disableMin) {
setValue("amount", depositData?.min || 0);
}
}, [depositData]);

useEffect(() => {
clearErrors();
setError("");
Expand Down Expand Up @@ -127,8 +133,8 @@ export const DeploymentDepositModal: React.FunctionComponent<Props> = ({ handleC
clearErrors();
const deposit = denomToUdenom(amount);

if (deposit < denomToUdenom(min)) {
setError(`Deposit amount must be greater or equal than ${min}.`);
if (!disableMin && deposit < depositData?.min) {
setError(`Deposit amount must be greater or equal than ${depositData?.min}.`);
return;
}

Expand Down Expand Up @@ -209,7 +215,7 @@ export const DeploymentDepositModal: React.FunctionComponent<Props> = ({ handleC
autoFocus
error={!!fieldState.error}
helperText={fieldState.error && helperText}
inputProps={{ min: min, step: 0.000001, max: depositData?.inputMax }}
inputProps={{ min: (!disableMin && depositData?.min) || 0, step: 0.000001, max: depositData?.inputMax }}
InputProps={{
startAdornment: <InputAdornment position="start">{depositData?.label}</InputAdornment>
}}
Expand Down Expand Up @@ -265,4 +271,3 @@ export const DeploymentDepositModal: React.FunctionComponent<Props> = ({ handleC
</Popup>
);
};

Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ export const DeploymentDetailTopBar: React.FunctionComponent<Props> = ({ address
{isDepositingDeployment && (
<DeploymentDepositModal
denom={deployment.escrowAccount.balance.denom}
disableMin
handleCancel={() => setIsDepositingDeployment(false)}
onDeploymentDeposit={onDeploymentDeposit}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,6 @@ export const ManifestEdit: React.FunctionComponent<Props> = ({ editedManifest, s
<DeploymentDepositModal
handleCancel={() => setIsDepositingDeployment(false)}
onDeploymentDeposit={onDeploymentDeposit}
min={5} // TODO Query from chain params
denom={sdlDenom}
infoText={
<Alert severity="info" className={classes.alert} variant="outlined">
Expand Down
1 change: 0 additions & 1 deletion deploy-web/src/components/sdl/RentGpusForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ export const RentGpusForm: React.FunctionComponent<Props> = ({}) => {
<DeploymentDepositModal
handleCancel={() => setIsDepositingDeployment(false)}
onDeploymentDeposit={onDeploymentDeposit}
min={5} // TODO Query from chain params
denom={currentService?.placement?.pricing?.denom || sdlDenom}
infoText={
<Alert severity="info" sx={{ marginBottom: "1rem" }} variant="outlined">
Expand Down
10 changes: 7 additions & 3 deletions deploy-web/src/hooks/useWalletBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ export const useDenomData = (denom: string) => {
const { walletBalances } = useWallet();
const [depositData, setDepositData] = useState<DenomData>(null);
const usdcIbcDenom = useUsdcDenom();
const { data: depositParams } = useDepositParams();
const { data: depositParams, refetch: getDepositParams } = useDepositParams({ enabled: false });

useEffect(() => {
getDepositParams();
}, []);

useEffect(() => {
if (isLoaded && walletBalances && depositParams) {
Expand All @@ -46,7 +50,7 @@ export const useDenomData = (denom: string) => {
case uAktDenom:
params = depositParams.find(p => p.denom === uAktDenom);
depositData = {
min: parseInt(params?.amount || 0),
min: uaktToAKT(parseInt(params?.amount || 0)),
label: "AKT",
balance: uaktToAKT(walletBalances.uakt, 6),
inputMax: uaktToAKT(Math.max(walletBalances.uakt - txFeeBuffer, 0), 6)
Expand All @@ -55,7 +59,7 @@ export const useDenomData = (denom: string) => {
case usdcIbcDenom:
params = depositParams.find(p => p.denom === usdcIbcDenom);
depositData = {
min: parseInt(params?.amount || 0),
min: udenomToDenom(parseInt(params?.amount || 0)),
label: "USDC",
balance: udenomToDenom(walletBalances.usdc, 6),
inputMax: udenomToDenom(Math.max(walletBalances.usdc - txFeeBuffer, 0), 6)
Expand Down
4 changes: 1 addition & 3 deletions deploy-web/src/queries/useSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ export function useSaveSettings() {
async function getDepositParams(apiEndpoint: string) {
const depositParamsQuery = await axios.get(ApiUrlService.depositParams(apiEndpoint));
const depositParams = depositParamsQuery.data as RpcDeposiParams;
const params = JSON.parse(depositParams.params.value) as DepositParams[];

console.log(depositParams, params);
const params = JSON.parse(depositParams.param.value) as DepositParams[];

return params;
}
Expand Down
2 changes: 1 addition & 1 deletion deploy-web/src/types/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ export interface BidDto {
}

export interface RpcDeposiParams {
params: {
param: {
subspace: string;
key: string;
// Array of { denom: string, amount: string }
Expand Down

0 comments on commit d158ca6

Please sign in to comment.