asdasd

asdasd


Установка Node.js

sudo apt install build-essential checkinstall
sudo apt install libssl-dev
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash

Перезагражаем убунту.

nvm install 12.19.1
nvm use 12.19.1
node -v

Установка докера.

sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
sudo apt update
apt-cache policy docker-ce
sudo apt install docker-ce
sudo passwd paah_vetosh
sudo usermod -aG docker paah_vetosh
su - paah_vetosh

https://learn.figment.io/

Near

1 шаг

mkdir near
cd near
npm init -y
npm install --save near-api-js dotenv js-sha256
nano .env

вставляем

NEAR_NODE_URL=https://near-testnet--rpc.datahub.figment.io/apikey/96eccd5a43cd1180de0744bbe222fbcf
NEAR_NETWORK=testnet
NEAR_ACCOUNT=medvedevhuk.testnet
nano connect.js

// Load environment variables

require("dotenv").config();


// Load NEAR Javascript API components

const near = require("near-api-js");


// Setup default client options

const options = {

 networkId:  process.env.NEAR_NETWORK,

 nodeUrl:   process.env.NEAR_NODE_URL,

 walletUrl:  `https://wallet.${process.env.NEAR_NETWORK}.near.org`,

 helperUrl:  `https://helper.${process.env.NEAR_NETWORK}.near.org`,

 explorerUrl: `https://explorer.${process.env.NEAR_NETWORK}.near.org`,

 keyStore:  {} // we will configure this later

}


async function main() {

 // Configure the client with options

 const client = await near.connect(options);

 const provider = client.connection.provider;

 console.log("Client config:", client.config);


 // Get current node status

 const status = await provider.status();

 console.log("Status:", status);

}


main();


node connect.js

2 шаг

nano create_account.js


// Load environment variables

require("dotenv").config();


// Load Near Javascript API components

const near = require("near-api-js");

const fs = require("fs");


// Configure the directory where NEAR credentials are going to be stored

const credentialsPath = "./credentials";


// Configure the keyStore to be used with the NEAR Javascript API

const UnencryptedFileSystemKeyStore = near.keyStores.UnencryptedFileSystemKeyStore;

const keyStore = new UnencryptedFileSystemKeyStore(credentialsPath);


// Setup default client options

const options = {

 networkId:  process.env.NEAR_NETWORK,

 nodeUrl:   process.env.NEAR_NODE_URL,

 walletUrl:  `https://wallet.${process.env.NEAR_NETWORK}.near.org`,

 helperUrl:  `https://helper.${process.env.NEAR_NETWORK}.near.org`,

 explorerUrl: `https://explorer.${process.env.NEAR_NETWORK}.near.org`,

 accountId:  process.env.NEAR_ACCOUNT, 

 keyStore:  keyStore

}


async function main() {

 let keyPair;


 // Configure the client with options and our local key store

 const client = await near.connect(options);


 // Configure the key pair file location

 const keyRootPath = client.connection.signer.keyStore.keyDir;

 const keyFilePath = `${keyRootPath}/${options.networkId}/${options.accountId}.json`;


 // Check if the key pair exists, and create a new one if it does not

 if (!fs.existsSync(keyFilePath)) {

  console.log("Generating a new key pair")

  keyPair = near.KeyPair.fromRandom("ed25519");

 } else {

  let content = JSON.parse(fs.readFileSync(keyFilePath).toString());

  keyPair = near.KeyPair.fromString(content.private_key);


  console.log(`Key pair for account ${options.accountId} already exists, skipping creation`);

 }


 // Create a key pair in credentials directory

 await client.connection.signer.keyStore.setKey(options.networkId, options.accountId, keyPair);


 // Determine if account already exists

 try {

  await client.account(options.accountId);

  return console.log(`Sorry, account '${options.accountId}' already exists.`);

 }

 catch (e) {

  if (!e.message.includes("does not exist while viewing")) {

   throw e;

  }

 }


 // Generate a public key for account creation step

 const publicKey = keyPair.getPublicKey()


 // Create the account

 try {

  const response = await client.createAccount(options.accountId, publicKey);

  console.log(`Account ${response.accountId} for network "${options.networkId}" was created.`);

  console.log("----------------------------------------------------------------");

  console.log("OPEN LINK BELOW to see account in NEAR Explorer!");

  console.log(`${options.explorerUrl}/accounts/${response.accountId}`);

  console.log("----------------------------------------------------------------");

 }

 catch(error) {

  console.log("ERROR:", error);

 }

}


main();


nano package.json
npm install
node create_account.js

3 шаг

nano query.js


// Load environment variables

require("dotenv").config();


// Load NEAR Javascript API components

const near = require("near-api-js");


// Setup default client options

const options = {

 networkId:  process.env.NEAR_NETWORK,

 nodeUrl:   process.env.NEAR_NODE_URL,

 walletUrl:  `https://wallet.${process.env.NEAR_NETWORK}.near.org`,

 helperUrl:  `https://helper.${process.env.NEAR_NETWORK}.near.org`,

 explorerUrl: `https://explorer.${process.env.NEAR_NETWORK}.near.org`,

 accountId:  process.env.NEAR_ACCOUNT, 

 keyStore:  {}

}


async function main() {

 // Configure the client with options and our local key store

 const client = await near.connect(options);

 const provider = client.connection.provider;


 // Make queries to the blockchain here:

 const status = await provider.status();

 console.log("network status:", status);

 // Get the latest block

 let block = await provider.block({ finality: "final" });

 console.log("current block:", block);


 // Get the block by number

 block = await provider.block({ blockId: status.sync_info.latest_block_height });

 console.log("block by height:", block);

 const validators = await provider.validators(block.header.height);

 console.log("network validators:", validators);

 const account = await client.account(options.accountId);

 console.log("account state:", await account.state());

 const gasPrice = await provider.sendJsonRpc("gas_price", [null]);

 console.log("gas price:", gasPrice);


 // Get current gas price from the block header

 const anotherBlock = await provider.sendJsonRpc("block", { finality: "final" });

 console.log("gas price from header:", anotherBlock.header.gas_price);


}


main()


node query.js

4 шаг

nano transfer.js

// Load environment variables

require("dotenv").config();


// Load NEAR components

const near = require("near-api-js");


// Formatter helper for Near amounts

function formatAmount(amount) {

 return BigInt(near.utils.format.parseNearAmount(amount.toString()));

};


// Directory where Near credentials are going to be stored

const credentialsPath = "./credentials";


// Configure the keyStore to be used with the SDK

const UnencryptedFileSystemKeyStore = near.keyStores.UnencryptedFileSystemKeyStore;

const keyStore = new UnencryptedFileSystemKeyStore(credentialsPath)


// Setup default client options

const options = {

 networkId:  process.env.NEAR_NETWORK,

 nodeUrl:   process.env.NEAR_NODE_URL,

 walletUrl:  `https://wallet.${process.env.NEAR_NETWORK}.near.org`,

 helperUrl:  `https://helper.${process.env.NEAR_NETWORK}.near.org`,

 explorerUrl: `https://explorer.${process.env.NEAR_NETWORK}.near.org`,

 accountId:  process.env.NEAR_ACCOUNT,

 deps: {

  keyStore: keyStore

 }

}


// Configure transaction details

const txSender = options.accountId;

const txReceiver = "pizza.testnet";

const txAmount = formatAmount(1);


async function main() {

 // Configure the client with options and our local key store

 const client = await near.connect(options);

 const account = await client.account(txSender);

 const provider = client.connection.provider;


 // Create a simple money transfer using helper method

 console.log(`Sending money to ${txReceiver}`);


 try {

  const result = await account.sendMoney(txReceiver, txAmount);


  console.log("Creation result:", result.transaction);

  console.log("----------------------------------------------------------------");

  console.log("OPEN LINK BELOW to see transaction in NEAR Explorer!");

  console.log(`${options.explorerUrl}/transactions/${result.transaction.hash}`);

  console.log("----------------------------------------------------------------");


  setTimeout(async function() {

   console.log("Checking transaction status:", result.transaction.hash);


   const status = await provider.sendJsonRpc("tx", [result.transaction.hash, options.accountId]);

   console.log("Transaction status:", status);

  }, 5000);

 }

 catch(error) {

  console.log("ERROR:", error);

 }

};


main()


node transfer.js
nano transfer_advanced.js

// Load environment variables

require("dotenv").config();


// Load NEAR components

const near = require("near-api-js");

const { sha256 } = require("js-sha256");

const fs = require("fs");


// Formatter helper for Near amounts

function formatAmount(amount) {

 return BigInt(near.utils.format.parseNearAmount(amount.toString()));

};


// Directory where Near credentials are going to be stored

const credentialsPath = "./credentials";


// Configure the keyStore to be used with the SDK

const UnencryptedFileSystemKeyStore = near.keyStores.UnencryptedFileSystemKeyStore;

const keyStore = new UnencryptedFileSystemKeyStore(credentialsPath)


// Setup default client options

const options = {

 networkId:  process.env.NEAR_NETWORK,

 nodeUrl:   process.env.NEAR_NODE_URL,

 walletUrl:  `https://wallet.${process.env.NEAR_NETWORK}.near.org`,

 helperUrl:  `https://helper.${process.env.NEAR_NETWORK}.near.org`,

 explorerUrl: `https://explorer.${process.env.NEAR_NETWORK}.near.org`,

 accountId:  process.env.NEAR_ACCOUNT,

 deps: {

  keyStore: keyStore

 }

}


// Configure transaction details

const txSender = options.accountId;

const txReceiver = "pizza.testnet";

const txAmount = formatAmount(1);


async function main() {

 // Configure the client with options and our local key store

 const client = await near.connect(options);

 const provider = client.connection.provider;


 // Private key configuration

 const keyRootPath = client.connection.signer.keyStore.keyDir;

 const keyFilePath = `${keyRootPath}/${options.networkId}/${options.accountId}.json`;


 // Load key pair from the file

 const content = JSON.parse(fs.readFileSync(keyFilePath).toString());

 const keyPair = near.KeyPair.fromString(content.private_key);


 // Get the sender public key

 const publicKey = keyPair.getPublicKey();

 console.log("Sender public key:", publicKey.toString())


 // Get the public key information from the node

 const accessKey = await provider.query(

  `access_key/${txSender}/${publicKey.toString()}`, ""

 );

 console.log("Sender access key:", accessKey);


 // Check to make sure provided key is a full access key

 if (accessKey.permission !== "FullAccess") {

  return console.log(`Account [${txSender}] does not have permission to send tokens using key: [${publicKey}]`);

 };


 // Each transaction requires a unique number or nonce

 // This is created by taking the current nonce and incrementing it

 const nonce = ++accessKey.nonce;

 console.log("Calculated nonce:", nonce);


 // Construct actions that will be passed to the createTransaction method below

 const actions = [near.transactions.transfer(txAmount)];


 // Convert a recent block hash into an array of bytes.

 // This is required to prove the tx was recently constructed (within 24hrs)

 const recentBlockHash = near.utils.serialize.base_decode(accessKey.block_hash);


 // Create a new transaction object

 const transaction = near.transactions.createTransaction(

  txSender,

  publicKey,

  txReceiver,

  nonce,

  actions,

  recentBlockHash

 );


 // Before we can sign the transaction we must perform three steps

 // 1) Serialize the transaction in Borsh

 const serializedTx = near.utils.serialize.serialize(

  near.transactions.SCHEMA,

  transaction

 );


 // 2) Hash the serialized transaction using sha256

 const serializedTxHash = new Uint8Array(sha256.array(serializedTx));


 // 3) Create a signature using the hashed transaction

 const signature = keyPair.sign(serializedTxHash);


 // Sign the transaction

 const signedTransaction = new near.transactions.SignedTransaction({

  transaction,

  signature: new near.transactions.Signature({

   keyType: transaction.publicKey.keyType,

   data: signature.signature

  })

 });


 // Send the transaction

 try {

  const result = await provider.sendTransaction(signedTransaction);


  console.log("Creation result:", result.transaction);

  console.log("----------------------------------------------------------------");

  console.log("OPEN LINK BELOW to see transaction in NEAR Explorer!");

  console.log(`${options.explorerUrl}/transactions/${result.transaction.hash}`);

  console.log("----------------------------------------------------------------");


  setTimeout(async function() {

   console.log("Checking transaction status:", result.transaction.hash);


   const status = await provider.sendJsonRpc("tx", [result.transaction.hash, options.accountId]);

   console.log("Transaction status:", status);

  }, 5000);

 }

 catch(error) {

  console.log("ERROR:", error);

 }

};


main()


node transfer_advanced.js

5 шаг

npm install --save near-sdk-as
nano asconfig.json

{

 "extends": "near-sdk-as/asconfig.json",

 "options": {

  "measure": true,

  "binaryFile": "./contract.wasm"

 }

}


npx asb
npm install -g near-cli

near deploy \
 --contractName=figment-learn.testnet \
 --keyPath=./credentials/testnet/figment-learn.testnet.json \
 --wasmFile=./contract.wasm

nano execute.js

// Load environment variables

require("dotenv").config();


// Load NEAR Javascript API components

const near = require("near-api-js");


// Directory where NEAR credentials are going to be stored

const credentialsPath = "./credentials";


// Configure the keyStore to be used with the NEAR Javascript API

const UnencryptedFileSystemKeyStore = near.keyStores.UnencryptedFileSystemKeyStore;

const keyStore = new UnencryptedFileSystemKeyStore(credentialsPath)


// Setup default client options

const options = {

 networkId:  process.env.NEAR_NETWORK,

 nodeUrl:   process.env.NEAR_NODE_URL,

 walletUrl:  `https://wallet.${process.env.NEAR_NETWORK}.near.org`,

 helperUrl:  `https://helper.${process.env.NEAR_NETWORK}.near.org`,

 explorerUrl: `https://explorer.${process.env.NEAR_NETWORK}.near.org`,

 accountId:  process.env.NEAR_ACCOUNT,

 deps: {

  keyStore: keyStore

 }

}


async function main() {

 // Configure the client with options and our local key store

 const client = await near.connect(options);

 const account = await client.account(options.accountId);


 // We'are using the same contract name, feel free to create a different one.

 const contractName = options.accountId;


 // Construct a new contract object, we'll be using it to perform calls

 const contract = new near.Contract(account, contractName, {

  viewMethods: ["getValue"],  // our read function

  changeMethods: ["setValue"], // our write function

  sender: options.accountId,  // account used to sign contract call transactions

 });


 // We will send the current date when calling `setValue`

 const value = (new Date()).toString();


 console.log(`Calling contract call 'setValue' with '${value}'`);

 await contract.setValue({ value: value });


 // Get the value we assigned

 console.log("Getting current value");

 result = await contract.getValue();

 console.log("Result:", result);


 // Alternative way of calling a function

 result = await account.functionCall(

  contractName,

  "getValue",

  {}

 );

 console.log(result);

};


main();


node execute.js



































Report Page