Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
fix: TLA accounts from 1.37
Browse files Browse the repository at this point in the history
  • Loading branch information
gagdiez committed Feb 8, 2024
1 parent ea51f4a commit 4114ab6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
14 changes: 7 additions & 7 deletions commands/account/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ async function create(options) {

if (options.useFaucet) {
if (options.networkId !== 'testnet') throw new Error('Pre-funding accounts is only possible on testnet');
}else{
if(!options.useAccount) throw new Error('Please specify an account to sign the transaction (--useAccount)');
} else {
if (!options.useAccount) throw new Error('Please specify an account to sign the transaction (--useAccount)');
await assertCredentials(options.useAccount, options.networkId, options.keyStore);
}

Expand All @@ -93,7 +93,7 @@ async function create(options) {
// Use an existing account
const account = await near.account(options.useAccount);
try { await account.state(); } catch (e) {
throw new Error(`Account ${options.useAccount} does not exist in ${options.networkId}. Are you using the right network?`);
throw new Error(`Account ${options.useAccount} does not exist in ${options.networkId}. Are you using the right network?`);
}

const initialBalance = utils.format.parseNearAmount(options.initialBalance);
Expand All @@ -117,7 +117,7 @@ async function create(options) {
try {
// Handle response
const response = await promise;
if (keyPair){
if (keyPair) {
storeCredentials(newAccountId, options.networkId, options.keyStore, keyPair, true);
} else {
console.log(chalk`{bold.white ${newAccountId}} created successfully, please add its credentials manually.`);
Expand All @@ -127,21 +127,21 @@ async function create(options) {
// Handle errors
switch (error.type) {
case 'CreateAccountNotAllowed':
console.error(chalk`\n{red.bold Error:} ${options.useFaucet? 'the faucet service' : options.useAccount} cannot create ${newAccountId} (networkId: ${options.networkId}).`);
console.error(chalk`\n{red.bold Error:} ${options.useFaucet ? 'the faucet service' : options.useAccount} cannot create ${newAccountId} (networkId: ${options.networkId}).`);
options.useAccount && console.error(chalk`${options.useAccount} can only create sub-accounts of itself, or .${network2TLA[options.networkId]} accounts.\n`);
options.useFaucet && console.error(chalk`Try using an account to fund it (--useAccount).`);
process.exit(1);
break;
case 'NotEnoughBalance':
console.error(chalk`\n{red.bold Error:} ${options.useFaucet? 'The faucet service' : options.useAccount} does not have enough balance.`);
console.error(chalk`\n{red.bold Error:} ${options.useFaucet ? 'The faucet service' : options.useAccount} does not have enough balance.`);
console.error(`Transaction hash: ${error.context.transactionHash}\n`);
console.error(`Signer: ${error.kind.signer_id}`);
console.error(`Balance: ${utils.format.formatNearAmount(error.kind.balance)}`);
console.error(`Action Cost: ${utils.format.formatNearAmount(error.kind.cost)}\n`);
process.exit(1);
break;
case 'CreateAccountOnlyByRegistrar':
console.error(chalk`\nYou cannot create Top Level Accounts with less than 32 chars (${newAccountId} has ${newAccountId.length} chars).`);
console.error(chalk`\nYou cannot create Top Level Accounts.`);
process.exit(1);
break;
default:
Expand Down
1 change: 0 additions & 1 deletion commands/transactions/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ async function sendMoney(options) {

const near = await connect(options);
const account = await near.account(options.sender);

try {
console.log(`Sending ${options.amount} NEAR to ${options.receiver} from ${options.sender}`);
const result = await account.sendMoney(options.receiver, utils.format.parseNearAmount(options.amount));
Expand Down
15 changes: 11 additions & 4 deletions test/test_account_creation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ fi
# An account can create zero-balance accounts
./bin/near create-account $zerobalance --accountId $testaccount1 --initialBalance 0

# An account can create a TLA
./bin/near create-account $tla --accountId $testaccount1

set +e

# Cannot create sub-accounts of other accounts
Expand All @@ -68,14 +65,24 @@ fi

# Cannot create a TLA with a short name
ERROR=$(./bin/near create-account tooshortfortla --accountId $testaccount1 2>&1)
EXPECTED_ERROR=".+cannot create Top Level Accounts with less than 32 chars.+"
EXPECTED_ERROR=".+cannot create Top Level Accounts.+"
if [[ ! "$ERROR" =~ $EXPECTED_ERROR ]]; then
echo FAILURE Unexpected output when creating a short top-level account
echo Got: $ERROR
echo Expected: $EXPECTED_ERROR
exit 1
fi

# Cannot create a TLA with a long name
ERROR=$(./bin/near create-account $tla --accountId $testaccount1 2>&1)
EXPECTED_ERROR=".+cannot create Top Level Accounts.+"
if [[ ! "$ERROR" =~ $EXPECTED_ERROR ]]; then
echo FAILURE Unexpected output when creating a long top-level account
echo Got: $ERROR
echo Expected: $EXPECTED_ERROR
exit 1
fi

# Cannot create a useFaucet account in mainnet
ERROR=$(./bin/near create-account $testaccount4 --useFaucet --networkId mainnet 2>&1)
EXPECTED_ERROR=".+Pre-funding accounts is only possible on testnet.+"
Expand Down
1 change: 0 additions & 1 deletion utils/credentials.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const chalk = require('chalk');

async function assertCredentials(accountId, networkId, keyStore) {

const key = await keyStore.getKey(networkId, accountId);
if(key) return;

Expand Down

0 comments on commit 4114ab6

Please sign in to comment.