DAPPCON 2018: Creating Type Safe DApps with Typescript and TypeChain - Krzysztof Kaczor (Neufund)

DappCon 2018 · July 2018 · 14:57

Krzysztof Kaczor of Neufund presents type-safe dapp development at DappCon 2018, covering how TypeScript and the TypeChain tool generate typed bindings for smart contracts to catch integration errors before deployment. The talk addresses practices for building more reliable decentralized applications on Ethereum.

Speakers

Topics

From DappCon 2018, hosted by Dappcon Berlin.

Transcript

hi I'm Chris and I work at no funk so briefly speaking no font is trying to build a centralized Nasdaq when you can invest in securities in the companies and you know don't get don't end up in jail because we're fully compliant so okay so word type happens in our in my title of the talk free time so this talk is gonna be about types and type safety while communicating with blockchain so let me start with how many people here use typescript in production already okay we have some people so that's good so let me start with a pretty bold

statement like javascript is pretty good language by some javascript devs so what I mean by this is that in the past JavaScript was pretty meme language like it there were lots of odd behaviors like scoping rules or you know one plus empty array gives you something something weird so there's like else of implicit conversions going on and so on and people you know used to make fun of JavaScript but the modern JavaScript like for example this code looks nothing like you know JavaScript from five years ago we have now dedicated syntax sugar for creating classes like you it looks

like in Java even though it's just a single a syntactic sugar on top of prototypical inheritance and so on but you know it looks similar for two other languages which makes people happy there's a our fine there are also our functions that tend to solve the issues with the function context at least at some point and for example we got also dedicated syntax for a wait for working with promises which is pretty awesome things to have since javascript is inherently a synchronous language but even with all of these improvements javascript is still dynamic language and even simple cover in this simple code snippets we have bug-like on line

free we forgot to await or I don't know catch an error in the promise so we're just dis called floating promise when we forget to handle it and if the promise would fro would have very hard time finding the error so type script is even better language this is an attempt to fix all these problems and sneak type safety into JavaScript without changing the language itself so it turns out that we don't have to introduce any changes to the first codes in pet this is a valid fully valid typescript code when typescript compiler can infer type for example of the meow function to return a promise of

void so typescript compiler is pretty good at guessing the types and here the same code as previously we're gonna just by using very basic static analyzes we can find things like floating promises so even though typescript compiler it doesn't really care about this such such earth it allows build tools on top of the typescript language services and so on that will vary that will find these mistakes in a very easy manner so this is just a type TS means no floating promises that for example we will find it when we have a promise that we forgot

to hand it in anyway so you can think about typescript as a javascript with types so on the Left we see JavaScript code and on the right typescript code so you see that we needed to provide some type hints I mean types really type informations for arguments of the function but return types get inferred and also like this function assign variable assignment the type gets inferred based based on the return type of the function so we don't need to put the types everywhere but probably you should because having like explicit function signatures is there other good things to have

so you might think that okay so if JavaScript is almost like Java hands the name then types gives is like definitely like Java right or c-sharp it turns out that it's not true like typescript type system is pretty unique so it's it's structure it features structure typing so in Java if you have a Java features nominal typing so if you have two classes with the same set of fields but with different names these are totally different types in typescript on the other hand the shape of an object determine eyes if this objects are has have the same types or not so this is this is something that goes very well

with duck typing from JavaScript so if something walks like a duck and quacks like a duck then probably it's a duck so this is the same principle but Express on the type in that type levels type safety manner so type it also has some pretty advanced type of features like for example conditional types or map types that allows us to create for example if you write a react Redux application we can have we can create fully typed messages actions in redux so when you act upon the action in reducers you know exactly what the payload is for example and this is fully types if the

same goes for example for creating the event emitters with you know the notes event emitter so you do on and and the string really that identifies the event and we can also make this type safe so we don't need to you know change the way that we write JavaScript so to make it fully type safe so types are entirely stripped out in from the output so in the end it's just JavaScript and having these types allows us for building tools that allow advanced refactoring so you know advanced refactoring like for example renaming symbols this doesn't work in JavaScript at all and this is like the

very basic thing that you should have and you know if types gifts all of this works also this type system is gradual so we don't have to put types everywhere you can put any any is a type that you can do whatever you want with so it doesn't also force you to use types everywhere but you should so let's get back to the blockchain words so currently interacting too if you would want to communicate with blockchain probably would use web free or Marla is the same applies for for other frameworks like we create we feed we create based on a bi definition some kind of contract wrapper and then we initiate it on on a given address and

then we can call call some some methods pure methods or we can create the transactions and come and create and send it to blockchain so there are multiple things wrong with this code example like first of all web free works in the way that if you would initiate a contract on an address that it's empty without cold it will happily invoke any calls and just return zeros instead of real answer so like developer experience is really not that great other problems includes like you know if you communicate with smart contracts you need to browse all the time ABI or original source code because we have no documentation you don't know what what

are the methods what are the arguments and also what we like to you know convert your arguments in the weird manner so you know you need to be careful and also like there's a huge problem that you don't know really which method is pure and which will expect you to produce transaction which is another pain in the ass to work with so enter type change so type ten solves all these problems by generating typed wrappers based on the based on ABI definitions so it takes ABI generates typescript wrapper and this happens is a separate

build step so you would do it normally before compile a compilation step so let me briefly show you how it works so imagine that we have some smart contract actually named dump contract because it doesn't do anything and we have a bunch of methods and events and a bunch of transactions and based on this we run type train with a comment more or less like this so we just specify a glob pattern to find all the API files and then it will generate wrappers like this so constructor of this wrapper expects

that you will provide web free instance and the address that the contract lives in and API definition is in line from the JSON file and then we have a bunch of wrappers on the methods that were that will be produced by web fee in the runtime but here it's fully typesafe so if you would like to I don't know check out what we can do then I don't know call encounter this actually I gather only so actually this is an issue and github that people don't like the the Gator you don't need to call it because it's just together but anyways you can work with events and you know I the cool

thing is that I don't really need to remember what I need to do because you know you just type comment space and you get all the of the country all the methods of the given contracts so yeah it supports transactions pure methods and so on so continuing so as I said it's fully statically typed so based on ABI we can generate ABI you can generate all the needed wrappers it has full ideas ID support because it's just typescript if your IDE supports types give it supports type train

so API has lots of tweaks and safety checks that allow for better developer experience for example there's a method to create and validate which you can use to initiate to initiate the the smart contract and this method will make sure that actually the contract is deployed so maybe it's not so useful in production but it's super great for for why you develop things because you know gonna check crashes use restarted and then your contract is not there and you're you know just wasting time so it's fully compatible with with existing libraries because it uses web free in the under the hood and it's extensible the wrappers that were created by type

train you just can't extend them and introduce additional checks or whatever you want so why would you want to use it you could see me clearly that I could just you know type comment space and get all the list of the smart contract and you know it saves me a lot of time and irritation really and another great feature is that it can greatly help when you migrate your smart contracts because then you just provide your ABI you regenerate the wrappers and then you get all the braking changes in your IP is by the found by the typescript compiler automatically for you so this is pretty cool use guys so what are the plans for

future currently current standard for ABI is quite limiting what I'm saying is that for example imagine that most of the times you don't work with single smart contracts you work with a whole set of smart contracts the graph you could say so ABI strips out the information about connection between the smart contracts because it's just an address for the ABI so if you would say in solidity that you have a variable that is it of a type of another contract in ABI it will be just an address so this is something that could be improved I remember talk from yesterday about the compiler metadata JSON so this is probably one way to solve it another way

would be to introduce to expand the ad I probably buy some new AP and improve it for other app for example the metadata to API API definition for example another thing that we're looking it is user defined templates so currently type train forces you to use the smart contract in a certain way what we could do in the future is just allow users to provide you know masked edge template or whatever and generate the smart contracts based on you know to fit your needs really what we need and currently typed in uses web free to point

something as zero 0.2 something because it was the most stable version that we used and we stick to this but i think that 1.0 is on the horizon and probably we should migrate but anyways if you would allow user-defined template templates then user could you know write the template to use web free or it urges or whatever suits you so type chain is fully open source it's available on github and we got pretty a lot of traction I would say there are at least two add to startups other than Noah font using it you get some pretty nice community contributions like for example

event support was done only partially because we don't didn't really need it but someone needed so he added it so you know feel free to check it out on github this is my Twitter you can also follow me and that's it thank you