Created
October 10, 2021 08:59
-
-
Save lukeledet/2d07f74d7dc1034630e3cc7eb96d2fb0 to your computer and use it in GitHub Desktop.
Schedule engineering meetings from a list of people on the blockchain and a given start date
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Mix.install([ | |
{:ethereumex, "~> 0.7.0"}, | |
{:ex_abi, "~> 0.5.2"}, | |
{:ex_keccak, path: "../ex_keccak", override: true} # fixed a bug locally | |
]) | |
# Application.put_env(:ethereumex, :url, "https://polygon-rpc.com/") | |
Application.put_env(:ethereumex, :url, "https://rpc-mumbai.matic.today") | |
defmodule MeetingScheduler do | |
# testnet contract address | |
@contract_address "0x9C55AE6cB109Bded7ee8336161D0a5c90EAAB897" | |
def generate_schedule(start_date) do | |
start_date = Date.from_iso8601!(start_date) | |
engineers = get_engineers() | |
engineers | |
|> Enum.reduce([], fn engineer, [{date, _}|_] = acc -> | |
next_date = Date.add(date, 7) | |
if next_date.day <= 7 do | |
[{Date.add(next_date, 7), engineer} | [{next_date, "Planning"} | acc]] | |
else | |
[{next_date, engineer} | acc] | |
end | |
engineer, acc -> | |
if start_date.day <= 7 do | |
[{Date.add(start_date, 7), engineer} | [{start_date, "Planning"} | acc]] | |
else | |
[{start_date, engineer} | acc] | |
end | |
end) | |
|> Enum.reverse() | |
|> IO.inspect | |
end | |
def get_engineers() do | |
abi_encoded_data = | |
ABI.encode("getAgenda()", []) | |
|> Base.encode16(case: :lower) | |
ethereum_call(@contract_address, abi_encoded_data) | |
end | |
defp ethereum_call(address, data) do | |
{:ok, result} = | |
Ethereumex.HttpClient.eth_call(%{ | |
data: "0x" <> data, | |
to: address | |
}) | |
result | |
|> String.slice(2..-1) | |
|> Base.decode16!(case: :lower) | |
|> ABI.TypeDecoder.decode_raw([{:array, :string}]) | |
|> List.first() | |
end | |
end | |
[start_date|_] = System.argv() | |
MeetingScheduler.generate_schedule(start_date) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment