/** * Create the settings page. */ public function create_settings_page() { $template = new Template(__DIR__ . "/../template/settings.tpl.php"); if (isset($_REQUEST["settings-updated"]) && $_REQUEST["settings-updated"]) { if (CryptoAccountsPlugin::instance()->isSetup()) { $wallet = CryptoAccountsPlugin::instance()->getWallet(); try { $info = $wallet->setup(); if ($info) { $template->set("message", $info); } } catch (Exception $e) { $template->set("error", "Error initializing wallet: " . $e->getMessage()); } } } $tab = "setup"; if (isset($_REQUEST["tab"])) { $tab = $_REQUEST["tab"]; } if ($tab == "withdraw" && $_REQUEST["transactionIds"]) { $wallet = CryptoAccountsPlugin::instance()->getWallet(); $wallet->setPassword($_REQUEST["password"]); try { foreach ($_REQUEST["transactionIds"] as $transactionId) { $transaction = Transaction::findOne($transactionId); $transaction->performWithdraw(); } $template->set("message", "Transactions completed."); } catch (Exception $e) { $template->set("error", $e->getMessage()); } } if ($tab == "withdraw") { $denomination = "btc"; $totalAmount = 0; $transactionViews = array(); $transactions = Transaction::findAllBy("state", Transaction::SCHEDULED); foreach ($transactions as $transaction) { $user = $transaction->getFromAccount()->getUser(); $transactionView = array("id" => $transaction->id, "when" => human_time_diff(time(), $transaction->timestamp) . " ago", "user" => $user->display_name . " (" . $user->user_email . ")", "amount" => $transaction->getAmount($denomination) . " " . $denomination); $transactionViews[] = $transactionView; $totalAmount += $transaction->getAmount($denomination); } $wallet = CryptoAccountsPlugin::instance()->getWallet(); $template->set("passwordLabel", $wallet->getPasswordLabel()); $template->set("transactions", $transactionViews); } $template->set("tab", $tab); $template->set("totalAmount", $totalAmount . " " . $denomination); $template->show(); }
function bca_make_transaction($denomination, $fromAccount, $toAccount, $amount, $options = array()) { $t = new Transaction(); $t->setFromAccount($fromAccount); $t->setToAccount($toAccount); $t->setAmount($denomination, $amount); if (isset($options["notice"])) { $t->setNotice($options["notice"]); } if (isset($options["confirming"])) { $t->setUseConfirming($options["confirming"]); } $t->perform(); return $t->id; }
/** * Make sure a new transaction is created if no one exists. */ public function testTransactionCreated() { $user_id = $this->factory->user->create(); $account = bca_user_account($user_id); $address = $account->getDepositAddress(); $this->assertTrue(strlen($address) > 4); $data = array("type" => "address", "data" => array("address" => $address, "txid" => "12345678", "balance_change" => "0.05000000", "confirmations" => 0)); BlockIoController::instance()->process($data); $transaction = Transaction::findOneBy("transactionHash", "12345678"); $this->assertEquals(0.05, $transaction->getAmount("btc")); $this->assertEquals($transaction->getState(), Transaction::CONFIRMING); $account = bca_user_account($user_id); $this->assertEquals(0, $account->getBalance("btc")); $this->assertEquals(0.05, $account->getConfirmingBalance("btc")); $data = array("type" => "address", "data" => array("address" => $address, "txid" => "12345678", "balance_change" => "0.05000000", "confirmations" => "3")); BlockIoController::instance()->process($data); $account = bca_user_account($user_id); $this->assertEquals(0.05, $account->getBalance("btc")); }
/** * Process incomming request. */ public function process($payload) { if ($payload["type"] != "address") { return; } $data = $payload["data"]; if (!$data["txid"]) { throw new Exception("No transaction id"); } if (!$data["address"]) { throw new Exception("No address"); } if (!$data["balance_change"]) { throw new Exception("No amount data"); } $transaction = Transaction::findOneBy("transactionHash", $data["txid"]); if (!$transaction) { $account = Account::findOneBy("depositAddress", $data["address"]); if (!$account) { throw new Exception("No matching account."); } $transaction = new Transaction(); $transaction->notice = "Deposit"; $transaction->transactionHash = $data["txid"]; $transaction->toAccountId = $account->id; $transaction->state = Transaction::CONFIRMING; $transaction->amount = BitcoinUtil::toSatoshi("btc", $data["balance_change"]); $transaction->save(); $account->getPubSub()->publish(); } if ($transaction->getState() == Transaction::COMPLETE) { return; } $transaction->confirmations = intval($data["confirmations"]); $account = $transaction->getToAccount(); if (!$account) { throw new Exception("unable to find account"); } if ($transaction->confirmations >= get_option("blockchainaccounts_notifications")) { $account->balance += $transaction->amount; $account->save(); $transaction->toAccountBalance = $account->balance; $transaction->timestamp = time(); $transaction->state = Transaction::COMPLETE; $transaction->save(); } $transaction->save(); $account->getPubSub()->publish(); }
/** * Uninstall. */ public static function uninstall() { Account::uninstall(); Transaction::uninstall(); }
/** * Withdraw funds. * If withdraw processing is manual, the transaction will * be stored as scheduled. If withdraw processing is automatic * the transaction will be performed immediately. */ public function withdraw($denomination, $address, $amount) { if ($amount < 0 || $amount > $this->getBalance($denomination)) { throw new Exception("Insufficient funds on account."); } if ($this->hasConfirming()) { throw new Exception("There are unconfirmed transactions for the account."); } if ($this->entity_type != "user") { throw new Exception("Can only withdraw from user accounts."); } $this->balance -= BitcoinUtil::toSatoshi($denomination, $amount); $t = new Transaction(); $t->withdrawAddress = $address; $t->fromAccountId = $this->id; $t->fromAccountBalance = $this->balance; $t->setAmount($denomination, $amount); $t->state = Transaction::SCHEDULED; $t->notice = "Withdraw"; $t->save(); $this->save(); if (get_option("blockchainaccounts_withdraw_processing") == "auto") { $t->performWithdraw(); } return $t; }
$transaction = Transaction::findOneBy("transactionHash", $_REQUEST["transaction_hash"]); if (!$_REQUEST["input_address"]) { exit("expected input address"); } if (!$_REQUEST["transaction_hash"]) { exit("expected transaction_hash"); } if ($_REQUEST["value"] < 0) { exit("*ok*"); } if (!$transaction) { $account = Account::findOneBy("depositAddress", $_REQUEST["input_address"]); if (!$account) { exit("no associated account or transaction"); } $transaction = new Transaction(); $transaction->notice = "Deposit"; $transaction->transactionHash = $_REQUEST["transaction_hash"]; $transaction->toAccountId = $account->id; $transaction->state = Transaction::CONFIRMING; $transaction->amount = $_REQUEST["value"]; $transaction->save(); } if ($transaction->state == Transaction::COMPLETE) { exit("*ok*"); } $transaction->confirmations = $_REQUEST["confirmations"]; if ($transaction->confirmations >= get_option("blockchainaccounts_notifications")) { $account = Account::findOneBy("id", $transaction->toAccountId); if (!$account) { exit("unable to find account");