-
Notifications
You must be signed in to change notification settings - Fork 3
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
6fcbe03
feat: update examples to the recent `near-api-js` version
denbite 56b195e
fix: use new `transfer` instead of deprecated `sendMoney`
denbite ba6ce7b
fix: use string representation of numbers wherever is possible
denbite d872b6d
style: fix formatting
denbite d26dda7
fix: make an example of read-only requests with just a `Provider` whe…
denbite 9ba2575
fix: update examples to use individual `@near-js/*` packages
denbite c351d80
fix: use Account instead of PublicAccount
denbite ae8a8fb
fix: update an example to use new package `@near-js/tokens` for sendi…
denbite 5415bc3
wip: changes to the new interface
675c7cf
Merge branch 'update_examples_to_recent_near-api-js' of github.com:ne…
00f1468
fix: update examples according to the new interface of some functions
denbite 51dd2b5
fix: examples
5c65c07
fix: simplified
8171f12
feat: add example for retrieving seat prices
denbite 70011f1
fix: impl example of simultaneous transactions properly
denbite 4244772
final version
89b81fe
fix: use freshly released `@near-js/*` packages
denbite File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,17 @@ | ||
import { connect } from "near-api-js"; | ||
import { PublicAccount, providers } from "near-api-js"; | ||
|
||
const connection = await connect({ | ||
networkId: "testnet", | ||
nodeUrl: "https://test.rpc.fastnear.com", | ||
// Create a connection to testnet RPC | ||
const provider = new providers.JsonRpcProvider({ | ||
url: "https://test.rpc.fastnear.com", | ||
}); | ||
|
||
// Create an account object | ||
const account = await connection.account("example-account.testnet"); | ||
const account = new PublicAccount("example-account.testnet", provider); | ||
|
||
// Gets the total, staked and available balance in yoctoNEAR | ||
const accountBalance = await account.getAccountBalance(); | ||
const accountBalance = await account.getBalance(); | ||
console.log(accountBalance); | ||
|
||
// Account's state, including its code hash and storage usage | ||
const accountState = await account.state(); | ||
const accountState = await account.getInformation(); | ||
console.log(accountState); | ||
|
||
// Gets a list of authorized apps for an account | ||
const accountDetails = await account.getAccountDetails(); | ||
console.log(accountDetails); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,46 @@ | ||
import { connect, keyStores, KeyPair, utils } from "near-api-js"; | ||
import { Account, providers, KeyPairSigner, KeyPair, utils } from "near-api-js"; | ||
import dotenv from "dotenv"; | ||
|
||
dotenv.config({ path: "../.env" }); | ||
const privateKey = process.env.PRIVATE_KEY; | ||
const beneficiaryAccountId = process.env.ACCOUNT_ID; | ||
|
||
const myKeyStore = new keyStores.InMemoryKeyStore(); | ||
const keyPair = KeyPair.fromString(privateKey); | ||
await myKeyStore.setKey("testnet", beneficiaryAccountId, 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 accountCreator = await nearConnection.account(beneficiaryAccountId); | ||
// Create an account object | ||
const account = new Account(beneficiaryAccountId, provider, signer); // example-account.testnet | ||
|
||
// First create a new account to be deleted | ||
// Generate a new account ID based on the current timestamp | ||
const accountToDeleteId = Date.now() + ".testnet"; | ||
const newKeyPair = KeyPair.fromRandom("ed25519"); | ||
const newPublicKey = newKeyPair.getPublicKey().toString(); | ||
|
||
await accountCreator.functionCall({ | ||
contractId: "testnet", | ||
methodName: "create_account", | ||
args: { | ||
new_account_id: accountToDeleteId, | ||
new_public_key: newPublicKey, | ||
}, | ||
gas: "300000000000000", | ||
attachedDeposit: utils.format.parseNearAmount("0.1"), | ||
}); | ||
await account.createTopLevelAccount( | ||
accountToDeleteId, | ||
newPublicKey, | ||
utils.format.parseNearAmount("0.1") | ||
); | ||
|
||
// Create a signer from a key pair that was added to new account | ||
const signerToDelete = new KeyPairSigner(newKeyPair); | ||
|
||
// Create an account object for the new account | ||
// and add the new key pair to the keystore | ||
const accountToDelete = await nearConnection.account(accountToDeleteId); | ||
await myKeyStore.setKey("testnet", accountToDeleteId, newKeyPair); | ||
// Create an account object for the new account with corresponding signer | ||
const accountToDelete = new Account( | ||
accountToDeleteId, | ||
provider, | ||
signerToDelete | ||
); | ||
|
||
// Delete the account with account ID of the account object | ||
// specifying the beneficiary account ID | ||
const deleteAccountResult = | ||
await accountToDelete.deleteAccount(beneficiaryAccountId); // example-beneficiary.testnet | ||
const deleteAccountResult = await accountToDelete.deleteAccount( | ||
beneficiaryAccountId | ||
); // example-beneficiary.testnet | ||
console.log(deleteAccountResult); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
Might not work for other provider and signer types
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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, therpcUrl
not so much, as aprovider
might need authenticationThere was a problem hiding this comment.
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 ofSigner
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