Skip to content

Commit

Permalink
Fix confirmation step (#757)
Browse files Browse the repository at this point in the history
* clean code

* fix getServiceFeeAmount

* history: preview format
  • Loading branch information
Thykof authored and peterjah committed Jul 16, 2024
1 parent 2b9a322 commit 1b4dea5
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/components/inputAmount/InputAmount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ export const InputAmount = (props: InputAmountProps) => {
return;
}
if (!amount) {
setAmounts(undefined, undefined);
setAmounts();
return;
}
setAmountError(undefined);

const newAmount = parseUnits(amount, decimals!);
const newAmount = parseUnits(amount, selectedToken.decimals);

const receivedAmount = massaToEvm
? getAmountToReceive(newAmount, serviceFee)
Expand Down
2 changes: 1 addition & 1 deletion src/custom/bridge/handlers/validateTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
useTokenStore,
} from '@/store/store';

export function validate(tokenBalanceEVM: any) {
export function validate(tokenBalanceEVM: bigint) {
const { inputAmount: amount, isMassaToEvm } = useOperationStore.getState();
const { selectedToken } = useTokenStore.getState();
const { setAmountError } = useGlobalStatusesStore.getState();
Expand Down
6 changes: 3 additions & 3 deletions src/pages/HistoryPage/Received.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ interface AmountProps {
export function Received(props: AmountProps) {
const { inputAmount, outputAmount, symbol = '', decimals = 9 } = props;

let outputFull = '-';
let output = '-';

if (outputAmount !== undefined && outputAmount !== null) {
const formattedResult = formatAmount(outputAmount, decimals);
outputFull = formattedResult.full;
output = formattedResult.preview;
}

const serviceFeeAmount = getServiceFeeAmount(
Expand All @@ -26,7 +26,7 @@ export function Received(props: AmountProps) {

return (
<div className="flex gap-2 items-center">
{outputFull} {symbol}
{output} {symbol}
<ServiceFeeTooltip
inputAmount={formatAmount(inputAmount, decimals).full}
serviceFeeAmount={formatAmount(serviceFeeAmount, decimals).full}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function EVMHeader() {
const isMainnet = getIsMainnet();

function handleChangeEvmNetwork(selectedEvm: SupportedEvmBlockchain) {
setAmounts(undefined, undefined);
setAmounts();
setSelectedEvm(selectedEvm);
resetSelectedToken();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function OperationLayout() {
const { serviceFee } = useServiceFee();

function handleToggleLayout() {
setAmounts(undefined, undefined);
setAmounts();

setSide(isMassaToEvm() ? SIDE.EVM_TO_MASSA : SIDE.MASSA_TO_EVM);
}
Expand Down
14 changes: 0 additions & 14 deletions src/store/operationStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,10 @@ export interface OperationStoreState {
setClaimTxId(currentTxID?: string): void;

inputAmount?: bigint;
setInputAmount(amount?: bigint): void;

outputAmount?: bigint;
setOutputAmount(amount?: bigint): void;

serviceFeeAmount?: bigint;
setServiceFeeAmount(amount?: bigint): void;

setAmounts(
inputAmount?: bigint,
Expand Down Expand Up @@ -165,19 +162,8 @@ export const useOperationStore = create<OperationStoreState>(
},

inputAmount: undefined,
setInputAmount(inputAmount?: bigint) {
set({ inputAmount });
},

outputAmount: undefined,
setOutputAmount(outputAmount?: bigint) {
set({ outputAmount });
},

serviceFeeAmount: undefined,
setServiceFeeAmount(serviceFeeAmount?: bigint) {
set({ serviceFeeAmount });
},

setAmounts(
inputAmount?: bigint,
Expand Down
13 changes: 5 additions & 8 deletions src/store/tokenStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export interface TokenStoreState {
getTokens(): IToken[];

/** Set the selected token if included in the list of the supported token on the current selected evm chain */
setSelectedToken: (token?: IToken) => void;
setSelectedToken: (token: IToken) => void;
/** Reset selected token to the first token in the list of the supported token on the current selected evm chain */
resetSelectedToken: () => void;
/** Refresh the list of supported tokens by reading the massa bridge smart contract */
Expand Down Expand Up @@ -131,18 +131,15 @@ export const useTokenStore = create<TokenStoreState>((set, get) => ({
}
},

setSelectedToken: (selectedToken?: IToken) => {
// if given undefined, set undefined
if (!selectedToken) {
set({ selectedToken });
}
setSelectedToken: (selectedToken: IToken) => {
const currentSelectedToken = get().selectedToken;
const inputAmount = useOperationStore.getState().inputAmount;
if (
inputAmount &&
currentSelectedToken?.decimals !== selectedToken?.decimals
currentSelectedToken &&
currentSelectedToken.decimals !== selectedToken?.decimals
) {
const decsDiff = selectedToken!.decimals - currentSelectedToken!.decimals;
const decsDiff = selectedToken.decimals - currentSelectedToken.decimals;
let newAmount: bigint;
if (decsDiff > 0) {
newAmount = inputAmount * 10n ** BigInt(decsDiff);
Expand Down
3 changes: 0 additions & 3 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ export function getServiceFeeAmount(
amount: bigint,
serviceFee: bigint,
): bigint {
if (serviceFee === 0n) {
return amount;
}
return (amount * serviceFee) / 10000n;
}

Expand Down

0 comments on commit 1b4dea5

Please sign in to comment.