Hey, iāve been struggling to find out what Iām doing wrong here. followed Ivanās video on doing a swap and trying to port that code over to React, but issues. I did a console.log and can see 1inch plugin is loaded. This line, ādex = Moralis.Plugins.oneInchā doesnt appear to be a thing anymoreā¦ I was able to get metamask to pop up using a small hardcoded send value of ā0.01ā Number(Moralis.Units.ETH(ā0.01ā)) will this conversion be the same for BEP20.
if someone could help a paid customer out thatād be great. i have redacted my token name from the code, it is active and does have liquidity.
**edit so I am able to get a successful transaction but it is not right compared to the token value I put in i get errors when i try using my variable in place of the ā0.01ā but if i use a small number like in the tutorial example it seems to work i put 0.05 in hit submit and it sent me 560 of my tokens valued around $3.70. if i try and buy 1.0 i get promise error in consoleā¦
import "./swap.scss";
import { useState, useEffect } from "react";
import { Form, InputNumber, Button, Statistic } from "antd";
import {
useWeb3ExecuteFunction,
useNativeBalance,
useMoralis,
} from "react-moralis";
import { optionsgetbalance } from "../../cakeABI";
const Swapper = () => {
const { Moralis, isWeb3Enabled, enableWeb3, isAuthenticated, isWeb3EnableLoading } =
useMoralis();
const [cake, setcake] = useState();
const [bnbprice, setbnb] = useState();
const { data: indBal, fetch: fetchindBal } = useWeb3ExecuteFunction();
const { data: balanceNative } = useNativeBalance();
const RenderSwapBalances = async () => {
let currentUser = Object.keys(localStorage).filter((o) =>
o.includes("currentUser")
)[0];
// eslint-disable-next-line
let res3 = await fetchindBal({
params: {
...optionsgetbalance,
params: {
who: JSON.parse(
JSON.parse(JSON.stringify(localStorage.getItem(currentUser)))
).ethAddress,
},
},
});
let res5 = await fetch(
"https://api.pancakeswap.info/api/v2/tokens/0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c"
);
setbnb(await res5.json());
let res4 = await fetch(
"https://api.pancakeswap.info/api/v2/tokens/0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82"
);
setcake(await res4.json());
};
useEffect(() => {
if (isAuthenticated && !isWeb3Enabled && !isWeb3EnableLoading) {
const connectorId = window.localStorage.getItem("connectorId");
enableWeb3({ provider: connectorId });
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isAuthenticated, isWeb3Enabled]);
useEffect(() => {
if (isAuthenticated && isWeb3Enabled) {
Moralis.initPlugins();
RenderSwapBalances();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isAuthenticated, isWeb3Enabled]);
let dex;
dex = Moralis.Plugins.oneInch; // not working anymore?
async function DoSwap(values) {
const cakeAmt = values.cakebuyamt;
const options = {
chain: "bsc", // The blockchain you want to use (eth/bsc/polygon)
fromTokenAddress: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", // The token you want to swap
toTokenAddress: "0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82", // The token you want to receive
amount: Number(Moralis.Units.ETH(cakeAmt)),
fromAddress: Moralis.User.current().get("ethAddress"), // Your wallet address
slippage: 1,
};
var receipt = await dex.swap(options)
console.log(receipt);
}
const [form] = Form.useForm();
return (
<div className="swapContainer">
<div className="showBalance">
<div>
<Statistic
title={<span>cake Price</span>}
valueStyle={{ fontFamily: "bank gothic", fontWeight: "bold" }}
value={cake?.data?.price}
precision={6}
prefix="$"
/>
</div>
<div>
<Statistic
title={<span>cake Balance</span>}
valueStyle={{ fontFamily: "bank gothic", fontWeight: "bold" }}
value={
indBal &&
parseInt(JSON.parse(JSON.stringify(indBal)).hex, 16) /
Math.pow(10, 18)
}
precision={2}
/>
</div>
<div>
<Statistic
title={<span>BNB Balance</span>}
valueStyle={{ fontFamily: "bank gothic", fontWeight: "bold" }}
value={
balanceNative.balance &&
parseInt(JSON.parse(JSON.stringify(balanceNative.balance))) /
Math.pow(10, 18)
}
precision={6}
/>
</div>
</div>
<div className="buycake">BUY cake</div>
<div className="swapForm">
<Form
form={form}
autoComplete="off"
onFinish={(values) => DoSwap(values)}
onFinishFailed={(error) => {
console.log({ error });
}}
>
<Form.Item
name="cakebuyamt"
rules={[
{
required: true,
},
]}
>
<InputNumber
prefix="cake - "
style={{
width: 200,
}}
min="0"
max="22000"
step="1.000000"
stringMode
/>
</Form.Item>
<Form.Item>
<Button type="primary" htmlType="submit">
Swap
</Button>
</Form.Item>
</Form>
</div>
</div>
);
};
export default Swapper;