Conor Svensson - Web 3 Java Dapp API

ETHCC 1 · 9 Mar 2018 · 31:39

Conor Svensson, author of web3j, introduces the Java library for working with Ethereum. He covers connecting to nodes over JSON-RPC, sending transactions, generated smart contract wrappers, Truffle integration, reactive event streams, command-line tools and a sample project, and reflects on running the open source project and enterprise use.

Speakers

Topics

From ETHCC 1, hosted by Ethereum France.

Transcript

you okay simone if you promise a contribute everyone I'm Connor Spence and I'm here today to talk about web 3j web three.js the java library for integrating with the theory before we get started i just wanted to give you a bit of background about myself so i come from originally the enterprise financial domain went from working in large investment retail banks into the startup space then got sucked into the whole blockchain

ecosystem and the rest of this history as i've just said I'm the author of web 3j I'm also the founder of a company called block IO which I'll talk about in a minute and very involved in the enterprise of the Ihram alliance I run the EA London meetup I also chair the core and working group the inspiration tools working group and there's a lot of work happening at the moment on a reference specification within the EA which you'll be hearing more about in the not-too-distant future so my company block IO we're focused on that integration layer in the blockchain and enterprise spaces you've got a sort of fairly well-established enterprise stack that is used in a lot of these large institutions and they're

all very interested in blockchain technology but when you've got a technology that's generally been you know written from the ground up focused on specifically public chain use cases there's no plumbing that needs to be built in order to make the to work nicely together and that's what blocka is focused on and so in the context of enterprises of course you know integration libraries like web 3j good for them but then also enterprises want tools to work with block chains as well so you know blockchain explorers for private chains authentication tools that plug into the you know enterprise integration services and so on and so forth so there's a lot there that needs to be addressed but anyway I'm here today to talk about web 3j and

data thinking of data in terms of streams where traditionally you had the observer pattern that was used in coding but the idea with these observables is that you have something that emits a stream of proce a stream of events and then you respond to those events these sort of event-driven architectures are nothing new but they've been become a lot more popular in the last few years and the RX Java and the ActiveX extensions that came out of Netflix they were the pioneers pioneering libraries in that space in terms of the mass adoption that they had it also supports ENS as well so for those of you who want to use ENS and and instead of talking in terms of you know the usual etherium addresses you can specify an ENS any NS

name when you work with the small contractors and transactions and more recently too it's also integrates with truffle so what this means is if you are developing smart contracts using truffle but then you actually once they're developed you want to then plug them in to Java code you can take your metadata files that are generated by truffle and then you can create the smart contract or a pass off the back of that so it integrates those workflows together just making it a bit more straightforward for them to work with so web 3 J it sits in front of a java application in the same way that web web 3 J asana theory and will sit in front of a JavaScript or dotnet library and there was a platoon in that respect in terms of the JSON RPC

support in web 3 J so it's it supports all of the course of aetherium JSON RPC clients api then it also supports the parity and get api's as well so if you have parity or get specific functionality that you want to make use of it's there these are actually separate modules as well so again if you know you're just going to be working with gets API or you're not going to be using any of the parity or get specific stuff and you can do that using web through J without having all that extra code pulled in and again especially with parity there's you know there's a lot of modules there and you know members of the community have been really good about just implementing them and country through contributing them back to the project the trace trace API being a good example of that so if anyone's off or

anything specific they're always welcome just to add it into the project and then it will get bundled with future releases in terms of creating applications using web 3j I've always tried to keep this stuff as simple as possible as per being a good software engineering practices and so in order to actually you know hook up to a theorem node on the network all you need to do is create a new instance of this web 3j object specify where your node is so if you're connecting to it via HTTP which is the default then you just specify you want to use HTTP service but if you want to connect to a different endpoint or if you want to use IPC as well that's

supported too then for the specific JSON RPC method that you want to call you just specify the method name and then you can send these requests synchronously asynchronously or grade and observable off the back of it the library itself is really accessible as well by this was one of the other key things especially you know with open source you've got to be you've got to think about you know how you can make it available to everyone who you want to make it available to especially in enterprises and so on they don't necessarily they're not necessarily able to use all of the standard artifact repositories that are out there and so web three.js available in all of the ones globally that are used for serving up Java or Android artifacts and the two

main ones being Nexus and entry by Jay frog and so what that means is it's just it's easy to get hold of the library included in your project there's also command-line tools that get uploaded with each release and then you can use homebrew as well to set up the command-line tools locally it's now a sample project available as well so you know one of the I think again it's another thing with with open source software especially when people start working with projects there might be instructions for working with it there might be lots of you know good test coverage and so on and so forth but especially with a theory and where there's so much you know you need to learn about as well in order to actually work with it I've

always thought about how can you make this as accessible to people who aren't familiar with the etherium ecosystem and so what I've got is a sample project there which gives you a simple smart contract that generates and then this associated Java code to deploy a smart contract transact with that smart contract to read a value out of that smart contract and then also query the events out of that smart contract as well and transfer ether so it gives you an overview of all of the different stuff that needs to happen while the core things that you use aetherium for in a sharpnel project in a minimal amount of code as well just to ease the accessibility I mentioned earlier on about how web 3js modular now as well and so here at the core modules that you

have they're done in a sort of dependency order there so we've got got utils you've got our LP encoders and decoders API as well and there's a library core is like the core part of web 3j then you've got pieces for working with the page and death parity in fewer two if you want to use that it's a simple example of how you actually connect the verify connection to an aetherium client you basically create a new instance of this web 3j object and then from there you just send the web 3 get client version and then you can display the client version 2 on

the command line so that's all this required and you know Java is known for being of a based language but again it's trying to do this in minimal amounts of code there but still have all of the benefits you have of static typing in the mature ecosystem associated with Java against nd for being one of the core things that we want to do in etherium we create a web 3j instance then we also have a credentials object so if you've got a you know if you've got a private key in the form of just a if it's in a file or if it's you using an etherium wallet file whatever format you've got it in you can load up these credential objects and then you just can send a transaction by specifying the

address you want to get to go to and then the amounts and you've got the different sort of levels of granularity that you get with the ether currency supported in there as well which you see in the convert piece ok and that's all you need to do to send a transaction in your code I'm start to finish so there's command-line tools for doing some of these common cart tasks yet you know you can do this using other tools as well but I thought you know if you if you're making use of the tooling and the library then might as well make something available there so you can you know you can just do brew install web 3j and then if you want to send ether from one wallet address to another address you just do web 3j

wallet send then you unlock your wallet file then you specify where your nodes running that you want to connect to so you can use in furor for instance and then specify how much you want to send the destination address and then you confirm it and then the transaction goes through you can also use the wallet files sorry the the web 3j command-line tools to create a new wallet and so again just web 3j wallet create and then you enter your password and then you've got a new wallet file created on the filesystem again it's just another option for doing this stuff there's there's other ways that can be done to do this used to do this but more importantly we have the

smart contract wrappers the smart contract wrappers these are you know one of the much more powerful features of web 3j you still need to work with solidity but at the same time once you've written your solidity code you can do everything else to Java and so with solidity once you compile the solidity files is you don't no doubt know you get your ABI files you get your binary files the binary files are the EVM instructions they need to be executed to push the smart contract onto the EVM and then the ABI files are your sort of point references to methods on the smart contracts you can also as I mentioned earlier on work with truffled schema files as well so if you're using

truffle base this is a very similar process and so then you get your Java code to deploy call into transact with smart contracts so without web 3je this is what your aetherium the sort of transaction process looks like so on the far left you've got your transaction objects then you've got your wallet key that then signs the transaction then your your signature data gets appended to the transaction it gets encoded sent to the etherium network and then that gets propagated around the nodes and the network then eventually a miner produces another transaction that gets appended to the ethereum blockchain with web 3j you have these nice abstractions at the

start so you know if you want to transfer a fee you want to deploy a smart contract you want to transact with a smart contract you want to call a smart contract you do that via method specifically to do that you don't have to think in terms of an arbitrary transaction object which is what you have to do if you're using stock aetherium without any of this stuff then it takes care of the the signing for you but then the other key thing is that with aetherium in its normal form its kind of fire-and-forget you put a you send a transaction into the network you don't get notified if their transactions taken place successfully and being added to the etherion blockchain and say what web 3j has is a transaction manager that can then be used to poll for when that transactions actually happens of course

though if you've got a lot of transactions going into a theorem you don't necessarily want lots and lots of separate threads proce polling again and again and again against specific nodes on the blockchain so there's different types of transaction managers that can be used you can have ones that run in a single thread but then a you know have a store of all of the pending transactions that you've sent to the blockchain and it'll just to ensure that it can it can scale up as you increase the transaction throughput so just to go through a trivial example we've got the the greeter contract for those of you who aren't familiar with it it's kind of like the hello world in the in etherium where we have a contract which stores a string value when you create that

contract you pass in the string value you want it to store aka the greeting and then you can query the value that's stored within that smart contract which is obviously the greeting here so you generate an ABI file for that greeter along with the actual binary code and so you've got those two artifacts and then at that point you generate your smart contract wrapper so using solidity first of all of course you compile it and then to generate the wrapper you just specify this web 3j solidity generate you say where the bin file is where the AVI file is and then you specify your Java package name that you want to use and then where your destination directory is and then a smart contract wrapper in Java gets

generated there and that's compatible weave that back down to Java 1/6 so if you want to generate wrappers for Android or Java at all it's the same process then from truffle it's a very similar process except you specify web 3j truffle then you just pass in a truffle metadata file this is what the code looks like again you shouldn't actually have to look at this stuff but it's just to give you a flavor of what's happening underneath the hood and so you have at the top there you've got a remote which is returned by all of these events which basically means you can choose when you try and perform a transaction deploying a smart contract or transacting with a smart contract

whether you want it to happen synchronously or asynchronously or put it into an observable and then here behind the scenes it's doing all of the type conversion so what web 3j does for you is it takes the native Java types converts them into avi types then encodes those avi types into the relevant binary format that can be interpreted by the EVM so you get complete type safety going from Java all the way through to the EVM with web 3j and so this is what our hello world looks like so again we create a new web 3j instance then we load our credentials and then we specify we want to deploy agree to contract this is the gas price

and gas limit we want to use and we want to send in the hello blockchain world because it's a hello world example and then we can read our greeting out of the contract so that's generated showing that entire sort of end to end life cycle with aetherium just in four lines of Java code so I mentioned earlier on about rx jar while reactive ex support and this notion of observables being used in web 3j and so it's a really really nice API that's used in reactive X and so if you're not familiar with it there's support for all of the major languages with it but it's it's just a nice abstraction that kind of takes these

ideas from functional programming and it enables you to have an event-based system and you can use functional composition and all those sorts of nice things you get with the functional paradigm but within wave 3j as well and so you have observables for all of the etherium JSON RPC client methods so what this basically means is that when you invoke a JSON RPC message within web 3j you pass in a callback and then that gets executed off the back of the observable it's not particularly useful when you see it in the context of this but it's just to make you where it's available where they're really powerful is composing multiple observables

together because you can just have an admit that you can have a series of events that just could be composed together and you can apply all of your sort of you know the things you associate more with you know functional programming with that so you can do your you know you'll match your reduces your folds or whatever else you want to work with they're all so nice for working with events as well so within these smart contract wrappers that get created they also support any events that have been defined in the smart contracts so what that means is that you have a smart contract you deploy it and then when you can create an observable so you deploy the smart contract and then you want to your code to be notified every time a

specific event takes place on that smart contract and again you can just do that here so I'll give you an example in the familiar ERC 20 contract so if we're using the human standard token which is one of the two I guess best-known ERC 20 implementations so we deploy our ERC 20 contract here and then what we can do is every time a token transfers from one address to another address on the blockchain you can be notified of it and this specific event can event handler can take place so what it's saying is every time this transfer happens between any range of

blocks so you can say you know it can be since the beginning of time so it will then go back and find any historical points in time where that contract might have been used and then each time this transfer event happens this callback is executed so it makes it really really easy to aggregate data or whatever else or you know just just re replay transactions and everything else in aetherium associated with smart contracts there's also an API for working with new blocks as was sorry for processing blocks too so what's so nice about you know the etherion blockchain it's an it's an immutable data structure and so you know it's event based and so you've you've got this really nice

synergy between the observables type abstraction where you're thinking in terms of series of events taking place and then you've also got the etherion blockchain which is each block is you know a new event that's being happening within the blockchain so to speak that's separate from the notion of a theorems event log or you know log events and so if we want to process all historic blocks that are in the theory and blockchain that's how we do it we just say we want a block observable then this will replay the blockchain every time a new block gets created on the etherium blockchain you've got some code there that can just do some arbitrary processing on that block we can also replay transactions as well

so if you want to replay a range of transactions in aetherium - it's just it's another it's another statement a single statement here where you just say replay transactions observables say what range you want to work with and then you just pass in your your methods for so your function that you want to act on every single block as they get replayed through the history and then you've also got very lengthy names but it's basically a method that enables you to replay the entire history of the blockchain from the beginning and then all future subsequent blocks as well so you give it you know it's one command you pass in your callback and this is all you need to do in order to replay the entire trip previous as well as

future transaction history on a new theory in blockchain and it's not too bad in terms of how long it takes so back when the ROPS the network was still you know before it went before it started being the favorite place for spammers to attack it you could replay the entire transaction history of Rob stand last last summer when it was just shy of a million blocks in seven minutes and then all transactions within you know in about 40 minutes and so the point is it's not about doing it as quickly as possible it's still going over the JSON RPC API but it gives you just a flavor of the fact it's not insurmountable if you just want to run something across the entire history of the blockchain okay so web 3 J 3.1 that

came out said at the end of last year and so that added in as I mentioned earlier on ENS support so now you have yeah with all of these examples here as shown in terms of smart contracts and using aetherium addresses but you can actually use ENS as well and I mentioned as well that they've got the chase the truffle JSON file support as of last week though web 3 J 3.3 came out and so that now supports pneumonic star wallets too so if you want to have like a series of phrases to create a wallet and the ADEs recoverability you can do that now it also supports ENS reverse resolution reverse the name resolution as

well this was something both of these members things that members of the community submitted and now to those encoders therefore checksum addresses which is something there was a bit overdue but it's now available in terms of the actual community so the project's 18 months old we've just surpassed a thousand stars on github and there's been 46 different country contributors to the projects as well it's averaging over 7,000 downloads per month and so these are the core a web 3j artifacts so if you tally up you know all of the artifacts it's a much bigger number but these are the the number of times it's just happening I'm also always on the lookout for you know trying to get pull people into the fold

as well and so we've got some really interesting mini projects that have highlighted that would be available for web 3j so one of these Nick Johnson and we he was hacking around before with adding in Java card support for get well adding Java card support into web 3j would be something that would be pretty straightforward because Oracle obviously provide a good amount of Java support for Java cards but you know that's something that would be you know a cool little project that would be fairly self-contained but is something that would be of great benefit to the community there also support for WebSockets as well in web 3j because right now it's it supports IPC HTTP it allows and support the WebSockets API which is we'd get and then to a Gradle

plugin as well but I'm sure that that's something it'll care before too long it's also helped wanted labels too so if you want to just do work on a small task as well there's this stuff there too that you can pick up and have highlighted those issues with with labels on the projects so this is what the overall web 3j ecosystem looks like now so there's the the Java library but then there's also the Android library there's a version of web 3j that supports quorum - there's a integration with spring boots as well which is used very widely within enterprise and Java space and there's a web 3j maven plug-in and it's got to the point now where there's enough interest in the projects

and enough users that there's even you know web 3j training courses like a two-day course that's being run later this year as well and so you know feel that it's been you know really steadily growing community but there's you know good growth of users and interest in the project there so for further information there's yeah everything's up there I've got some web 3j t-shirts here as well there's a couple of stickers left down there that's all I had to say about web 3j anyone have any questions hello do you have any support or plan to

support any kind of good interface for blog reorganizations or confirmation times to deal with events that happened and stopped happening and then happen again to do block realizations anything at the moment not as such within the library I think really it comes down to you know what information is being admitted to us via the you know the etherium client that we're working with ourselves I know that there there has been issues around good as you say with certain events and of changing over time but it's it's not something that users

have expressed much of a need for and I think the challenge you have as with all open source projects is that it's a balancing actually do this stuff for free and as long as there's not lots of people affected by it or highlighting it then it tends not to be dealt with as a priority can you tell us a little bit about how do you work with contributors sorry how do I work with contributors to the code requests like is there any set any

process in place or how do you hope to deal with with them in the future to encourage them to contribute to in terms of encouraging people it's yes this is I try and you know put out as much information as I can about the project itself so there's been information published to the wider because I feel anyway they're connecting to the wider tech community outside of the blockchain community is really important so I've I've had Oracle published content and web 3j of spoken at a number of regular tech conferences about it as well and so that's in order to pull people into the fold that's one way of doing it in terms

of what the contribution process looks like though it's much like with any open source project whereby there's there's issues there that people can address or they might find their own thing problem that they want to solve they submit a pull request there then we review the pull request and then it gets merged and so it's in terms of the timelines associated with it it varies because again it's just a project that's never received any external funding it's been driven entirely and off my own back and then with you know the community getting involved but it's following that sort of fairly standard process there in terms of ensuring quality though there's that's an area that I think is important

and so you have you you you've got like your Travis CI which is standard because you get that for free with open source projects but then also it's amusing check style there as well so you've got code quality and styling rules that have to be adhered to but and so you get that degree of automation where if someone submits a pull request the entire you know regression suite get sorry there or it's got to pass all the unit testing as per the CI the code quality's got to be good as well and the overall test coverage can't change massively either I can't change downwards it says it's fairly standard the in that regard okay all right thanks everyone

[Applause]

Related talks