near-api-js 7.0.4 → 7.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,8 +3,8 @@ import { type Provider } from '../providers/index.js';
3
3
  import type { RpcTransactionResponse } from '../rpc/types.gen.js';
4
4
  import { type SignedMessage, type Signer } from '../signers/index.js';
5
5
  import type { SignDelegateActionReturn } from '../signers/signer.js';
6
- import type { FungibleToken, NativeToken } from '../tokens/index.js';
7
- import { type Action, type DelegateAction, type SignedTransaction } from '../transactions/index.js';
6
+ import { FungibleToken, type NativeToken } from '../tokens/index.js';
7
+ import { type Action, type DelegateAction, type SignedDelegate, type SignedTransaction } from '../transactions/index.js';
8
8
  import type { SerializedReturnValue, TxExecutionStatus } from '../types/index.js';
9
9
  export declare const DEFAULT_WAIT_STATUS: TxExecutionStatus;
10
10
  export interface AccountState {
@@ -307,6 +307,13 @@ export declare class Account {
307
307
  * @returns The available balance of the account in units (e.g. yoctoNEAR).
308
308
  */
309
309
  getBalance(token?: NativeToken | FungibleToken): Promise<bigint>;
310
+ /**
311
+ * Relay a meta-transaction signed by a different account
312
+ *
313
+ * @param signedDelegate The signed delegate action to relay
314
+ * @returns The transaction response from the relayed meta-transaction
315
+ */
316
+ relayMetaTransaction(signedDelegate: SignedDelegate): Promise<RpcTransactionResponse>;
310
317
  /**
311
318
  * Transfers a token to the specified receiver.
312
319
  *
@@ -3,7 +3,7 @@ import { parseTransactionExecutionError } from '../providers/errors/parse.js';
3
3
  import { InvalidNonceError } from '../providers/errors/transaction_execution.js';
4
4
  import { JsonRpcProvider } from '../providers/index.js';
5
5
  import { KeyPairSigner } from '../signers/index.js';
6
- import { NEAR } from '../tokens/index.js';
6
+ import { FungibleToken, NEAR } from '../tokens/index.js';
7
7
  import { actions, buildDelegateAction, createTransaction, } from '../transactions/index.js';
8
8
  import { baseDecode, getTransactionLastResult } from '../utils/index.js';
9
9
  import { NonceManager } from './nonce-manager.js';
@@ -488,6 +488,18 @@ export class Account {
488
488
  async getBalance(token = NEAR) {
489
489
  return token.getBalance(this);
490
490
  }
491
+ /**
492
+ * Relay a meta-transaction signed by a different account
493
+ *
494
+ * @param signedDelegate The signed delegate action to relay
495
+ * @returns The transaction response from the relayed meta-transaction
496
+ */
497
+ async relayMetaTransaction(signedDelegate) {
498
+ return this.signAndSendTransaction({
499
+ receiverId: signedDelegate.delegateAction.senderId,
500
+ actions: [actions.signedDelegate(signedDelegate)],
501
+ });
502
+ }
491
503
  /**
492
504
  * Transfers a token to the specified receiver.
493
505
  *
@@ -500,6 +512,15 @@ export class Account {
500
512
  *
501
513
  */
502
514
  async transfer({ receiverId, amount, token = NEAR }) {
515
+ if (token instanceof FungibleToken) {
516
+ const isRegistered = await token.isAccountRegistered({ accountId: receiverId, provider: this.provider });
517
+ if (!isRegistered) {
518
+ await token.registerAccount({
519
+ accountIdToRegister: receiverId,
520
+ fundingAccount: this,
521
+ });
522
+ }
523
+ }
503
524
  return token.transfer({ from: this, receiverId, amount });
504
525
  }
505
526
  }
package/lib/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export * from './accounts/index.js';
2
2
  export type { CurveType, KeyPairString, Signature as CryptoSignature, } from './crypto/index.js';
3
- export { KeyPair, KeyPairEd25519, KeyPairSecp256k1, KeyType, PublicKey, } from './crypto/index.js';
3
+ export { KeyPair, KeyPairEd25519, KeyPairSecp256k1, KeyType, keyToImplicitAddress, PublicKey, } from './crypto/index.js';
4
4
  export * from './providers/index.js';
5
5
  export * from './signers/index.js';
6
6
  export * from './transactions/index.js';
package/lib/index.js CHANGED
@@ -8,7 +8,7 @@ export * from './accounts/index.js';
8
8
  // ============================================================================
9
9
  // Cryptography
10
10
  // ============================================================================
11
- export { KeyPair, KeyPairEd25519, KeyPairSecp256k1, KeyType, PublicKey, } from './crypto/index.js';
11
+ export { KeyPair, KeyPairEd25519, KeyPairSecp256k1, KeyType, keyToImplicitAddress, PublicKey, } from './crypto/index.js';
12
12
  // ============================================================================
13
13
  // Providers
14
14
  // ============================================================================
@@ -1,4 +1,5 @@
1
1
  import type { Account } from '../../accounts/account.js';
2
+ import type { Provider } from '../../providers/provider.js';
2
3
  interface FTMetadata {
3
4
  spec?: string;
4
5
  name: string;
@@ -61,7 +62,10 @@ export declare class FungibleToken extends BaseFT {
61
62
  receiverId: string;
62
63
  amount: string | number | bigint;
63
64
  }): Promise<any>;
64
- getBalance(account: Account): Promise<bigint>;
65
+ getBalance({ accountId, provider }: {
66
+ accountId: string;
67
+ provider: Provider;
68
+ }): Promise<bigint>;
65
69
  /**
66
70
  * Transfer tokens and call a function on the receiver contract,
67
71
  * only works if the receiver implements the `ft_on_transfer` method
@@ -100,6 +104,18 @@ export declare class FungibleToken extends BaseFT {
100
104
  account: Account;
101
105
  force: boolean;
102
106
  }): Promise<any>;
107
+ /**
108
+ * Checks if an account is registered to the fungible token contract
109
+ *
110
+ * @param param
111
+ * @param param.accountId The AccountID to check
112
+ * @param param.provider A Provider to use for the query
113
+ * @returns Whether the account is registered
114
+ */
115
+ isAccountRegistered({ accountId, provider, }: {
116
+ accountId: string;
117
+ provider: Provider;
118
+ }): Promise<boolean>;
103
119
  }
104
120
  /**
105
121
  * The NEAR token is the native token of the NEAR blockchain
@@ -1,4 +1,5 @@
1
1
  import { actions } from '../../transactions/action_creators.js';
2
+ import { teraToGas } from '../../units/gas.js';
2
3
  import { formatAmount, parseAmount } from './format.js';
3
4
  class BaseFT {
4
5
  constructor(metadata) {
@@ -51,15 +52,15 @@ export class FungibleToken extends BaseFT {
51
52
  amount: amount.toString(),
52
53
  receiver_id: receiverId,
53
54
  },
54
- gas: '30000000000000',
55
+ gas: teraToGas(30),
55
56
  deposit: 1,
56
57
  });
57
58
  }
58
- async getBalance(account) {
59
- const balance = await account.provider.callFunction({
59
+ async getBalance({ accountId, provider }) {
60
+ const balance = await provider.callFunction({
60
61
  contractId: this.accountId,
61
62
  method: 'ft_balance_of',
62
- args: { account_id: account.accountId },
63
+ args: { account_id: accountId },
63
64
  });
64
65
  return BigInt(balance);
65
66
  }
@@ -82,7 +83,7 @@ export class FungibleToken extends BaseFT {
82
83
  amount: amount.toString(),
83
84
  msg,
84
85
  },
85
- gas: '30000000000000',
86
+ gas: teraToGas(30),
86
87
  deposit: 1,
87
88
  });
88
89
  }
@@ -107,7 +108,7 @@ export class FungibleToken extends BaseFT {
107
108
  account_id: accountIdToRegister,
108
109
  registration_only: true,
109
110
  },
110
- gas: '30000000000000',
111
+ gas: teraToGas(30),
111
112
  deposit: requiredDeposit,
112
113
  });
113
114
  }
@@ -123,10 +124,35 @@ export class FungibleToken extends BaseFT {
123
124
  contractId: this.accountId,
124
125
  methodName: 'storage_unregister',
125
126
  args: { force },
126
- gas: '30000000000000',
127
+ gas: teraToGas(30),
127
128
  deposit: 1,
128
129
  });
129
130
  }
131
+ /**
132
+ * Checks if an account is registered to the fungible token contract
133
+ *
134
+ * @param param
135
+ * @param param.accountId The AccountID to check
136
+ * @param param.provider A Provider to use for the query
137
+ * @returns Whether the account is registered
138
+ */
139
+ async isAccountRegistered({ accountId, provider, }) {
140
+ const [storage, required] = await Promise.all([
141
+ provider.callFunction({
142
+ contractId: this.accountId,
143
+ method: 'storage_balance_of',
144
+ args: { account_id: accountId },
145
+ }),
146
+ provider.callFunction({
147
+ contractId: this.accountId,
148
+ method: 'storage_balance_bounds',
149
+ args: {},
150
+ }),
151
+ ]);
152
+ if (!storage)
153
+ return false;
154
+ return BigInt(storage.total) >= BigInt(required.min);
155
+ }
130
156
  }
131
157
  /**
132
158
  * The NEAR token is the native token of the NEAR blockchain
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "near-api-js",
3
3
  "description": "JavaScript library to interact with NEAR Protocol via RPC API",
4
- "version": "7.0.4",
4
+ "version": "7.1.1",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/near/near-api-js.git"