NEAR Blockchain
Video/Text

Creating Accounts and Deploying Contracts

Lesson 6 Chapter 1 Module 2

NEAR has accounts and every account can have either 0 or 1 contracts associated with it.  I think of it like this - if I need to deploy a smart contract, it needs an account and that account name is the name used by the frontend to connect to that smart contract.  Once I've deployed a contract to that account - it's effectively a contract account.  It can receive and transfer tokens in same way that any other account can.

In addition to contract accounts, there are user accounts which are exactly as you'd think they'd be.  These are accounts people use to send and receive tokens or as identities to login to dapps.  As a person, I have several NEAR accounts including guildleader.testnet and vitalpointai.testnet.  In future, on mainnet, it will be possible to have top level names (although will be some kind of auction or cost associated with shorter names) to allow branding - i.e., accountname.vitalpointai or accountname.near, etc...

Accounts can also own other accounts.  So, for instance if my company is vitalpointai and I own vitalpointai.testnet (which I do), I can achieve some branding by deploying contracts to  subaccounts of the master account vitalpointai.testnet (e.g. guilds.vitalpointai.testnet).  These basically act like subdomains.  Only stipulation when creating is that you need some NEAR in the master account to transfer to the sub account on creation.

Development Cycle

One thing that completely messed me up coming from Ethereum development and using Truffle was how to reset state and/or upgrade the contracts I had deployed to NEAR.  In Truffle a command like truffle migrate --reset would push new versions of the contracts up and overwrite whatever was there.  That workflow in NEAR development is done by deleting the current contract account, recreating it, and redeploying the contract.  It's not as hard as it seems.  Let's give it a go.

Assuming you have installed near-cli globally, you should be able to go to your terminal and type near login.  That will pop open a browser where you'll be asked to either authorize an existing account you've created on NEAR or ask you to create a new one.  It's a straight forward process to create an account, so I'm not going to cover it step by step.  Ask if you run into any problems.

After you've authorized/created an account you'll be redirected back to your terminal; however, if you're not for whatever reason, just enter the name of the account and you'll get or see a message saying your account is logged in with such and such public key.

That created a directory .near-credentials under your home directory (in WSL) that contains a default directory and then json files of any accounts you've logged in with.  Those json files contain the account name, public and private keys to that account.  In case it's not obvious, you don't want to share those with anyone.

When you did the login process, you may have noticed that the app requested full access to your account.  There is a concept of full access and function access keys associated with account permissions that we'll cover a bit later - for now just be aware that we have the ability to limit what an app can/can't do with your account.

Now that we're logged in (and because we didn't specify specific parameters or networks, we're logged into the default testnet) - we can create an account and deploy a contract.

But first, one last thing.  In the last lesson, when we ran npm run start, it built the assemblyscript contracts that were in the assemblyscript folder, changing them to WASM code and putting them in a directory called out.  It's actually that compiled WASM contract that we'll be deploying (should be a file in the out directory called main.ts).

Alright, here we go.

Step 1 - create an account.  Replace name-of-desired-account with whatever you want to call your contract and of course replace my account - vitalpointai.testnet with the name of the account you're using or created during the near login process.

near create-account name-of-desired-account.vitalpointai.testnet --masterAccount vitalpointai.testnet

Step 2 - deploy the contract.  Again, replace the contract and account name with your own info.

near deploy --wasm-file out/main.wasm --accountId name-of-contract.vitalpointai.testnet

That's it - now if you visit localhost:1234 (if it's not still running from the last lesson, type npm run start to fire it up) - you should be able to click the login button and login to the app using your NEAR account.

Updating the Contract

Getting your app to connect to a new version of a contract or making some changes in it that result in state changes such as models or storage necessitates that you delete the account, recreate it and then deploy the new contract.  This effectively resets everything.  First, don't forget to rebuild your contract which creates a new WASM main.ts (or whatever you've called it) file.  From there, the steps above are the same, but first you'll need to delete the account before you run the create-account command.  Do that like this:

near delete name-of-contract.vitalpointai.testnet vitalpointai.testnet

Note that the delete command will automatically transfer any tokens in the account being deleted to the account specified (typically it's master account).

Cool, good stuff.  Now we're at the point where we know enough and have things at a point where we can start building out our project on NEAR.

Up Next

Building the fungible token contract.

Pen
>