⌨️Submit Order
Below we provide some example code for making a trade on Bebop.
Bebop submits order on chain having received maker and taker signatures. This means that Bebop pays the network (gas) fees as they are already included in the price. This is the default mode.
Bebop web interface users and most API users would choose Bebop's default mode where user signs the order transaction and Bebop sends it on-chain.
In some cases, API users may need to submit the transaction themselves, and we have a mode for that too.
Example
import axios from "axios";
import {ethers} from "ethers"; //ethers-v6
// Trade Info (Insert your values)
const privateKey = ""
const tokensAddressesSell = ["0x4200000000000000000000000000000000000006"] // WETH
const tokensSellAmounts = [ethers.parseEther("0.001")] // 0.001 WETH
const tokensAddressBuy = ["0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913"] // USDC
const chain = {
chainId: 8453,
name: "base" // "polygon" | "ethereum" | "arbitrum" | "blast" | "optimism"
}
// Constants
const BEBOP_ADDRESS = "0xbbbbbBB520d69a9775E85b458C58c648259FAD5F"
const SINGLE_ORDER_TYPES = {
"SingleOrder": [
{ "name": "partner_id", "type": "uint64" },
{ "name": "expiry", "type": "uint256" },
{ "name": "taker_address", "type": "address" },
{ "name": "maker_address", "type": "address" },
{ "name": "maker_nonce", "type": "uint256" },
{ "name": "taker_token", "type": "address" },
{ "name": "maker_token", "type": "address" },
{ "name": "taker_amount", "type": "uint256" },
{ "name": "maker_amount", "type": "uint256" },
{ "name": "receiver", "type": "address" },
{ "name": "packed_commands", "type": "uint256" },
]
}
const MULTI_ORDER_TYPES = {
"MultiOrder": [
{ "name": "partner_id", "type": "uint64" },
{ "name": "expiry", "type": "uint256" },
{ "name": "taker_address", "type": "address" },
{ "name": "maker_address", "type": "address" },
{ "name": "maker_nonce", "type": "uint256" },
{ "name": "taker_tokens", "type": "address[]" },
{ "name": "maker_tokens", "type": "address[]" },
{ "name": "taker_amounts", "type": "uint256[]" },
{ "name": "maker_amounts", "type": "uint256[]" },
{ "name": "receiver", "type": "address" },
{ "name": "commands", "type": "bytes" },
]
}
const AGGREGATE_ORDER_TYPES = {
"AggregateOrder": [
{ "name": "partner_id", "type": "uint64" },
{ "name": "expiry", "type": "uint256" },
{ "name": "taker_address", "type": "address" },
{ "name": "maker_addresses", "type": "address[]" },
{ "name": "maker_nonces", "type": "uint256[]" },
{ "name": "taker_tokens", "type": "address[][]" },
{ "name": "maker_tokens", "type": "address[][]" },
{ "name": "taker_amounts", "type": "uint256[][]" },
{ "name": "maker_amounts", "type": "uint256[][]" },
{ "name": "receiver", "type": "address" },
{ "name": "commands", "type": "bytes" },
]
}
const PARAM_DOMAIN = {
name: "BebopSettlement",
version: "2",
chainId: chain.chainId,
verifyingContract: BEBOP_ADDRESS,
}
// Getting quote and submitting it onchain
async function sendTx() {
// Init you wallet
let account = new ethers.Wallet(privateKey)
// Get quote
let quote = (await axios.get(`https://api.bebop.xyz/pmm/${chain.name}/v3/quote`, {
params: {
buy_tokens: tokensAddressBuy.toString(),
sell_tokens: tokensAddressesSell.toString(),
sell_amounts: tokensSellAmounts.toString(),
taker_address: account.address
}
})).data
console.log(quote)
if (quote.error !== undefined) {
return
}
// Choosing order-type for signing
let PARAM_TYPES;
switch (quote.onchainOrderType) {
case "SingleOrder":
PARAM_TYPES = SINGLE_ORDER_TYPES
break
case "MultiOrder":
PARAM_TYPES = MULTI_ORDER_TYPES
break
case "AggregateOrder":
PARAM_TYPES = AGGREGATE_ORDER_TYPES
break
default:
throw Error("Unknown quote type")
}
// Sign quote
let signature = await account.signTypedData(PARAM_DOMAIN, PARAM_TYPES, quote.toSign)
console.log("Signature", signature)
// Send Transaction
let response = (await axios.post(`https://api.bebop.xyz/pmm/${chain.name}/v3/order`, {
signature: signature,
quote_id: quote.quoteId,
})).data
console.log(response)
}
sendTx()
Example for many-to-one swap ( 1 USDC + 2 USDbC -> WETH):
Example for one-to-many swap ( WETH -> USDC + USDbC):
Executing Permit2+gasless PMM-quote from router
Last updated