Anchor is a development framework that simplifies Solana program development by abstracting away boilerplate code such as serialization, deserialization, account validation, and security checks; it uses macros like declare_id! for program ID declaration, program! for instruction logic, and accounts! with derive(Accounts) for automatic account validation and data handling, making it easier to build secure Solana programs while still compiling down to native Rust bytecode.
Anchor Basics: Solana Development Course Module 5, Part 1
Added:hello and welcome to another Solana tutorial today's finally the day where we're gonna talk about anchor I'm gonna give you an introduction to anchor because here at the Solana development course we reach the point where we can do module 5 anchor program development and we're gonna start with an introduction to Anchor in short anchor is just a framework that builds on top of the Solana rust that we already know so the Solana program stuff and it helps us do some stuff more easily like all the owner checks and some security validations and some derivations anchor abstracts those away to make it easier to build on Solana now I say easier if you already know native rust Solana then it takes a little bit of time to get used to Anchor but Anker automated a lot of stuff that you always had to do manually so in the long run it makes sense to also learn anchor if you haven't yet started learning programming Solana programs then anchor could actually be a good place to start because you don't have to deal with a lot of the deserialization serialization stuff because anchor just does that automatically and I'm still myself not sure if I prefer native rust or anchor both has its advantages and disadvantages for me personally anchor is a bit tougher to maintain like in terms of the libraries because yes it is easier to program in Anchor but then also if something doesn't work then it's harder to fix it than it is in Native Frost at least that's my personal experience but I want you to make your own experiences so that's what we're gonna talk about anchor today today we're gonna learn how to use the anchor framework to build a basic program we're gonna describe the basic structure of an anchor program and we're going to explain how to implement basic account validation and Security checks yes because that's what anchor helps us with a lot the tldr of today's lesson anchors a frame work for building Solana programs yes I think we established that cool anchor macros speed up the process of building Selana programs by abstracting away a significant amount of boilerplate code yes so all those things that we constantly need to do the serialization deserialization decoding accounts those kind of things just anchor optimizes them like account checks owner checks anchor allows you to build secure programs more easily yes because it does a lot of the security checks itself because it also just compiles down to the native rust which then also just compiles down to the BPA for now SPF bytecode and it's also providing a simple way to implement additional checks yeah they have like those constraints and stuff which actually in the beginning are hard to understand but that's why we have those lessons and otherwise we just go through anchor documentation don't you worry I gotcha so overview what the hell is Anchor it's a development framework easier faster more secure all the good things it's the go-to framework for Solana development for a good reason yes so most of the Solana programs are now written in Anchor that's maybe also an interesting thing to mention makes it easier to organize reason about organize and reason about your code Implement common Security checks automatically and Abstract to a significant amount of boilerplate yes so anchor if you choose Google anchor you will not find the thing that you're looking for so better put Solana there as well we have anchor Lang the introduction we have the anchor book which is a great place to go and then Coral has the actual source code so that is the anchor source code in case you wanted to find that we'll also put those resources in the description below the anchor book definitely a go-to place to get started the anchor program structure anchor has macros And Trades that generate such boilerplate rust code Frost boilerplate code by the way is a term for code that appears the same way several times like it's always the same code so we can just hide that behind the macro so here are some of the main ones declare ID that's a macro that declares the program's on-chain address in a native Solana rust program we don't have to do that that program ID then just is whatever the account it lives on but Anka wants to know where it lives so it can make all of those owner checks and say oh I just accept this account if I own it so we want to declare our program ID within anchor then program that's where all the instruction logic goes so anchor abstracts away the entry point and provides you instructions you can just write the functions in there and then anchor will automatically you know map those instructions based on instruction data and I think the first eight bytes are reserved for instruction data if I remember correctly accounts that's a trade applied to structs representing a list of accounts required for instruction meaning we can basically build a struct that defines which accounts we expect because in earlier lessons let's see uh for instance there we always had to do like next accounting for next accounting for next account info and then essentially check on those that it's actually the accounts we expect and we have to do all of those checks manually it would be much nicer if we predefine what we expect and then anchor does all the checks for us and that's what's happening so we can use this accounts trade and then the account as an attribute will Define what the one specific account actually requires but let's have a look at that in detail and I think I want to just get started and and get the code out because then we can have a look at that in here as well I'm not going to go through the installation stuff I'm just gonna assume that you have anchor installed that's somewhat easily good googleable and it should probably also be in here somewhere their installation there you go that's the just follow those instructions and then you should have anchor CLI available we're currently in version 0.26 that's what I use as reference because stuff changes and it might not stay the same we've got anchor and what we can do now is we say anchor init which creates us a new project and then we give it the project name first anchor program yeah just gonna name it like that why not I'm not trying to be creative then it downloads all of the dependencies which usually takes a while then we have that folder first anchor program and we can check out our stuff here or look at it here in the list we can see it created this anchored Tomo the cargo which is an automatically created file a package Json and front-end stuff goes in here node modules you know what that is and the pro program will be in programs slash project name and in here we have the source elip RS which holds our program so let's look at this sleep RS if we don't do anything else then that's what it comes with if that address looks familiar it's because every goddamn anchor program that you initialize just has this address declared don't ask me where that points to where does it point to anything there nothing there actually on mainnet at least so anyway we will need to replace that anyway with our program ID so that's the program file and then we have the anchor terminal but we're gonna probably have a look at that later so let's stay with that declare ID we said is the thing where we Define which program address we have so the program ID and in Anchor where do we find the program ID when we build it it will create as a key file but let's see what James says to specify the on-chain address when you build a program for the first time the framework will generate a new key pair yes that's what I was just talking about this becomes the default keeper used to deploy the program unless specified otherwise the corresponding public key should be used as the program ID specified in declare ID macro yes so if I were to just go here and tell anchor to build me this thing with anchor build which will then take a while because it's going to download all the dependencies and install them and compile them and so we're gonna let that run in the background while we discuss the next thing which is the program macro so as you can already see here program that's that's where we can put our individual instructions and all the public functions inside this module will be treated as individual instructions for our program so in this case we have one instruction called initialize and if we wanted to create a new instruction we could do so simply by creating a new function like this and then we might also want to change that but we'll get to that each of those instructions requires one parameter of type context and can include function parameters representing the instruction data that's the next cool thing anchor automatically does the decoding of instruction data so the deserialization based on what you put in here as Arguments for your function so for instance if I put here number of type u64 then anchor will decode those 8 bytes from instruction data so the next eight bytes and write whatever it has there as our number and we can also you know put anything else there Vector string a byte and that will then automatically be decoded which is quite sweet so we don't need to deal with that that's also why in the beginning it might actually be handy to start with anchor I actually started programming with anchor and then I went to Native Frost now I'm coming back to anger so what is this context thing that is the first parameter here though that holds a bunch of metadata for our instruction for instance the accounts and the program ID itself is also once again passed in here in the context then we have remaining accounts that actually you can ignore for now because you shouldn't really use that anyway in some cases it makes sense but that's just if there are more accounts than we have specified in our list of expected accounts if one instruction can take an arbitrary number of accounts that might make sense but like for most of the cases you don't want to use that and then bump seeds if you use pdas then the bumps automatically come in here as well as a map for address to bump so you don't have to recalculate them with find program address now this context comes as a generic with type T where we can actually Define which accounts we expect so here we'll have those accounts and which accounts we can Define and then through this context through this argument we can then actually access all those fields of course with context dot accounts program ID remaining accounts etc etc so let's have a look at this T what should we put in there we can use the account straight to define a data structure data structure of validated accounts validated accounts so anchor does checks on them that's the important part if we Define it like this with those accounts then anchor makes sure it's actually those accounts that we expect otherwise we get nice errors and another nice helpful pages anchor s o errors it's a nice cheat sheet still installing anchor errors cheat sheet yes that's what I want because there we will see all of the errors they will come as a custom program error as we learned in previous lessons where we talked about arrows and then the error code of the custom program error will be those and for instance those are the constraints errors and there are the account errors not mutable invalid program ID all those things are being checked by anchor and if it doesn't match then this custom program error is returned and the transaction fails of course see I mean there could be another disadvantage of anchor it just blows up the program a lot it has a lot of dependencies and if you just want one quick little program then that might just be too much to include which also increases your rent obviously in Anchor program that does the same thing or the same thing is more expensive because it needs more space for all that checking stuff but those are like peanuts as my dad would say it's like make that a deciding factor of whether or not to use anchor those Security checks actually are useful so yeah might as well keep them in okay moving on and since we put in this accounts field is the type for the generic of the context we can then access those accounts inside this context and we don't need to manually deserialize them anymore you typically apply their account straight through the derived macro EG derive accounts this implements and accounts deserializer onto given struct and removes the need to deserialize each account manually yes that's why we do that I'm gonna pretend I know what this means I don't though and I'm also too lazy to Google point is we can then just Define the accounts we want it's an introduction we don't want to get into the very details of all of that yet right so the accounts trade has all those implementations that do the actual checking and constraints are provided for each field using the account attribute more on that shortly that will be this then so in the beginning you just need to get used to how that's written you just use the derive accounts with a capital account because that's the trade and then all of those are account in lowercase letters because that's the attribute in this example we have the instruction one that uses the context of type instruction accounts and then the instruction accounts here are derived accounts with the following three accounts and there we can already see one account is a special account of our type so we say which layout that account should have one account is a signer and one account is a program more specifically the system program so anchor also has predefined types for that and when this instruction one is invoked then the program so the implementation is in this trade it does all the checks that the account actually matches or the accounts those actually match what we expect here in these instruction accounts struct and and it also checks any additional constraints specified we can put more constraints in here but we'll have a look at that in a second I think no not really in this lesson but there is yet again a great pitch in the docs of the accounts of the derived accounts implements accounts to serial as a forgiven struct and all the constraints you can find here in this list so there are a bunch of normal constraints we'll go through a few of them but then we also have like has one or like constraint here that's an arbitrary constraint with any expression that needs to evaluate to True otherwise an error is returned and then we have special constraints like from the SPL like we can check for token mints and stuff anyway that's another thing for you to have a look at that goes further than the introduction that I want to do today so yeah just pointing you in the right direction with all of those things so after a [ __ ] turn of time this has now compiled and it created us in here in Target it it created us the deploy which contains the program itself the so file we know that from like normal rust and then also created as a key pair so we could now be like Solana address and check what this target deploy first anchor program key pair would have as an address for kfn and of course we could replace it with whatever if we wanted a specific program address anchor program like that like a pro although that might already be too many letters there we go approve [Music] if I want that to be my program address then I could just move that thing to Target deploy and then my first anchor program keypad Jason boom then if we check again now this file has this address and then I could put that in here into my declare and then I would have to do anchor build one more time of course because we changed some something anyway let's remove this initialize and just call it my instruction like if we created an empty anchor program it already lists us this this derive accounts here that we should do that as an example it makes it easier for you to get started anyway where are we account validation now inside this derive accounts struct for our initialize he has a instruction accounts let's do it like that instruction accounts and inside this instruction accounts here we can specify the individual hashtag accounts mute for writable like mutable so this account is written to and then we Define what kind of account it is so here we give it a name like the data account and we say it should be an account of type and there we could specify our own type or it could be a token account or something or a system program if we say it's a signer that's nothing else then an account where anchor checks that it's the account info says is signer so it does a sign or check for this one and as already mentioned we can have our own custom account type which remember in the movie thingy we defined like one change it with a string or a byte where we say that's the account discriminator anchor does the same thing but it's much nicer to just create a struct of what this account contains and then we can directly work with that anchor provides a number of types that can be used to represent accounts each type implements different account validation that's the full list again the docs the signer we have a program we can make it boxed so that it's saved on the Heap not on the stack we have CIS vars system accounts unchecked accounts that's basically any account and doesn't do a check that's just if we want the normal account info an account there we check for ownership so if we provide account then we expect that account to be our account and we can make optional accounts starting with account that account wraps the account info and verifies ownership plus the serializes the data into a rust type so we can Define struct and then the account automatically gets deserialized how does that work well we provide such a struct here and we have to Define it somewhere James once again shows us the account info and telling us the data is automatically deserialized and the owner is checked that it's the the program we're calling from yes T is this and then whoever owns this so it's not always all our program but whoever owns the struct the the type that we put in here if we Define that then we that's actually quite nice that's actually a quite nice system yes yes so when the type of this account is specified within this same crate using the account attribute macro then the program ownership checks against the program ID defined in this declare ID macro and if that was declared somewhere else in another space then there declare ID would be used here for the owner if we have a signer type then anchor checks that this account signed the transaction no no other ownership checks are done you should only use the signer when the underlying account data is not required in the instruction okay no more words on that fine then program checks that account is a certain program for instance we could check for the system program and already provide such a type which includes the program ID of the system program to check against so then the following checks are made is the public key of the expected program and is that account executable because obviously a program needs to be executable and so all those checks they are hidden from us we can see them here now but they are hidden and if we just you know write a line like this then that happens in the background so you don't need to worry about that anymore when using anchor there is the same link I think that's that right yep cool so James also has that in his course already cool because we can Define additional constraints for instance for this account name here he uses the constraints in it pair and space is that a constraint do we call them constraints and basically all of those things hide away some functionality for instance init creates the account where CPI to the system program and initializes it so that does a create account instruction with the system program and also writes some discriminated data there anchor automatically for all of the anchor accounts that we've defined like this they use an 8 byte discriminator at the very start of the account to make sure that the account is actually what we expect and therefore anchor reserves eight bytes that's why we have to make it eight plus eight here given that we were to store like one u64 or something payer specifies who pays for this initialization and we can just say Paya equals user and anchor then automatically knows that the payer is the user key so that the account address of the signer that we have here so we can use those things up in here that will work so we can cross-reference that but yeah we can just say payer equals user then it automatically gives that public key of that account into the system program create account instruction as the payer the fee payer who pays the rent for the account and then space specifies how much or how many bytes are allocated and in this case it's eight plus eight the first eight bytes is for this discriminator as already mentioned and and the next eight bytes is the actual data then which we Define in the account struct type which so far I didn't see it where is it oh here down here that account struct is just a u64 so 8 bytes that's why we need eight plus eight here then for the user we specify that this account is mutable AKA is written too or can be written to and we need to do that because obviously we did use lampards from there because we have it as a payer so we want to you know change the lampards so this account needs to be mutable needs to be writable if I make a normal data account mutable then anchor automatically writes the data there after the instruction is done so we don't need to deserialize and serialize again anchor does it automatically as soon as I say for instance with this account dot data equals some number then it actually writes the data onto that account already but maybe I'm skipping ahead too much note that the init constraint based on the account name automatically includes a mutable constraint aha okay so that's why we don't need to say mute in it because in it we can only do that if it's mutable so that already contains that okay cool good to know so both of them are mutable the account attribute is applied to structs representing the data structure of a Solana account it implements the following traits accounts serialized digitalized anchor serialized anchor deserialize clone discriminator owner you can read more about that here those things we already know I guess and also clone that's standard rust trait creating as a copy well not quite creating as a clone but you know the first eight bytes are reserved for a unique account discriminator self-described by the first eight bytes of the shell 65 of the accounts rust ident interesting so now we also know what's written in there don't know what a rust ident is but that's hashed and then stored and then deserializations will check for that discriminant and if it doesn't match then return an error and that's how we're making sure that it's actually off that type because again the user can put in anything here but the first eight bytes also need to match plus the owner needs to match so then we can be sure that it's the account we expect of the account types name so just the name implements the owner using the program ID but what in other words all accounts initialized using an account type defined in the account attribute within the program are also owned by the program okay so if we're here with this declare ID and here we have the those accounts those accounts like this and accounts track like this this account in here is being checked for this program ID so if I have this account struct that's here and that's on the same lever as this so this program ID is checked for for the owner I think that's how it works this account attributes ensures that it can be used as an account found in instruction accounts so because we Define this attribute here we can then use it in this type here if we have that all together now with this account we'll use this account struct from here then on initialization the first eight bytes this discriminator is written into the data field and the remaining data or the data field of the account in the instruction I would say account name dot data that will then match the actual struct in here so I could say account name dot data so it's not the same as this data because that contains the discriminator as well and then bad example because we called that data I can call that whatever right I can call this number one and then I can have another thing like a number two which is I don't know I use 16 for instance and that will all be written inside this data field of the account so in the actual account data but automatically and I can access it using in my case dataaccount DOT number one and data account DOT number two so essentially here I Define what data I want and then anchor automatically does the serialization and deserialization for me I just say context so let's do that let's do the example here it could just be like context accounts and then I have the account so for instance also the data account and then here I could directly access number one and number two those data fields and if I were to write something in here then that would automatically be written into the account data so no need for me to serialize and deserialize stuff anymore like manually as I would have to do a native first cool that's that where are we cool they bring it all together all together now that's how a anchor program looks like and what it does is initializes an account and it updates the data field here account name data to some instruction data in this case okay we can do the same in our program we can also put a number here input number which should then also be a u64 and then I can just set it to the input number and that's then automatically stored in there so that's our simple first program I would need to also use that paste the user and space in my case would be eight plus eight plus two because I have another number here that I don't really use but I have it let's see if that would compile anchor build because it's giving me red stuff damn it Undeclared lifetime yeah I still don't fully get what lifetimes are so I guess I'm just gonna put that in here you need a lifetime there you go now you have a lifetime ah there we have it non-optional init constraint requires non-optional System Program right so to do the init obviously you need to be able to call the system program but it's like anchor has okay error messages you just need to find the right one we do another hashtag account that is not mutable then apparently we don't need to specify that if we don't have any further constraints so we don't need that we just say pop System Program which is a program off type have the lifetime and then system then that looks better there we go see and there was the error like that was that was my that was what I was missing but the arrows were like all like method not found in the the not implemented for so it's all quite confusing but if we start with the first era then in this case we were able to fix it but that's like one of the issues I still have with anchors since I don't understand everything as well most of the times I just can't like I can't pinpoint what the actual error is and then I'm just confused and then I just give up and then I'm like screw anchor for such simple programs that's all easy that works well the demo goes through what we essentially already did except for Anchor Key list what does that do did you mean Keys yes I meant keys first anchor program and then it gives me my oh so that's the quick version of doing what I did earlier with the Solana address and finding the address of this key pair which you know fair enough update this and then we also need to put it in the anchor Tomo so we're gonna copy this and put it in the anchor Tomo here and then James builds a counter we can use the same things here like the message macro also works in Anchor pretty much all of what works in Native rust Works in Anchor you just need to have the right Imports data account created but you know what why why build such a counter if we already have a nice first program like this but we just write some input into some account might as well test this right or is there anything else that James does here that is different he adds another instruction for increment and update and build yeah I mean why not let's build this and execute it that is our basic program now just a very simple we write some data into some account and that's it let's try and build and deploy this and then test it you can if you prefer build that counter as James does but it's essentially the same except that he has a second function for increment and we will build a second function but like so first let's get this thing deployed we've already done anchor build let's do it one more time just to make sure there we go boom done which gives us in the deploy this anchor so which we don't want to open and then we can just say anchor deploy and it will deploy to whatever I specify here and I specify localnet as the cluster and this is my key pair both of them don't exist yet so might as well start up my Solana test validator get that running meanwhile create myself a key pair for myself here so that's the authority authority and then I'll just use this thing here because I can this is gonna be my wallet and then I say Solana config set also this is my wallet and then I Solana airdrop myself one two three Soul because I can I'm on localnet I can airdrop myself as much as I want and then I just say anchor deploy boom and that will deploy to localnet that's it it's as easy as that if everything works which in this simple example it does and it tells me program ID is this April as we grind it ourselves earlier and now I can call this program right I can now send the transaction I can manually build that or anchor also creates those nice idls here for us that contains useful metadata for our program like all the instructions like here we have the my instruction and see it already automatically kind of javascripts that or typed scripts it changes from snake case as we headed here to camel case as we would expect it in typescript and it also lists which accounts are required here and whether they're writable and signers so the writable and signer Flex as we know them and then the arguments for this specific instruction it that's the name and that's the type and then it also defines the accounts and in the metadata it has our program address so that's all very helpful stuff using this IDL we can now easily test our program and we don't have to manually build transactions but we can use this yeah so that's next lesson stuff but essentially what we want to do is test it anyway inside our test which anchor already neatly provides for us here in this test file that's already automatically set up for us where it you know loads the correct program type so we'll just use this and it has a first test called is initialized where it calls to initialize method and actually we don't have a initialize method anymore but we will use the my instruction and then we can provide the correct data for that first it just expects the input number and I never know how that's done like every time I forget how that's done at some point I'm gonna remember this but like not yet just make a big number of 666.
new big number there you go so we can call them my instruction with this data but then we also need to provide the accounts so I'm going to say accounts and here we have the data account which we will just I don't know use a new key pair damn it don't you have a key pair oh that's how you do it in Anchor okay fine see it's it's all a bit different than we used from the web 3 that we have anchor dot web3 hides the Solana web 3 stuff okay so we just generate a new key pair said that public hears the data account what else do we need in here the user is my account so wait who do I sign here with I guess I need another keypad to sign with I mean I could also sign as the data account which nah would work but like I'm gonna be nice and say there's another user and then we need the system program and that we find in web 3 System Program ID cool so that's the accounts then we provide the signers in this case the data wait actually the user and the data account keeper need to sign however that's not gonna work if they ain't got no soul on there so probably for the user I do want to create a an actual keeper file so I can drop some soul on there because the next thing would be to call RPC which basically does the send transaction I could also just do dot instruction get the instruction out and then manually send but we do it like that and then here we say anchor test skip deploy what do you try to do when I say anchor test ah that automatically starts the local test validator the plus the problem and runs the test kind of merely manually run those tests TS configures okay let me stop my local test validator for a second and then run anchor test then it just starts one up here there we go and we get the error failed to send transaction this program will not be used for executing instructions okay that's not the error I expected kind of looks like it hasn't been deployed also it was way too fast anyway but it did not deploy then did it what can I say here there I'm just gonna use the one I had and I'm gonna use this flag to skip local validator there we go now it's deploying there and then there we go that's more what I expected okay so I have my local test related running here now and I use the skip local validator for it to not start up another one and then it's failing with zero X1 which should sound familiar we don't have enough lampers and we don't have enough lampers because obviously we create a new key pair here and then we try to v-pay with it which obviously doesn't work okay so let's create ourselves inside the tests you know what I think I'm just going to be lazy because I can't like it's always the same it's always the same but I think I'm just gonna be lazy and copy that over here that's how you should not do it but that's how I'm gonna do it put the private key directly in there because I can't be bothered to read the file yes I'm lazy it's just a test and I don't use that for anything anyway but then I can be like Solana airdrop tensor to this user key pair and then here's some soul and then I can do my anchor test again of course with the skip local validator I could also skip deploy because it doesn't need to redeploy every time there we go and now this test passes I mean that doesn't really mean much but like the transaction was sent because I don't have any asserts in here or expect so yeah but what we can then do is Solana confirm Dash V this signature and we see what happened in the program block the data account was created now that doesn't really say much let's be honest but it if we look at the instructions this account 0 the 81s this account was created we should also be able to see that in here because the system program was invoked all in all but it doesn't show us with which account so if we say Solana account and then this so check this out now we have the actual data of the account here and we see that the first eight bytes that's just random stuff that's the hash of the account type this here is my u64 yeah that's my 8 bytes of the number that I put in so if I convert that from hexadecimal to decimal then that should give me one two three no six six the nine a02 Little Engine so two nine a two nine a hex 2 decimal there we go it's 666 in decimal easy and then the remaining two bytes they are still zero zero that is my number two that's this thing because I didn't write anything in here I could write something in there as well so let's write I don't know two because it's number two ha Lord um and do the same thing again this time I wanted to redeploy build and redeploy there we go I'm just gonna do a new text for oh [ __ ] I already lost it what was the account now the 81 if we Solana go from this thing then this account was the DK something Solana account this thing there we see now that 2 is in here and the same data here pretty simple huh and just to do something ourselves now that's now this account let's create a function where we sum the numbers of two accounts or something so I would do a an instruction like the one we had my sum and we don't do any input data for that and we'll provide some accounts and the some accounts look as follows we don't need the system program anymore we don't need a user anymore I mean somebody needs to sign but I don't need to access the account so I'll leave it out and I will just make that account mutable so data account 1 and data account two two mutable accounts actually do they need to be mutable no because I just want to I don't know print the account so that I don't even need that but the point is they need to be of type account struct still so they still need to have the number one and number two and inside that thing I just calculate myself the sum which is context accounts data one number one Plus this from account to number one so I sum up the two number ones and then I say the sum is some that's all the my sum does it just calculates the sum and then prints it because it can't be bothered to do something more complicated so that's that that's basically all we need then say anchor build again to update the IDL if we now have a look in our IDL I said if we now have a look in our in our IDL we hopefully have yep the my sum instruction here as well so that we can use in our test now just create another test test some where we do the my sum which doesn't take any parameters but it needs accounts and it needs the following accounts the data account one which I'm just gonna hard code in here it's gonna be this one and then the data account 2 is gonna be this one all right let's test this again oh the sum fails unknown signer um I mean if I don't use this I'm pretty sure I need some signer otherwise I can't pay transaction fee oh don't ask me where that transaction fee comes from but without the signer that seems to work let's look at that we have the signature this account this account that's the program oh I am the fee pair then it automatically just uses my public key the one that I also deployed to program with okay that's fine good anchor you do that that's that's okay because it's inside this program here now inside the provider but we will learn about that next time so don't don't worry too much about that we do have a v pair that's all those are the two accounts this one and this one and essentially here we call the my sum and the sum is one three three two because that's 666 times two I would have liked to be this a bit higher five higher so that's the that's the happy part the happy path if everything works now just to demonstrate that I can't just put any account in here now if I were to for instance put in my account and try to build the sum from that then anchor will automatically fail that transaction there oh yeah cannot read property of undefined okay maybe I don't want to skip deploy there anchor error caused by account data account two error code number 3007 the given account is owned by a different program than expected so this account is not one of ours and even if it was one of ours it also needs to be of that type I could use a different account construct in here now this account is now created with a different account struct so Solana account this thing will have different data here the rest is the same though so the numbers stay the same but the discriminator is different and if I put in that account in here then it's owned by my program so the owner check will pass but then the account check will still fail because different error now 8 by discriminator did not match what was expected so anchor checks that the correct accounts are put in because here I wrote it needs to be of that type so I'm expecting this account but I put in one that has a different one from a different type which in that example doesn't really make sense because they have the same things but you know in in reality you will have different things like think of the token program that has the mint account and the token accounts they they have a different layout so it makes sense to distinguish between them and anchor implements all of those checks for us we don't need to deal with that we just say what we want and anchor automatically does the checks so that's super helpful and that concludes my introduction to Anchor I think I'm good with that we'll have a look more at this front-end stuff with the providers and whatnot next time you can do a challenge for yourself build something like the thing I built or what James did here or do Your Own Thing a counter with an incremented a decrement try not to copy too much from us try to do it yourself or do it once with copying and then with trying it yourself the more you do it the easier it will be for you to write anchor programs is just getting used to all of those how do you write that write this and then this and where's where all those things it's just practice so just write your own anchor programs you'll get used to those things I hope that this video was helpful and I hope that you enjoy your journey of learning Solana programming anchor mixed writing programs a lot easier so you can write programs faster once you get a hang of it once you know how all of this works so yeah we're making steps in that direction we are now done with the intro to Anchor development next time we do client-side and then we'll have more more anchor stuff coming up so make sure to subscribe like the video that's the thing for subscribe no sips subscribe like the video and check out those other videos as well and then I see you in the next one till then bye bye
Up Next

The Impact of Anchor on Solana Smart Contract Security
@chainlink
2.6K views•2022-04-26

Torrent File Format & Bencoding: A Technical Deep Dive
@AsliEngineering
12.5K views•2022-08-08

Operational Security Essentials: A Guide for Hacktivists (OPSEC)
@hitbsecconf
157.4K views•2012-11-26

Understanding Ethereum: A Comprehensive Beginner's Overview
@99Bitcoins
3.1M views•2018-06-26
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Blockchain & Crypto







![How To Get Started on Solana [Tel Aviv Hacker House Talk] - Jun 26th '23](https://i.ytimg.com/vi/ygtSTydWV-g/maxresdefault.jpg)






![How To Read Data From The Network [Solana Development Course: Module 1, Part 1] - Aug 1st '22](https://i.ytimg.com/vi_webp/HavGDGUTmgs/maxresdefault.webp)


![Fundermentals: Accounts, Instructions and Transactions [Solana Basics Day2] - Feb 7th '26](https://i.ytimg.com/vi_webp/fXKyOrudpgA/maxresdefault.webp)








![Advanced CPI calls in Anchor [Solana Tutorial] - May 9th '23](https://i.ytimg.com/vi_webp/AKOWg65QWbQ/maxresdefault.webp)












![[Part 12] Solana Crash Course - Token Metadata](https://i.ytimg.com/vi/IltIDMH2CtQ/maxresdefault.jpg)
![[Solana Bootcamp Summer 2024] #1 - Kick off | Introduction to Solana](https://i.ytimg.com/vi/8kkGIod41rg/maxresdefault.jpg)

