Skip to content

Instantly share code, notes, and snippets.

@alexdesousa
Last active March 7, 2022 20:41
Show Gist options
  • Save alexdesousa/e8cd1d64a62b74aed14f07b34ce811f3 to your computer and use it in GitHub Desktop.
Save alexdesousa/e8cd1d64a62b74aed14f07b34ce811f3 to your computer and use it in GitHub Desktop.
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