跳到主要内容
版本:near-api-js@1.1.0

Class: Account

account.Account

This class provides common account related RPC calls including signing transactions with a KeyPair.

Hint

Use WalletConnection in the browser to redirect to NEAR Wallet for Account/key management using the BrowserLocalStorageKeyStore.

See

Hierarchy​

Constructors​

constructor​

new Account(connection, accountId)

Parameters​

NameType
connectionConnection
accountIdstring

Defined in​

account.ts:163

Properties​

accountId​

Readonly accountId: string

Defined in​

account.ts:161


connection​

Readonly connection: Connection

Defined in​

account.ts:160

Methods​

addKey​

addKey(publicKey, contractId?, methodNames?, amount?): Promise<FinalExecutionOutcome>

See

https://docs.near.org/concepts/basics/accounts/access-keys

Todo

expand this API to support more options.

Parameters​

NameTypeDescription
publicKeystring | PublicKeyA public key to be associated with the contract
contractId?stringNEAR account where the contract is deployed
methodNames?string | string[]The method names on the contract that should be allowed to be called. Pass null for no method names and '' or [] for any method names.
amount?BNPayment in yoctoⓃ that is sent to the contract during this function call

Returns​

Promise<FinalExecutionOutcome>

Defined in​

account.ts:443


createAccount​

createAccount(newAccountId, publicKey, amount): Promise<FinalExecutionOutcome>

Parameters​

NameTypeDescription
newAccountIdstringNEAR account name to be created
publicKeystring | PublicKeyA public key created from the masterAccount
amountBN-

Returns​

Promise<FinalExecutionOutcome>

Defined in​

account.ts:374


createAndDeployContract​

createAndDeployContract(contractId, publicKey, data, amount): Promise<Account>

Create a new account and deploy a contract to it

Parameters​

NameTypeDescription
contractIdstringNEAR account where the contract is deployed
publicKeystring | PublicKeyThe public key to add to the created contract account
dataUint8ArrayThe compiled contract code
amountBNof NEAR to transfer to the created contract account. Transfer enough to pay for storage https://docs.near.org/docs/concepts/storage-staking

Returns​

Promise<Account>

Defined in​

account.ts:349


deleteAccount​

deleteAccount(beneficiaryId): Promise<FinalExecutionOutcome>

Parameters​

NameTypeDescription
beneficiaryIdstringThe NEAR account that will receive the remaining Ⓝ balance from the account being deleted

Returns​

Promise<FinalExecutionOutcome>

Defined in​

account.ts:385


deleteKey​

deleteKey(publicKey): Promise<FinalExecutionOutcome>

Parameters​

NameTypeDescription
publicKeystring | PublicKeyThe public key to be deleted

Returns​

Promise<FinalExecutionOutcome>

Defined in​

account.ts:466


deployContract​

deployContract(data): Promise<FinalExecutionOutcome>

Parameters​

NameTypeDescription
dataUint8ArrayThe compiled contract code

Returns​

Promise<FinalExecutionOutcome>

Defined in​

account.ts:398


findAccessKey​

findAccessKey(receiverId, actions): Promise<{ accessKey: AccessKeyView ; publicKey: PublicKey }>

Finds the AccessKeyView associated with the accounts PublicKey stored in the KeyStore.

Todo

Find matching access key based on transaction (i.e. receiverId and actions)

Parameters​

NameTypeDescription
receiverIdstringcurrently unused (see todo)
actionsAction[]currently unused (see todo)

Returns​

Promise<{ accessKey: AccessKeyView ; publicKey: PublicKey }>

{ publicKey PublicKey; accessKey: AccessKeyView }

Defined in​

account.ts:297


functionCall​

functionCall(__namedParameters): Promise<FinalExecutionOutcome>

Execute function call

Parameters​

NameType
__namedParametersChangeFunctionCallOptions

Returns​

Promise<FinalExecutionOutcome>

Defined in​

account.ts:414


getAccessKeys​

getAccessKeys(): Promise<AccessKeyInfoView[]>

Get all access keys for the account

See

https://docs.near.org/api/rpc/access-keys#view-access-key-list

Returns​

Promise<AccessKeyInfoView[]>

Defined in​

account.ts:595


getAccountBalance​

getAccountBalance(): Promise<AccountBalance>

Returns calculated account balance

Returns​

Promise<AccountBalance>

Defined in​

account.ts:629


getAccountDetails​

getAccountDetails(): Promise<{ authorizedApps: AccountAuthorizedApp[] }>

Returns a list of authorized apps

Todo

update the response value to return all the different keys, not just app keys.

Returns​

Promise<{ authorizedApps: AccountAuthorizedApp[] }>

Defined in​

account.ts:609


getActiveDelegatedStakeBalance​

getActiveDelegatedStakeBalance(): Promise<ActiveDelegatedStakeBalance>

Returns the NEAR tokens balance and validators of a given account that is delegated to the staking pools that are part of the validators set in the current epoch.

NOTE: If the tokens are delegated to a staking pool that is currently on pause or does not have enough tokens to participate in validation, they won't be accounted for.

Returns​

Promise<ActiveDelegatedStakeBalance>

Defined in​

account.ts:653


sendMoney​

sendMoney(receiverId, amount): Promise<FinalExecutionOutcome>

Parameters​

NameTypeDescription
receiverIdstringNEAR account receiving Ⓝ
amountBNAmount to send in yoctoⓃ

Returns​

Promise<FinalExecutionOutcome>

Defined in​

account.ts:363


signAndSendTransaction​

Protected signAndSendTransaction(__namedParameters): Promise<FinalExecutionOutcome>

Sign a transaction to preform a list of actions and broadcast it using the RPC API.

See

JsonRpcProvider.sendTransaction

Parameters​

NameType
__namedParametersSignAndSendTransactionOptions

Returns​

Promise<FinalExecutionOutcome>

Defined in​

account.ts:228


signTransaction​

Protected signTransaction(receiverId, actions): Promise<[Uint8Array, SignedTransaction]>

Create a signed transaction which can be broadcast to the network

See

JsonRpcProvider.sendTransaction

Parameters​

NameTypeDescription
receiverIdstringNEAR account receiving the transaction
actionsAction[]list of actions to perform as part of the transaction

Returns​

Promise<[Uint8Array, SignedTransaction]>

Defined in​

account.ts:208


stake​

stake(publicKey, amount): Promise<FinalExecutionOutcome>

See

https://near-nodes.io/validator/staking-and-delegation

Parameters​

NameTypeDescription
publicKeystring | PublicKeyThe public key for the account that's staking
amountBNThe account to stake in yoctoⓃ

Returns​

Promise<FinalExecutionOutcome>

Defined in​

account.ts:479


state​

state(): Promise<AccountView>

Returns basic NEAR account information via the view_account RPC query method

See

https://docs.near.org/api/rpc/contracts#view-account

Returns​

Promise<AccountView>

Defined in​

account.ts:172


viewFunction​

viewFunction(...restArgs): Promise<any>

Invoke a contract view function using the RPC API.

See

https://docs.near.org/api/rpc/contracts#call-a-contract-function

Parameters​

NameType
...restArgsany

Returns​

Promise<any>

Defined in​

account.ts:512


viewFunctionV1​

viewFunctionV1(contractId, methodName, args?, __namedParameters?): Promise<any>

Parameters​

NameType
contractIdstring
methodNamestring
argsany
__namedParametersObject
__namedParameters.blockQuery?BlockReference
__namedParameters.jsContract?boolean
__namedParameters.parse?(response: Uint8Array) => any
__namedParameters.stringify?(input: any) => Buffer

Returns​

Promise<any>

Defined in​

account.ts:524


viewFunctionV2​

viewFunctionV2(__namedParameters): Promise<any>

Parameters​

NameType
__namedParametersViewFunctionCallOptions

Returns​

Promise<any>

Defined in​

account.ts:535


viewState​

viewState(prefix, blockQuery?): Promise<{ key: Buffer ; value: Buffer }[]>

Returns the state (key value pairs) of this account's contract based on the key prefix. Pass an empty string for prefix if you would like to return the entire state.

See

https://docs.near.org/api/rpc/contracts#view-contract-state

Parameters​

NameTypeDescription
prefixstring | Uint8Arrayallows to filter which keys should be returned. Empty prefix means all keys. String prefix is utf-8 encoded.
blockQueryBlockReferencespecifies which block to query state at. By default returns last "optimistic" block (i.e. not necessarily finalized).

Returns​

Promise<{ key: Buffer ; value: Buffer }[]>

Defined in​

account.ts:577