Giter Club home page Giter Club logo

Build Status codecov

web3scala

web3scala allows seamless integration with Ethereum blockchain, using Scala programming language.

Lightweight, efficient, using Scala idioms, it spares you the trouble of writing own low-level code controlling the work of Ethereum nodes.

Features

  • Complete implementation of JSON-RPC Ethereum client API over HTTP
  • Support for Whisper v5 (work in-progress)

Getting started

SBT

libraryDependencies += "org.web3scala" % "core" % "0.1.0"

Ethereum client

$ geth --rpcapi personal,db,eth,net,web3,shh --shh --rpc --testnet

Sending requests

  val service = new Service
  
  // synchronous call (returns Either[Error, Response])
  service.web3ClientVersion match {
    case Left(e) => println("Error: " + e.error)
    case Right(s) => println("Client Version: " + s.result)
  }

  // asynchronous call (returns a future wrapped in AsyncResponse)
  val future = service.asyncWeb3ClientVersion.future
  val response = future().as[GenericResponse]
  response.result match {
    case Some(s) => println(s)
    case None => println(response.error)
  }

Stacking futures

Assuming you have three Ethereum wallets:

  val rq1 = ("0x1f2e3994505ea24642d94d00a4bcf0159ed1a617", BlockName("latest"))
  val rq2 = ("0xf9C510e90bCb47cc49549e57b80814aE3A8bb683", BlockName("pending"))
  val rq3 = ("0x902c4fD71e196E86e7C82126Ff88ADa63a590d22", BlockNumber(1559297))

and want to choose one with most Ether in it:

  val result = highestBalance(rq1, rq2, rq3)

  println("Highest Balance: " + result())

Here's how to achieve that with web3scala:

   def highestBalance(requestParams: (String, Block)*) = {
     
     // execute async requests
     val responses =
       for (requestParam <- requestParams)
         yield requestParam._1 -> service.asyncEthGetBalance(requestParam._1, requestParam._2)
 
     // parse responses
     val futures =
       for (response <- responses)
         yield for (json <- response._2.future)
           yield response._1 -> Utils.hex2long((json \ "result").extract[String])
 
     // select max balance and return corresponding address
     for (future <- Future.sequence(futures))
       yield future.maxBy(_._2)._1
   }

Result:

$ Highest Balance: 0x1f2e3994505ea24642d94d00a4bcf0159ed1a617

The code is non-blocking on I/O at any point, and http requests execution fully parallelized. You'll find a working sample in the examples directory.

Dependencies

The library has following runtime dependencies:

Web3scala's Projects

web3scala icon web3scala

Scala library for integration with Ethereum clients

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.