Skip to content

Commit

Permalink
Updated: eth_sendTransaction to handle ERC20 transfers properly
Browse files Browse the repository at this point in the history
  • Loading branch information
juanolmedo1 committed Jan 23, 2025
1 parent 0cb618f commit 4d3f2f4
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 10 deletions.
7 changes: 7 additions & 0 deletions source/scenes/home/SendAsset/Confirm/Confirm.container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ const ConfirmContainer = () => {
);
let history: any;
let isExternalRequest: boolean;
let isTransfer: boolean = false;

if (location) {
isExternalRequest = location.pathname.includes('confirmTransaction');
Expand All @@ -106,10 +107,15 @@ const ConfirmContainer = () => {
const vaultActiveAsset = vault.activeAsset;

if (isExternalRequest) {
const { data } = StargazerExternalPopups.decodeRequestMessageLocationParams<{
isTransfer: boolean;
}>(location.href);
const { to, chain, metagraphAddress } = StargazerExternalPopups.decodeLocationParams(
location.href
);

isTransfer = data.isTransfer;

activeAsset = useSelector((state: RootState) =>
find(state.assets, { address: Array.isArray(to) ? to[0] : to })
) as IAssetInfoState;
Expand Down Expand Up @@ -331,6 +337,7 @@ const ConfirmContainer = () => {
handleConfirm={handleConfirm}
disabled={disabled}
isL0token={isL0token}
isTransfer={isTransfer}
/>
</Container>
);
Expand Down
16 changes: 10 additions & 6 deletions source/scenes/home/SendAsset/Confirm/Confirm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ interface ISendConfirm {
) => void;
disabled: boolean;
isL0token: boolean;
isTransfer: boolean;
}

const SendConfirm = ({
Expand All @@ -59,6 +60,7 @@ const SendConfirm = ({
handleConfirm,
disabled,
isL0token,
isTransfer,
}: ISendConfirm) => {
const callbackSuccess = async (
message: StargazerRequestMessage,
Expand Down Expand Up @@ -136,12 +138,14 @@ const SendConfirm = ({
</div>
</section>
<section className={styles.confirm}>
<div className={styles.row}>
Max Total
<span>
{isL0token ? `${totalAmount} ${assetInfo.symbol}` : `$${totalAmount} USD`}
</span>
</div>
{!isTransfer && (
<div className={styles.row}>
Max Total
<span>
{isL0token ? `${totalAmount} ${assetInfo.symbol}` : `$${totalAmount} USD`}
</span>
</div>
)}
<div className={styles.actions}>
<Button
type="button"
Expand Down
4 changes: 4 additions & 0 deletions source/scenes/home/SendAsset/Send/Send.container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ const SendContainer: FC<IWalletSend> = ({ initAddress = '' }) => {
let gas: string;
let memo: string;
let metagraphAddress: string;
let isTransfer = false;
let history;
let assetInfo: IAssetInfoState;

Expand All @@ -126,6 +127,7 @@ const SendContainer: FC<IWalletSend> = ({ initAddress = '' }) => {
metagraphAddress: any;
chain: string;
chainLabel: string;
isTransfer: boolean;
} | null>(location.href);

if (data) {
Expand All @@ -136,6 +138,7 @@ const SendContainer: FC<IWalletSend> = ({ initAddress = '' }) => {
chain = data.chain;
feeAmount = data.fee;
metagraphAddress = data.metagraphAddress;
isTransfer = data.isTransfer;
}

useEffect(() => {
Expand Down Expand Up @@ -625,6 +628,7 @@ const SendContainer: FC<IWalletSend> = ({ initAddress = '' }) => {
decimalPointOnFee={decimalPointOnFee}
networkTypeOptions={networkTypeOptions}
basePriceId={basePriceId}
isTransfer={isTransfer}
/>
</Container>
);
Expand Down
5 changes: 3 additions & 2 deletions source/scenes/home/SendAsset/Send/Send.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const WalletSend: FC<IWalletSend> = ({
handleGasPriceChange,
handleClose,
onSubmit,
isTransfer,
isExternalRequest,
isDisabled,
isValidAddress,
Expand Down Expand Up @@ -118,7 +119,7 @@ const WalletSend: FC<IWalletSend> = ({
)}
</li>
<li>
<label>{`${assetInfo.symbol} Amount`} </label>
<label>{isTransfer ? 'Amount' : `${assetInfo.symbol} Amount`} </label>
<TextInput
type="number"
placeholder="Enter amount to send"
Expand Down Expand Up @@ -169,7 +170,7 @@ const WalletSend: FC<IWalletSend> = ({
)}
</ul>
<div className={styles.status}>
{!!assetInfo?.priceId && (
{!!assetInfo?.priceId && !isTransfer && (
<span className={styles.equalAmount}>
{getFiatAmount(Number(amount) + Number(fee), 6)}
</span>
Expand Down
1 change: 1 addition & 0 deletions source/scenes/home/SendAsset/Send/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ export interface IWalletSend {
decimalPointOnFee?: boolean;
networkTypeOptions: IInputClickableOptions;
basePriceId: string;
isTransfer: boolean;
}
35 changes: 33 additions & 2 deletions source/scripts/Provider/evm/methods/eth_sendTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,24 @@ import {
StargazerExternalPopups,
StargazerWSMessageBroker,
} from 'scripts/Background/messaging';
import { getChainId, getNetworkId, getWalletInfo } from '../utils';
import { getChainId, getNetworkId, getNetworkInfo, getWalletInfo } from '../utils';
import { BigNumber } from 'ethers';
import StargazerRpcProvider from '../StargazerRpcProvider';
import { tokenContractHelper } from 'scripts/Background/helpers/tokenContractHelper';

const calculateTransferAmount = async (
amount: BigNumber,
contractAddress: string
): Promise<number> => {
// Fetch contract info to get the decimals
const networkInfo = getNetworkInfo();
const provider = new StargazerRpcProvider(networkInfo.rpcEndpoint);
const contractInfo = await tokenContractHelper.getTokenInfo(provider, contractAddress);
const decimals = contractInfo?.decimals || 18;

// Calculate the value based on the decimals
return Number(amount.toString()) / 10 ** decimals;
};

export const eth_sendTransaction = async (
request: StargazerRequest & { type: 'rpc' },
Expand All @@ -23,6 +40,7 @@ export const eth_sendTransaction = async (
let decodedContractCall: ContractInputData | null = null;

let route = 'sendTransaction';
let isTransfer = false;

// chainId should match the current active network if chainId property is provided.
if (trxData?.chainId) {
Expand Down Expand Up @@ -62,9 +80,22 @@ export const eth_sendTransaction = async (

if (decodedContractCall?.method === 'approve') {
route = 'approveSpend';
} else if (decodedContractCall?.method === 'transfer') {
isTransfer = true;

if (!decodedContractCall?.inputs?.length || decodedContractCall?.inputs?.length < 2) {
throw new EIPRpcError('Invalid transfer method call', EIPErrorCodes.Rejected);
}

// Get the amount from the decoded contract call
const amount = decodedContractCall.inputs[1] as BigNumber;
const contractAddress = trxData.to;

// Assign the value to the trxData object
trxData.value = await calculateTransferAmount(amount, contractAddress);
}

const data = { ...trxData, chain: getNetworkId(), chainLabel };
const data = { ...trxData, isTransfer, chain: getNetworkId(), chainLabel };

const { windowUrl, windowSize, windowType } = getWalletInfo();

Expand Down

0 comments on commit 4d3f2f4

Please sign in to comment.