Contract method is not found

痴心易碎 提交于 2020-12-13 04:45:05

问题


So I'm working out of index.ts where my contract code is and utils.js where I added my methods and I'm testing my functions in the console but receive the above response.

index.ts

import { Context, logging, storage, PersistentMap } from 'near-sdk-as'
import { string } from 'prop-types';

// Key value pair
// Value = array ; Key = voter ID
let votersReg= new PersistentMap<string, bool>("Voters Registration");
let candidateReg= new PersistentMap<string, i32>("Candidate Registration");

// Context.sender == 'usernmae.testnet'

export function addVote(candidate:string):i32{
    if(!votersReg.contains(Context.sender)){
        votersReg.set(Context.sender, true)
        if(!candidateReg.contains(candidate)){
            candidateReg.set(candidate, 1)
        }
        else{
            let currentCount=candidateReg.getSome(candidate);
            currentCount += 1;
            candidateReg.set(candidate, currentCount);
        }  
    }
    else{
        logging.log("You have already voted!")
    }
    return candidateReg.getSome(candidate)
}

export function getVotes(candidate:string):i32{
    if(candidateReg.contains(candidate)){
        return candidateReg.getSome(candidate)
    }
    else {return 0}
}

utils.js

import { connect, Contract, keyStores, WalletConnection } from 'near-api-js'
import getConfig from './config'

//const nearConfig = getConfig(process.env.NODE_ENV || 'development')
const nearConfig = getConfig('testnet')

// Initialize contract & set global variables
export async function initContract() {
  // Initialize connection to the NEAR testnet
  const near = await connect(Object.assign({ deps: { keyStore: new keyStores.BrowserLocalStorageKeyStore() } }, nearConfig))

  // Initializing Wallet based Account. It can work with NEAR testnet wallet that
  // is hosted at https://wallet.testnet.near.org
  window.walletConnection = new WalletConnection(near)

  // Getting the Account ID. If still unauthorized, it's just empty string
  window.accountId = window.walletConnection.getAccountId()

  // Initializing our contract APIs by contract name and configuration
  window.contract = await new Contract(window.walletConnection.account(), nearConfig.contractName, {
    // View methods are read only. They don't modify the state, but usually return some value.
    viewMethods: ['getVotes'],
    // Change methods can modify the state. But you don't receive the returned value when called.
    changeMethods: ['addVote'],
  })
}

export function logout() {
  window.walletConnection.signOut()
  // reload page
  window.location.replace(window.location.origin + window.location.pathname)
}

export function login() {
  // Allow the current app to make calls to the specified contract on the
  // user's behalf.
  // This works by creating a new access key for the user's account and storing
  // the private key in localStorage.
  window.walletConnection.requestSignIn(nearConfig.contractName)
}

testing addVote() with await window.contract.addVote({candidate: "dummy"}) returns

Failure [dev-1605381216128-9269896]: Error: Contract method is not found
printLogsAndFailures @ account.js:62
signAndSendTransaction @ account.js:132
async function (async)
signAndSendTransaction @ account.js:77
signAndSendTransaction @ wallet-account.js:159
async function (async)
signAndSendTransaction @ wallet-account.js:151
functionCall @ account.js:223
(anonymous) @ contract.js:47
addVote @ contract.js:14
(anonymous) @ VM77:1
errors.js:19 Uncaught Error: Contract method is not found
    at Object.parseRpcError (rpc_errors.js:38)
    at ConnectedWalletAccount.signAndSendTransaction (account.js:139)
    at async ConnectedWalletAccount.signAndSendTransaction (wallet-account.js:159)
    at :1234/async http:/localhost:1234/src.e31bb0bc.js:48839
    at :1234/async <anonymous>:1

testing getVotes() with await window.contract.getVotes() returns

json-rpc-provider.js:69 Uncaught Error: Querying call/dev-1605381216128-9269896/getVotes failed: wasm execution failed with error: FunctionCallError(MethodResolveError(MethodNotFound)).
{
  "error": "wasm execution failed with error: FunctionCallError(MethodResolveError(MethodNotFound))",
  "logs": [],
  "block_height": 24423413,
  "block_hash": "96FXs7Y2a53uvfpuJhm9eU8asjNicf87SbqwFGvmBn6K"
}
    at JsonRpcProvider.query (json-rpc-provider.js:68)
    at async ConnectedWalletAccount.viewFunction (account.js:276)
    at :1234/async <anonymous>:1

I am not exactly sure what is going on here. I was under the impression I did not need to deploy my contracts prior to testing, perhaps this is what is screwing things up but I thought deploying contracts was a finality step. Thanks in advance for any help!


回答1:


You do need to deploy your contracts in order to test them. You will have to do this for each function you add for it to be available in the frontend.



来源:https://stackoverflow.com/questions/64852672/contract-method-is-not-found

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!