Main method which allows to obtain information about a swap: amounts of asset in and asset out for a given swap. Can be set for execution using only Orion pools.
import { simpleFetch } from "@orionprotocol/sdk";
const swapInfo = await simpleFetch(orionUnit.orionAggregator.getSwapInfo)(
// Use 'exactSpend' when 'amount' is how much you want spend. Use 'exactReceive' otherwise
"exactSpend", // type
"ORN", // asset in
"USDT", // asset out
25.23453457 // amount
// Exchanges. OPTIONAL! Specify 'pools' if you want "pool only" swap execution. Specify 'cex' if you want "cex only" execution
);
Sends an order to the Orion Aggregator. All orders placed with this method are limit orders set at a given price.Token addresses are obtainable with asset tickers among the assets available on the Orion Protocol.
import { simpleFetch, crypt } from "@orionprotocol/sdk";
import { Exchange__factory } from "@orionprotocol/contracts";
const myAddress = await signer.getAddress(); // or wallet.address (without await)
const baseAssetAddress = "0xfbcad2c3a90fbd94c335fbdf8e22573456da7f68";
const quoteAssetAddress = "0xcb2951e90d8dcf16e1fa84ac0c83f48906d6a744";
const amount = "345.623434";
const price = "2.55";
const feeAssetAddress = "0xf223eca06261145b3287a0fefd8cfad371c7eb34";
const fee = "0.7235"; // Orion Fee + Network Fee in fee asset
const side = "BUY"; // or 'SELL'
const isPersonalSign = false; // https://docs.metamask.io/guide/signing-data.html#a-brief-history
const { chainId } = orionUnit;
const {
matcherAddress, // The address that will transfer funds to you during the exchange process
} = await simpleFetch(orionUnit.orionBlockchain.getInfo)();
const signedOrder = await crypt.signOrder(
baseAssetAddress,
quoteAssetAddress,
side,
price,
amount,
fee,
myAddress,
matcherAddress,
feeAssetAddress,
isPersonalSign,
wallet, // or signer when UI
chainId
);
const exchangeContract = Exchange__factory.connect(
exchangeContractAddress,
orionUnit.provider
);
const orderIsOk = await exchangeContract.validateOrder(signedOrder);
if (!orderIsOk) throw new Error("Order invalid");
const { orderId } = await simpleFetch(orionUnit.orionAggregator.placeOrder)(
signedOrder,
false // True if you want place order to "internal" orderbook. If you do not want your order to be executed on CEXes or DEXes, but could be filled with the another "internal" order(s).
);
Source code
Orion Aggregator Websocket
Subscription for information – aggregator, update of pair configurations, bids and asks, internal balances used in the Orion Bridge.
ADDRESS_UPDATES_SUBSCRIBE = 'aus', // Orders history, balances info
SWAP_SUBSCRIBE = 'ss', // Swap info updates
AGGREGATED_ORDER_BOOK_UPDATES_SUBSCRIBE = 'aobus', // Bids and asks
ASSET_PAIRS_CONFIG_UPDATES_SUBSCRIBE = 'apcus',
BROKER_TRADABLE_ATOMIC_SWAP_ASSETS_BALANCE_UPDATES_SUBSCRIBE = 'btasabus', // Need for Orion Bridge
Swap info
Returns swap parameters for a given ID.
const swapRequestId = orionUnit.orionAggregator.ws.subscribe(
"ss", // SWAP_SUBSCRIBE
{
payload: {
i: assetIn, // asset in
o: assetOut, // asset out
e: true, // true when type of swap is exactSpend, can be omitted (true by default)
es: ['ORION_POOL'] // OPTIONAL! Specify ['ORION_POOL'] if you want "pool only" swap execution
a: 5.62345343, // amount
},
// Handle data update in your way
callback: (swapInfo) => {
switch (swapInfo.kind) {
case "exactSpend":
console.log(swapInfo.availableAmountIn);
break;
case "exactReceive":
console.log(swapInfo.availableAmountOut);
break;
}
},
}
);
orionAggregator.ws.unsubscribe(swapRequestId);
Source code
Balances and order history stream
Returns updated balance with changed assets.
orionUnit.orionAggregator.ws.subscribe(
"aus", // ADDRESS_UPDATES_SUBSCRIBE — orders, balances
{
payload: "0x0000000000000000000000000000000000000000", // Some wallet address
callback: (data) => {
switch (data.kind) {
case "initial":
if (data.orders) console.log(data.orders); // All orders. "orders" is undefined if you don't have any orders yet
console.log(data.balances); // Since this is initial message, the balances contain all assets
break;
case "update": {
if (data.order) {
switch (data.order.kind) {
case "full":
console.log("Pool order", data.order); // Orders from the pool go into history with the SETTLED status
break;
case "update":
console.log("Order in the process of execution", data.order);
break;
default:
break;
}
}
if (data.balances) console.log("Balance update", data.balances); // Since this is an update message, the balances only contain the changed assets
}
}
},
}
);
orionUnit.orionAggregator.ws.unsubscribe(
"0x0000000000000000000000000000000000000000"
);