[Solved] Self hosted Moralis V1, how to implement token swap functionnality using vanilla js

I am using a Self Hosted Moralis V1, and I am trying to implement the swap toke functionality using vanilla js, but apparently, it’s not possible, you have to implement all the functionality by yourself,

and I have no idea why.

Hi @elevatex, you can use the 1inch API to implement swapping feature in your dapp. I would suggest checking our tutorials below where the 1inch API is used to:

Build a Dex
Create a token swap dapp

Please note that Moralis v2 is used in the videos, as you are using Moralis v1, you might need to make some adjustments to your code for it to work.

i tried, with no luck whatsoever

is there any other way?

I am currently stuck at the 1inch swap api call, cuz you should make allowance, and how to make that call?

for reference here’s how to approve sending of x amount

        let data = {
            contractAddress : USDT_CONTRACT_ADDRESS, 
            abi             : USDT_CONTRACT_ABI, 
            functionName    : "approve", 
            params          : {
                spender         : walletAddress, 
                amount          : Moralis.Units.Token("2", "18")
            }
        
        }
        const message = await Moralis.executeFunction(data)
        console.log( message )

hi, in order to follow the tutorials we shared here you need to approve 1inch to use your tokens, so they can route the swap for you.

You can check this file, this is pretty much were all the logic happens in our recent dex tutorial
https://github.com/IAmJaysWay/dexFinal/blob/main/dexFinal/dex/src/components/Swap.js

Take a look on how we did the api calls to 1inch. Also keep in mind that from 1inch you will get raw tx data, you need to send this using ethere to make it work properly

1 Like

just one small question,
i successfully! allowed allowance to 1inch
params : { spender : _1InchSpenderAddress, amount : amount }
and now i am initialising the swap
i got this data from 1inch swap API

{ “toAmount”: “42036255012254”, “tx”: { “from”: “0xa32e2eb56D1CEB94D7D4C993457bB756EB5ea273”, “to”: “0x1111111254eeb25477b68fb85ed929f73a960582”, “data”: “0x12aa3caf000000000000000000000000170d2ed0b2a5d9f450652be814784f964749ffa400000000000000000000000055d398326f99059ff775485246999027b3197955000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee000000000000000000000000170d2ed0b2a5d9f450652be814784f964749ffa4000000000000000000000000a32e2eb56d1ceb94d7d4c993457bb756eb5ea273000000000000000000000000000000000000000000000000002386f26fc1000000000000000000000000000000000000000000000000000000002451f63ecc89000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000018200016c0001305120830c9e0295730bbb220bd78346c6499ff37851fb55d398326f99059ff775485246999027b319795500a4a5dcbcdf00000000000000000000000055d398326f99059ff775485246999027b3197955000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c0000000000000000000000007ba9a18a46b7320224de22a4c7f9e6aaca2083dd000000000000000000000000170d2ed0b2a5d9f450652be814784f964749ffa4ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004101bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c00042e1a7d4d0000000000000000000000000000000000000000000000000000000000000000c0611111111254eeb25477b68fb85ed929f73a960582cfee7c08”, “value”: “0”, “gas”: 298869, “gasPrice”: “3500000000” } }

i don’t know how to execute it using moralis

any ideas on how to do that?

Nice! Now that you got the data you can use it with a package like ethers
For reference you can check this video to see how they used ethers with raw data there
https://youtu.be/Qewe1CnUErA

You should end up with something like this

const res = responseFrom1inchAPI // your data 
const tx = {
      from: res.from,
      to: res.to,
      data: res.data,    
}
const sentTx = await wallet.sendTransaction(tx)

This article should help too
https://piyopiyo.medium.com/how-to-send-ether-with-ethers-js-b1bb0b17a855

You will have to replicate this process for the swap transaction too

and the variable “wallet” is?
and I actually prefer if I can execute it using moralis sdk

i prefer if i can do it with Moralis.executeFunction and execute a contract function?

is that possible?

cuz i don’t think this solution is gonna work, if integrate walletconnect

Im not sure if the v1 sdk has the ability to send raw transactions like that, thats why i recommended a different approach