Skip to content

Query order balance via smart contract bypassing API #1

Description

@saturn-network

Using Saturn API simplifies a lot of the tricky bigint math that is essential in making Saturn Network exchange work. There are certain places where living without API is almost impossible, i.e. serving the order book for a token in under <100ms - you'd have to reimplement the API yourself. The API simply acts as a high-performance low-latency cache, and blockchain acts as the slow & secure source of truth for it.

Querying for the blockchain directly for the order balance, however, right before executing the order makes perfect sense. A good first contribution would be to modify this function to stop relying on the API and query the blockchain directly.

private async verifyCapacity(amount: number, order: Order) {
if (amount > Number(order.balance)) {
throw new Error(`
You attempted to trade more tokens (${amount}) than are available in the order (${order.balance}) for order_tx ${order.transaction}.
`.trim())
}
}

Some helpful notes to get you started:

  1. Review package.json of saturn.js and look at the scripts section. Hint: npm run build does something useful.
  2. Learn to use yarn link or npm link to test drive your local copy of saturn.js with your scripts.
  3. Some code snippets that you might find useful
let decimals = await token.decimals()
let rawOrderBalance = await exchange.remainingAmount(order.order_id)
let parsedOrderBalance = new Fraction(rawOrderBalance.toString()).div(new Fraction(10).pow(Number(decimals.toString())))

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions