25
loading...
This website collects cookies to deliver better user experience
npm install -g npx
npx
binary create-near-app. It has some options to choose what type of frontend you are going to use and also what type of smart contract you are going to use. Here are the option you can use:➜ ~ npx create-near-app -h
create-near-app <projectDir>
Create a new NEAR project
Options:
--version Show version number [boolean]
--frontend template to use
[choices: "vanilla", "react", "vue", "angular"] [default: "vanilla"]
--contract language for smart contract
[choices: "assemblyscript", "rust"] [default: "assemblyscript"]
--help Show help [boolean]
Examples:
create-near-app new-app Create a project called "new-app"
react
as a frontend and assemblyscript
as a smart contract.npx create-near-app near-tutorial --frontend=react --contract=assemblyscript
near login
bash
cd near-tutorial
code .
src
- React source codecontract
- Smart contract source codepackage.json
- Project dependencies and running scriptsReadme.md
- Project documentation and development tipsneardev
- Configuration for smart contract developmentyarn
command:
bash
yarn
bash
yarn dev
dev-
and have some random numbers, in our case its dev-1638480997606-36755974489881
. You can also see the link to the smart contract transaction deployment: https://explorer.testnet.near.org/transactions/7N4BWLVyuo9gXp9sGe8WKXBZEX7iJUq5AvZXAGqoRij1contract/assembly/index.ts
:
javascript
const DEFAULT_MESSAGE = "Hello";
getGreeting(accountId: string)
and setGreeting(message: string)
javascript
export function setGreeting(message: string): void {
const accountId = Context.sender;
// Use logging.log to record logs permanently to the blockchain!
logging.log(
Saving greeting "${message}" for account "${accountId}");
storage.set(accountId, message);
}
message
which was send when we approved the transaction. Inside the method we are extracting a the sender accountId from the Context
class:
javascript
const accountId = Context.sender;
near-sdk-as
and it has some useful data you may need in your during the development:storage
and its method storage.set
:
javascript
storage.set(accountId, message);
near cli
.CONTRACT_NAME
env variable, and to do so we can call neardev/dev-account.env
which has our contract name inside:
bash
source neardev/dev-account.env
echo $CONTRACT_NAME
ID
env variable:
bash
export ID=your-account.testnet
echo $ID
near-cli
you can pass a json string afther the contract name.near-cli
:
bash
near call $CONTRACT_NAME setGreeting '{"message": "Near CLI Greeting"}' --accountId $ID
getGreeting
method is a readonly method, which mean we cannot use the context.sender
to get the account id, its only accessible in mutating state calls:
javascript
export function getGreeting(accountId: string): string | null {
// This uses raw
storage.get, a low-level way to interact with on-chain
// storage for simple contracts.
// If you have something more complex, check out persistent collections:
// https://docs.near.org/docs/concepts/data-storage#assemblyscript-collection-types
return storage.get < string > (accountId, DEFAULT_MESSAGE);
}
storage
to get the greeting from the smart contract storage or the default method, if there is no message in the storage for the account we use. Readonly methods are using view
instead of call
we used for setGreeting
:
bash
near view $CONTRACT_NAME getGreeting "{\"accountId\": \"$ID\"}"
two
configuration files where we connect to the blockchain: config.js
and utils.js
:config.js
we define our contract name, which is also taken from environment variable :
javascript
const CONTRACT_NAME = process.env.CONTRACT_NAME || "near-tutorial";
getConfig
function with the blockchain configuration for testnet
, mainnet
and some other environments:
`javascript
utils.js
where we use the config from config.js
, wand the main is initContract()
method, where we connect to the blockchain rpc
and list all the available methods in our contract:
`javascript
window
object with the methods we will be using to interact with the blockchain and our smart contract. And here we also list viewMethods
which we were calling with near view
and changeMethods
which we were calling with near call
. So whenever you add new methods to your contract you have to update this file and list all the methods in the appropriate section, so that you can also use them later in your React Components.src/App.js
you can see how the contract is used:
`javascript
contract/assemble/index.ts
and add replace setGreeting
method with the following:
javascript
export function setGreeting(message: string): void {
const accountId = Context.sender;
const timestamp = Context.blockTimestamp;
// Use logging.log to record logs permanently to the blockchain!
logging.log(
Saving greeting "${message}" with timestamp: ${timestamp} for account "${accountId}"
);
storage.set(accountId, message);
storage.set(
${accountId}_last_updated,
${new Date(timestamp / 1000000).toDateString()} ${new Date(
);
}
javascript
const timestamp = Context.blockTimestamp;
javascript
storage.set(
${accountId}_last_updated,
${new Date(timestamp / 1000000).toDateString()} ${new Date(
);
${accountId}_last_updated
key:
javascript
export function getUpdateDate(accountId: string): string | null {
return (
storage.get < string > (
${accountId}_last_updated, "No custom greeting.")
);
}
src/utils.js
. Go and add new method getUpdateDate
to viewMethods
and save file so it will look like this:src/App.js
and add a new state variable to store our update date:
javascript
const [updateDate, setUpdateDate] = React.useState();
useEffect
hook where we are getting the greeting add one more call to get the getLastUpdate
and when we fetch the value we can update our updateDate
state hook by calling setUpdateDate
. The code we add should look as following:
javascript
window.contract
.getUpdateDate({ accountId: window.accountId })
.then((greetingUpdateDate) => {
setUpdateDate(greetingUpdateDate);
});
h1
tag where you show current greeting and add some other tag for example h3
after to show the last update date.
`javascript
setGreeting
again to save the timestamp in the smart contract storage.yarn dev
again and it will start up.package.json
for all the commands available, but to simply deploy it as is you can use yarn deploy:pages
or to deploy everything including your smart contract you can use command yarn deploy
which will build and deploy both the contract, and also the ui app.homepage
property to the package.json
. More details can be found here