Building a Solana Program from Scratch

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so i just wanted to very quickly go over some basic solana concepts i think a lot of the tutorials out there um skip over what's going on under the hood and i always like to play with very fundamental building blocks of a blockchain first so that i understand if something goes wrong and i can debug easily and i have a wider range of tools at my disposal for debugging or i guess it's just fun it's more fun to code that way anyway so uh i've created some projects before but i'll ignore them first um so i i'm gonna assume that you already know the command line a little at least it's very basic uh if you don't have it set up then go to solana docs.solana.com and click command line guide i have 1.7.11 i don't know if that's the latest or whatever but because i had to compile this myself um to run on an m1 um it's not which also is not that surprisingly pretty easy um anyway um set it up understand it there's not too much to it you don't actually have to use this cli tool to be productive on solana you can completely bypass it if you need to um but i i just i just think it's faster for some things one thing is creating keygens which is essentially just a wallet a wallet consists of a public key and a private key um those are kind of mashed together in a um [Music] ed something something i can't remember the name of it it's a it's an elliptical curve encryption mechanism anyway uh so you can sort of like uh use the cli tool to create wallets or create key pairs um and then pull them back out the reason that you would want to do this or pull back up in your code the reason you would want to do this is because unlike ethereum just to run a blockchain query that queries a program or a smart contract costs some solana and it's only a minute amount but it does cost you need to have a funded wallet if you don't it won't work you can query basic information um in a read-only fashion just like ethereum but not if it's if it's in any way dynamic and that differs from ethereum ethereum does allow you to sort of have some processing on the node end and you can sort of call read-only methods called view methods and they are free the reason is because obviously if something mutates data on the ethereum blockchain they need to it needs to go through the consensus mechanism which means that all the nodes need to agree which again it takes more processing power is more expensive and will cost money uh just reading though is is is free on those chains so it's important to understand that solana is built differently it consists of accounts and accounts can contain programs and also data and accessing that data that big chunk of data is free but modifying it or calling a program costs um the data itself is just this big binary blob um how you deal with it is completely up to you and i find that interesting and off-putting and cool all at the same time but uh the uh the crux of it is you're going to have to decide how you want to serialize and deserialize that means like putting the data into this binary trunk and getting it back out on the other end but everyone's sort of using a library called borscht to do that we won't be using that today i'm going to show you how to do it um yourself just because there's some interesting stuff that you would skip over if you just followed the other tutorials that just sort of show you how to use anchor i'll show you how to just basically serialize and dc realize uh using bush um so i've created a keypair already and i'm going to show you my private key oops i'm going to show you that i have like a um key pair yeah so uh it stores like your private key as a um as an as an array which is kind of weird um you may think at first like why would it have a serialized array and it's just um simplicity of reading it back into a buffer so a lot of things in solana use the node buffer to transport data around and this is no different so each of these obviously represents u8uint8 and um uh once it is collected into a i think it's a base64 encoded string i'm not really sure but when it converts it into a string it then uses that as both your public key and private key so it creates a an account from that not a big deal uh it's just that we will need it in the future so i'm just explaining it now um okay so the first thing we might want to do is uh create a new project so let's go into workspaces lana choose wherever you want um all right cargo new so i want to create a new cargo project which is obviously rust package manager i'll call it toki okay oh uh i think we have to do is that right yeah so the reason i want to do layers is because i'm not creating a binary here i'm creating a library um and that's because uh the output that we want cargo to produce is a byte code for the frame for the virtual machine that sits on solana which is called uh berkeley packet filter or in this case it's ebpf um other blockchains use different kinds of bytecode there's evm for ethereum or a lot of the other cool kid blockchains are using webassembly this uses a archaic weird system that is usually used on routers and the reason is because i guess the developer felt that low-level access to the network layer was important and more maybe more important than relational data or various other niceties that are for that are given for free uh in other in other chains um perfect personally i find it's annoying but we'll see so um uh yeah i've got a cargo and cargo.tml and a source directory um so i think we want our lib to be a okay i'm going to reference my other um project a little here um i don't know everything off the top of my head and i don't know any developer who does highly recommend that you get like intellisense or um use idea or and install every kind of help that you can and i never used to be like this i used to use my bim and it never used to hook up to anything i'd use it as a text editor and before that text mate that was a mistake the good thing about compiler is that it can help you if you let it it can also annoy you if you let it the way that you can let it help you is through your editor so you hook up tools like uh rust analyzer or you know type scripts um tsa or whatever it is um to your ide or your editor and then it'll push you in the right direction and i'll show you that a bit more of that later of how how i might do that but there's no point memorizing everything it's a waste of time your editor can help you your compiler can help you and this is why types are so amazing um they will save your life anyway so we're creating a c dynamic library um it's just we're not actually creating a dialog to be honest it's just the format that we hand it over to the real compiler cargo kind of creates a byte code that sends it to a compiler in in our case our compiler will eventually compile to ebpf by code so um let's look at our library it's junk um first first things first um there is actually some interesting stuff i did first look at this article um paulx.dev i think it's the only article on his site um it's a it's a it's a um escrow which is really interesting we're not going to create anything that's sophisticated we're actually going to be way more ghetto um and uh i'm gonna start by oh yeah he's got this program he's got this um he points to this um this isn't his actually he points to this as an example for where to set up and this is also really really basic i don't think we need this i think we can start from scratch um but you can sort of look at his uh cargo tunnel he's got like solana program okay so what i usually do is i'll get the latest version of course first um [Music] okay so his is on 1.7.11 1.4 okay uh we need to cargo build just to make sure that everything's working this is blocked because of my stupid editor running cargo check in the background which i just said was a good thing but i'm not so sure hurry up uh okay while we're waiting um i'm just gonna check out uh yeah so if we go into his or this templates source and lib okay so you can see this this is just test most of this is just test so this is the only bit of program that they give you let's just chuck that in so this is quite small um yeah this is like nothing um and actually some of this is optional too because it's just printing out a message uh which is like a console.log um and it's printing out what you give it so essentially it's just one macro like that's the the core of all this is telling the um compiler where to mark the entry point the main i guess um and it's saying hey this function is going to be our main um macros is a system of pre-compilation so it's it's going to do stuff under the hood that marks it as as as an entry point um any entry point is given the same data all the time so you're always going to get this a program id an array of accounts and possibly data um and on the client side you have to remember this because that's exactly how it's called um so let's go back to our lib what are you doing oh it worked okay now another thing that um you need which would have been described in the solana docks if you followed it is uh installing a the build tool build bpf build bpf and instead of cargo build using cargo build bpf if you have wasm or whatever they have their own build tools as well i believe that this first compilation was actually a system compilation for x86 so that the ide can work it just runs cargo check you could compile all the solana programs just for regular x86 and run them like cli tools if you wanted to but why anyway so once this is done okay so this is successful um if you get this far i'm good to go um everything's working so you can see here that the selana cli tool has a deploy mechanism um if i deploy this uh this is actually pushing it to the devnet um and it's using my wallet um as i said before you need something you need some uh you need some soul in your account if you don't have that i'll show you how to do it i'll create another tab okay so if we go solana balance okay you can see here 3.6 blah blah blah if you want more just line up airdrop you can't do that on mainnet just devnet and testnet and local net uh yeah and that'll just uh oh yeah you need an amount so one requesting airdrop of one sol oh it failed just uh yeah rate limit okay this is another thing uh solana is stingy as anything they will rate limit you for anything for requests for anything the the devnet can go down at any time they really need to fix that it is it makes development a painful painful experience anyway you know how to do it um and that will append that air drop into um your the wallet that is in your the key pair that i the the wallet that uh is uh pointed to here and i'll show you how to use that in code later so uh okay thanks anyway uh you can see here that the program has deployed to this id program id is just like public key just like your regular salon addresses works the same way has a private key um but uh you're not privy to it anyway uh if we go to the explorer hopefully you know what a block explorer is uh just jump over okay uh whoops devnet okay uh you can see here it's a program so let's mark this executable it transfers some sold to the account um this is for rent rent basically um if if programs have a certain or if accounts have a certain amount of sole in them they are rent exempt otherwise it's just going to chew up any sold that's in that account that's for temporary accounts i guess anyway um it's sort of a staking type of mechanism like i suppose anyway upgradeable yes okay so this is very important um this means that this this program is actually mutable at the moment i can override it you know every time that i deploy i will override it uh what's interesting is i'll show you how to do that um so if i'm like if i use the lineup logs tool uh and followed by an address so the address of my the address of my deployed program there um this is just going to create a websocket now it doesn't last very long it lasts a couple of minutes this websocket which is kind of annoying but it is listening for any transactions that uh take place uh mention that address so i'm just deploying again and it should overwrite the previous deployment so 279 transactions that's great okay finalizing okay upgraded program so you can't actually mark it as non-upgradable that's another topic so um yeah i guess anytime that you look at other code um they generally tend to uh follow a bunch of best practices i guess you could say um programs usually have a t at the start so it's eight bits of data right and it's like a number um the reason that this is good is because you can just pull off the first bit uh first byte sorry of uh of the data buffer data buffer and then you um uh switch it based on you know what what event type it is it's usually called key but it can be called anything um so uh if i was to split off the first here um a key oh wait i just use key equals instruction data dot get it first i think um that's all right uh key should be a ui instruction data can't use a no here i'll come back to that um oh yeah automatically uh no method named get first i think it's like um option now never use unwrapped in your actual code um cool all right uh yeah so in your production code you don't want to use unwrapped i am this is same with all rust you only use it in messy code that you're just trying to like work out what's going on um push production code or never never push code that other people are going to use with unwrapped it's very implied anyway so if i match on this key um say we have a 0 one and a default okay so um you can see here that this is using this message i'm gonna reuse this macro um i'll go zero one and two oh no there are um okay cool uh now i don't think we need that anymore so i'm just going to remove that all right now we've got a little switchery thing um with our uh with our basic program but what's the use of all this if we can't call it so i guess i'm just going to create a little client um so mr cli i guess cdcli okay let's node uh yarn okay what are we gonna call it cli yup yup yup all good yep okay list uh your oh wait earn ad um dev type script node types uh what else do we need um let's say here okay let's make our main file type file touch index of typescript oh that's done with me types note maybe i'm dyslexic does this let's say apply the words i don't know um okay so i uh okay uh now mpx um oh of course this is really important i like to create these like little um cli tools because it uh if you start creating like actual integration code actual production code whatever if you start creating that in your react app to start with not only is that going to end up not portable it's also not very testable i mean you could argue with that but i like to create like a an isolated area where i can quickly test things and work out what's going on um not for every project but particularly for this example cli tool is just really useful and i've had a lot of celina projects agree with that they create their own little cli tools okay um so if you want to do that with typescript it's a little bit more complicated so what we need is a thing called ts node what that's doing is that's going to run the typescript compiler as if it was in a node environment [Music] so how you'd run that now that i've installed that package is tsx which means like use the package dependencies i guess ts node s node um index.ts now i don't think there's anything yeah okay so console log hello oh hello okay that's right oh mpx what am i doing tsx npx okay yep cool hello done all right that's our cli hold on no just kidding um okay so we've got we've gotta uh we've gotta add a bunch of stuff i guess um i'll just copy the uh the i usually like create examples with uh in a certain way so i like to create it like this um it's i actually borrowed this from hard hat um they uh they have this bootstrapping code and it's really good because uh it makes sure that all of your code is running asynchronously and it catches all your code in one spot so calls main um then it if it if everything's okay it doesn't proper node exit otherwise it catches zero prints the error and the cool thing about that oh and it returns a correct error level so it'll integrate with other scripts and stuff if you really they already like that um yeah the other thing is like it's it's one simple place to put a breakpoint if you're using a debugger anyway uh so we've got that let's run again just to make sure yep okay and okay cool so um one of the tools that um if you're familiar with ethereum there are two major client tools or providers for ethereum um web3 and ethers so if you're familiar with ethers it splits um the types of requests i've touched on this before into providers and signers and assigner is um a transaction that has a signature to it um and it usually is used for mutating data if you are a signer you have the permission or your your permissions are elevated to the point that you you can then uh create a mutable request or a signed request selena doesn't have this um it everything's assigner so uh the web3.js library is going to assist you to create signers now there is some uh some uh what you call it um like uh read-only immutable data that you can get you can read any any program any accounts data um or you can read any account and get the balance from it or whatever but you can't run or execute any program methods so the library that we want to use is celina slash or celina web3 so let's add that and that will allow us to create a connection and the connection is analogous to a provider in ethers and then you can get your signer from various different ways you can get it from an injected wallet like phantom or metamask or you can get it from a ledger or you can get it from a key pair and in our case of course we want to use a key pair particularly that one that we created before so i was scratching my head on how to do this for a while um but it's pretty basic um whoops uh another thing uh when i import libraries i don't do the other esm syntax where it's like imports all these little bits and pieces it's just messy and you end up with all these orphans like imports and stuff and people a lot of people don't a lot of people checking code with orphaned um dependencies and stuff like that it's easier to tell where something's coming from if you declare it as a in a module namespace and you can also like just do would three dot as i said like i use the editor to try and help me all the time and this is like a great way to um to help with that it just is very obvious where everything comes from and there's no overlap like especially in web3 you get like key pair being defined somewhere else or different names being defined somewhere else where did this one come from so can be tricky it's a misdirection anyway uh so the first thing that i need to do is create a connection so i can do this outside the file i know it's not that great but connection equals web3.connection and uh web3 dot get cluster api url now all this is doing is returning a string um for devnet but it's kind of a best practice way to do it um to grab the string using this function uh you don't have to you can just use the the uh node interface node url directly anyway um well we don't actually need that okay cool so uh if we wanted to um uh just query the blockchain now we can uh but only a certain amount of data we can uh so all of the um the read-only transactions are also talking about this provides like a layer that just under the hood it just crea uses like some internal light rpc request rfc whatever um that that makes a request to to the node um given some method the predefined methods you won't be doing that actually unlike ethereum uh where methods are pretty explicit um in this you're you're not really calling methods you're calling a program and stuffing some data into it and on the other end it's kind of like deciphering that and going like what do i do with this and then that's all up to you so uh let's have fun there um yeah so uh as i was saying before like uh this is my secret key so i you can see here 61. uh oh wait no that's different okay if i want to create a secret key from the wallet um from the sorry the solana cli wallet um i can just grab this again like this is just test.net data like if you really want to use this as a um if you want to steal my devnet soul you can i don't care okay um so here's our key uh web 3. oh no we're creating a signer which is again that key pair um we can create a type for it if we want to keep pair equals secret key key okay and there we go we've got designer and that is the same sign that is used on our this one if you want to verify that we can use the connection dot get balance and then we'll use signers dot designers public key and then on return balance and then we'll return it console.log uh soul and balance times oh divided by three to lan parts per sol again like uh the reason for this is um land parts for solar is about one billion i think yeah so a lan port is like a satoshi or like a way uh it's a it's a fragment of the base currency and uh the reason that we have to do all this dividing and addition um that that most uh contract languages don't support floats is because of rounding errors and crazy stuff that it's trying to avoid during uh conversion from uh a floating point um into an integer it's just cutting down the amount of times that it's being carried as a float because uh yeah it's notorious for for for producing producing errors basically uh anyway uh yeah contract languages shouldn't be shouldn't shouldn't have floating points there are arguments against that but you know so let's try it um actually yeah mpx ts node index.ts that did not work we've got to do a wait there we go three point six now if i go salana balance yep all right uh you could also do the same with the dress to a verified but whatever anyway so let's look that um okay so i guess we want to call our program so how we do that is we collect together a bunch of instructions into a transaction so [Music] let's build a transaction transaction three dot transaction equals one three new uh you don't actually need to put that type if it's directly followed i guess yeah whatever it's nice it uh helps you i guess in some points but if you are chaining a whole bunch of functions you definitely want to make sure that you're restraining by type if you're not then your editor can't help you just doesn't understand and i don't blame it how could you anyway uh so new and transaction.add okay so we're just adding one transaction so may as well you can just keep changing these ads if you want new web three dot transaction instruction okay so transactions this consists of instructions uh again like this is a this is a case of like um using your editor to find out what the hell is going on like uh it says here the ops is not provided so let me provide some options oh what are the options so this here keys program id keys let's make it an array gonna let's make it undefined no let's let's just yeah okay so uh this looks familiar right keys program id remember keys program id data or yeah program id accounts account spring keys data okay so in the ros side this is where it's receiving it on the client side this is where it's sending it as as i said like keys are all of the signatories for signing this transaction um and yeah the reason for signing is of course security and the program id is uh the uh program that we launched previously so um i was monitoring it here and this was the address of it so i can just um put this as a constant so program id yeah so program id as i said is public key so we just provide that to our transaction we don't need any keys at the moment because we're not really modifying data um yeah so how do we send this transaction okay so await connection dot most transactions are created with either down down the line somewhere they're using this web 3ds or yeah you can either use connection or web3 directly um so there's like a get transaction get transaction that's the most basic form of of uh of a transaction but most people tend to use web3.0 [Music] send and confirm transaction yeah connection um transaction which we created before and signers which i explained before and there is also an options but we don't need that right now um i might actually just put then and then just see so this is a signature that it ends up with send it confirm transaction as you can see it returns a string and this is the txid or the signature in uh most other blockchains it's called the txid it's transaction id but in solana for some reason they call it signature everything's called something different in salon it's really annoying okay so um this is a pretty bare-bones transaction okay it's not sending any data it's just kind of like pinging the pinging the program for no real reason let's check let's see if we can get it to listen um so if i execute this program okay no signers we do need a signer so let's pop it in the designer array and oh let's see here okay that's our log so uh these three around here around our our transaction are created by the program's parent um i'm not sure if that in this case is the system program or if it's the bpo loader or bpf loader or whatever uh there's one of them and you can see here that it does it consumes solana which is kind of crazy because we didn't do anything and it's charging us i feel ripped off but whatever so this is our log um this is what our program spat out what we paid for um so let's look at the rust again um and you can see here it's purely printing ah it's putting out process instruction ah because we didn't employ we didn't deploy the latest version okay let's uh let's deploy the latest version cargo build the pdf carbo sounds cool i'm just gonna make sure that the log is working okay yeah take your time come on turkey you can do it okay done now deploy this should override should upgrade i need that upgrade oh yeah okay we're upgraded all right now let's see if we can um actually let's just create a script for this because i'm annoyed scripts okay go mpx uh ts node uh index.ts go nope it's not going unexpected oh i i always do that i'm i'm such a bad coder you don't go okay so what do we got simulation failed okay this is because um we are expecting data on our on this end we're expecting data this is this unwrap is throwing an error so if i was just to print out a regular message it would be fine but yeah we're expecting data so let's go back here let's give it data const data equals oh wait it has to be a buffer so yeah buffer buffer from blah okay now let's try oops it's a race error dumb i feel stupid um my code is insulting me um okay why is that okay error dumb let's make it print out the instruction data and let me get an error oh debug okay let's not be so dumb this time it's still going to call us dumb just be prepared for that oh it's hurtful oh you need to deploy it again okay and um i can't i don't know what it is i can't remember okay it's a lot of program deploy don't really compile that okay whatever yeah we'll get there now don't cut out upgraded okay there we go yay okay you can see this is the data that was sent to us uh if we look at the data from the client side um blah and this this is blah as a union 8 array cool okay we're getting it so the first byte here will be 98 um and what we want is to match it up with our match or um yeah whatever to be zero or one anything else is considered an error and this is this is known as a key so let's um let's stick that in there somehow um so what we do is go a uint view into a array dot from i think zero okay is that right okay hang on yeah buffer from of not from yeah so um oh zero okay it's about to get a little technical but not too bad so at least we can just do this oh all right there we go zero yay it worked and if we wanted to go with one you know just check it for fun should say one one okay all right so our switch is working this is how the serializer works uh sorry not the cereal uh yeah it's how the serializer works um but instead of doing it into numbers it pulls it off and compares it to an enum anyway now we might want to deserialize some data um i'm not going to use sorry i'm sorry or bush or whatever just going to kind of wing it so let's say if we get zero then uh i'm going to create a block here what what happened message where'd you go okay uh let um okay so we want value this will be the we'll pull an additional value it's just a like um a u 64 off the um of the stack or whatever you want to call it now uh this means it's going to need padding you're going to need like um well 64 bits so for eight bits so it's going to look like an array of whatever four things um anyway so if we start by declaring our data type this is what we want and we're not going to compromise so let's get instruction data oh wait wait wait uh first yeah okay um this is actually a better way to do this so if we go with split first yeah split first okay split first then that's going to give us two values so the key and then the rem remaining um okay uh i'm just gonna read that for a second okay so these are pointers to uh you could type them but i'm super lazy oh should i no okay you get it um okay so key's still matching uh instruction but we've got this is ram so ram dot split at oh wait no um it's uh get so get will split a splice from an array so naught two it's actually one two nine which is wait is that right not two nine i think so uh if it's not i'll work it out okay now this is what i was saying before about letting your touch system work for you so [Music] it's it's giving you hints about type types hot typings so it's still in its buffer form even though it's like a slice it's still an array of unsigned 8-bit integers so we need to somehow chorus that um and in rus that's done with traits so we use standard see this is again beautiful auto complete stuff because i'm i've been stored on my on my garbage uh i think it's like into or something [Music] uh i can't remember convert yeah it's gonna convert and then try into yeah okay so that's sort of applying like a trait to all uh to various kinds of you can go into here you can go into the source and read it i highly recommend that you always read the source because the rust source is really cool and it'll give you great practices on how to deal with things and what everything's doing under the hood so um we need to okay so once we get you can see here that it's returning an option so if we want to map that option or whatever we want to transform that option we have the option of map or we can use and then there are various different like functions we can use to sort of pull in this is the basics of of rust programming is dealing with all these monads like result types and stuff like that and then and map and all those things are things that you end up getting really really useful used to because you end up knowing what the type is of each thing is as you're getting it back some people even comment like here what the type is but i think that's overkill um so right now we're at and then and uh so we we've got a splice out of that splice and then we want to splice dot um try into i think and then that's we want to u64 i guess uh and then that's going to be a result so it should say that somewhere trying to but um that's going to be a result so we actually need it to be a option so i guess it's like okay now what do we have um expected u64 and it's got an option so i guess we could just unwrap it like again don't do this in in regular code but um yeah so trying to this takes uh no generic argument okay um oh yeah so it's just gonna coerce it based on what it finds um trade bounds from is not satisfied for use 64. um okay so try i'm gonna do here trying to yeah should be fine get oh it's not eight not eight it's all right um oh yeah cause it's a remainder yeah yeah actually you don't even need to slice this to be honest like it should be the whole rest of the data but of course you could get under or over and then it wouldn't throw an error okay so we have uh trying to uh another splice just let's try in to get in there yeah that should be fine go to u64 ah okay yeah right i've got to convert the bytes so um essentially like what we did on the client side where are we here okay um yeah we we need to pull the transform the data into little endian bytes so um anything that we pass through um the bpf program deals in little indian um little ndm bytes so uh it's not not too much to worry about just to make sure that um yeah borscht will take care of it for you but it's it's really valid to sort of understand uh that conversion process is going on anyway under the hood and you can see like there's so many like spaces here for errors um so error handling is really important again not something that we need to really get into so uh this should convert the second part of the data into um into a an extra variable so zero and then uh value and i'll put that here so if we do it now it should throw an error i think because we're not providing any additional data so that unwrap will cause us to fail um oh okay one oh yeah because we yep right okay so one is fine because it's not it's not actually going to slice that it's if we hit this zero block so if i hit zero should give us an uh error are we zero okay i guess it just decoded it as nothing oh we didn't um deploy so cargo build bpf come on toki okay let me deploy you could actually create a task for this really easy but super lazy there we go and should get an error yeah okay so unwrap on a non-value so it's a typical roster when you're trying to um call unwrap on invalid data so it's actually an error um yeah or in this case it's a non-value the monad is set to none so yeah it's it was an option tried to unwrap it and it was a none you can use unwrap or else or whatever that would be the best way to sort of deal with this so go unwrap or else zero oh here yeah that would that would mean that anything that you pass there if was invalid it just gives you a zero like a default but it doesn't complain if yeah so it's really not adequate for proper error checking um or error handling okay so our next step is to actually posit some data that's the last thing so that is here this is actually an array or a bunch of values that it compresses into a unint array so if we create a new thing anything that we put in here it will add to our you in array so we want a big number you can't put anything actually but i guess we could put a number four does that work yeah okay we can't do that but i want to put a big number in in a second because i want to encode it but um let's just check if that works actually i don't think it will because it's trying to pull eight bytes so it'll just be zero okay yeah unwrapping known value again so that's because we didn't deploy our recent change with the unwrap handling so um there's not enough bytes there so basically like it needs to be encoded into little endian and it needs to be padded to eight bytes so how we might do that um well we can use uh bn which is handy because it's got like it's basically a wrapped buffer so it's a lot easier to use um so if we use bn um new and and i think like you can import uh bn from the end.js or becomes require requirement.js i think that's right and then yep okay so new b and and we want like say 44 because that's cool and then uh we want to we want to create an array out of it okay so uh i'm just checking my last code yeah because my brain is dying okay so yeah we convert it to an array that's right so again we want to convert it to a little endian array so this is like a function this is why i was using bn instead of just um just a number by itself uh it's got this handy helper function here um yeah so much easier now also like most of selena deals with big numbers anyway okay so uh now we've got an array that is padded to eight bytes uh little endian encoded okay so let's run it 44. it's like bingo uh okay done um yeah so you can imagine how you would like encode and decode into these like buffers that look exactly like this any kind of data that you're sending back and forth and borscht helps with that but at least now like you can you can understand what's happening under the hood um there is a little bit more complexity around like how keys works how signing works how multisign works and all that stuff but not much once you kind of understand like this stuff in the in the in the structure of solana accounts and programs it all becomes pretty straightforward and you're just collecting together these little lego blocks that are accounts with programs and data attached so um i hope that was in some way helpful um yeah from here i guess you would want to sort of fill out these um switches and maybe you would even deserialize these switches into an enum and then this is what all this stuff uh all this example pro all the example programs do is they're just deserializing the data into like a series of instructions and then processing those instructions so you can already hear the words that are present in the different rust files that are in those typical examples like instruction.rs and processor.rx and state.rs all of those things are for storing like this they would store the the model data like the structure model schema or structure and instructions would be each different uh the serializer and serializer for the instruction and then the processor is the functions that actually get called out of those um uh out of those those those instruction enum arms it's very similar like if you've ever written an emulator it's it's a similar sort of structure um yeah as as like interpreting bright code from a rom or something like that of course you don't have the same like memory stuff but it's very similar anyway um the way that the processor works i hope that helped and yeah let me know if you want me to make anything else
Info
Channel: Death Disco
Views: 4,159
Rating: undefined out of 5
Keywords:
Id: lkLliOjkB2c
Channel Id: undefined
Length: 57min 20sec (3440 seconds)
Published: Tue Oct 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.