Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Getting sICX/bnUSD quote prices using Elixir's ICON SDK
#!/usr/bin/env elixir
Mix.install([
{:icon, "~> 0.1"}
])
defmodule Quotes do
use Yggdrasil
alias Icon.Schema.Types.EventLog
@dex_contract "cxa0af3165c08318e988cb30993b3048335b94af6c"
@sicx_bnusd_id 2
@signature "Swap(int,Address,Address,Address,Address,Address,int,int,int,int,int,int,int,int,int)"
@channel [
adapter: :icon,
name: %{
source: :event,
data: %{
addr: @dex_contract,
event: @signature,
indexed: [@sicx_bnusd_id, nil]
}
}
]
def start_link, do: Yggdrasil.start_link(__MODULE__, [@channel])
@impl Yggdrasil
def handle_event(_channel, %EventLog{} = swap_event, _state) do
[_, _, _, _, _, _, timestamp, _, _, _, _, price, _] = swap_event.data
datetime =
timestamp
|> DateTime.from_unix!(:microsecond)
|> DateTime.to_iso8601()
price = price / 1_000_000_000_000_000_000
IO.puts("[#{datetime}] sICX/bnUSD price: #{price}")
{:ok, nil}
end
end
# Avoid ending too soon
{:ok, pid} = Quotes.start_link()
ref = Process.monitor(pid)
receive do
{:DOWN, ^ref, _, _, _} -> :ok
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment