assets system

This commit is contained in:
admin@arionum.com
2019-04-14 22:55:16 +03:00
parent 7e28ce9246
commit 1baba2ab3a
8 changed files with 686 additions and 40 deletions

View File

@@ -10,7 +10,7 @@ class Transaction
global $db;
$acc = new Account();
$r = $db->run("SELECT * FROM transactions WHERE block=:block ORDER by `version` ASC", [":block" => $block]);
$r = $db->run("SELECT * FROM transactions WHERE block=:block ORDER by `version` DESC", [":block" => $block]);
foreach ($r as $x) {
_log("Reversing transaction $x[id]", 4);
if (empty($x['src'])) {
@@ -29,19 +29,19 @@ class Transaction
} else {
// other type of transactions
if ($x['version']!=100&&$x['version']<111) {
if ($x['version']!=100 && $x['version']<111 && $x['version'] != 54 && $x['version'] != 57 && $x['version'] != 58 ) {
$rez=$db->run(
"UPDATE accounts SET balance=balance-:val WHERE id=:id",
[":id" => $x['dst'], ":val" => $x['val']]
);
if ($rez!=1) {
_log("Update accounts balance minus failed", 3);
_log("Update accounts balance minus failed ", 3);
return false;
}
}
}
// on version 0 / reward transaction, don't credit anyone
if ($x['version'] > 0 && $x['version']<111) {
if ($x['version'] > 0 && $x['version']<111 && $x['version'] != 54 && $x['version'] != 57 && $x['version'] != 58) {
$rez=$db->run(
"UPDATE accounts SET balance=balance+:val WHERE id=:id",
[":id" => $x['src'], ":val" => $x['val'] + $x['fee']]
@@ -121,8 +121,80 @@ class Transaction
}
}
// asset transactions
if($x['version']==50){
$db->run("DELETE FROM assets WHERE id=:id",[":id"=>$x['src']]);
$db->run("DELETE FROM assets_balance WHERE asset=:id",[":id"=>$x['src']]);
} elseif ($x['version']==51){
$t=json_decode($x['message'],true);
$db->run("UPDATE assets_balance SET balance=balance-:balance WHERE account=:account and asset=:asset",
[":account"=>$x['dst'], ":asset"=>san($t[0]), ":balance"=>intval($t[1])]);
$db->run("UPDATE assets_balance SET balance=balance+:balance WHERE account=:account and asset=:asset",[":account"=>$x['src'], ":asset"=>san($t[0]), ":balance"=>intval($t[1])]);
} elseif ($x['version']==52){
$t=json_decode($x['message'],true);
if($t[4]=="ask"){
$type="ask";
} else {
$type="bid";
}
if($type=="ask"){
$db->run("UPDATE assets_balance SET balance=balance+:val WHERE account=:account AND asset=:asset", [
":account"=>$x['src'],
":asset"=>san($t[0]),
":val"=>intval($t[2])
]);
} else {
$val=number_format($t[2]*$t[1], 8, '.', '');
$db->run("UPDATE accounts SET balance=balance+:val WHERE id=:id",[":id"=>$x['src'], ":val"=>$val]);
}
$db->run("DELETE FROM assets_market WHERE id=:id",[':id'=>$x['id']]);
} elseif ($x['version']==53){
$order=$db->row("SELECT * FROM assets_market WHERE id=:id AND account=:account AND status=2",[":id"=>san($x['message']), ":account"=>$x['src']]);
if($order)
{
$remaining=$order['val']-$order['val_done'];
if ($remaining>0) {
if ($order['type']=="ask") {
$db->run("UPDATE assets_balance SET balance=balance-:val WHERE account=:account AND asset=:asset", [
":account"=>$x['src'],
":asset"=>san($order['asset']),
":val"=>intval($remaining)
]);
} else {
$val=number_format($order['price']*$remaining, 8, '.', '');
$db->run("UPDATE accounts SET balance=balance-:val WHERE id=:id",[":id"=>$x['src'], ":val"=>$val]);
}
$db->run("UPDATE assets_market SET status=0 WHERE id=:id", [":id"=>san($x['message'])]);
}
}
} elseif ($x['version']==54||$x['version']==57){
//nothing to be done
} elseif ($x['version']==55){
// the message stores the increment value
$plus=intval($x['message']);
$db->run("UPDATE assets_balance SET balance=balance-:plus WHERE account=:account AND asset=:asset", [":account"=>$x['src'], ":asset"=>$x['src'], ":plus"=>$plus]);
} elseif ($x['version']==58){
// the message stores the number of units
$use=intval($x['message']);
// we stored the bid order id in the public key field and the ask in the dst field
$db->run("UPDATE assets_market SET val_done=val_done-:done WHERE id=:id",[":id"=>$x['public_key'], ":done"=>$use]);
$db->run("UPDATE assets_market SET val_done=val_done-:done WHERE id=:id",[":id"=>$x['dst'], ":done"=>$use]);
$bid=$db->row("SELECT * FROM assets_market WHERE id=:id",[':id'=>$x['public_key']]);
$ask=$db->row("SELECT * FROM assets_market WHERE id=:id",[':id'=>$x['dst']]);
$db->run("UPDATE assets_balance SET balance=balance-:balance WHERE account=:account AND asset=:asset",[":account"=>$bid['account'], ":asset"=>$bid['asset'], ":balance"=>$use]);
$aro=$x['val'];
$db->run("UPDATE accounts SET balance=balance-:balance WHERE id=:id",[":balance"=>$aro, ":id"=>$ask['account']]);
}
// add the transactions to mempool
if ($x['version'] > 0 && $x['version']<=110) {
if ($x['version'] > 0 && $x['version']<=110 && $x['version'] != 59 && $x['version'] != 58 && $x['version'] != 57) {
$this->add_mempool($x);
}
$res = $db->run("DELETE FROM transactions WHERE id=:id", [":id" => $x['id']]);
@@ -160,6 +232,7 @@ class Transaction
if (count($r) > 0) {
$i = 0;
$balance = [];
$assets=0;
foreach ($r as $x) {
$trans = [
"id" => $x['id'],
@@ -177,6 +250,15 @@ class Transaction
break;
}
//only a single asset creation per block
if($x['version']==50){
$assets++;
if($assets>1){
continue;
}
}
if (empty($x['public_key'])) {
_log("$x[id] - Transaction has empty public_key");
continue;
@@ -270,9 +352,14 @@ class Transaction
{
global $db;
$acc = new Account();
$acc->add($x['public_key'], $block);
if ($x['version']==1) {
$acc->add_id($x['dst'], $block);
// not a valid or useful public key for internal transactions
if ($x['version']!=58 && $x['version']!=59) {
// add the public key to the accounts table
$acc->add($x['public_key'], $block);
if ($x['version']==1 || $x['version'] == 51) {
// make sure the destination address in on the accounts table as well
$acc->add_id($x['dst'], $block);
}
}
$x['id'] = san($x['id']);
$bind = [
@@ -295,8 +382,16 @@ class Transaction
if ($res != 1) {
return false;
}
// market order side chain
if($x['version']==58){
return true;
}
if ($x['version'] == 2&&$height>=80000) {
$db->run("UPDATE accounts SET balance=balance+:val WHERE alias=:alias", [":alias" => $x['dst'], ":val" => $x['val']]);
} elseif ($x['version']==50||$x['version']==54||$x['version']==57){
// asset creation and dividend distribution
} elseif ($x['version']==100&&$height>=80000) {
//master node deposit
} elseif ($x['version']==103&&$height>=80000) {
@@ -310,8 +405,8 @@ class Transaction
// no debit when the transaction is reward
if ($x['version'] > 0) {
// no debit when the transaction is reward or dividend distribution
if ($x['version'] > 0 && $x['version'] != 54 && $x['version'] != 57) {
$db->run(
"UPDATE accounts SET balance=(balance-:val)-:fee WHERE id=:id",
[":id" => $x['src'], ":val" => $x['val'], ":fee" => $x['fee']]
@@ -344,7 +439,121 @@ class Transaction
}
}
}
// asset system
if($x['version']==50){
// asset creation
$bind=[];
$asset=json_decode($x['message'],true);
$bind[':max_supply']=intval($asset[0]);
$bind[':tradable']=intval($asset[1]);
$bind[':price']=number_format($asset[2], 8, '.', '');
$bind[':dividend_only']=intval($asset[3]);
$bind[':auto_divident']=intval($asset[4]);
$bind[':allow_bid']=intval($asset[5]);
$bind[':height']=$height;
$bind[':id']=$x['src'];
$db->run("INSERT into assets SET id=:id, max_supply=:max_supply, tradable=:tradable, price=:price, dividend_only=:dividend_only, auto_dividend=:auto_divident, height=:height, allow_bid=:allow_bid",$bind);
if($bind[':max_supply']>0){
$db->run("INSERT into assets_balance SET account=:account, asset=:asset, balance=:balance",[":account"=>$x['src'], ":asset"=>$x['src'], ":balance"=>$bind[':max_supply']]);
}
} elseif($x['version']==51){
// send asset
$t=json_decode($x['message'],true);
$db->run("INSERT into assets_balance SET account=:account, asset=:asset, balance=:balance ON DUPLICATE KEY UPDATE balance=balance+:balance2",
[":account"=>$x['dst'], ":asset"=>san($t[0]), ":balance"=>intval($t[1]), ":balance2"=>intval($t[1])]);
$db->run("UPDATE assets_balance SET balance=balance-:balance WHERE account=:account and asset=:asset",[":account"=>$x['src'], ":asset"=>san($t[0]), ":balance"=>intval($t[1])]);
} elseif($x['version']==52){
// market order
$t=json_decode($x['message'],true);
if($t[4]=="ask"){
$type="ask";
} else {
$type="bid";
}
$bind=[":id" => san($x['id']),
":account" => $x['src'],
":asset" => san($t[0]),
":price" => number_format($t[1], 8, '.', ''),
":date" => $x['date'],
":val"=>intval($t[2]),
":type" => $type,
":cancel" => intval($t[3])
];
$db->run("INSERT into assets_market SET id=:id, account=:account, asset=:asset, price=:price, `date`=:date, status=0, `type`=:type, val=:val, val_done=0, cancelable=:cancel",$bind);
if($type=="ask"){
$db->run("UPDATE assets_balance SET balance=balance-:val WHERE account=:account AND asset=:asset", [
":account"=>$x['src'],
":asset"=>san($t[0]),
":val"=>intval($t[2])
]);
} else {
$val=number_format($t[2]*$t[1], 8, '.', '');
$db->run("UPDATE accounts SET balance=balance-:val WHERE id=:id",[":id"=>$x['src'], ":val"=>$val]);
}
} elseif($x['version']==53){
// cancel order
$order=$db->row("SELECT * FROM assets_market WHERE id=:id AND account=:account AND status=0",[":id"=>san($x['message']), ":account"=>$x['src']]);
if($order)
{
$remaining=$order['val']-$order['val_done'];
if ($remaining>0) {
if ($order['type']=="ask") {
$db->run("UPDATE assets_balance SET balance=balance+:val WHERE account=:account AND asset=:asset", [
":account"=>$x['src'],
":asset"=>san($order['asset']),
":val"=>intval($remaining)
]);
} else {
$val=number_format($order['price']*$remaining, 8, '.', '');
$db->run("UPDATE accounts SET balance=balance+:val WHERE id=:id",[":id"=>$x['src'], ":val"=>$val]);
}
$db->run("UPDATE assets_market SET status=2 WHERE id=:id", [":id"=>san($x['message'])]);
}
}
} elseif($x['version']==54||$x['version']==57){
//distribute dividends - only from asset wallet and only to other holders
$r=$db->run("SELECT * FROM assets_balance WHERE asset=:asset AND balance>0 AND account!=:acc",[":asset"=>$x['src'], ":acc"=>$x['src']]);
$total=0;
foreach($r as $g){
$total+=$g['balance'];
}
_log("Asset dividend distribution: $total units",3);
foreach ($r as $g){
$coins=number_format(($g['balance']/$total)*$x['val'], 8, '.', '');
$fee=number_format(($g['balance']/$total)*$x['fee'], 8, '.', '');
$hash = hex2coin(hash("sha512", $x['id'].$g['account']));
_log("Distributing to $g[account] for $g[balance] units - $coins ARO",3);
$new = [
"id" => $hash,
"public_key" => $x['public_key'],
"dst" => $g['account'],
"val" => $coins,
"fee" => $fee,
"signature" => $x['signature'],
"version" => 59,
"date" => $x['date'],
"src" => $x['src'],
"message" => '',
];
$res=$this->add($block,$height,$new);
if(!$res) return false;
}
} elseif($x['version']==55){
// increase max supply
$plus=intval($x['message']);
$db->run("INSERT into assets_balance SET balance=:plus, account=:account, asset=:asset ON DUPLICATE KEY UPDATE balance=balance+:plus2", [":account"=>$x['src'], ":asset"=>$x['src'], ":plus"=>$plus, ":plus2"=>$plus]);
}
$db->run("DELETE FROM mempool WHERE id=:id", [":id" => $x['id']]);
@@ -362,6 +571,7 @@ class Transaction
// check the transaction for validity
public function check($x, $height = 0)
{
global $db;
// if no specific block, use current
if ($height === 0) {
$block = new Block();
@@ -371,13 +581,15 @@ class Transaction
$acc = new Account();
$info = $x['val']."-".$x['fee']."-".$x['dst']."-".$x['message']."-".$x['version']."-".$x['public_key']."-".$x['date'];
$src = $acc->get_address($x['public_key']);
// hard fork at 80000 to implement alias, new mining system, assets
// if($x['version']>1 && $height<80000){
// return false;
// }
// internal transactions
if ($x['version']>110) {
if ($x['version']>110 || $x['version'] == 57 || $x['version'] == 58 || $x['version'] == 59) {
return false;
}
@@ -411,6 +623,11 @@ class Transaction
_log("The account already has an alias", 3);
return false;
}
if($x['dst']!=$src){
// just to prevent some bypasses in the future
_log("DST must be SRC for this transaction", 3);
return false;
}
}
//masternode transactions
@@ -436,7 +653,12 @@ class Transaction
return false;
} elseif ($x['version']!=100) {
$mn=$acc->get_masternode($x['public_key']);
if($x['dst']!=$src){
// just to prevent some bypasses in the future
_log("DST must be SRC for this transaction", 3);
return false;
}
if (!$mn) {
_log("The masternode does not exist", 3);
return false;
@@ -461,7 +683,211 @@ class Transaction
}
}
}
// assets
if ($x['version']==50) {
// asset creation
// fixed asset price 100 +. The 100 are burned and not distributed to miners.
if ($x['val']!=100) {
_log("The asset creation transaction is not 100", 3);
return false;
}
// stored in message in json format - [max supply, tradable, fixed price, dividend only, autodividend]
$asset=json_decode($x['message'],true);
if($asset==false){
_log("Invalid asset creation json", 3);
return false;
}
// minimum 0 (for inflatable assets) and maximum 1.000.000.000
if($asset[0]>1000000000||$asset[0]<0||intval($asset[0])!=$asset[0]){
_log("Invalid asset max supply", 3);
return false;
}
// 0 for non-tradable, 1 for tradable on the blockchain market
if($asset[1]!==1&&$asset[1]!==0){
_log("Invalid asset tradable", 3);
return false;
}
// If the price is set, it cannot be sold by the asset wallet at a dfferent price. Max price 1.000.000 aro
if(number_format($asset[2], 8, '.', '')!=$asset[2]||$asset[2]<0||$asset[2]>1000000){
_log("Invalid asset price", 3);
return false;
}
// 1 to allow only dividend distribution, 0 to allow all transfers
if($asset[3]!==1&&$asset[3]!==0){
_log("Invalid asset dividend setting", 3);
return false;
}
// automatic dividend distribution every 10000 blocks
if($asset[4]!==1&&$asset[4]!==0){
_log("Invalid asset autodividend setting", 3);
return false;
}
// do not allow this asset to buy other assets via the market
if($asset[5]!==1&&$asset[5]!==0){
_log("Invalid asset bid_only setting", 3);
return false;
}
// make sure there is no similar asset with the same alias
$chk=$db->single("SELECT COUNT(1) FROM assets WHERE id=:id",[":id"=>$src]);
if($chk!==0){
_log("The asset already exists", 3);
return false;
}
}
// asset transfer
if($x['version']==51){
// Transfer details in json format, stored in the message. format: [asset id, units]
// The transfer is done to the dst address of the transactions
$asset=json_decode($x['message'],true);
if($asset==false){
_log("Invalid asset creation json", 3);
return false;
}
// check if the asset exists
$blockasset=$db->row("SELECT id, price FROM assets WHERE id=:id",[":id"=>san($asset[0])]);
if(!$blockasset){
_log("Invalid asset", 3);
return false;
}
// minimum 1 unit is transfered
if(intval($asset[1])!=$asset[1]||$asset[1]<1){
_log("Invalid amount", 3);
return false;
}
//make sure the wallet has enough asset units
$balance=$db->single("SELECT balance FROM assets_balance WHERE account=:account AND asset=:asset",[":account"=>$src, ":asset"=>san($asset[0])]);
if($balance<=$asset[1]){
_log("Not enough balance", 3);
return false;
}
if($blockasset['price']>0 && $src == $blockasset['id'] && $blockasset['price']!=$asset[1] && $blockasset['tradable'] ==1 ){
// if the asset has a price defined, check if the asset wallet owns all the asset units and only in this case allow transfers. In such cases, the asset should be sold on market
// on a fixed price always
$chk=$db->single("SELECT COUNT(1) FROM assets_balance WHERE asset=:asset AND account!=:account",[":account"=>$src, ":asset"=>$src]);
if($chk!=0){
_log("Initial asset distribution already done. Market orders only on fixed price.", 3);
return false;
}
}
}
// make sure the dividend only function is not bypassed after height X
if(($x['version']==1||$x['version']==2)&&$height>11111){
$check=$db->single("SELECT COUNT(1) FROM assets WHERE id=:id AND dividend_only=1",[":id"=>$src]);
if($check==1){
_log("This asset wallet cannot send funds directly", 3);
return false;
}
}
// asset market orders
if ($x['version']==52) {
// we store the order details in a json array on the format [asset_id, price, amount of asset units, cancelable, order type ]
$asset=json_decode($x['message'], true);
if ($asset==false) {
_log("Invalid asset creation json", 3);
return false;
}
// only ask and bid allowed
if($asset[4]!="ask"&&$asset[4]!="bid"){
_log("Invalid asset order type", 3);
return false;
}
$type=san($asset[4]);
$blockasset=$db->row("SELECT * FROM assets WHERE id=:id",[":id"=>san($asset[0])]);
if(!$blockasset||$blockasset['tradable']!=1){
_log("Invalid asset", 3);
return false;
}
// the sale price per unit has to be at least 0.00000001 or max 1000000 aro
if(number_format($asset[1], 8, '.', '')!=$asset[1]||$asset[1]<=0||$asset[1]>1000000){
_log("Invalid asset price", 3);
return false;
}
// integer min 1 and max 1000000
if(intval($asset[2])!=$asset[2]||$asset[2]<1||$asset[2]>1000000){
_log("Invalid asset value", 3);
return false;
}
// if the order should be cancelable or not
if($asset[3]!=1&&$asset[3]!=0){
_log("Invalid asset cancel setting", 3);
return false;
}
// the type of order, ask or bid
if($type=="ask"){
$balance=$db->single("SELECT balance FROM assets_balance WHERE asset=:asset AND account=:account",[":account"=>$src, ":asset"=>$asset[0]]);
if($balance<$asset[2]){
_log("Not enough asset balance", 3);
return false;
}
} else {
$balance=$acc->balance($src);
if ($balance<$asset[2]*$asset[1]) {
_log("Not enough aro balance", 3);
return false;
}
if($blockasset['id']!=$src){
$asset_bids_allowed=$db->single("SELECT COUNT(1) FROM assets WHERE id=:id AND allow_bid=0",[":id"=>$src]);
if($asset_bids_allowed==1){
_log("This wallet asset is not allowed to buy other assets", 3);
return false;
}
}
}
if($blockasset['id']==$src && $blockasset['price']>0 && $blockasset['price']!=$asset[1]){
// In case the asset has fixed price, the asset wallet cannot sell on a different price (to prevent abuse by the owner)
_log("This asset has fixed market price when sold by it's wallet", 3);
return false;
}
}
if($x['version']==53){
if(san($x['message'])!=$x['message']){
_log("Invalid order id - $x[message]", 3);
return false;
}
$chk=$db->single("SELECT COUNT(1) FROM assets_market WHERE id=:id AND account=:src AND val_done<val AND status=0 AND cancelable=1",[":id"=>san($x['message']), ":src"=>$src]);
if($chk!=1){
_log("Invalid order - $x[message]", 3);
return false;
}
}
if($x['version']==54){
$balance=$acc->balance($src);
if ($balance<$x['val']||$x['val']<0.00000001) {
_log("Not enough aro balance", 3);
return false;
}
}
if($x['version']==55){
$plus=intval($x['message']);
if($x['message']!=$plus){
_log("Invalid asset value", 3);
return false;
}
$test=$db->single("SELECT COUNT(1) FROM assets WHERE id=:id AND max_supply=0",[":id"=>$src]);
if($test!=1){
_log("Asset not inflatable", 3);
return false;
}
$total=$db->single("SELECT SUM(balance) FROM assets_balance WHERE asset=:id",[":id"=>$src]);
$total+=$db->single("SELECT SUM(val-val_done) FROM assets_market WHERE status=0 AND type='ask' AND asset=:id",[":id"=>$src]);
if($total+$plus>1000000000){
_log("Maximum asset unit limits reached", 3);
return false;
}
}
// max fee after block 10800 is 10
if ($height > 10800 && $fee > 10) {