Skip to content

Instantly share code, notes, and snippets.

@janus
Last active February 23, 2019 07:40
Show Gist options
  • Save janus/29a24b21d972446ce1861d44a0e28b96 to your computer and use it in GitHub Desktop.
Save janus/29a24b21d972446ce1861d44a0e28b96 to your computer and use it in GitHub Desktop.
Get Address Type
open Nethereum
open System
open System.Threading
open Nethereum.Web3
exception NetworkOrServerError of string
let exMsg = "Could not communicate with EtherServer"
type AddressTypes =
| Contract
| Account
| CommunicationIssue of string
let private RetnFromIsAddressFromContract (web3: Web3) (addr: string): Async<string> =
if (web3 = null ) then
invalidArg "web3" "web3 argument should not be null"
async {
try
let task = web3.Eth.GetCode.SendRequestAsync addr
let operation = task |> Async.AwaitTask
let timeout = TimeSpan.FromSeconds 3.0
let cancel = new CancellationTokenSource()
let maybeResult = Async.RunSynchronously(operation, timeout.Milliseconds, cancel.Token)
if (not (maybeResult.StartsWith("0x"))) then
return raise <| NetworkOrServerError(exMsg)
else
return maybeResult
with
| ex -> return raise <| NetworkOrServerError(ex.Message)
}
let IsAddressFromContract (addr: string ) (url: string): AddressTypes =
let web3 = new Web3(url)
try
let response = RetnFromIsAddressFromContract web3 addr
let maybeFound = response |> Async.RunSynchronously
let rslt =
if (maybeFound.StartsWith("0x") && (maybeFound.Length > 3)) then
Contract
else
Account
rslt
with
| ex -> CommunicationIssue(ex.Message)
let PrintAddressType (atp: AddressTypes) =
match atp with
| Account -> printfn "This is an Address<account>"
| Contract -> printfn "This is an Address<contract>"
| CommunicationIssue(msg) -> printfn "Network Issues: <%s>" msg
[<EntryPoint>]
let main argv =
//Testing and with expected results
//Account Address
let fouts = IsAddressFromContract "0xa5Acc472597C1e1651270da9081Cc5a0b38258E3" "https://mainnet.infura.io"
PrintAddressType(fouts)
//Contract address
let fouts = IsAddressFromContract "0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae" "https://mainnet.infura.io"
PrintAddressType(fouts)
//Wrong server
let fouts = IsAddressFromContract "0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae" "https://mainnewt.infura.io"
PrintAddressType(fouts)
//Bad address structure
let fouts = IsAddressFromContract "0xe0b295669a9fd93d5f28d9ec85e40f4cb697bae" "https://mainnet.infura.io"
PrintAddressType(fouts)
0// return an in eger e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment