Bitcoin and Altcoin Wallets 6.0.0-RC6

Wallet extends Post_Type

Wallet class

Represents a wallet. A wallet:

  • has one or more currencies
  • encapsulates connection/authentication settings
  • contains a wallet adapter

Use the wallet to read/write the connection settings, or communicate with the hot wallet directly via its wallet adapter.

Iterate all wallets, find one, enable it, then save it:

foreach ( \DSWallets\get_wallets() as $wallet ) {
	error_log( "id: $wallet->post_id name: $wallet->name \n" );

	if ( false !== strpos( strtolower( $wallet->name ), 'bitcoin' ) ) {
		error_log( "This wallet provides the Bitcoin currency, according to its title");

		if ( ! $wallet->is_eabled ) {
			// the wallet adapter is not enabled, we're enabling it now
			$wallet->is_enabled = true;

			try {
				// here we save the state to DB, so that the wallet remains enabled

				$wallet->save();

			} catch ( \Exception $e ) {
				error_log( "Could not enable Bitcoin wallet with id $wallet->post_id, due to: " . $e->getMessage() );
			}
		}
	}
}

Get the hot balance for a currency's wallet.

$bitcoin = \DSWallets\get_first_currency_by_symbol( 'BTC' );

$wallet = $bitcoin->wallet;

if ( $wallet->is_enabled && $wallet->adapter ) {
	error_log( "This wallet is marked enabled and has an instantiated wallet adapter" );

	// NOTE: We don't strictly need to pass a currency argument if the wallet only has one currency.
	// For multicurrency wallets we always pass a currency into `get_hot_balance()`.
	// It's safest if we always pass the currency.
		$hot_balance = $wallet->adapter->get_hot_balance( $bitcoin );

	// The balance we get is that of the connected wallet. It is an integer.
	// We convert it to a float by dividing by 10^8 to get satoshis:
	$hot_balance_float = $hot_balance * 10 ** - $bitcoin->decimals;

	// Finally we make a string using the currency's pattern.
	error_log(
		sprintf(
			"The hot wallet balance for our Bitcoin wallet is $bitcoin->pattern",
			$hot_balance_float
		)
	);
	} else {
		error_log( "Wallet is not connected or enabled" );
	}

For more information about wallet adapters, see the \DSWallets\Wallet_Adapter class.

Tags
see
Wallet_Adapter
since
6.0.0

Introduced.

author

alexg

Table of Contents

$post_id  : int
The id of the post that this object represents.
$adapter  : Wallet_Adapter|null
Wallet Adapter instance for this wallet.
$adapter_settings  : array<string, mixed>
Wallet adapter settings.
$is_enabled  : bool
Wallet adapter status.
$name  : string
Wallet name.
__get()  : mixed
Allows getting one of this object's fields.
__set()  : mixed
Sets a field of this Wallet object.
__toString()  : string
To String.
delete()  : mixed
Trashes this object in the DB.
load()  : Wallet
Load a Wallet from its custom post entry.
save()  : void
Saves this object to the DB.
render_bool_field()  : mixed
render_boolean_field()  : mixed
render_number_field()  : mixed
render_secret_field()  : mixed
render_select_field()  : mixed
render_string_field()  : mixed
render_strings_field()  : mixed

Properties

$adapter

Wallet Adapter instance for this wallet.

private Wallet_Adapter|null $adapter = null

The wallet adapter is the middleware between the plugin and your wallet backend.

Tags
see
Wallet_Adapter

$adapter_settings

Wallet adapter settings.

private array<string, mixed> $adapter_settings = []

Assoc array of adapter setting names to actual values.

These are injected into the coin adapter when the wallet is instantiated.

Tags
see

$this->adapter

$is_enabled

Wallet adapter status.

private bool $is_enabled = false

Wallet can be enabled/disabled. When wallet is disabled, its post_status is draft. When enabled, it's publish.

$name

Wallet name.

private string $name

Free text string, stored on the post_title column in the DB.

Methods

__get()

Allows getting one of this object's fields.

public __get(mixed $name) : mixed
Parameters
$name : mixed

The name of the field to retrieve.

Return values
mixed

__set()

Sets a field of this Wallet object.

public __set(string $name, mixed $value) : mixed
Parameters
$name : string

Can be: post_id, name, adapter, is_enabled, adapter_settings.

$value : mixed

The value to set to the field.

Tags
see
Post_Type::__set()
throws
InvalidArgumentException

If value is not appropriate for field or if field does not exist.

Return values
mixed

delete()

Trashes this object in the DB.

public delete([mixed $force = false ]) : mixed
Parameters
$force : mixed = false
Tags
throws
Exception

If the object was not on the DB or if it could not be trashed.

Return values
mixed

load()

Load a Wallet from its custom post entry.

public static load(int $post_id) : Wallet
Parameters
$post_id : int

The post_id for the object to load.

Tags
inheritdoc
see
Post_Type::load()
throws
InvalidArgumentException

If the post id is not valid.

Return values
Wallet

save()

Saves this object to the DB.

public save() : void

If the object already had a post_id, then the post is updated. Else, a new custom post is created and the new post_id is assigned to the object.

Return values
void

render_bool_field()

protected static render_bool_field(mixed $schema, mixed $value) : mixed
Parameters
$schema : mixed
$value : mixed
Return values
mixed

render_boolean_field()

protected static render_boolean_field(mixed $schema, mixed $value) : mixed
Parameters
$schema : mixed
$value : mixed
Return values
mixed

render_number_field()

protected static render_number_field(mixed $schema, mixed $value) : mixed
Parameters
$schema : mixed
$value : mixed
Return values
mixed

render_secret_field()

protected static render_secret_field(mixed $schema, mixed $value) : mixed
Parameters
$schema : mixed
$value : mixed
Return values
mixed

render_select_field()

protected static render_select_field(mixed $schema, mixed $value) : mixed
Parameters
$schema : mixed
$value : mixed
Return values
mixed

render_string_field()

protected static render_string_field(mixed $schema, mixed $value) : mixed
Parameters
$schema : mixed
$value : mixed
Return values
mixed

render_strings_field()

protected static render_strings_field(mixed $schema, mixed $value) : mixed
Parameters
$schema : mixed
$value : mixed
Return values
mixed

Search results