Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

[#3] Add returnTicker API #40

Merged
merged 6 commits into from
Feb 21, 2018
Merged

[#3] Add returnTicker API #40

merged 6 commits into from
Feb 21, 2018

Conversation

freeatnet
Copy link
Contributor

@freeatnet freeatnet commented Feb 16, 2018

See #3 and previous work in #18.

With massive shouts to @JonathonDunford for previous digging.

@freeatnet freeatnet added enhancement New feature or request WebSocket Server labels Feb 16, 2018
@freeatnet freeatnet force-pushed the 3-return-of-the-ticker branch 2 times, most recently from 044d7ac to 70c1d12 Compare February 17, 2018 11:45
With massive shouts to @JonathonDunford for previous digging.
@JonathonDunford
Copy link
Contributor

return of the ticker LOL

Copy link
Contributor

@JonathonDunford JonathonDunford left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most comments are for clarification. Nothing holding us back from pushing.

depends_on = None


def upgrade():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks beautiful. Like something I would write :P


getcontext().prec = 8

# TODO: finally extract these into constants
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of these days, we will make this a constant

ZERO_ADDR_BYTES = Web3.toBytes(hexstr=ZERO_ADDR)

tokens_queue = Queue()
# TODO: Populate from our own DB
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is pretty important. Is there any way to import it from the main config for now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really.

WHERE ("token_give" = $2 OR "token_get" = $2)
AND "date" >= NOW() - '1 day'::INTERVAL
""",
Web3.toBytes(hexstr=ZERO_ADDR), # TODO: We can probably do with just token address
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is future proof allowing a secondary address to check against though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough!


async with App().db.acquire_connection() as conn:
return await conn.fetchrow("""
SELECT
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beautiful. Has this SQL been tested?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeap, I've snuck it into production at this point, even.

spread = await get_market_spread(token_addr, current_block)
if spread["ask"]:
# Spread comes back in token/base native values, denormalize it here
ticker_info["ask"] = spread["ask"] * base_contract.denormalize_value(1.0) \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we multiplying by denormalized 1?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally, we'd convert amount_get and amount_give and then compute the price; however, ask and bid returned by the DB are max/min prices for their respective sides of the orderbook, so we convert them to eth-values using this formula.

token = tokens_queue.get()
await update_ticker(token)
tokens_queue.put(token)
await asyncio.sleep(2.0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did you arrive at 2 seconds?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Update every ticker about every half hour", with N_tokens = 1000 gives us ~2 seconds, ignoring processing time. Open for updates and discussion though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we run multiple threads of this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could. Do we need to?

""")

def ticker_key(ticker):
return "{}_{}".format("ETH", Web3.toHex(ticker["token_address"])[:9])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a little comment saying what's going on here.


def format_tickers(tickers):
return {
ticker_key(ticker): format_ticker(ticker)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python voodoo magic

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AKA magic

for ticker in tickers
}

@routes.get('/returnTicker')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OOOH Http too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed!

@freeatnet freeatnet changed the title [WIP][#3] Add returnTicker API [#3] Add returnTicker API Feb 20, 2018
@freeatnet freeatnet merged commit 27ec5a4 into master Feb 21, 2018
@freeatnet freeatnet deleted the 3-return-of-the-ticker branch February 21, 2018 17:59
@freeatnet freeatnet restored the 3-return-of-the-ticker branch February 21, 2018 18:48
@freeatnet freeatnet deleted the 3-return-of-the-ticker branch February 22, 2018 21:47
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request WebSocket Server
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants