Skip to content

Commit

Permalink
cleanup transaction fees code
Browse files Browse the repository at this point in the history
  • Loading branch information
jdogresorg committed Apr 26, 2024
1 parent 3c8ecab commit 0b8483c
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 128 deletions.
68 changes: 14 additions & 54 deletions indexer/includes/actions/airdrop.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
********************************************************************/
function btnsAirdrop($params=null, $data=null, $error=null){
global $mysqli, $reparse, $addresses, $tickers;
global $mysqli, $reparse, $addresses, $tickers, $credits, $debits;

// Define list of known FORMATS
$formats = array(
Expand Down Expand Up @@ -89,22 +89,16 @@ function btnsAirdrop($params=null, $data=null, $error=null){
// Get source address balances
$balances = getAddressBalances($data->SOURCE, null, $data->BLOCK_INDEX, $data->TX_INDEX);

// Get SOURCE address preferences
$preferences = getAddressPreferences($data->SOURCE, $data->BLOCK_INDEX, $data->TX_INDEX);
// Create the fees object
$fees = createFeesObject($data);

// Store original error value
$origError = $error;

// Array of credits and debits
// reset credits and debits arrays
$credits = [];
$debits = [];

// Copy base BTNS transaction data object into fees object
$fees = clone($data);
$fees->TICK = 'GAS';
$fees->AMOUNT = 0;
$fees->METHOD = ($preferences->FEE_PREFERENCE==1) ? 1 : 2; // 1=Destroy, 2=Donate

// Loop through airdrops and process each
foreach($airdrops as $info){

Expand Down Expand Up @@ -197,8 +191,7 @@ function btnsAirdrop($params=null, $data=null, $error=null){
$db_hits += 4; // 1 debits, 1 balances, 1 airdrops

// Determine total transaction FEE based on database hits
$airdrop->FEE_TICK = 'GAS';
$airdrop->FEE_AMOUNT = getTransactionFee($db_hits, $airdrop->FEE_TICK);
$fees->AMOUNT = getTransactionFee($db_hits, $fees->TICK);

// Verify SOURCE has enough balances to cover TICK total DEBIT amount
if(!$error && !hasBalance($balances, $airdrop->TICK, $airdrop->DEBIT))
Expand All @@ -209,12 +202,12 @@ function btnsAirdrop($params=null, $data=null, $error=null){
$balances = debitBalances($balances, $airdrop->TICK, $airdrop->DEBIT);

// Verify SOURCE has enough balances to cover FEE AMOUNT
if(!$error && !hasBalance($balances, $airdrop->FEE_TICK, $airdrop->FEE_AMOUNT))
if(!$error && !hasBalance($balances, $fees->TICK, $fees->AMOUNT))
$error = 'invalid: insufficient funds (FEE)';

// Adjust balances to reduce by FEE amount
if(!$error)
$balances = debitBalances($balances, $airdrop->TICK, $airdrop->DEBIT);
$balances = debitBalances($balances, $fees->TICK, $fees->AMOUNT);

// Determine final status
$airdrop->STATUS = $status = ($error) ? $error : 'valid';
Expand All @@ -229,33 +222,13 @@ function btnsAirdrop($params=null, $data=null, $error=null){
if($status=='valid'){

// Store the SOURCE and TICK in addresses list
addAddressTicker($airdrop->SOURCE, [$airdrop->TICK, $airdrop->FEE_TICK]);

// Debit SOURCE with total DEBIT and FEE_AMOUNT
array_push($debits, array($airdrop->TICK, $airdrop->DEBIT));
array_push($debits, array($airdrop->FEE_TICK, $airdrop->FEE_AMOUNT));

// Update FEES object with to AMOUNT
$fees->AMOUNT = bcadd($fees->AMOUNT, $airdrop->FEE_AMOUNT, 8);

// Handle using FEE according the the users ADDRESS preferences
if($preferences->FEE_PREFERENCE>1){

// Determine what address to donate to
$address = ($preferences->FEE_PREFERENCE==2) ? DONATE_ADDRESS_1 : DONATE_ADDRESS_2;
addAddressTicker($airdrop->SOURCE, [$airdrop->TICK, $fees->TICK]);

// Update the $fees object with the destination address
$fees->DESTINATION = $address;
// Debit SOURCE with total DEBIT
array_push($debits, array($airdrop->TICK, $airdrop->DEBIT));

// Store the donation ADDRESS and TICK in addresses list
addAddressTicker($address, $airdrop->FEE_TICK);

// Credit donation address with FEE_AMOUNT
array_push($credits, array($airdrop->FEE_TICK, $airdrop->FEE_AMOUNT, $address));
}

// Create record of FEE in `fees` table
createFeeRecord($fees);
// Handle any transaction FEE according the users's ADDRESS preferences
processTransactionFees('AIRDROP', $fees);

// Loop through recipient addresses
foreach($recipients as $address){
Expand All @@ -269,21 +242,8 @@ function btnsAirdrop($params=null, $data=null, $error=null){
}
}

// Consolidate the credit and debit records to write as few records as possible
$debits = consolidateCreditDebitRecords('debits', $debits);
$credits = consolidateCreditDebitRecords('credits', $credits);

// Create records in debits table
foreach($debits as $debit){
[$tick, $amount] = $debit;
createDebit('AIRDROP', $data->BLOCK_INDEX, $data->TX_HASH, $tick, $amount, $data->SOURCE);
}

// Create records in credits table
foreach($credits as $credit){
[$tick, $amount, $destination] = $credit;
createCredit('AIRDROP', $data->BLOCK_INDEX, $data->TX_HASH, $tick, $amount, $destination);
}
// Process any transaction credit/debit records
processTransactionCreditsDebits('AIRDROP', $data);

// If this is a reparse, bail out before updating balances
if($reparse)
Expand Down
43 changes: 7 additions & 36 deletions indexer/includes/actions/dividend.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,8 @@ function btnsDividend($params=null, $data=null, $error=null){
// Clone the raw data for storage in dividends table
$dividend = clone($data);

// Get SOURCE address preferences
$preferences = getAddressPreferences($data->SOURCE, $data->BLOCK_INDEX, $data->TX_INDEX);

// Copy base BTNS transaction data object into fees object
$fees = clone($data);
$fees->TICK = 'GAS';
$fees->AMOUNT = 0;
$fees->METHOD = ($preferences->FEE_PREFERENCE==1) ? 1 : 2; // 1=Destroy, 2=Donate
// Create the fees object
$fees = createFeesObject($data);

// Validate TICK exists
if(!$error && !$btInfo)
Expand Down Expand Up @@ -130,16 +124,15 @@ function btnsDividend($params=null, $data=null, $error=null){
$db_hits += 4; // 1 debits, 1 balances, 1 dividend

// Determine total transaction FEE based on database hits
$data->FEE_TICK = 'GAS';
$data->FEE_AMOUNT = getTransactionFee($db_hits, $data->FEE_TICK);
$fees->AMOUNT = getTransactionFee($db_hits, $fees->TICK);

// Verify SOURCE has enough balances to cover FEE AMOUNT
if(!$error && !hasBalance($balances, $data->FEE_TICK, $data->FEE_AMOUNT))
if(!$error && !hasBalance($balances, $fees->TICK, $fees->AMOUNT))
$error = 'invalid: insufficient funds (FEE)';

// Adjust balances to reduce by FEE amount
if(!$error)
$balances = debitBalances($balances, $data->FEE_TICK, $data->FEE_AMOUNT);
$balances = debitBalances($balances, $fees->TICK, $fees->AMOUNT);

// Determine final status
$data->STATUS = $dividend->STATUS = $status = ($error) ? $error : 'valid';
Expand All @@ -159,8 +152,8 @@ function btnsDividend($params=null, $data=null, $error=null){
// Debit DIVIDEND_TOTAL from SOURCE address
createDebit('DIVIDEND', $data->BLOCK_INDEX, $data->TX_HASH, $data->DIVIDEND_TICK, $data->DIVIDEND_TOTAL, $data->SOURCE);

// Debit FEE_AMOUNT from SOURCE address
createDebit('DIVIDEND', $data->BLOCK_INDEX, $data->TX_HASH, $data->FEE_TICK, $data->FEE_AMOUNT, $data->SOURCE);
// Handle any transaction FEE according the users's ADDRESS preferences
processTransactionFees('DIVIDEND', $fees);

// Loop through TICK holders and credit them with DIVIDEND_TICK amount
foreach($holders as $address => $amount){
Expand All @@ -172,28 +165,6 @@ function btnsDividend($params=null, $data=null, $error=null){
// Store the recipient ADDRESS and TICK in addresses list
addAddressTicker($address, $data->DIVIDEND_TICK);
}

// Update FEES object with to AMOUNT
$fees->AMOUNT = bcadd($fees->AMOUNT, $data->FEE_AMOUNT, 8);

// Handle using FEE according the the users ADDRESS preferences
if($preferences->FEE_PREFERENCE>1){

// Determine what address to donate to
$address = ($preferences->FEE_PREFERENCE==2) ? DONATE_ADDRESS_1 : DONATE_ADDRESS_2;

// Update the $fees object with the destination address
$fees->DESTINATION = $address;

// Store the donation ADDRESS and TICK in addresses list
addAddressTicker($address, $data->FEE_TICK);

// Credit donation address with FEE_AMOUNT
createCredit('DIVIDEND', $data->BLOCK_INDEX, $data->TX_HASH, $data->FEE_TICK, $data->FEE_AMOUNT, $fees->DESTINATION);
}

// Create record of FEE in `fees` table
createFeeRecord($fees);
}

// If this is a reparse, bail out before updating balances
Expand Down
47 changes: 10 additions & 37 deletions indexer/includes/actions/sweep.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,14 @@ function btnsSweep($params=null, $data=null, $error=null){
if(!$error)
$data = setActionParams($data, $params, $formats[$format]);

// Get SOURCE address preferences, balances, and ownership information
$preferences = getAddressPreferences($data->SOURCE, $data->BLOCK_INDEX, $data->TX_INDEX);
// Get SOURCE address balances and ownership information
$balances = getAddressBalances($data->SOURCE, null, $data->BLOCK_INDEX, $data->TX_INDEX);
$ownerships = getAddressOwnership($data->SOURCE, null, $data->BLOCK_INDEX, $data->TX_INDEX);

// Copy base BTNS transaction data object into fees object
$fees = clone($data);
$fees->TICK = 'GAS';
$fees->AMOUNT = 0;
$fees->METHOD = ($preferences->FEE_PREFERENCE==1) ? 1 : 2; // 1=Destroy, 2=Donate
// Create the fees object
$fees = createFeesObject($data);

/*****************************************************************
/*****************************************************************
* FORMAT Validations
****************************************************************/

Expand Down Expand Up @@ -87,16 +83,15 @@ function btnsSweep($params=null, $data=null, $error=null){
$db_hits += ($data->OWNERSHIP) ? count($ownerships) : 0; // 1 issues

// Determine total transaction FEE based on database hits
$data->FEE_TICK = 'GAS';
$data->FEE_AMOUNT = getTransactionFee($db_hits, $data->FEE_TICK);
$fees->AMOUNT = getTransactionFee($db_hits, $fees->TICK);

// Verify SOURCE has enough balances to cover FEE AMOUNT
if(!$error && !hasBalance($balances, $data->FEE_TICK, $data->FEE_AMOUNT))
if(!$error && !hasBalance($balances, $fees->TICK, $fees->AMOUNT))
$error = 'invalid: insufficient funds (FEE)';

// Adjust balances to reduce by FEE amount
if(!$error)
$balances = debitBalances($balances, $data->FEE_TICK, $data->FEE_AMOUNT);
$balances = debitBalances($balances, $fees->TICK, $fees->AMOUNT);

// Determine final status
$data->STATUS = $sweep->STATUS = $status = ($error) ? $error : 'valid';
Expand All @@ -111,10 +106,10 @@ function btnsSweep($params=null, $data=null, $error=null){
if($status=='valid'){

// Store the SOURCE and FEE_TICK in addresses and tickers arrays
addAddressTicker($data->SOURCE, $data->FEE_TICK);
addAddressTicker($data->SOURCE, $fees->TICK);

// Debit FEE_AMOUNT from SOURCE address
createDebit('SWEEP', $data->BLOCK_INDEX, $data->TX_HASH, $data->FEE_TICK, $data->FEE_AMOUNT, $data->SOURCE);
// Handle any transaction FEE according the users's ADDRESS preferences
processTransactionFees('SWEEP', $fees);

// Transfer Balances
if($data->BALANCES==1){
Expand Down Expand Up @@ -147,28 +142,6 @@ function btnsSweep($params=null, $data=null, $error=null){
}
}

// Update FEES object with to AMOUNT
$fees->AMOUNT = bcadd($fees->AMOUNT, $data->FEE_AMOUNT, 8);

// Handle using FEE according the the users ADDRESS preferences
if($preferences->FEE_PREFERENCE>1){

// Determine what address to donate to
$address = ($preferences->FEE_PREFERENCE==2) ? DONATE_ADDRESS_1 : DONATE_ADDRESS_2;

// Update the $fees object with the destination address
$fees->DESTINATION = $address;

// Store the donation ADDRESS and TICK in addresses list
addAddressTicker($address, $data->FEE_TICK);

// Credit donation address with FEE_AMOUNT
createCredit('SWEEP', $data->BLOCK_INDEX, $data->TX_HASH, $data->FEE_TICK, $data->FEE_AMOUNT, $fees->DESTINATION);
}

// Create record of FEE in `fees` table
createFeeRecord($fees);

// If this is a reparse, bail out before updating balances and token information
if($reparse)
return;
Expand Down
59 changes: 58 additions & 1 deletion indexer/includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2587,6 +2587,63 @@ function getTicker( $tick_id=null ){
}
}


// Create the basic $fees object used to calculate platform transaction fees
function createFeesObject($data=null){
// Get SOURCE address preferences
$preferences = getAddressPreferences($data->SOURCE, $data->BLOCK_INDEX, $data->TX_INDEX);
// Copy base BTNS transaction data object into fees object
$fees = clone($data);
$fees->TICK = 'GAS';
$fees->AMOUNT = 0;
$fees->METHOD = ($preferences->FEE_PREFERENCE==1) ? 1 : 2; // 1=Destroy, 2=Donate
return $fees;
}

// Process any transaction FEE according the user's ADDRESS preferences
function processTransactionFees($action=null, $fees=null){
global $credits, $debits;
$immediate = true;
if(in_array($action,array('AIRDROP')))
$immediate = false;
// Debit FEE from SOURCE
if($immediate){
createDebit($action, $fees->BLOCK_INDEX, $fees->TX_HASH, $fees->TICK, $fees->AMOUNT, $fees->SOURCE);
} else {
array_push($debits, array($fees->TICK, $fees->AMOUNT));
}
// Handle using FEE according the the users ADDRESS preferences
if($fees->METHOD>1){
// Determine what address to donate to
$fees->DESTINATION = ($fees->METHOD==2) ? DONATE_ADDRESS_1 : DONATE_ADDRESS_2;
// Store the donation ADDRESS and TICK in addresses list
addAddressTicker($fees->DESTINATION, $fees->TICK);
// Credit donation address with FEE
if($immediate){
createCredit($action, $fees->BLOCK_INDEX, $fees->TX_HASH, $fees->TICK, $fees->AMOUNT, $fees->DESTINATION);
} else {
array_push($credits, array($fees->TICK, $fees->AMOUNT, $fees->DESTINATION));
}
}
// Create record of FEE in `fees` table
createFeeRecord($fees);
}

// Process any transaction credit/debit records
function processTransactionCreditsDebits($action=null, $data=null){
global $credits, $debits;
// Consolidate the credit and debit records to write as few records as possible
$debits = consolidateCreditDebitRecords('debits', $debits);
$credits = consolidateCreditDebitRecords('credits', $credits);
// Create records in debits table
foreach($debits as $debit){
[$tick, $amount] = $debit;
createDebit($action, $data->BLOCK_INDEX, $data->TX_HASH, $tick, $amount, $data->SOURCE);
}
// Create records in credits table
foreach($credits as $credit){
[$tick, $amount, $destination] = $credit;
createCredit($action, $data->BLOCK_INDEX, $data->TX_HASH, $tick, $amount, $destination);
}
}

?>
3 changes: 3 additions & 0 deletions indexer/indexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@
// Define global assoc arrays to track address/ticker changes
$addresses = [];
$tickers = [];
// Define global assoc arrays to track debits/credits changes
$credits = [];
$debits = [];

// Handle rollbacks
if($rollback)
Expand Down

0 comments on commit 0b8483c

Please sign in to comment.