Hi coders,
I know that moralis has stopped using the dex function on their boilerplate but im still interested. but getting a error 400 trying to get a quote
My Code :
const useInchDex = (chain) => {
const { account, chainId, active } = useWeb3React();
const [tokenList, setTokenlist] = useState();
const web3 = new Web3(window.ethereum);
**useEffect((chain) => {**
** if (!account&& !active) return null;**
** **
** listAvailableTokens(); **
** })**
** }, [account, chain, active]);**
// useEffect - to load the list from function below.
//const chain = {
//'bsc' : responseTwo,
//'eth': responseOne,
//}
async function listAvailableTokens() {
const ethList = "https://api.1inch.exchange/v4.0/1/tokens";
const bscList = "https://api.1inch.exchange/v4.0/56/tokens";
const polyList = "https://api.1inch.exchange/v4.0/137/tokens";
const testList = "https://api-polygon-tokens.polygon.technology/tokenlists/testnet.tokenlist.json";
const requestOne = axios.get(ethList);
const requestTwo = axios.get(bscList);
const requestThree = axios.get(polyList)
await axios.all([requestOne, requestTwo,requestThree]).then(axios.spread((...responses) => {
const responseOne = responses[0]
const responseTwo = responses[1]
const responseThree = responses[2]
console.log(responseOne.data.tokens,responseTwo.data.tokens)
//);
//}
if (chain == 0x1 ){
setTokenlist(
responseOne.data.tokens,// responseTwo.data.tokens
// responseTwo.data.tokens
);
}
if (chain == 0x38 ){
setTokenlist(
//responseTwo.data.tokens,// responseTwo.data.tokens
responseTwo.data.tokens
);
}
if (chain == 0x89 ){
setTokenlist(
responseThree.data.tokens,// responseTwo.data.tokens
// responseTwo.data.tokens
);
}
// use/access the results
})).catch(errors => {
// react on errors.
})
//let tokens = result.tokens;
// console.log(tokens);
// setTokenlist(tokens);
//setTokenlist(tokens);
//for (const address in tokens){
// let token = tokens;
// tokenList([tokens]);
//}
//}
// }catch(error){
// console.log(error);
//}
};
/*------------------- ----------------- ------------------------------------------------------------*/
const walletAddress = account;
//const apiBaseUrl = 'https://api.1inch.io/v4.0/' + chainId;
//function apiRequestUrl(quote, getQuote) {
// return apiBaseUrl + quote + '?' + (new URLSearchParams(getQuote)).toString();
//}
// console.log('quote:',apiRequestUrl)
// const getQuote = async (params) => ({
// chain: params.chain, // The blockchain you want to use (eth/bsc/polygon)
///// fromTokenAddress: params.fromToken.address, // The token you want to swap
// toTokenAddress: params.toToken.address, // The token you want to receive
// amount: Web3.utils.toWei(
//new BN( params.fromAmount),
// params.fromToken.decimals,
// 'ether'
//).toString(),
// }
//)
// params.fromToken.decimals,
// const getQuote before
// tokenList[tokens].address
const apiBaseUrl = 'https://api.1inch.io/v4.0/' + chainId;
// --- comps from 1inch
function apiRequestUrl(methodname, queryParams) {
return apiBaseUrl + methodname + '?' + (new URLSearchParams(queryParams)).toString();
}
const broadcastApiUrl = 'https://tx-gateway.1inch.io/v1.1/' + chainId + '/broadcast';
async function broadCastRawTransaction(rawTransaction) {
return fetch(broadcastApiUrl, {
method: 'post',
body: JSON.stringify({rawTransaction}),
headers: {'Content-Type': 'application/json'}
})
.then(res => res.json())
.then(res => {
return res.transactionHash;
});
}
/*
async function buildTxForApproveTradeWithRouter(tokenAddress, amount) {
const url = apiRequestUrl(
'/approve/transaction',
amount ? {tokenAddress, amount} : {tokenAddress}
);
const transaction = await fetch(url).then(res => res.json());
const gasLimit = await web3Broard.eth.estimateGas({
...transaction,
from: walletAddress
});
return {
...transaction,
gasLimit
};
}
async function signAndSendTransaction(transaction) {
const {rawTransaction} = await web3Broard.eth.accounts.signTransaction(transaction,walletAddress);
return await broadCastRawTransaction(rawTransaction);
}
*/
async function buildTxForSwap(swapParams) {
const url = apiRequestUrl('/swap', swapParams);
return fetch(url).then(res => res.json()).then(res => res.tx);
}
async function getQuotef( params) {
const qp = {
chain: params?.chain, // The blockchain you want to use (eth/bsc/polygon)
fromTokenAddress: params?.fromToken.address, // The token you want to swap
toTokenAddress: params?.toToken.address, // The token you want to receive
amount: Web3.utils.toWei(
new BN(params?.fromAmount,
//params.fromToken.decimals,
'ether'
)).toString() ,
// amount: ethers.utils.parseEther(params?.fromAmount.toString()).toString(), //8 for decimals //formatether works
fee: 1
}
const url = apiRequestUrl ('/quote', qp);
//return await fetch(url).then( res => res.json());
const response = await fetch (url);
const quoteJson = await response.json();
console.log("quotef", quoteJson);
//.then(res => { return res.toTokenAmount});
//return fetch(url).then(res => res.data).
};
getQuotef();
}];
}
}
return { getQuotef,tokenList };
}
export default useInchDex;
I keep getting this error when trying to get a quote :
{statusCode: 400, error: ‘Bad Request’, description: ‘undefined is wrong address’, meta: Array(1), requestId: ‘571b4e69-478d-4353-8aa4-8c52001a9ff4’}
description
:
“undefined is wrong address”
error
:
“Bad Request”
meta
:
[{…}]
requestId
:
“571b4e69-478d-4353-8aa4-8c52001a9ff4”
statusCode
:
400
[[Prototype]]
:
Object
Thanks for your help , this is also connected to the InchDex component. (https://github.com/ethereum-boilerplate/ethereum-nextjs-boilerplate/blob/cee918d68651e9396c4d3dda0c4cd6b754674d26/src/components/InchDex/InchDex.jsx)