Code Example
Here is an example of a cross-chain swap using MUWP's api.
// Define the input amount for the swap
const inputAmount = {
'ETH:ETH:0x0000000000000000000000000000000000000000': '2000000000000000000'
};
// Define the sender and recipient addresses
const fromAddress = '0x27b4A938802b1278317eD0fC0135b6E1E14F43dC';
const toAddress = '0x27b4A938802b1278317eD0fC0135b6E1E14F43dC';
// Define the input chain ID (Ethereum mainnet)
const inputChain = 1;
// Define the input tokens
const inputTokens = [{
address: '0x0000000000000000000000000000000000000000',
value: 'ETH:ETH:0x0000000000000000000000000000000000000000'
}];
// Define the output chain ID (Arbitrum)
const outputChain = 42161;
// Define the output tokens and their respective distributions
const outputTokens = [
{
address: '0xaf88d065e77c8cC2239327C5EDb3A432268e5831',
value: 'USDC:USD Coin:0xaf88d065e77c8cC2239327C5EDb3A432268e5831'
},
{
address: '0x0000000000000000000000000000000000000000',
value: 'ETH:ETH:0x0000000000000000000000000000000000000000'
}
];
const distribution = [50, 50];
// Define options (e.g., allowed/denied bridges and exchanges)
const options = { bridges: { deny: [] }, exchanges: { deny: [] } };
// Prepare the request body
const requestBody = JSON.stringify({
inputAmount,
fromAddress,
toAddress,
inputChain,
inputTokens,
outputChain,
outputTokens,
distribution,
options
});
// Send the request to MUWP's API
const quote = await fetch('https://www.muwp.xyz/api/quote', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: requestBody
})
.then(response => response.json())
Now that we have the quote, we can use it to execute the swap.
const input = {
"from": {
"0x0000000000000000000000000000000000000000": {
"0x27b4A938802b1278317eD0fC0135b6E1E14F43dC": "1000000000000000000" // 1 ETH from this wallet
}
},
"gasPayer": "0x27b4A938802b1278317eD0fC0135b6E1E14F43dC",
"account": "0xFF1A17372333830B26Eda4E86afcA821323A50F2",
"chainId": 1,
"routes": Object.keys(quote.routes).map((key) => quote.routes[key][0]), // Or choose whatever route you want
"maxFeePerGas": "63225902238",
"maxPriorityFeePerGas": "1000000"
}
const initiate = await fetch("https://www.muwp.xyz/api/initiate", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(input)
})
.then(response => response.json())
And we can now send the transaction to the Ethereum network.
const hash = await walletClient.sendTransaction({
chain: txn.chain,
data: txn.data,
to: txn.to,
value: txn.value
});
console.log(`Transaction hash: ${hash}`);
const notifyBackend = await fetch("https://www.muwp.xyz/api/receive-funds", {
method: "POST",
body: JSON.stringify({
chainId: 1,
transactionHash: hash,
accountAddress: quote.tempAccount
}),
headers: {
"Content-Type": "application/json"
}
}).then((res) => res.json());
console.log(`Backend notified: ${notifyBackend.status}`)
await walletClient.waitForTransaction(hash);
console.log("Transaction confirmed");
const notifyBackendTwo = await fetch("/api/chain-confirmed", {
method: "POST",
body: JSON.stringify({
chainId: 1,
transactionHash: hash,
accountAddress: tempAccount
}),
headers: {
"Content-Type": "application/json"
}
}).then((res) => res.json());
And that's it! You've successfully executed a cross-chain swap using MUWP's API.