Skip to content

play with dex

Yang Xu edited this page Dec 20, 2019 · 5 revisions

Please refer the client/transactions-api to see how to play with the dex chain. You can:

  • issue the token

    await api.tx.tokenModule.issue(symbol, amount).signAndSend(account);

    You can also subscribe result and confirm status is finalized:

    api.tx.tokenModule.issue(symbol, amount).signAndSend(account, (result) => {
        if (result.status.isFinalized) {
            const record = result.findRecord("tokenModule", "Issued");
            const event = record.toJSON().event.data;
            console.log(event); // Issued(AccountId, Hash, Balance)
        }
    });
  • transfer the token

    await api.tx.tokenModule.transfer(tokenHash, toAccountId, amount).signAndSend(account);

    You can also subscribe result and confirm status is finalized:

    api.tx.tokenModule.transfer(tokenHash, toAccountId, amount).signAndSend(account, (result) => {
        if (result.status.isFinalized) {
            const record = result.findRecord("tokenModule", "Transferd");
            const event = record.toJSON().event.data;
            console.log(event); // Transferd(AccountId, AccountId, Hash, Balance)
        }
    });
  • create the trade pair

    await api.tx.tradeModule.createTradePair(baseTokenHash, quoteTokenHash).signAndSend(account);

    You can also subscribe result and confirm status is finalized:

    api.tx.tradeModule.createTradePair(baseTokenHash, quoteTokenHash).signAndSend(account, (result) => {
        if (result.status.isFinalized) {
            const record = result.findRecord("tokenModule", "TradePairCreated");
            const event = record.toJSON().event.data;
            console.log(event); // TradePairCreated(AccountId, Hash, TradePair)
        }
    });
  • create the limit order

    await api.tx.tradeModule.createLimitOrder(baseTokenHash, quoteTokenHash, type, price * 10 ** 8, amount).signAndSend(account); // type: [buy: 0, sell: 1], if order is buy, amount = price * want_to_buy_amount

    You can also subscribe result and confirm status is finalized:

    api.tx.tradeModule.createLimitOrder(baseTokenHash, quoteTokenHash, type, price * 10 ** 8, amount).signAndSend(account, (result) => {
        if (result.status.isFinalized) {
            const record = result.findRecord("tokenModule", "OrderCreated"); // OrderCreated (accountId, baseTokenHash, quoteTokenHash, orderHash, LimitOrder)
            const record2 = result.findRecord("tradeModule", "TradeCreated"); // TradeCreated (accountId, baseTokenHash, quoteTokenHash, tradeHash, Trade)
    
            const event = record.toJSON().event.data;
            console.log(event);
            const event2 = record.toJSON().event.data; // if matched will received tradeCreated event
        }
    });
  • cancel the limit order

    await api.tx.tradeModule.cancelLimitOrder(orderHash).signAndSend(account);

    You can also subscribe result and confirm status is finalized:

    api.tx.tradeModule.cancelLimitOrder(orderHash).signAndSend(account, (result) => {
        if (result.status.isFinalized) {
            const record = result.findRecord("tokenModule", "OrderCanceled");
            const event = record.toJSON().event.data;
            console.log(event); // OrderCanceled(accountId, orderHash)
        }
    });

Tips: If you want to use it to connect your node with some other runtime modules. You need append additional types In config.js/types and replace endPoint url. This is nessusary if the runtime modules uses types not available in the base Substrate runtime.

Of course you can play with it by polkadotjs wallet frontend with:

git clone https://github.com/polkadot-js/apps
cd apps
yarn
yarn start

Then open the link http://localhost:3000/#/?rpc=ws://127.0.0.1:9944

Clone this wiki locally