Skip to content

Commit

Permalink
Merge pull request #535 from onflow:534-feature-restrict-token-decima…
Browse files Browse the repository at this point in the history
…ls-on-bridge-to-cadence

Now allows 18 decimal places for EVM - EVM flow transfers. Restricts transfers to Cadence to 8 decimal places
  • Loading branch information
tombeckenham authored Feb 18, 2025
2 parents 93f1ffd + ac479d4 commit 78e1e02
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/background/service/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,8 @@ const defaultFlowToken = {
},
logoURI:
'https://cdn.jsdelivr.net/gh/FlowFans/flow-token-list@main/token-registry/A.1654653399040a61.FlowToken/logo.svg',
decimals: 8,
// On Evm networks we can use up to 18 decimals
decimals: 18,
symbol: 'flow',
};

Expand Down
68 changes: 66 additions & 2 deletions src/ui/reducers/__tests__/transaction-reducer.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { type TokenInfo } from 'flow-native-token-registry';
import { describe, expect, it } from 'vitest';

import { type Contact } from '@/shared/types/network-types';
import {
type NetworkType,
type TokenType,
type TransactionStateString,
type TransactionState,
} from '@/shared/types/transaction-types';
import { type CoinItem, type WalletAddress } from '@/shared/types/wallet-types';

Expand Down Expand Up @@ -316,6 +315,71 @@ describe('Transaction Reducer', () => {
// Should preserve trailing zeros for precision in crypto transactions
expect(newState.amount).toBe('100.100000');
});

describe('network decimals handling', () => {
it('should handle EVM to EVM transfers with up to 18 decimals', () => {
const stateWithEvmNetworks: TransactionState = {
...stateWithBalance,
fromNetwork: 'Evm' as NetworkType,
toNetwork: 'Evm' as NetworkType,
selectedToken: {
...stateWithBalance.selectedToken,
decimals: 18,
},
fiatOrCoin: 'coin',
};

const action = {
type: 'setAmount' as const,
payload: '1.123456789012345678', // 18 decimals
};

const newState = transactionReducer(stateWithEvmNetworks, action);
expect(newState.amount).toBe('1.123456789012345678');
});

it('should limit non-EVM transfers to 8 decimals', () => {
const stateWithMixedNetworks: TransactionState = {
...stateWithBalance,
fromNetwork: 'Evm' as NetworkType,
toNetwork: 'Cadence' as NetworkType,
selectedToken: {
...stateWithBalance.selectedToken,
decimals: 18,
},
fiatOrCoin: 'coin',
};

const action = {
type: 'setAmount' as const,
payload: '1.123456789012345678', // 18 decimals
};

const newState = transactionReducer(stateWithMixedNetworks, action);
expect(newState.amount).toBe('1.12345678'); // Should be truncated to 8 decimals
});

it('should respect token decimals even if less than network maximum', () => {
const stateWithLowDecimalToken: TransactionState = {
...stateWithBalance,
fromNetwork: 'Evm' as NetworkType,
toNetwork: 'Evm' as NetworkType,
selectedToken: {
...stateWithBalance.selectedToken,
decimals: 6,
},
fiatOrCoin: 'coin',
};

const action = {
type: 'setAmount' as const,
payload: '1.123456789012345678', // 18 decimals
};

const newState = transactionReducer(stateWithLowDecimalToken, action);
expect(newState.amount).toBe('1.123456'); // Should be truncated to 6 decimals
});
});
});

describe('setAmountToMax', () => {
Expand Down
9 changes: 8 additions & 1 deletion src/ui/reducers/transaction-reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,15 @@ export const transactionReducer = (
// Calculate the remaining balance after the transaction
remainingBalance = balance.minus(new BN(amountInCoin));
} else if (state.fiatOrCoin === 'coin') {
// Should limit non-evm networks to 8 decimals
const maxNetworkDecimals =
state.fromNetwork === 'Evm' && state.toNetwork === 'Evm' ? 18 : 8;
// Check if the amount entered has too many decimal places
amountInCoin = trimDecimalAmount(action.payload, state.selectedToken.decimals, 'entering');
amountInCoin = trimDecimalAmount(
action.payload,
Math.min(maxNetworkDecimals, state.selectedToken.decimals),
'entering'
);

// Check if the balance is exceeded
const amountBN = new BN(
Expand Down

0 comments on commit 78e1e02

Please sign in to comment.