next.jsnearprotocolnear-api-js

Unable to transfer NEAR tokens between accounts using near-api-js


I am trying to transfer NEAR tokens between 2 testnet wallets using the near-api-js library in NextJS

Running send money function of the account, I am getting the following error

import { connect, keyStores } from "near-api-js";

export const NearConfig = async () => {
    const config = {
        networkId: "testnet",
        keyStore: new keyStores.BrowserLocalStorageKeyStore(),
        nodeUrl: "https://rpc.testnet.near.org",
        walletUrl: "https://wallet.testnet.near.org",
        helperUrl: "https://helper.testnet.near.org",
        explorerUrl: "https://explorer.testnet.near.org",
    };
    return await connect(config);
};


setNear(await NearConfig());

const sendTokens = async () => {
        try {
            const account = await near.account(account_id);

            await account.sendMoney(
                "itissandeep98.testnet", // receiver account
                "100000000" // amount in yoctoNEAR
            );
        } catch (error) {
            console.log(error);
            showAlert(error.message, "error");
        }
    };

enter image description here

On running account.getAccessKeys(); there are full access keys as well as functional access keys available, then why I am not able to send tokens?

Moreover, I don't understand the below screenshot from the docs(https://docs.near.org/docs/develop/front-end/introduction); why isn't it allowed?

enter image description here


Solution

  • Found this after one week of struggle: Connect FullAccess account with near-api-js

    const PENDING_ACCESS_KEY_PREFIX = "pending_key";
    
    const loginFullAccess = async (options) => {
      const currentUrl = new URL(window.location.href);
      const newUrl = new URL(wallet._walletBaseUrl + "/login/");
        newUrl.searchParams.set('success_url', options.successUrl || currentUrl.href);
      newUrl.searchParams.set('failure_url', options.failureUrl || currentUrl.href);
    
      const accessKey = KeyPair.fromRandom("ed25519");
      newUrl.searchParams.set("public_key", accessKey.getPublicKey().toString());
      await wallet._keyStore.setKey(
        wallet._networkId,
        PENDING_ACCESS_KEY_PREFIX + accessKey.getPublicKey(),
        accessKey
      );
    
      window.location.assign(newUrl.toString());
    };
    

    After login you can use the sendMoney function to transfer NEAR tokens between accounts