Wallet
extends Post_Type
in package
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
Table of Contents
- $object_cache : mixed
- $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.
- from_values() : Wallet
- Factory to construct a wallet in one go from database values.
- load() : Wallet
- Load a Wallet from its custom post entry.
- load_many() : array<string|int, Wallet>
- Retrieve many wallets by their post_ids.
- 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
$object_cache
protected
static mixed
$object_cache
= []
$post_id
The id of the post that this object represents.
protected
int
$post_id
$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
$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
$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
Return values
mixed —__toString()
To String.
public
__toString() : string
Tags
Return values
string —delete()
Trashes this object in the DB.
public
delete([mixed $force = false ]) : mixed
Parameters
- $force : mixed = false
Tags
Return values
mixed —from_values()
Factory to construct a wallet in one go from database values.
public
static from_values(int $post_id, string $post_title, string $post_status, array<string|int, mixed> $postmeta) : Wallet
Parameters
- $post_id : int
-
The ID of the post in the database
- $post_title : string
-
The post's title to be used as wallet name
- $post_status : string
- $postmeta : array<string|int, mixed>
-
Key-value pairs
Return values
Wallet —The constructed instance of the wallet object
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
Return values
Wallet —load_many()
Retrieve many wallets by their post_ids.
public
static load_many(array<string|int, int> $post_ids[, string|null $unused = null ]) : array<string|int, Wallet>
Any post_ids not found are skipped silently.
Parameters
- $post_ids : array<string|int, int>
-
The post IDs
- $unused : string|null = null
-
Do not use. Ignored.
Tags
Return values
array<string|int, Wallet> —The wallets
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