【问题标题】:Unable to transfer NEAR tokens between accounts using near-api-js无法使用 near-api-js 在账户之间转移 NEAR 代币
【发布时间】:2022-07-20 18:13:32
【问题描述】:

我正在尝试使用 NextJS 中的 near-api-js 库在 2 个测试网钱包之间转移 NEAR 代币

运行账户的汇款功能,我收到以下错误

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\");
        }
    };

在运行account.getAccessKeys(); 时,有完整的访问密钥和功能访问密钥可用,那么为什么我无法发送令牌?

此外,我不明白文档中的以下屏幕截图(https://docs.near.org/docs/develop/front-end/introduction);为什么不允许?

    标签: next.js nearprotocol near-api-js


    【解决方案1】:

    经过一周的奋斗找到了这个: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());
    };
    

    登录后,您可以使用sendMoney 功能在账户之间转移 NEAR 代币

    【讨论】:

      猜你喜欢
      • 2020-10-21
      • 2020-04-03
      • 2021-01-10
      • 2021-02-20
      • 2020-09-09
      • 1970-01-01
      • 2021-01-15
      • 2021-07-13
      相关资源
      最近更新 更多