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

Support max input #24

Merged
merged 2 commits into from
May 8, 2024
Merged
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
2 changes: 1 addition & 1 deletion src/components/asset-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default function AssetSelect({ value, options, disabled, onChange = () =>
}
disabled={disabled}
sameWidth
labelClassName="flex items-center gap-middle shrink-0 w-32 justify-between bg-component h-full border-radius px-1 hover:opacity-80 transition-[transform,color] disabled:translate-y-0 disabled:opacity-100 disabled:cursor-not-allowed"
labelClassName="flex items-center gap-middle shrink-0 w-32 justify-between bg-component h-full rounded-r-2xl px-1 hover:opacity-80 transition-[transform,color] disabled:translate-y-0 disabled:opacity-100 disabled:cursor-not-allowed"
childClassName="flex flex-col py-small bg-component border-primary border border-radius gap-[1px]"
>
{options?.length ? (
Expand Down
34 changes: 31 additions & 3 deletions src/components/balance-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BN, BN_ZERO, bnToBn } from "@polkadot/util";
import AssetSelect from "./asset-select";
import { ChangeEventHandler, useCallback, useEffect, useMemo, useRef, useState } from "react";
import { formatBalance } from "@/utils";
import { parseUnits } from "viem";
import { formatUnits, parseUnits } from "viem";
import { InputAlert } from "./input-alert";

interface Value {
Expand All @@ -19,7 +19,7 @@ interface Props {
placeholder?: string;
balance?: BN;
cross?: Cross;
asset?: Asset;
asset: Asset;
assetSupply?: BN;
assetLimit?: BN;
assetOptions?: Asset[];
Expand Down Expand Up @@ -119,7 +119,35 @@ export default function BalanceInput({
value={value?.input}
onChange={handleInputChange}
/>
{asset ? <AssetSelect disabled={disabled} value={asset} options={assetOptions} onChange={onAssetChange} /> : null}
<div className="flex h-full items-center gap-[2px]">
<button
className="h-full rounded-l-2xl bg-component px-2 transition-opacity hover:opacity-80 disabled:cursor-not-allowed disabled:opacity-100"
onClick={() => {
if (balance && assetSupply) {
if (assetLimit && assetSupply.gte(assetLimit)) {
onChange({ valid: !(min && min.gt(BN_ZERO)), input: "0", amount: BN_ZERO });
} else if (assetLimit) {
const remaining = assetLimit.sub(assetSupply);
const amount = remaining.lte(balance) ? remaining : balance;
const input = formatUnits(BigInt(amount.toString()), asset.decimals);
onChange({ valid: !(min && min.gt(amount)), input, amount });
} else {
onChange({
amount: balance,
valid: !(min && min.gt(balance)),
input: formatUnits(BigInt(balance.toString()), asset.decimals),
});
}
} else {
onChange({ valid: !(min && min.gt(BN_ZERO)), input: "0", amount: BN_ZERO });
}
}}
disabled={disabled}
>
Max
</button>
<AssetSelect disabled={disabled} value={asset} options={assetOptions} onChange={onAssetChange} />
</div>

{/* Alert */}
{requireLimit ? (
Expand Down
1 change: 1 addition & 0 deletions src/config/chains/darwinia-chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export const darwiniaChain: ChainConfig = {
],
wallets: [WalletID.RAINBOW, WalletID.TALISMAN],
addressType: "evm",
hasAssetLimit: true,

/**
* Substrate
Expand Down
Loading