Skip to content

Update examples to the most recent, yet unreleased near-api-js version #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

denbite
Copy link

@denbite denbite commented Apr 11, 2025

It's a companion PR that demonstrates the new API of near-api-js (introduced in near/near-api-js#1513) in action

};
const nearConnection = await connect(connectionConfig);
// Create a connection to testnet RPC
const provider = new providers.JsonRpcProvider({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add networkId to the providers? I think it makes sense for them to know what network they are operating with... but I also understand if we prefer to not add it

Does ethers or [insert name of the other library] instanciate providers passing a networkId?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer adding a method async getNetwork(): Promise<string> method to a provider to retrieve the network name on demand and avoid issues if somebody instantiates Provider as testnet while the actual RPC points out to mainnet

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that idea, lets do it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just cache the answer (since I don't expect the network to ever change for an instantiated provider), it would even be cool to maybe just do it once in the constructor and set it up

Comment on lines 13 to 23

const myKeyStore = new keyStores.InMemoryKeyStore();
const keyPair = KeyPair.fromString(privateKey);
await myKeyStore.setKey("testnet", accountId, keyPair);
// Create a signer from a private key string
const signer = KeyPairSigner.fromSecretKey(privateKey); // ed25519:5Fg2...

const connectionConfig = {
networkId: "testnet",
keyStore: myKeyStore,
nodeUrl: "https://test.rpc.fastnear.com",
};
const nearConnection = await connect(connectionConfig);
// Create a connection to testnet RPC
const provider = new providers.JsonRpcProvider({
url: "https://test.rpc.fastnear.com",
});

const account = await nearConnection.account(accountId);
// Create an account object
const account = new Account(accountId, provider, signer); // example-account.testnet

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be an unpopular opinion, but if signer and provider are always created from just one arg why not just pass those directly into the Account constructor?

const privateKey = process.env.PRIVATE_KEY;
const accountId = process.env.ACCOUNT_ID;
const rpcUrl = process.env.RPC_URL

const account = new Account(accountId, rpcUrl, privateKey);

Might not work for other provider and signer types

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I always end up recreating this util in every single project I work on

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the privateKey makes sense to be honest, the rpcUrl not so much, as a provider might need authentication

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't change that as I hope in the future the KeyPairSigner won't be the only implementation of Signer
but I like the idea of having a function to create an account in one line, so I suggest we add a static function to an Account

const account = Account.fromPrivateKey(accountId, rpcUrl, privateKey);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants