Skip to content

Commit

Permalink
feat: eth chain id numeric instead of hex
Browse files Browse the repository at this point in the history
  • Loading branch information
peronczyk committed May 28, 2024
1 parent 80fd11a commit 07cb2e1
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/popup/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,7 @@
"required": "This field is required",
"url": "Invalid URL",
"address": "Enter valid blockchain address or .chain name",
"numeric": "The value should be a number",
"name": "Enter valid .chain name",
"nameRegisteredAddress": "Invalid address or .chain name",
"nameUnregistered": "This name has already been registered.",
Expand All @@ -1250,7 +1251,6 @@
"enoughAeSigner": "Insufficient balance on your signing account to propose a transaction",
"maxLength": "The value should be no longer than {0} characters",
"maxValue": "This field must be {0} or less",
"hexFormat": "The value should be in hex format",
"maxRedeem": "Maximum redeemable amount is {0} AE.",
"maxValueVault": "Amount is exceeding the vault's balance: {0}",
"notToken": "Tokens are currently not allowed, please switch to AE",
Expand Down
19 changes: 6 additions & 13 deletions src/popup/plugins/veeValidate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineRule } from 'vee-validate';
import { required } from '@vee-validate/rules';
import { numeric, required } from '@vee-validate/rules';
import BigNumber from 'bignumber.js';
import { debounce } from 'lodash-es';
import { isAddressValid, isNameValid } from '@aeternity/aepp-sdk';
Expand Down Expand Up @@ -32,6 +32,11 @@ defineRule(
(value: string) => !value || isUrlValid(value) || tg('validation.url'),
);

defineRule(
'numeric',
(value: string) => numeric(value) || tg('validation.numeric'),
);

defineRule(
'account',
(value: string) => isAddressValid(value) || isNameValid(value) || tg('validation.address'),
Expand Down Expand Up @@ -109,18 +114,6 @@ defineRule(
},
);

defineRule(
'is_hex_format',
(value: string) => (
(
value?.toString()?.startsWith('0x')
&& value.length >= 3
&& parseInt(value.slice(2), 16).toString(16) === value.slice(2).toLowerCase()
)
|| tg('validation.hexFormat')
),
);

export default () => {
const { balance, updateBalances } = useBalances();
const { currencyRates } = useCurrencies({ pollingDisabled: true });
Expand Down
5 changes: 3 additions & 2 deletions src/protocols/ethereum/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const ETH_COIN_PRECISION = 18; // Amount of decimals

export const ETH_COINGECKO_COIN_ID = 'ethereum';
export const ETH_GAS_LIMIT = 21000;
export const ETH_CHAIN_NAMESPACE = 'eip155';

/**
* Estimated time we need to wait for the middleware (etherscan) to sync it's state
Expand Down Expand Up @@ -70,11 +71,11 @@ export const ETH_SAFE_CONFIRMATION_COUNT = 12;
export const ETH_NETWORK_DEFAULT_SETTINGS: IDefaultNetworkTypeData<IEthNetworkSettings> = {
[NETWORK_TYPE_MAINNET]: {
nodeUrl: 'https://ethereum-rpc.publicnode.com', // TODO replace temp values - use our own node
chainId: '0x1',
chainId: '1',
},
[NETWORK_TYPE_TESTNET]: {
nodeUrl: 'https://ethereum-sepolia-rpc.publicnode.com', // TODO replace temp values - use our own node
chainId: '0xaa36a7',
chainId: '11155111',
},
};

Expand Down
8 changes: 4 additions & 4 deletions src/protocols/ethereum/libs/EthereumAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import type {
NetworkTypeDefault,
} from '@/types';
import { PROTOCOLS } from '@/constants';
import { getLastNotEmptyAccountIndex } from '@/utils';
import { getLastNotEmptyAccountIndex, toHex } from '@/utils';
import Logger from '@/lib/logger';
import { BaseProtocolAdapter } from '@/protocols/BaseProtocolAdapter';
import { tg } from '@/popup/plugins/i18n';
Expand Down Expand Up @@ -100,7 +100,7 @@ export class EthereumAdapter extends BaseProtocolAdapter {
defaultValue: ETH_NETWORK_DEFAULT_ENV_SETTINGS.chainId,
validationRules: {
url: false,
is_hex_format: true,
numeric: true,
},
getPlaceholder: () => tg('pages.network.chainIdPlaceholder'),
getLabel: () => tg('pages.network.chainIdLabel'),
Expand Down Expand Up @@ -296,7 +296,7 @@ export class EthereumAdapter extends BaseProtocolAdapter {

// All values are in wei
const txData: FeeMarketEIP1559TxData = {
chainId,
chainId: toHex(chainId),
nonce,
to: contractId,
data: contract.methods.transfer(recipient, hexAmount).encodeABI(),
Expand Down Expand Up @@ -435,7 +435,7 @@ export class EthereumAdapter extends BaseProtocolAdapter {

// All values are in wei
const txData: FeeMarketEIP1559TxData = {
chainId,
chainId: toHex(chainId),
nonce,
to: recipient,
value: hexAmount,
Expand Down
3 changes: 2 additions & 1 deletion src/protocols/ethereum/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { INetworkProtocolSettings } from '@/types';
/**
* Settings specific to this protocol.
*/
export type EthNetworkProtocolSettings = 'chainId';
export type EthNetworkProtocolSettings =
| 'chainId'; // ref: https://docs.simplehash.com/reference/supported-chains-testnets

/**
* Settings that are not editable by the user but are assigned to specific network types.
Expand Down
4 changes: 4 additions & 0 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,10 @@ export function splitAddress(address: string | null): string {
: '';
}

export function toHex(value: string) {
return `0x${parseInt(value, 10).toString(16)}`;
}

export function toShiftedBigNumber(value: number | string, precision: number): BigNumberPublic {
return new BigNumber(value).shiftedBy(precision);
}
Expand Down

0 comments on commit 07cb2e1

Please sign in to comment.