Skip to main content

Trade Token

There are different ways to trade tokens over OTR.

Buy Token

To buy a token, you must call buyToken on dataset(Dataset.TOKEN). buyToken takes an object of type TradeTokenTangleRequest as parameter in which you can specify the token you want to buy, the price for the offer and the amount.

    const otrRequestBuy = await otr(otrAddress).dataset(Dataset.TOKEN).buyToken({
count: 10,
symbol: 'IOTA',
price: 0.002,
});

buyToken returns an oject of type OtrRequest<TradeTokenTangleRequest>

Sell Token

Sell Base Token

To sell the base token, you must call sellBaseToken on dataset(Dataset.TOKEN). sellBaseToken takes an object of type TradeTokenTangleRequest as parameter in which you can specify the price for the offer and the amount.

    const otrRequestSellBase = await otr(otrAddress).dataset(Dataset.TOKEN).sellBaseToken({
count: 10,
symbol: 'SMR',
price: 0.002,
});

sellBaseToken returns an oject of type OtrRequest<TradeTokenTangleRequest>

Sell Native Token

To sell a token, you must call sellMintedToken on dataset(Dataset.TOKEN). sellMintedToken takes an object of type TradeTokenTangleRequest as parameter in which you can specify the token you want to buy, the price for the offer and the amount.

    const otrRequestSell = await otr(otrAddress).dataset(Dataset.TOKEN).sellMintedToken('tokenId', {
count: 10,
symbol: 'IOTA',
price: 0.002,
});

sellMintedToken returns an oject of type OtrRequest<TradeTokenTangleRequest>

Get executed trades:

  // Get all active BUYs through live stream. Use getMemberBidsLive to get member's one.
const tokenId = 'tokenId';
await https(origin)
.project(SoonaverseApiKey[origin])
.dataset(Dataset.TOKEN_MARKET)
.getBidsLive(tokenId, TokenTradeOrderType.BUY, TokenTradeOrderStatus.ACTIVE)
.subscribe((bids) => {
console.log(bids);
});

// Get all active SELLs through live stream. Use getMemberBidsLive to get member's one.
await https(origin)
.project(SoonaverseApiKey[origin])
.dataset(Dataset.TOKEN_MARKET)
.getBidsLive(tokenId, TokenTradeOrderType.SELL, TokenTradeOrderStatus.ACTIVE)
.subscribe((bids) => {
console.log(bids);
});

// Get live stream of token purchases
await https(origin)

Full How-To Code

import { Dataset } from '@build-5/interfaces';
import { otr, SoonaverseOtrAddress } from '@build-5/sdk';

async function main() {
// @ts-ignore
const otrAddress = SoonaverseOtrAddress[origin];

try {
const otrRequestBuy = await otr(otrAddress).dataset(Dataset.TOKEN).buyToken({
count: 10,
symbol: 'IOTA',
price: 0.002,
});

var fireflyDeeplink = otrRequestBuy.getFireflyDeepLink();
console.log(fireflyDeeplink);

const otrRequestSellBase = await otr(otrAddress).dataset(Dataset.TOKEN).sellBaseToken({
count: 10,
symbol: 'SMR',
price: 0.002,
});

fireflyDeeplink = otrRequestSellBase.getFireflyDeepLink();
console.log(fireflyDeeplink);

const otrRequestSell = await otr(otrAddress).dataset(Dataset.TOKEN).sellMintedToken('tokenId', {
count: 10,
symbol: 'IOTA',
price: 0.002,
});

fireflyDeeplink = otrRequestSell.getFireflyDeepLink();
console.log(fireflyDeeplink);
} catch (e) {
console.log(e);
return;
}
}

main().then(() => process.exit());