Skip to content

Instantly share code, notes, and snippets.

@leeduckgo
Created February 10, 2024 09:18
Show Gist options
  • Save leeduckgo/288ea0eb7f249d44c02a7ecb18eee464 to your computer and use it in GitHub Desktop.
Save leeduckgo/288ea0eb7f249d44c02a7ecb18eee464 to your computer and use it in GitHub Desktop.
CodesOnChain.Contracts.RMUDLottery.ex
defmodule CodesOnChain.Contracts.RMUDLottery do
@moduledoc """
the interact with RMUDLottery on mappo chain.
> https://mainnet.optimism.io
> 10
> https://explorer.optimism.io
"""
alias Ethereumex.HttpClient
alias Components.Transaction
require Logger
@endpoint "https://rpc.maplabs.io"
@chain_id 10
@contract_addr "0xed8b05159460c900f12075c3b901ca274fd7486f"
@func %{
balance_of: "balanceOf(address)",
}
@gas_limit 1_000_000
def get_module_doc, do: @moduledoc
def if_bigger_than(addr, num) do
num_dec = Decimal.new("#{num}")
not Decimal.lt?(balance_of(addr), num_dec)
# bigger or eq
end
def balance_of(addr) do
# addr_bin
addr_bin = TypeTranslator.addr_to_bin(addr)
# get data
data = TypeTranslator.get_data(@func.balance_of, [addr_bin])
# get raw response
{:ok, raw} =
HttpClient.eth_call(
%{
data: data,
to: @contract_addr
},
"latest",
[url: @endpoint, request_timeout: 1000]
)
# decode
[balance] =
raw
|> Binary.drop(2)
|> Base.decode16!(case: :lower)
|> ABI.TypeDecoder.decode_raw([{:uint, 256}])
balance
|> Decimal.div(1_000_000_000_000_000_000)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment