Skip to content

Commit

Permalink
Add an example script for funding a testnet account.
Browse files Browse the repository at this point in the history
  • Loading branch information
artob committed Feb 3, 2025
1 parent d3efb1c commit 6d3eb73
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 13 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ result = testnet.import_account_with_private_key(
# Create an account funded from a faucet (testnet only):
result = testnet.create_account_with_faucet(
'mynewaccount.testnet',
'ed25519:HVPgAsZkZ7cwLZDqK313XJsDyqAvgBxrATcD7VacA8KE'
public_key: 'ed25519:HVPgAsZkZ7cwLZDqK313XJsDyqAvgBxrATcD7VacA8KE'
)

# Create an account funded by another account:
Expand All @@ -207,8 +207,8 @@ result = testnet.create_implicit_account('/path/to/credentials/folder')
```ruby
# Delete an existing account:
result = testnet.delete_account(
'todelete.testnet', # account to delete
'beneficiary.testnet' # account receiving remaining balance
'my-obsolete-account.testnet', # account to delete
beneficiary: 'v2.faucet.nonofficial.testnet' # account receiving remaining balance
)
```

Expand Down
17 changes: 17 additions & 0 deletions examples/fund_testnet_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env ruby -Ilib -I../lib
require 'near'

trap(:SIGINT) { abort '' }

testnet = NEAR::CLI.new(network: NEAR.testnet)

fund_account = NEAR::Account.parse(ARGV.shift || "v2.faucet.nonofficial.testnet")
temp_account = NEAR::Account.temp

warn "Creating account #{temp_account}..."
testnet.create_account_with_faucet(temp_account)

sleep 3

warn "Deleting account #{temp_account}..."
testnet.delete_account(temp_account, beneficiary: fund_account)
10 changes: 10 additions & 0 deletions lib/near/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,20 @@
#
# @see https://nomicon.io/DataStructures/Account
class NEAR::Account
##
# @param [String, #to_s] id
# @return [NEAR::Account]
def self.parse(id)
self.new(id.to_s)
end

##
# @return [NEAR::Account]
def self.temp
timestamp = (Time.now.to_f * 1_000).to_i
self.new("temp-#{timestamp}.testnet")
end

##
# @param [String, #to_s] id
def initialize(id)
Expand Down
31 changes: 21 additions & 10 deletions lib/near/cli/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,20 @@ def import_account_with_private_key(private_key)
##
# Creates a new account sponsored by the faucet service.
#
# @param [String] new_account_id
# @param [String] public_key
# @param [NEAR::Account, #to_s] new_account
# @param [String, nil] public_key
# @return [String]
def create_account_with_faucet(new_account_id, public_key)
def create_account_with_faucet(new_account, public_key: nil)
stdout, stderr = execute(
'account',
'create-account',
'sponsor-by-faucet-service', new_account_id,
'use-manually-provided-public-key', public_key,
'sponsor-by-faucet-service', new_account.to_s,
*case public_key
when nil then ['autogenerate-new-keypair', 'save-to-keychain']
when String then ['use-manually-provided-public-key', public_key]
when Array then public_key
else raise ArgumentError
end,
'network-config', @network,
'create'
)
Expand All @@ -88,16 +93,22 @@ def create_account_with_faucet(new_account_id, public_key)
##
# Creates a new account funded by another account.
#
# @param [NEAR::Account] new_account
# @param [NEAR::Account] signer Account that signs & funds the transaction
# @param [NEAR::Balance] deposit Amount of NEAR to attach
# @param [NEAR::Account, #to_s] new_account
# @param [NEAR::Account, #to_s] signer Account that signs & funds the transaction
# @param [String, nil] public_key
# @param [NEAR::Balance, #to_s] deposit Amount of NEAR to attach
# @return [String]
def create_account_with_funding(new_account, signer:, deposit: nil)
def create_account_with_funding(new_account, signer:, public_key: nil, deposit: nil)
stdout, stderr = execute(
'account',
'create-account',
'fund-myself', new_account.to_s, (deposit ? deposit.to_s : '0') + ' NEAR',
'autogenerate-new-keypair', 'save-to-keychain',
*case public_key
when nil then ['autogenerate-new-keypair', 'save-to-keychain']
when String then ['use-manually-provided-public-key', public_key]
when Array then public_key
else raise ArgumentError
end,
'sign-as', signer.to_s,
'network-config', @network,
'sign-with-keychain',
Expand Down

0 comments on commit 6d3eb73

Please sign in to comment.