const web3 = Moralis.web3ByChain("0x1"); // mainnet
Moralis.Cloud.define("swapDex", async (request) => {
const logger = Moralis.Cloud.getLogger();
const network = request.params.network;
const fromAddress = request.params.fromAddress;
const fromToken = request.params.from;
const toToken = request.params.to;
const amount = request.params.amount;
const slippage = request.params.slippage;
const referrerAddress = "**REDACTED**";
const fee = 3;
const networkChainId = network == "ethereum" ? 1 : 56;
const url = `https://api.1inch.exchange/v3.0/${networkChainId}/swap?fromAddress=${fromAddress}&fromTokenAddress=${fromToken}&toTokenAddress=${toToken}&amount=${amount}&slippage=${slippage}&fee=${fee}&referrerAddress=${referrerAddress}`;
logger.info(`1inch Swap: ${url}`);
const response = await Moralis.Cloud.httpRequest({
url: url,
headers: {
"Content-Type": "application/json;charset=utf-8",
},
});
const EthTransactions = Parse.Object.extend("EthTransactions");
const query = new Parse.Query(EthTransactions);
query.equalTo("from_address", fromAddress);
query.descending("createdAt");
const resultQuery = await query.first();
let gasPrice = parseInt(response.data.tx["gasPrice"]);
console.log(gasPrice);
gasPrice = "0x" + gasPrice.toString(16);
const result = {
data: response.data.tx["data"],
to: response.data.tx["to"],
value: `0x${response.data.tx["value"]}`,
//gasPrice: `0x${parseInt(response.data.tx["gasPrice"])}`,
//gas: web3.utils.toHex(response.data.tx["gas"]),
from: response.data.tx["from"],
nonce: web3.utils.toHex(resultQuery.attributes.nonce + 1),
};
return result;
});
P.S I have redacted my referrer address