near-in-minutes / community

Contribute and collaborate on educational content for the "NEAR in Minutes" Community.
3 stars 0 forks source link

[NEW CONTENT] NEAR CLI -- `near state` #3

Open amgando opened 3 years ago

amgando commented 3 years ago

Submission

I've created this video covering NEAR CLI near state command requested by https://github.com/near-in-minutes/community/issues/1

Video

Related Links

Community Votes ❤️ I love it! 🚀 I can help you!

amgando commented 3 years ago

Transcript

Hi. Um, I would like to talk about near CLI, uh, specifically the command near state. Uh, so I can use here, uh, the command, which near, uh, on any, uh, Unix compatible machine to tell me that, um, this is where near, uh, uh, binary is located.

I'm using NVM and that's why this path is a little bit longer maybe than yours. And also I can check the version of near using this command.

I'm using version two, I've just made that update. Now it's May 17th, 2021, and now I'm going to call out near state.

And then my account, um, by, by default near CLI uses, Testnet when I hit enter on this command, what I get back is this blob of Jason data that tells me something about the number of tokens that my account has, uh, both in Yocto near, which is 10 to the 24th and a format and amount, which shows here that I am flushed with fake, uh, near tokens on Testnet, uh, 21,701 tokens, and then some change.

Um, also I can tell, uh, from this response that I have a contract deployed, uh, to, uh, Shareef dot Testnet, but I don't know which contract actually, um, I would need to discover that, um, uh, you know, through some other means by, you know, uh, checking transaction history on this account to see, uh, you know, what, uh, contract was deployed there.

I, I might be able to pull down the byte code for this and just assemble it. Uh, and then, uh, look at the methods on that contract. Uh, but as of today, uh, near doesn't support kind of discovering this contract, you'd need to know what this contract is, but, uh, it is clear that there is a contract there it's because it's not all once.

Let me show you what it looks like. Uh, when we have an account that has no, uh, contract on it, we'll see the code hash is all ones. That's true for every account that has no contract on it. So near state is telling me how much money, uh, how many tokens this account has both in a convenient, uh, and, uh, in format, whether or not there's a contract deployed something about whether or not these tokens are locked.

This would make sense if I were validating. Um, and then, uh, the storage used and the storage uses made up of several pieces in the data structure. We can talk about what that looks like in a minute, uh, storage paid at, uh, and then some sanity check information here.

Because again, when we request this information from the network, there are a number of computers, a number of nodes that could respond. Uh, and so we want to make sure that the data that we're getting back is current data, and that's why the blockade is there.

And then the block hash is to verify that it hasn't been tampered with. So I can use both of these pieces of information to verify that this is the most current piece of information and your blocks ticket about one per second. And so, uh, that's how that will work. I can also prefix this command with an EMV, so near ENV main net, and then, uh, request near state of, uh, my account. And, uh, that will show, uh, I have no contract, but now we're on main net. Uh, I have no contract deployed.

I have a couple of hundred near tokens there. Uh, and, um, uh, and, uh, you know, a little bit of storage that's being used.

So that's the near state command. Uh, we can learn more about, uh, the, uh, near CLI in general, just by searching Google.

We'll find here a link to the source code, uh, and in the docs. Uh, we can just dive right in, uh, from the docs here at the top level, I see near CLI documentation, and I've already opened up this documentation to the near state command.

So here you can see it basically takes an account ID. Uh, you use it like this. This is what the example response looks like.

It's quite a simple command, but it's very useful to check that an account exists. If we try calling the near state on some arbitrary account.

Well, it just so happens that that account exists, but on some account that doesn't exist, I'll get some command. Like this account is not found on whatever the network is and, uh, I can switch that to some other account, I'll see the same thing.

So in that sense, near state can give us some useful information about the existence of an account, and then some of the metadata on the account, um, in terms of, uh, how this command actually works.

If we dive into the newer source code, um, here, there is this state command. That is one of the options, uh, that allows us to view account state.

And what this is calling is that view account, um, uh, function that's exported, uh, from, uh, the, uh, collection of commands and, and otherwise, uh, this is just sort of a standard format for a command here.

There's not, there are no options, uh, like, uh, you know, with some of these other commands, you might see an option block or something like that.

Here. It's quite a simple command actually. And we can see the implementation of that command matches up. One-to-one with some of the metadata here, we're basically connecting to then your network.

Uh, we await for the near account, uh, to be, um, hydrated by this connection. Uh, and then we call the state command on the account object, uh, and this is straight out of near CLI.

Um, and, and your API JS is, is what has this method exposed? If we find a state, then let's go ahead and add a little bit of a formatted amount if there's a state of out there.

And then we just log out this information account and that account ID, uh, and then inspecting the response. We just format that so you can see that's account and then the account ID just like that.

So that that's literally this command implemented in near CLI right here. Uh, there's also a little bit more information in the con.

Uh, here, you can see all the data that makes up the storage used by, uh, an account, uh, the balance, the lock balance for staking in this case, zero, the code of the contract, this particular account, uh, has no contract deployed, but if it's not all ones, then there is a contract deployed and the code hash is like a fingerprint of that contract.

Um, and then of course the key value storage, although we don't see it here, there is storage that is used by, especially if you have a contract on the account storage that's used by the, uh, contract, any number of keys, uh, and then cross contract calls that are either his action receipts or resolved, uh, as data receipts.

Uh, and so a little bit more description here in the non-con. And we will include these links, uh, in the, uh, uh, in the video, uh, notes. So that's it, uh, I hope that helped.