How to swap tokens via miraAmm?

  • I tried to implement a token swap based on the example on github, but the transaction was never uploaded to the blockchain. No errors were reported during the execution.
  • here is the code
import { Account, Provider, WalletUnlocked } from 'fuels';
import { MiraAmm, ReadonlyMiraAmm, buildPoolId, getAssetId} from 'mira-dex-ts/dist/sdk/index.js';

async function futureDeadline(provider) {
    const block = await provider.getBlock("latest");
    return block?.height.add(1000) ?? 1000000000;
}

const provider = await Provider.create('https://mainnet.fuel.network/v1/graphql');
const wallet = new WalletUnlocked('', provider);


const asset_eth = {
    bits: "0xf8f8b6283d7fa5b672b530cbb84fcccb4ff8dc40f8176ef4544ddb1f1952ad07",
};
const asset_usdc = {
    bits: "0x286c479da40dc953bddc3bb4c453b608bba2e0ac483b077bd475174115395e6b",
};

const miraAmm = new MiraAmm(wallet);
const poolId = buildPoolId(asset_eth, asset_usdc, false);
const deadline = await futureDeadline(provider);
const txParams = {
    gasLimit: 999999,
    maxFee: 999999,
};
const txRequest = await miraAmm.swapExactInput(
    39899276, asset_eth, 164931, [poolId], deadline, txParams
);
console.log(txRequest)
1 Like

The same question.
I have tried Rust and TS SDK’s.
I receive TX informtion however balances ar enot changing

Fuel team. Could you provide some tests or examples how to use Swap function. It is not clear and a lot of developers are confused

Hey @Antik21 :wave:

Apologies for the late reply.

Just taken a look over your code and it looks like you’ve built the request for the swap, without actually sending the transaction.

The final 10 lines of the following snippet will estimate, fund and send the transaction (docs).

import { Account, Provider, WalletUnlocked } from 'fuels';
import { MiraAmm, ReadonlyMiraAmm, buildPoolId, getAssetId} from 'mira-dex-ts/dist/sdk/index.js';

async function futureDeadline(provider: Provider) {
    const block = await provider.getBlock("latest");
    return block?.height.add(1000) ?? 1000000000;
}

// Setup
const provider = await Provider.create('https://mainnet.fuel.network/v1/graphql');
const wallet = new WalletUnlocked(process.env.WALLET_PRIVATE_KEY!, provider);
const miraAmm = new MiraAmm(wallet);

// Configure assets + pool
const asset_eth = {
    bits: "0xf8f8b6283d7fa5b672b530cbb84fcccb4ff8dc40f8176ef4544ddb1f1952ad07",
};
const asset_usdc = {
    bits: "0x286c479da40dc953bddc3bb4c453b608bba2e0ac483b077bd475174115395e6b",
};
const poolId = buildPoolId(asset_eth, asset_usdc, false);

// Generate the transaction request
const txParams = {
    gasLimit: 999999,
    maxFee: 999999,
};
const deadline = await futureDeadline(provider);
const txRequest = await miraAmm.swapExactInput(
    10, asset_eth, 10, [poolId], deadline, txParams
);

// Fund the transaction request
const txCost = await wallet.getTransactionCost(txRequest);
txRequest.gasLimit = txCost.gasUsed;
txRequest.maxFee = txCost.maxFee;
await wallet.fund(txRequest, txCost);

// Send the transaction
const tx = await wallet.sendTransaction(txRequest);
const txResponse = await tx.waitForResult();

console.log('Transaction sent', txResponse.id)

Let me know if you need further assistance.

Thank you, it works !

@wwwwww Hey.
You can check my sample:
Github user: Antik21
Project: mira-dex-v1-sample