{"id":"BA7VeUS_RAA","title":"NEAR Live Contract Review | Episode 1: Voting Contracts","description":"The inaugural episode of our new live contracts review series is about Voting Smart Contracts. Get insights and explanations directly from NEAR Core team members as they talk through the intricacies of important contracts on NEAR.\n\nFollow the latest from NEAR:\n\nWebsite: https://nearprotocol.com/\nDiscord: https://near.ai/discord\nMedium: https://near.ai/medium\nTwitter: https://near.ai/twitter\nGitHub: https://near.ai/github\n\n#LiveContractReview #Blockchain #FutureIsNEAR","channel_name":"NEAR Protocol","published_at":"2020-10-29T00:00:00.000Z","thumbnail_url":"https://img.youtube.com/vi/BA7VeUS_RAA/hqdefault.jpg","transcript_text":"hey um my name is Eugene and I'm here with uh near and today we'll try something new we'll go through one of our contracts in Rust and we will review how it's all done and this is the first session so let me um introduce myself so I'm Ain I work at near I work on runtime and contracts and um let's go next with me is Mike hey I'm I'm Mike I'm a devx engineer I've got some Rust experience but these contracts we're looking at today are going to be new for me so it's exciting all I'm Chad in the same position as Mike devx engineer this contract is new to me less rust experience even than Mike but like some familiarity so Josh who are you uh I am Josh I am also a devx engineer and my rust experience is even lower um than than these individuals so I'm really looking forward to to learning more uh my name is Sharie I work on education in year I can spell rust confidently looking forward to it all right let's start let me present um my screen um today we gonna discuss a voting contract and this is the contract that was used by by um myet to trans to move from um phase one to phase two so validators had to vote to enable transfers which essentially made the network fully decentralized okay so um this is cine I'm using it as one of the greatest tool to work on rest and this s so on the left you can see um a reper for core contracts this is open sourced on GitHub and our page and uh there are a bunch of contracts and one of them is voting so let me first go through quick structure of how this all works so on R you have SRC and leap leap is a main file it's a library because we're not really compiling a binary but we compil in a library into web assembly there is a cargo toml this is the way to describe dependencies so we saying we going to build a voting contract and we use a nearest DEC version 2.0.0 and there's a bunch of flags that we use to tell how to compile with release build so there are a bunch of optimization that improves the size of the code then we say there is no debug info in release and also Panic is uh aboring this is important for web assembly and finally we have overflow checks equals true this is means when um integer overflow happens something like you can like subscribe or you can subtract an a number so let's say you subtract two from one one and this goes to negative and if your in integer is unsigned this will make it um uh Panic at this operation so on C++ it usually silently rolls over so on Rust you can enable a flag that will make it Panic on any overflow or if you exceed the limit of the integer in either direction so if you go beyond Q32 this will L pen and when you develop contracts for any like smart contract platform usually overflows silent overflows are not intended behavior and can lead to abuse so ideally your contract will panic and you'll just uh handle it so this just for safety um then we have other files and we'll go there later but first uh let's start with the main contract so this is a single file and it is fairly simple at the same time it has some interesting stuff so the way the way contracts are done at near protocol in Rust is we have a main structure so in this case we using a structure called voting contract structure is similar to class in other languages and we have a few macres here so near by Bon is a macros from near SDK that turns the structure into something that we use for contracts and I'll go later into details and b d and caliz are two matrices that allows this structure and all fields in it to be serialized to our binary format called Porsche and deize it and this is needed to be able to store this entire contract into a state so the way contracts work is they have some persistent State um from this which they can read information on every invocation of the contract and then they have a local memory which is only available when a contract started so let's say um it probably going to be better explained later so so we have a structure and then in Rust you have implement ation of a structure implementation of the structure is basically methods of the class so one of the methods is new this is a Constructor it doesn't take any arguments for this particular contract and it returns self self is equivalent of a voting contract basically it's saying that it constructs a new instance of this class we have a decorator macro called init and this init allows us to initialize a state of the contract without having a previous state um the way how contracts usually deployed on near protocol is you deploy the code of the contract essentially you deployed a class and then you need to initialize an instance by calling a Constructor this will write a state in our case this particular structure into a persistent storage of the contract so it will be available every time you call this contract does it me sense so far okay um so far so good so far so good there's a couple of um uh ideas that you're you're talking about that are a little bit new but I expect watching the video again would help okay um so let's go through the Constructor first um so when you first initialize the contract the voting contract it doesn't take any AR ments because well it doesn't need to know anything because it will only rely on the current context of the blockchain and this context comes from uh module called n which is short for environment from nearest Decay this is where we'll need uh information down the line and we'll use arguments that uh other um other contracts or users call this contract so there's two things happening at the Constructor first the check that the state doesn't exist it means the contract is not yet initialized so this mro um allows to for the contract to be called multiple times so it doesn't check if the existing state already there it allows to do this uh we are working to add another macro which is we call init once that will allow only to initialize a contract once and it will do this check automatically so assert is similar to unit test um it just verifies that the given um expression is true and if it's not true then it throws a panic which is stops the current function call and uh sends this message as an error so it's saying if the state doesn't exist or if the state already exists basically inverse of this then then the contract was already initialized um and we cannot initialize this again by so if we we didn't have this then we would be able to overwrite whatever was there with this new state then yes does State exists work together with in nit is that where it comes from like what state is it check no so State exist is actually from near protocol and um this is the internals how it works so we probably need to look at the how nearest Decay uh in Rust Works to better explain so every time not a Constructor is called so let's say this is a method it takes the argument mutable self and by using this decorator near B engine it creates a specific code from this contract that um does a bunch of operations before getting into the um execution and one of them is if you take mutable self or self it first tries to deriz the current state the way it works is on a lowlevel API we have a state key so this is the key for which we store data of main structure so let's say we in a Constructor we constructed this state basically this is a return rest if the last um value we return this new structure which we initialized the way we want it and then and then this structure by this macro getting serialized to a binary format bch and getting written into this persistent storage under a key State and the value going to be the current value of this structure so next time a method is called like this it first will read the binary part out of storage deserialize it and create this structure so and then only after this this part will start getting executed so this is part um that comes from nearest DEC it's basically a rapper around all it does it checks if the given storage has this particular if the storage has this particular key and if it has a key then it knows that the state already exist okay I I was thinking that maybe the contract itself the binary of the contract was being stored in the same location in some database with the state so that was part of my confusion the other part is what does AIT actually do so init allows you to um at the end of the so it expect that you return a new class or a new instance and it will write it to the storage so this is what it does basically just saying that um I don't expect any arguments but you will return me a you state that I will write into the storage so it's it's indifferent as to how many arguments to clarify right like you you could pass arguments okay yes yes okay so we can also have a default on the contract and we don't want to our contract to be initialized with a default argument without call initialization so we say if a default method is called so this happens when the state doesn't exist so it was not initialized then this method is called default so we don't want this to happen so we're saying it should be initialized first so basically just panics uh with the error you isn't isn't default called automatically when you call any other method on the interface that's why you have to kind of override it with this Panic so yes nearest Decay um it basically tries to first read the state from the storage from the state key and if it doesn't exist then it will call default but if it exist then it's not going to call default gotcha cool so let's go to the first um method and see what we have so let me just um see the interface first of the contract so we have a few methods and rust has a decorator of Pub which means visibility is public and if there's no uh visibility it means it's private so near Bon decorator only turns public methods into the methods that you can call on the contract which means check result method is not exposed to the contract it cannot be called by someone else all the rest of these methods uh can be called um by anyone essentially just a double check in in Rust syntax the pub modifier could be on the implementation all the methods could be non-pb but the implementation's pub and then they would all be part of the interface um I'm not sure about this I think implementation doesn't have a well maybe okay I don't know either that's I was just checking I think I saw that before okay I I don't think it's true but I don't want to argue because I'm not sure about this so one more important thing about nearest DEC is a difference in arguments that you pass so if you pass argument mute state it means um the method will be will allow will be allowed to modify the state uh we call this type of methods um Regular function calls so they canot only come through a transaction and in contrast there is a des mute self so this the state of this um structure will be IM mutable it basically readon State you cannot modify it and this type of methods can be called using uh view call and then not going to modify the state and so that's a quick way to tell if it's a view method or a change method in Rust is just if it if it is mutable you can assume it's a change method right it can be called as a change method basically um and I think if you don't have mute self then it will not write a state at the end I'm not sure we enforce this is a view method only on nearest d okay uh you may want to check this on the next call uh but the important part is we should treat them as non-changeable methods so they are not modifying the state um and usually like cap usually they intended to be a view methods versus mute intended to be a change methods so um before we dive into any particular method let's look into this one it has an argument in addition to state so by default near Buon expects um methods to be passed or arguments to be passed as Json so default deserializer of arguments Json so this piece expects a method to be called with something like the following so method name vote and arguments is going to be U adjacent in format of is vote for example true so this is the arguments the arguments that the method expects and this is important because you cannot pass arguments using positional um call into a contract but internally within this implementation we can call vote this positional arguments and within unit tests we can also use positional arguments you can use either or uh no so basically if you VIs in Rust same rust not calling the web assembly binary before near B does it magic to turn it into a contract then you can use you should use positional arguments but if you um calling it either from simulation test or from um like on a blockchain actually then you are already calling a pre compile it web assembly Library binary contract and it will expect it in Json format so the row web assembly doesn't know any type uh inputs and the pars input completely different probably it makes sense next time to go through expanded macro I don't want to go on this call but we can do basically um run macro expansion on the compiler to see what's the raw code is generated for every method and it will be easier to see what's happening Bes behind the scenes uh before we actually get to execution of the body the meod um okay and finally return arguments so this view call returns an option of WRA time St so um the return types uh after near B and contract micro expansion eljon so this type is a specific um custom type which is u64 so this is coming from near SDK Json types and it is a structure that's probably too too much rust it's a structure that has a row u64 type but when Json distalization is called on it or realization it turns it into a string the reason for this is Json standard doesn't support all u64 types like not full range it only comes from um I think that the limit is something like two in the power of 53 because uh JavaScript Al had this limitation on numbers and Json standard the default Json standard that comes from Front End will not serialize a large g64 numbers uh into a proper number so instead we up um 64 integers u64 is an signed 64 bits integer and U1 128 integer into Strings so that method will return you um adjacent something like this which basically a string if um where the actual number is uh like a thousand or if it doesn't exist the result doesn't exist then it will return you no okay so this is um high level of nearest DEC and we can now go into one of the methods and probably the main method how we get into this contract is a voting voting call so when you're an active validator so you have an account with at least one seat in near protocol so you have a pretty large state to hold um to to have stake to to be active validator then you can before phase two you can call this method in order to cast your vote so if is vote is true it means you want to vote for phase two if you said his vote is false it means you're withdrawing your vote so let's say you first casted your vote and then you want to cancel it for for some reason so for example you feel uh you didn't know something and you want to retreat it so let's see how it works so this is the change method and it automatically gets an argument deserialized by near Bion even so as we discussed before it was expecting Jason internally just a Boolean where variable that you can use normally in Rust and the method is like this so it's nothing about how the state has changed or actions you did so you don't need to manually save the state it will be automatically saved if the method completes so first thing it does it's called pink pink we will go back go to this method later but it does um update the internal state by checking if the epoch has changed and who's the active validator to update the data then it checks if we already reach a result in our case I guess we need to first um no it's s so result is is a field that's saying we already reached two3 of the validator then it's just a time stamp at which um phase two started and this is a time stamp in NCS because um this is how we expose time stamp to within a context option means it either exist or doesn't exist so if it exist then it's given time stamp so it means voting already happened and we reach the phase two at a given time stamp in NCS if it none so it doesn't exist it means uh the voting was not reached so we check that if we already reached a vote if you reach phase two then we don't need to do anything your vote doesn't matter because it's already phase two and T temp already there um then and for yeah for like rust folks or or people who are not familiar with rust this whole option some is some none thing is not like a near SDK RS thing that's just like a straight up rust thing right yes so it's similar how JavaScript has three types of Boolean or maybe more but it can be true false and null so null is the variable doesn't exist um so similar a lot of languages have null but it's worth saying that rest does not have null instead it has option types which can return some value or none and you have to explicitly deal with it or it yells at you so but when it gets encoded for the outside world it is getting like turn converted to Json for example it is getting stor sent as null I MH cool so next is the query the context of new SDK and we check who called us so there's three types of accounts that available for from Context uh one of them is predecessor this is the account ID of immediate predecessor of who called vote so um near is a synchronous environment and you can have a chain of cross contract calls so let's say Alice calls Bob contract Bob's calls um chl's contract and in this case predecessor going to be Bob if WEA Charles then predecessor will be immediate account who called us in this case in this example it was Bob um in contrast there also sign their account ID this is the account who signed the original transaction uh in my example let's say it was Alice who called contra Bob and then Bob culture so signer account ID will be Alice and current account ID is the account of the contract so in this case it's going to be Charles um the account of that holds the contract so on near the contract always belongs to some account on which it's for state and has balance and has um code deployed for security reasons it's very important to always rely not on the signer of the transaction but on the immediate predecessor because let's say Bob is a malicious contract and if he would rely on the signer that Bob can call some token transfer contract and withdraw a balance from Alice to themsel or their owner instead of uh act on by acting on Alice behalf so if would if Bob would try to do this then the token contract will get the predecessor who is Bob so uh contract Bob can only um spend balance of their own so it has some limitations but uh this guarantees you that if you call in some contract this contract cannot really spend your um your cannot really act on your behalf except on itself make sense yeah awesome cool um so next thing we do is check the argument that was passed so if it is dev vot then we um it means we want to add this our state from the caller to set of active validators who voted for phase two so otherwise uh the value is zero it means we want to set our stake to zero so we call a special method on the environment which is um calls a host function on near protocol runtime to get the active stake from the current Epoch of a given account ID it will return um for a given validator how much stake they have and this is will be done immediately so this is a synchronous call we verify that your stake is zero or above zero to make sure that uh we don't pollute State this nonactive validator so this way we can limit how many validators are can get into our state so because we only have 100 SS when this contract was used um it means you can limit the number of validators in the state of this contract to 100 so basically um if a AER triggers terms of stake is zero then we throw a panic and everything we did before this all changes to the state will be rolled back to the beginning of the function call only for this country so in this case again coming to example of Alice Bob Charles um only state of Charles going to be reverted but but state of BU is not because of a synchronous environment next um uh we check if we already voted before if we did then we just remove so votes is a hashmap um hashmap is in memory map it means um every time it's serialized or deserialized all values has to be serialized into one pie and then deize back um why did you I think the uh hashmap is interesting I was just recently talking about this with Mike actually um and maybe Josh was on that call too but in general we give the uh advice to people that like most of the time you want to avoid hashmap and use a collection from near SDK instead yes um and I'm curious why um what makes this a better case for hashmap good point um so hashmap as I said whenever we call a method on this um contract it will distalize all fields of the hashmap so all 100 potential accounts will be distalized at once um but it will do this into a single storage read instead of 100 storage reads so this is um that even so the overall size of this is large um because each account can be up to 64 characters and balance is um 16 bytes the overall size of this is not as large so it's 100 * 80 so it's like 8 kilobytes um but we need it in pink in pink we have to iterate on every single vote in order to update the stake of the validator and if we would have to iterate on every single key from a persistent collection for example an ordered map then it will we will have to do something in order of 100 reads and in this case we we actually need to overwrite them which means we need a 100 wres and 100 reads is much more expensive than one time bulk rate of 100 accounts because we know that the that structure is limited to 100 we don't need to introduce pagination that you would might need to do with a persistent map if you would want to trate and this is because the try was stored in oh the the the storage is uh kept as a try and this is makes it expensive to to do a lot of reads so in this case it was beneficial to have one map that you will be able to iterate so the guideline here would be if you need to iterate on every single key you probably want to have um a hashmap or a vector but it's also that you also know that it's bounded that there won't be more than aund of these right otherwise yeah in this case that's specifically because there's not going to be more than a 100 validators for now yes is that correct correct and if near were fully sharded then like this will need to be modified potentially with a potentially persistent map one of persistent map or maybe you can do something like a shed map uh we don't have it right now not or like bulk map so it will be a persistent map of like bulk objects because you still need to iterate on all of them um maybe you can like split your accounts into 20 buckets and then every bucket will be a vector but you will store them separately into persistent map so when you need to iterate you only need to read 20 keys or you introduce some pation for pink am I correct in saying I mean that that was a really good explanation uh thanks uh am I correct in saying that like this is kind of Min maxing in the sense that even if you used a near persistent uh collection instead of a hashmap it's not like it would be crazy expens expensive right um but this is just it's just it's just like better but like yeah I guess that's my question right so so currently our storage is probably one of the most expensive operation that you will usually face by default um and if you do like overwriting of them then potentially it will be more expensive than a hash map and that's why we picked a hash map right now but it's not like dramatically worse but to be safe we we can pretty much estimate that 100 FTS into our current gas limits and it makes it a good choice for this particular contract cool so I think we we have like 10 minutes that we want to spend more on this and let's try to finish some of this so what happens here is um you basically remove the old Stak uh this is just a safety check and it's an invariant as it's saying here which means it's unlikely to trigger in real in environment so it verifies that the current stake is less than a total stake then we subtract the old stake and introduce a new stake well I would argue that order of this would be better to be first substracting the old stake and then introducing a new stake in order to avoid overflows even so is our limit on total Supply it's not possible to overflow but you always want to be careful and finally if this was an active vertice it was not a withdrawal then we introduce this into um into the storage and at the end we will check the result again so let's go to check result first so we verify so when check result is called we check that the result is not some that's another way of checking it and it also in variant and we see what's the total validator State this another method that is return from Context and let's say it's saying we have 100 million total State and then internally this is active stake means all validators who voted on this and we saying if active stake is more than 23 rounded down of total stake then we reach the result and that's pretty much over this contract so voting has completed so this is fairly simple method and we probably want to go to Pink So pink um this is happens here on the boundary of epo so after an EPO switches it means the new set of validators became active within the epoch on your protocol um validators do not change so even if someone kicked out they will be only inactive on the next ebook so what we do we verify that we didn't reach the result um then we take the current Epoch heite it's just an integer that always in cursing and we verify that it doesn't match the previous Epoch heite so we started with okite zero um but every time a pin disc called it is um it can update the eoch so if Epoch has switched so it can pass one eoch or five EP that matter what we're going to do we're going to go through all previous active votes who voted for phase two we reset the total stake to zero and this is the rust way of um taking all the data out of fresh map and and setting it to default so basically we got a copy of the map from internal structure and now internal structure contains an empty hashmap it's a new think that it was introduced in like recent trust so that that's the M false so that all like the same size of votes but all of them are set of false no it it basically just empty hashmap so it it calls hashmap default set it to this variable so let's go here basically callose replace replace do swap but a second argument to to the replace is a default of hashmap which is empty map so it gave us a data from our structure and now this contract structure contains an empty map and this is just so we can do in one go modify the current map or the new map based on the data from the old map so we basically just need to canel this we don't need to know what was the previous St so we check if someone was uh removed from the map like removed from the active validators so we get their stake if there stake uh is positive we add them to the total stake and we keep their vote but if they were kicked out they have to vote again um I don't think this happened during the phase one no one was kicked out really from the validating boards maybe someone was I'm not familiar actually I think yeah one validator was kicked out and um and basically we update total state by Rec Computing what's the new Total stake and then we store all the results back to our contract State and at the end we do two things we check the result so after Epoch has switched if validators acquired more stake or the total stake decreased then the result might happen already and we remember at which Epoch we did this so next time someone called pink or vot we don't need to go through this if Epoch hasn't switched so that's pretty much the voting contract basically every time some calls a vote we first update data from the previous validators in and write it into internal structure most important we get the total voted stake updated and then we call Checkers out twice occasionally within the one call and what's important is even so we we can call pink here from one method but also we can call pink directly by a transaction so you don't need to revote you can just call pink so it's pretty much a contract and the way rust works so there's a bunch of uh view calls so one of them just Returns the result and another one of them is um returns how much Stakes It Go and the final is a functional way of returning all the votes it basically iterates on the map and then wraps it into a specific type of the balance using into so we almost out of time but I want to point out that there is also a unit test in the same code and it's just a rust way of doing this and unit test can validate some logic within a CL by faking the context and stuff like this okay any questions to follow up before we close this is great all right so let's go back to the call okay um thank you very much thanks everyone um all right let me stop C","transcript_synced_at":"2026-11-04T04:23:43.530Z","segments":[{"start_seconds":2,"text":"hey um my name is Eugene and I'm here with uh near and today we'll try","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2s"},{"start_seconds":10,"text":"something new we'll go through one of our contracts in Rust and we will review how it's all done and this is the first","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=10s"},{"start_seconds":18,"text":"session so let me um introduce myself so I'm Ain I work at near I work on runtime and","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=18s"},{"start_seconds":26,"text":"contracts and um let's go next with me is Mike hey I'm I'm Mike I'm a devx","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=26s"},{"start_seconds":34,"text":"engineer I've got some Rust experience but these contracts we're looking at today are going to be new for me so it's","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=34s"},{"start_seconds":43,"text":"exciting all I'm Chad in the same position as Mike devx engineer this contract is new to me less rust","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=43s"},{"start_seconds":51,"text":"experience even than Mike but like some familiarity so Josh who are you uh I am Josh I am","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=51s"},{"start_seconds":60,"text":"also a devx engineer and my rust experience is even lower um than than these individuals so I'm really looking","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=60s"},{"start_seconds":67,"text":"forward to to learning more uh my name is Sharie I work on education in year I can spell rust","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=67s"},{"start_seconds":76,"text":"confidently looking forward to it all right let's start let me present um my screen um today we gonna discuss a","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=76s"},{"start_seconds":85,"text":"voting contract and this is the contract that was used by by um myet to trans to","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=85s"},{"start_seconds":95,"text":"move from um phase one to phase two so validators had to vote to enable","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=95s"},{"start_seconds":103,"text":"transfers which essentially made the network fully decentralized okay","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=103s"},{"start_seconds":110,"text":"so um this is cine I'm using it as one of the greatest tool to work on","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=110s"},{"start_seconds":117,"text":"rest and this s so on the left you can see um a reper for core contracts this","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=117s"},{"start_seconds":125,"text":"is open sourced on GitHub and our page and uh there are a bunch of contracts and one of them is voting so let me","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=125s"},{"start_seconds":134,"text":"first go through quick structure of how this all works so on R you have SRC and","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=134s"},{"start_seconds":141,"text":"leap leap is a main file it's a library because we're not really compiling a binary but we compil in a library into","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=141s"},{"start_seconds":148,"text":"web assembly there is a cargo toml this is the way to describe dependencies so we saying we","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=148s"},{"start_seconds":156,"text":"going to build a voting contract and we use a nearest DEC version","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=156s"},{"start_seconds":164,"text":"2.0.0 and there's a bunch of flags that we use to tell how to compile with","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=164s"},{"start_seconds":171,"text":"release build so there are a bunch of optimization that improves the size of the code then we say there is no debug","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=171s"},{"start_seconds":180,"text":"info in release and also Panic is uh aboring this is important for web assembly and finally we have overflow","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=180s"},{"start_seconds":190,"text":"checks equals true this is means when um integer overflow happens something like","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=190s"},{"start_seconds":199,"text":"you can like subscribe or you can subtract an a number so let's say you","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=199s"},{"start_seconds":208,"text":"subtract two from one one and this goes to negative and if your in integer is","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=208s"},{"start_seconds":215,"text":"unsigned this will make it um uh Panic at this operation so on","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=215s"},{"start_seconds":223,"text":"C++ it usually silently rolls over so on Rust you can enable a flag that will","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=223s"},{"start_seconds":230,"text":"make it Panic on any overflow or if you exceed the limit of the integer in","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=230s"},{"start_seconds":236,"text":"either direction so if you go beyond Q32 this will L pen and when you develop","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=236s"},{"start_seconds":243,"text":"contracts for any like smart contract platform usually overflows silent overflows are not intended behavior and","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=243s"},{"start_seconds":252,"text":"can lead to abuse so ideally your contract will panic and you'll just uh handle it so this just for","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=252s"},{"start_seconds":261,"text":"safety um then we have other files and we'll go there later but first uh let's start with the","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=261s"},{"start_seconds":270,"text":"main contract so this is a single file and it is fairly simple at the same","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=270s"},{"start_seconds":279,"text":"time it has some interesting stuff so the way the way contracts are done at near","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=279s"},{"start_seconds":288,"text":"protocol in Rust is we have a main structure so in this case we using a","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=288s"},{"start_seconds":295,"text":"structure called voting contract structure is similar to class in other","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=295s"},{"start_seconds":302,"text":"languages and we have a few macres here so near by Bon is a macros from near","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=302s"},{"start_seconds":312,"text":"SDK that turns the structure into something that we use for","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=312s"},{"start_seconds":319,"text":"contracts and I'll go later into details and b d and caliz are two matrices that","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=319s"},{"start_seconds":329,"text":"allows this structure and all fields in it to be serialized to our binary format","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=329s"},{"start_seconds":337,"text":"called Porsche and deize it and this is needed to be able to","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=337s"},{"start_seconds":345,"text":"store this entire contract into a state so the way contracts work is they","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=345s"},{"start_seconds":355,"text":"have some persistent State um from this which they can read","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=355s"},{"start_seconds":361,"text":"information on every invocation of the contract and then they have a local memory which is only","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=361s"},{"start_seconds":370,"text":"available when a contract started so let's say um it probably going to be better","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=370s"},{"start_seconds":380,"text":"explained later so so we have a structure and then in Rust you have","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=380s"},{"start_seconds":389,"text":"implement ation of a structure implementation of the structure is basically methods of the class so one of","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=389s"},{"start_seconds":398,"text":"the methods is new this is a Constructor it doesn't take any arguments for this particular","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=398s"},{"start_seconds":407,"text":"contract and it returns self self is equivalent of a voting","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=407s"},{"start_seconds":415,"text":"contract basically it's saying that it constructs a new instance of this","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=415s"},{"start_seconds":423,"text":"class we have a decorator macro called init and this","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=423s"},{"start_seconds":429,"text":"init allows us to initialize a state of the contract","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=429s"},{"start_seconds":437,"text":"without having a previous state um the way how contracts usually","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=437s"},{"start_seconds":444,"text":"deployed on near protocol is you deploy the code of the contract essentially you","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=444s"},{"start_seconds":452,"text":"deployed a class and then you need to initialize an instance by calling a","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=452s"},{"start_seconds":461,"text":"Constructor this will write a state in our case this particular","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=461s"},{"start_seconds":467,"text":"structure into a persistent storage of the contract so it will be","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=467s"},{"start_seconds":473,"text":"available every time you call this contract does it me sense so","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=473s"},{"start_seconds":484,"text":"far okay um so far so good so far so good there's a couple of um uh ideas","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=484s"},{"start_seconds":493,"text":"that you're you're talking about that are a little bit new but I expect watching the video again would help okay","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=493s"},{"start_seconds":499,"text":"um so let's go through the Constructor first um so when you first initialize","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=499s"},{"start_seconds":506,"text":"the contract the voting contract it doesn't take any AR ments because well it doesn't need to know anything because it will only rely on","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=506s"},{"start_seconds":515,"text":"the current context of the blockchain and this context comes","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=515s"},{"start_seconds":524,"text":"from uh module called n which is short for environment from nearest","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=524s"},{"start_seconds":532,"text":"Decay this is where we'll need uh information down the line and we'll use","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=532s"},{"start_seconds":539,"text":"arguments that uh other um other contracts","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=539s"},{"start_seconds":547,"text":"or users call this contract so there's two things happening at the Constructor","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=547s"},{"start_seconds":554,"text":"first the check that the state doesn't exist it means the contract is not yet","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=554s"},{"start_seconds":560,"text":"initialized so this mro um allows to for the contract to be called","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=560s"},{"start_seconds":569,"text":"multiple times so it doesn't check if the existing state already there it allows to do this uh we are working to add another macro which is we call init","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=569s"},{"start_seconds":578,"text":"once that will allow only to initialize a contract once and it will do this check","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=578s"},{"start_seconds":586,"text":"automatically so assert is similar to unit","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=586s"},{"start_seconds":593,"text":"test um it just verifies that the given","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=593s"},{"start_seconds":602,"text":"um expression is true and if it's not true then it throws a panic which is","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=602s"},{"start_seconds":611,"text":"stops the current function call and uh sends this message as an error so","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=611s"},{"start_seconds":620,"text":"it's saying if the state doesn't exist or if the state already exists basically inverse of this then","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=620s"},{"start_seconds":629,"text":"then the contract was already initialized um and we cannot initialize","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=629s"},{"start_seconds":636,"text":"this again by so if we we didn't have this then we would be able to overwrite","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=636s"},{"start_seconds":642,"text":"whatever was there with this new state then yes does State exists work","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=642s"},{"start_seconds":652,"text":"together with in nit is that where it comes from like what state is it check no so State exist is actually from near","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=652s"},{"start_seconds":660,"text":"protocol and um this is the internals how it works so we probably","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=660s"},{"start_seconds":668,"text":"need to look at the how nearest Decay uh in Rust Works to better explain","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=668s"},{"start_seconds":677,"text":"so every time not a Constructor is called so let's say this is a method it takes the","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=677s"},{"start_seconds":685,"text":"argument mutable self and by using this decorator near B","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=685s"},{"start_seconds":694,"text":"engine it creates a specific code from this contract that um does a bunch of","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=694s"},{"start_seconds":701,"text":"operations before getting into the um execution and one of them is if you take","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=701s"},{"start_seconds":709,"text":"mutable self or self it first tries to deriz the current","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=709s"},{"start_seconds":715,"text":"state the way it works is on a lowlevel API we have a state key so this is the","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=715s"},{"start_seconds":725,"text":"key for which we store data","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=725s"},{"start_seconds":732,"text":"of main structure so let's say we in a Constructor we","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=732s"},{"start_seconds":740,"text":"constructed this state basically this is a return rest if the last um value","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=740s"},{"start_seconds":749,"text":"we return this new structure which we initialized the way we want it and","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=749s"},{"start_seconds":759,"text":"then and then this structure by this macro getting serialized to a binary","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=759s"},{"start_seconds":768,"text":"format bch and getting written into this","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=768s"},{"start_seconds":774,"text":"persistent storage under a key State and the value going to be the","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=774s"},{"start_seconds":783,"text":"current value of this structure so next time a method is called like this it","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=783s"},{"start_seconds":790,"text":"first will read the binary part out of","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=790s"},{"start_seconds":797,"text":"storage deserialize it and create this structure","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=797s"},{"start_seconds":805,"text":"so and then only after this this part will start getting executed so this is","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=805s"},{"start_seconds":811,"text":"part um that comes from nearest DEC it's basically a rapper around all it does it","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=811s"},{"start_seconds":820,"text":"checks if the given storage has this particular if the storage has this particular key and if it has a key then","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=820s"},{"start_seconds":829,"text":"it knows that the state already exist okay I I was thinking that maybe","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=829s"},{"start_seconds":837,"text":"the contract itself the binary of the contract was being stored in the same location in some database with the state so that was part of my confusion the","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=837s"},{"start_seconds":845,"text":"other part is what does AIT actually do so init allows you to um at the end of","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=845s"},{"start_seconds":854,"text":"the so it expect that you return a new class or a new instance and it will write it to the storage so this is what","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=854s"},{"start_seconds":863,"text":"it does basically just saying that um I don't expect any arguments","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=863s"},{"start_seconds":871,"text":"but you will return me a you state that I will write into the","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=871s"},{"start_seconds":880,"text":"storage so it's it's indifferent as to how many arguments to clarify right like you you could pass arguments okay yes","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=880s"},{"start_seconds":890,"text":"yes okay so we can also have a default on the contract and we don't want to our","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=890s"},{"start_seconds":899,"text":"contract to be initialized with a default argument without call initialization so we say if a default","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=899s"},{"start_seconds":907,"text":"method is called so this happens when the state doesn't exist so it was not initialized then","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=907s"},{"start_seconds":916,"text":"this method is called default so we don't want this to happen so we're saying it should be initialized first so","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=916s"},{"start_seconds":923,"text":"basically just panics uh with the error you isn't isn't default called","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=923s"},{"start_seconds":930,"text":"automatically when you call any other method on the interface that's why you have to kind of override it with this","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=930s"},{"start_seconds":937,"text":"Panic so yes nearest Decay um it basically tries to first read the state","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=937s"},{"start_seconds":945,"text":"from the storage from the state key and if it doesn't exist then it will call default but if it exist then it's not","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=945s"},{"start_seconds":954,"text":"going to call default gotcha cool so let's go to the first um","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=954s"},{"start_seconds":962,"text":"method and see what we have so let me just","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=962s"},{"start_seconds":969,"text":"um see the interface first of the contract so we have a few","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=969s"},{"start_seconds":976,"text":"methods and rust has a decorator of Pub which means visibility is public and if","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=976s"},{"start_seconds":984,"text":"there's no uh visibility it means it's private so near Bon decorator only turns","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=984s"},{"start_seconds":993,"text":"public methods into the methods that you can call on the contract which means check result method","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=993s"},{"start_seconds":1003,"text":"is not exposed to the contract it cannot be called by someone else all the rest of these methods uh can be","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1003s"},{"start_seconds":1011,"text":"called um by anyone essentially just a double check in in","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1011s"},{"start_seconds":1018,"text":"Rust syntax the pub modifier could be on the implementation all the methods could be non-pb but the implementation's pub","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1018s"},{"start_seconds":1025,"text":"and then they would all be part of the interface um I'm not sure about this I","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1025s"},{"start_seconds":1032,"text":"think implementation doesn't have a well maybe okay I don't know either that's I was just checking I think I saw that before","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1032s"},{"start_seconds":1040,"text":"okay I I don't think it's true but I don't want to argue because I'm not sure","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1040s"},{"start_seconds":1046,"text":"about this so one more important thing about nearest DEC is a difference in","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1046s"},{"start_seconds":1054,"text":"arguments that you pass so if you pass argument mute state it means um the","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1054s"},{"start_seconds":1064,"text":"method will be will allow will be allowed to modify the state uh we call","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1064s"},{"start_seconds":1072,"text":"this type of methods um Regular function calls so they canot only come through a","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1072s"},{"start_seconds":1081,"text":"transaction and in contrast there is a des mute self so this the state of this","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1081s"},{"start_seconds":1089,"text":"um structure will be IM mutable it basically readon State you cannot modify","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1089s"},{"start_seconds":1097,"text":"it and this type of methods can be called using uh view","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1097s"},{"start_seconds":1107,"text":"call and then not going to modify the state and so that's a quick way to tell if it's a view method or a change method","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1107s"},{"start_seconds":1115,"text":"in Rust is just if it if it is mutable you can assume it's a change method right it can be called as a","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1115s"},{"start_seconds":1123,"text":"change method basically um and I think if you don't have mute self then it will not write a state at","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1123s"},{"start_seconds":1131,"text":"the end I'm not sure we enforce this is a view method only on nearest d okay uh","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1131s"},{"start_seconds":1140,"text":"you may want to check this on the next call uh but the important part is we","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1140s"},{"start_seconds":1149,"text":"should treat them as non-changeable methods so they are not modifying the","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1149s"},{"start_seconds":1159,"text":"state um and usually like cap usually they intended to be a view methods versus mute intended to be a change","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1159s"},{"start_seconds":1167,"text":"methods so um before we dive into any particular","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1167s"},{"start_seconds":1176,"text":"method let's look into this one it has an argument in addition to state so by default near","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1176s"},{"start_seconds":1186,"text":"Buon expects um methods to be passed or arguments to be passed as Json so default","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1186s"},{"start_seconds":1195,"text":"deserializer of arguments Json so this piece expects a method to be called with","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1195s"},{"start_seconds":1203,"text":"something like the following so method name vote and arguments is going to","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1203s"},{"start_seconds":1209,"text":"be U adjacent in format of is vote for example","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1209s"},{"start_seconds":1216,"text":"true so this is the arguments the arguments that the method","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1216s"},{"start_seconds":1223,"text":"expects and this is important because you cannot pass arguments using","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1223s"},{"start_seconds":1232,"text":"positional um call into a contract but internally","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1232s"},{"start_seconds":1239,"text":"within this implementation we can call vote this positional arguments and within unit tests we can","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1239s"},{"start_seconds":1249,"text":"also use positional arguments you can use either or uh no so basically if you VIs in Rust","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1249s"},{"start_seconds":1259,"text":"same rust not calling the web assembly binary before near B does it magic to turn it into a","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1259s"},{"start_seconds":1267,"text":"contract then you can use you should use positional arguments but if","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1267s"},{"start_seconds":1274,"text":"you um calling it either from simulation test or","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1274s"},{"start_seconds":1283,"text":"from um like on a blockchain actually then you are already calling a pre compile it web assembly Library binary","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1283s"},{"start_seconds":1293,"text":"contract and it will expect it in Json format so the row web assembly doesn't know any","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1293s"},{"start_seconds":1303,"text":"type uh inputs and the pars input completely","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1303s"},{"start_seconds":1310,"text":"different probably it makes sense next time to go through expanded macro I don't want to go on this call but","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1310s"},{"start_seconds":1319,"text":"we can do basically um run macro expansion on the compiler to","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1319s"},{"start_seconds":1327,"text":"see what's the raw code is generated for every method and it will be easier to see what's happening Bes","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1327s"},{"start_seconds":1335,"text":"behind the scenes uh before we actually get to execution of the body the","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1335s"},{"start_seconds":1345,"text":"meod um okay and finally return arguments so","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1345s"},{"start_seconds":1352,"text":"this view call returns an option of WRA time St so um the return","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1352s"},{"start_seconds":1363,"text":"types uh after near B and contract micro expansion","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1363s"},{"start_seconds":1369,"text":"eljon so this type is a specific um custom type which","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1369s"},{"start_seconds":1379,"text":"is u64 so this is coming from near SDK Json types","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1379s"},{"start_seconds":1386,"text":"and it is a structure that's probably too too much rust it's a structure that","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1386s"},{"start_seconds":1396,"text":"has a row u64 type but","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1396s"},{"start_seconds":1403,"text":"when Json distalization is called on it or realization it turns it into a string","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1403s"},{"start_seconds":1412,"text":"the reason for this is Json standard doesn't support all u64 types like not","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1412s"},{"start_seconds":1420,"text":"full range it only comes from um I think that the limit is something","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1420s"},{"start_seconds":1429,"text":"like two in the power of 53","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1429s"},{"start_seconds":1436,"text":"because uh JavaScript Al had this limitation on","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1436s"},{"start_seconds":1442,"text":"numbers and Json standard the default Json standard that comes from Front","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1442s"},{"start_seconds":1450,"text":"End will not serialize a large g64","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1450s"},{"start_seconds":1457,"text":"numbers uh into a proper number so instead","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1457s"},{"start_seconds":1465,"text":"we up um 64 integers u64 is an signed 64 bits","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1465s"},{"start_seconds":1475,"text":"integer and U1 128 integer into Strings so that method","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1475s"},{"start_seconds":1483,"text":"will return you um adjacent something like this which","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1483s"},{"start_seconds":1491,"text":"basically a string if um where","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1491s"},{"start_seconds":1499,"text":"the actual number is uh like a thousand or if it doesn't exist the result","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1499s"},{"start_seconds":1505,"text":"doesn't exist then it will return you no","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1505s"},{"start_seconds":1513,"text":"okay so this is um high level of nearest DEC and we can","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1513s"},{"start_seconds":1521,"text":"now go into one of the methods and probably the main method how we get into this","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1521s"},{"start_seconds":1530,"text":"contract is a voting voting call so when you're an active validator so","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1530s"},{"start_seconds":1539,"text":"you have an account with at least one seat in near protocol","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1539s"},{"start_seconds":1547,"text":"so you have a pretty large state to hold um","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1547s"},{"start_seconds":1554,"text":"to to have stake to to be active validator then you","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1554s"},{"start_seconds":1562,"text":"can before phase two you can call this method in order to cast your vote so if","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1562s"},{"start_seconds":1571,"text":"is vote is true it means you want to vote for phase two if you said his vote is false it","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1571s"},{"start_seconds":1580,"text":"means you're withdrawing your vote so let's say you first casted your vote and then you want to cancel it for","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1580s"},{"start_seconds":1589,"text":"for some reason so for example you feel uh you didn't know something and you want to retreat","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1589s"},{"start_seconds":1596,"text":"it so let's see how it works so this is the change method and","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1596s"},{"start_seconds":1604,"text":"it automatically gets an argument deserialized by near Bion even so as we","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1604s"},{"start_seconds":1613,"text":"discussed before it was expecting Jason internally just a Boolean where variable that you can use normally in","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1613s"},{"start_seconds":1622,"text":"Rust and the method is like this so it's nothing about how","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1622s"},{"start_seconds":1630,"text":"the state has changed or actions you did so you don't need to manually save","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1630s"},{"start_seconds":1639,"text":"the state it will be automatically saved if the method completes so first thing it does it's","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1639s"},{"start_seconds":1648,"text":"called pink pink we will go back go to this method later but it does um update","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1648s"},{"start_seconds":1655,"text":"the internal state by checking if the epoch has changed and who's the active validator to update the","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1655s"},{"start_seconds":1662,"text":"data then it checks if we already reach a result in our case I guess we need to","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1662s"},{"start_seconds":1669,"text":"first um no it's s so result is is a field that's saying we already","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1669s"},{"start_seconds":1679,"text":"reached two3 of the validator then it's just a time stamp at which","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1679s"},{"start_seconds":1688,"text":"um phase two started and this is a time stamp in NCS","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1688s"},{"start_seconds":1695,"text":"because um this is how we expose time stamp to within a context option","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1695s"},{"start_seconds":1704,"text":"means it either exist or doesn't exist so if it exist then it's given time stamp so it means voting already","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1704s"},{"start_seconds":1712,"text":"happened and we reach the phase two at a given time stamp in NCS if it none so it","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1712s"},{"start_seconds":1720,"text":"doesn't exist it means uh the voting was not reached so we check that if we already","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1720s"},{"start_seconds":1730,"text":"reached a vote if you reach phase two then we don't need to do anything your vote doesn't matter because it's already","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1730s"},{"start_seconds":1737,"text":"phase two and T temp already there um then and for yeah for like rust","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1737s"},{"start_seconds":1745,"text":"folks or or people who are not familiar with rust this whole option some is some none thing is not like a near SDK RS","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1745s"},{"start_seconds":1753,"text":"thing that's just like a straight up rust thing right yes so it's similar how JavaScript has three types of Boolean or","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1753s"},{"start_seconds":1762,"text":"maybe more but it can be true false and null so null is the variable doesn't","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1762s"},{"start_seconds":1770,"text":"exist um so similar a lot of languages have null but it's worth saying that rest does not have null instead it has","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1770s"},{"start_seconds":1779,"text":"option types which can return some value or none and you have to explicitly deal with it or it yells at you so but when","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1779s"},{"start_seconds":1788,"text":"it gets encoded for the outside world it is getting like turn converted to Json for example it is getting stor sent as","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1788s"},{"start_seconds":1797,"text":"null I MH cool so next is the query the context","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1797s"},{"start_seconds":1808,"text":"of new SDK and we check who called us so","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1808s"},{"start_seconds":1816,"text":"there's three types of accounts that available for from Context uh one of them is","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1816s"},{"start_seconds":1824,"text":"predecessor this is the account ID of immediate predecessor of who called","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1824s"},{"start_seconds":1833,"text":"vote so um near is a synchronous environment","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1833s"},{"start_seconds":1840,"text":"and you can have a chain of cross contract calls so let's say Alice calls","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1840s"},{"start_seconds":1846,"text":"Bob contract Bob's calls um chl's contract","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1846s"},{"start_seconds":1854,"text":"and in this case predecessor going to be Bob if WEA Charles","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1854s"},{"start_seconds":1863,"text":"then predecessor will be immediate account who called us in this case in this example it was","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1863s"},{"start_seconds":1870,"text":"Bob um in contrast there also sign their account ID this is the","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1870s"},{"start_seconds":1879,"text":"account who signed the original transaction uh in my example let's say it was Alice who called contra","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1879s"},{"start_seconds":1888,"text":"Bob and then Bob culture so signer account ID will be Alice","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1888s"},{"start_seconds":1896,"text":"and current account ID is the account of the contract so in this case it's going to be","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1896s"},{"start_seconds":1903,"text":"Charles um the account of that holds the contract so on near the contract always","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1903s"},{"start_seconds":1911,"text":"belongs to some account on which it's for state and has balance and has um","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1911s"},{"start_seconds":1918,"text":"code deployed for security reasons it's very important to always rely not on the","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1918s"},{"start_seconds":1927,"text":"signer of the transaction but on the immediate predecessor because let's say Bob is a malicious contract and if he","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1927s"},{"start_seconds":1935,"text":"would rely on the signer that Bob can call some token transfer contract and","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1935s"},{"start_seconds":1942,"text":"withdraw a balance from Alice to themsel or their owner instead","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1942s"},{"start_seconds":1949,"text":"of uh act on by acting on Alice behalf so if would if Bob would try to do this","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1949s"},{"start_seconds":1958,"text":"then the token contract will get the predecessor who is Bob so uh contract","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1958s"},{"start_seconds":1964,"text":"Bob can only um spend balance of their own so it has some limitations but uh","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1964s"},{"start_seconds":1973,"text":"this guarantees you that if you call in some contract this contract cannot really spend","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1973s"},{"start_seconds":1979,"text":"your um your cannot really act on your behalf","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1979s"},{"start_seconds":1988,"text":"except on itself make","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1988s"},{"start_seconds":1996,"text":"sense yeah awesome cool um so next thing we do","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=1996s"},{"start_seconds":2004,"text":"is check the argument that was passed so if it is dev vot then we um it means we","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2004s"},{"start_seconds":2013,"text":"want to add this our state from the caller","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2013s"},{"start_seconds":2022,"text":"to set of active validators who voted for phase","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2022s"},{"start_seconds":2027,"text":"two so otherwise uh the value is zero it means we want","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2027s"},{"start_seconds":2034,"text":"to set our stake to zero so we call a special method on the","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2034s"},{"start_seconds":2043,"text":"environment which is um calls a host function on near","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2043s"},{"start_seconds":2050,"text":"protocol runtime to get the active stake from the current","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2050s"},{"start_seconds":2057,"text":"Epoch of a given account ID it will","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2057s"},{"start_seconds":2063,"text":"return um for a given validator how much stake they","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2063s"},{"start_seconds":2070,"text":"have and this is will be done immediately so this is a synchronous","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2070s"},{"start_seconds":2077,"text":"call we verify that your stake is zero or above zero to make sure that uh we","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2077s"},{"start_seconds":2085,"text":"don't pollute State this nonactive validator so this way we","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2085s"},{"start_seconds":2093,"text":"can limit how many validators are can get into our","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2093s"},{"start_seconds":2102,"text":"state so because we only have 100 SS when this contract was","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2102s"},{"start_seconds":2109,"text":"used um it means you can limit the number of validators in the state of","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2109s"},{"start_seconds":2117,"text":"this contract to 100 so basically um if a AER triggers","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2117s"},{"start_seconds":2128,"text":"terms of stake is zero then we throw a panic and everything we did before this","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2128s"},{"start_seconds":2136,"text":"all changes to the state will be rolled back to the beginning of the function","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2136s"},{"start_seconds":2143,"text":"call only for this country so in this case again coming to example of Alice Bob","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2143s"},{"start_seconds":2151,"text":"Charles um only state of Charles going to be reverted but but state of BU is","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2151s"},{"start_seconds":2159,"text":"not because of a synchronous environment next","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2159s"},{"start_seconds":2167,"text":"um uh we check if we already voted before if we did then we just remove so","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2167s"},{"start_seconds":2176,"text":"votes is a hashmap um hashmap is in memory","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2176s"},{"start_seconds":2183,"text":"map it means um every time it's serialized or deserialized all","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2183s"},{"start_seconds":2192,"text":"values has to be serialized into one pie and then deize","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2192s"},{"start_seconds":2202,"text":"back um why did you I think the uh hashmap is","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2202s"},{"start_seconds":2209,"text":"interesting I was just recently talking about this with Mike actually um and maybe Josh was on that call too but in general we","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2209s"},{"start_seconds":2218,"text":"give the uh advice to people that like most of the time you want to avoid hashmap and use a collection from near","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2218s"},{"start_seconds":2225,"text":"SDK instead yes um and I'm curious why um what makes this a better case for","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2225s"},{"start_seconds":2234,"text":"hashmap good point um so hashmap as I said whenever we call a method on this","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2234s"},{"start_seconds":2243,"text":"um contract it will distalize all fields of the hashmap so all 100","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2243s"},{"start_seconds":2252,"text":"potential accounts will be distalized at once","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2252s"},{"start_seconds":2258,"text":"um but it will do this into a single storage read instead of 100 storage","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2258s"},{"start_seconds":2266,"text":"reads so this is um that even so the overall size of","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2266s"},{"start_seconds":2274,"text":"this is large um because each account can be up to 64 characters and balance","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2274s"},{"start_seconds":2282,"text":"is um 16 bytes the overall size of this is not as","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2282s"},{"start_seconds":2290,"text":"large so it's 100 * 80 so it's like 8 kilobytes","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2290s"},{"start_seconds":2298,"text":"um but we need it in pink in pink we have to iterate","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2298s"},{"start_seconds":2308,"text":"on every single vote in order to update the stake of the","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2308s"},{"start_seconds":2318,"text":"validator and if we would have to iterate on every single key from a","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2318s"},{"start_seconds":2325,"text":"persistent collection for example an ordered map then it will we will have to","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2325s"},{"start_seconds":2333,"text":"do something in order of 100 reads and in this case we we actually need to","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2333s"},{"start_seconds":2340,"text":"overwrite them which means we need a 100 wres and 100 reads is much more","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2340s"},{"start_seconds":2346,"text":"expensive than one time bulk rate of 100 accounts because we know that the that","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2346s"},{"start_seconds":2355,"text":"structure is limited to 100 we don't need to introduce pagination that you would might need to do with a persistent","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2355s"},{"start_seconds":2363,"text":"map if you would want to trate and this is because the try was stored in oh the","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2363s"},{"start_seconds":2372,"text":"the the storage is uh kept as a try and this is makes it expensive to to do a","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2372s"},{"start_seconds":2381,"text":"lot of reads so in this case it was beneficial to have one map that you will","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2381s"},{"start_seconds":2390,"text":"be able to iterate so the guideline here would be if you need to iterate on every single key you probably want to","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2390s"},{"start_seconds":2399,"text":"have um a hashmap or a vector but it's also that you also know that it's bounded that there won't be more than","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2399s"},{"start_seconds":2407,"text":"aund of these right otherwise yeah in this case that's specifically because there's not going","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2407s"},{"start_seconds":2415,"text":"to be more than a 100 validators for now yes is that correct correct and if near were fully sharded then like this will","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2415s"},{"start_seconds":2423,"text":"need to be modified potentially with a potentially persistent map one of persistent map or maybe you","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2423s"},{"start_seconds":2432,"text":"can do something like a shed map uh we don't have it right now not or like bulk map so it will be a persistent map of","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2432s"},{"start_seconds":2441,"text":"like bulk objects because you still need to iterate on all of them","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2441s"},{"start_seconds":2448,"text":"um maybe you can like split your accounts into 20 buckets and then every","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2448s"},{"start_seconds":2455,"text":"bucket will be a vector but you will store them separately into persistent map so when you need to","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2455s"},{"start_seconds":2462,"text":"iterate you only need to read 20 keys or you introduce some pation for pink am I correct in saying I mean that","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2462s"},{"start_seconds":2471,"text":"that was a really good explanation uh thanks uh am I correct in saying that like this is kind of Min maxing in the","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2471s"},{"start_seconds":2478,"text":"sense that even if you used a near persistent uh collection instead of a hashmap it's not like it would be crazy","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2478s"},{"start_seconds":2486,"text":"expens expensive right um but this is just it's just it's just like better but like yeah I guess that's my","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2486s"},{"start_seconds":2494,"text":"question right so so currently our storage is probably one of the most expensive operation that you will","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2494s"},{"start_seconds":2502,"text":"usually face by default um and if you","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2502s"},{"start_seconds":2509,"text":"do like overwriting of them then potentially it will be more expensive than a hash map and that's why we picked a hash map right now","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2509s"},{"start_seconds":2518,"text":"but it's not like dramatically worse but to be safe we we can pretty","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2518s"},{"start_seconds":2526,"text":"much estimate that 100 FTS into our current gas limits and it makes it a good choice for this particular contract","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2526s"},{"start_seconds":2535,"text":"cool so I think we we have like 10 minutes that we want to spend more on this and let's try to finish some of","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2535s"},{"start_seconds":2544,"text":"this so what happens here is um you basically remove the old","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2544s"},{"start_seconds":2552,"text":"Stak uh this is just a safety check and it's an invariant as it's saying here","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2552s"},{"start_seconds":2560,"text":"which means it's unlikely to trigger in real in environment so it verifies that the current","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2560s"},{"start_seconds":2569,"text":"stake is less than a total stake then we subtract the old stake and introduce","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2569s"},{"start_seconds":2577,"text":"a new stake well I would argue that order of this would be better to be first","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2577s"},{"start_seconds":2584,"text":"substracting the old stake and then introducing a new stake in order to avoid overflows even","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2584s"},{"start_seconds":2592,"text":"so is our limit on total Supply it's not possible to overflow but you always want to be","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2592s"},{"start_seconds":2600,"text":"careful and finally if this was an active vertice it was not a withdrawal","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2600s"},{"start_seconds":2606,"text":"then we introduce this into um into","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2606s"},{"start_seconds":2614,"text":"the storage and at the end we will check the result again so let's go to check result first","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2614s"},{"start_seconds":2623,"text":"so we verify so when check result is called we check that the result is not","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2623s"},{"start_seconds":2631,"text":"some that's another way of checking it and it also in","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2631s"},{"start_seconds":2638,"text":"variant and we see what's the total validator State this another method that is return from","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2638s"},{"start_seconds":2647,"text":"Context and let's say it's saying we have 100 million total State and then internally","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2647s"},{"start_seconds":2654,"text":"this is active stake means all validators who voted on","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2654s"},{"start_seconds":2660,"text":"this and we saying if active stake is more than 23","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2660s"},{"start_seconds":2668,"text":"rounded down of total stake then we reach the result and that's pretty much","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2668s"},{"start_seconds":2675,"text":"over this contract so voting has","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2675s"},{"start_seconds":2682,"text":"completed so this is fairly simple method and we probably want to go to","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2682s"},{"start_seconds":2691,"text":"Pink So pink um this is happens here","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2691s"},{"start_seconds":2698,"text":"on the boundary of epo so after an EPO switches it means the new set of validators became active within the","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2698s"},{"start_seconds":2707,"text":"epoch on your protocol um validators do not","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2707s"},{"start_seconds":2713,"text":"change so even if someone kicked out they will be only inactive on the next ebook so what we do we verify that we","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2713s"},{"start_seconds":2723,"text":"didn't reach the result um","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2723s"},{"start_seconds":2730,"text":"then we take the current Epoch heite it's just an integer that always in","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2730s"},{"start_seconds":2737,"text":"cursing and we verify that it doesn't match the previous Epoch","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2737s"},{"start_seconds":2743,"text":"heite so we started with okite zero","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2743s"},{"start_seconds":2749,"text":"um but every time a pin disc called it is um","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2749s"},{"start_seconds":2758,"text":"it can update the eoch so if Epoch has switched so it can pass one eoch or five EP that matter what we're going to do","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2758s"},{"start_seconds":2767,"text":"we're going to go through all previous active votes who voted for phase two we","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2767s"},{"start_seconds":2774,"text":"reset the total stake to zero and this is the rust way","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2774s"},{"start_seconds":2781,"text":"of um taking all the data out of fresh map and and setting it to","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2781s"},{"start_seconds":2790,"text":"default so basically we got a copy of the map from internal structure and now internal structure","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2790s"},{"start_seconds":2798,"text":"contains an empty hashmap it's a new think that it was introduced in like recent","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2798s"},{"start_seconds":2806,"text":"trust so that that's the M false so that all like the same size of","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2806s"},{"start_seconds":2815,"text":"votes but all of them are set of false no it it basically just empty hashmap so it it calls hashmap default set it to","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2815s"},{"start_seconds":2823,"text":"this variable so let's go here basically callose replace replace do","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2823s"},{"start_seconds":2830,"text":"swap but a second argument to to the replace is a default of hashmap which is","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2830s"},{"start_seconds":2837,"text":"empty map so it gave us a data from our structure and now this contract","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2837s"},{"start_seconds":2846,"text":"structure contains an empty map and this is just so we can do in one","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2846s"},{"start_seconds":2853,"text":"go modify the current map or the new map based on the data from the old map so we basically just need to canel this we","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2853s"},{"start_seconds":2862,"text":"don't need to know what was the previous St so we check if someone was uh removed from the","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2862s"},{"start_seconds":2871,"text":"map like removed from the active validators so we get their stake if","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2871s"},{"start_seconds":2876,"text":"there stake uh is positive we add them to the total","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2876s"},{"start_seconds":2883,"text":"stake and we keep their vote but if they were","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2883s"},{"start_seconds":2890,"text":"kicked out they have to vote again um I don't think this happened during the phase one no one was kicked","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2890s"},{"start_seconds":2897,"text":"out really from the validating boards maybe someone was I'm not familiar actually I think yeah one","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2897s"},{"start_seconds":2907,"text":"validator was kicked out and","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2907s"},{"start_seconds":2916,"text":"um and basically we update total state by Rec Computing","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2916s"},{"start_seconds":2923,"text":"what's the new Total stake and then we store all the results back to our contract","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2923s"},{"start_seconds":2931,"text":"State and at the end we do two things we check the result so after Epoch has","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2931s"},{"start_seconds":2938,"text":"switched if validators acquired more stake or the total stake","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2938s"},{"start_seconds":2943,"text":"decreased then the result might happen already and we remember at which Epoch","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2943s"},{"start_seconds":2953,"text":"we did this so next time someone called pink or vot we don't need to go through this if Epoch hasn't switched so that's","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2953s"},{"start_seconds":2961,"text":"pretty much the voting contract basically every time some calls a vote","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2961s"},{"start_seconds":2967,"text":"we first update data from the previous validators in and write it into internal","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2967s"},{"start_seconds":2975,"text":"structure most important we get the total voted stake updated and then we call Checkers out","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2975s"},{"start_seconds":2983,"text":"twice occasionally within the one call and what's important is even so we we can","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2983s"},{"start_seconds":2991,"text":"call pink here from one method but also we can call pink directly by a transaction so you don't need to","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2991s"},{"start_seconds":2999,"text":"revote you can just call pink so it's pretty much a","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=2999s"},{"start_seconds":3007,"text":"contract and the way rust works so there's a bunch of uh view calls so one of them just Returns the result and","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=3007s"},{"start_seconds":3015,"text":"another one of them is um returns how much Stakes It Go and the","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=3015s"},{"start_seconds":3022,"text":"final is a functional way of returning all the","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=3022s"},{"start_seconds":3031,"text":"votes it basically iterates on the map and then wraps it into a specific type of","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=3031s"},{"start_seconds":3039,"text":"the balance using into so we almost out of time but I want","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=3039s"},{"start_seconds":3046,"text":"to point out that there is also a unit test in the same code and it's just a rust way of doing this and unit test can","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=3046s"},{"start_seconds":3055,"text":"validate some logic within a CL by faking the context and stuff like","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=3055s"},{"start_seconds":3062,"text":"this okay any questions to follow up before we close this is","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=3062s"},{"start_seconds":3070,"text":"great all right so let's go back to the call okay um thank you very much thanks","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=3070s"},{"start_seconds":3080,"text":"everyone um all right let me stop C","url":"https://www.youtube.com/watch?v=BA7VeUS_RAA&t=3080s"}],"youtube_url":"https://youtube.com/watch?v=BA7VeUS_RAA","url":"https://near-pedia.xyz/videos/BA7VeUS_RAA"}