Skip to content

Commit

Permalink
feat: provide fee_paying_asset params to transactions feature
Browse files Browse the repository at this point in the history
Closes #8
  • Loading branch information
xLogic committed Mar 13, 2019
1 parent bfbd7fd commit 5820c67
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -62,7 +62,7 @@ function getObjects(Array $object_ids);
// get block by block height
function getBlock(Integer $blockHeight);
//send transfer request to entryPoint node
function transfer(String $to, String $memo, String $amount_asset, Boolean $broadcast);
function transfer(String $to, String $memo, String $amount_asset, Boolean $broadcast, String $fee_paying_asset);
//vote for accounts
function vote(Array $accounts, String $fee_paying_asset, Boolean $broadcast);
//broadcast transaction
Expand Down Expand Up @@ -97,11 +97,11 @@ function getAsset(String $symbol);

``` php
// call smart contract method
function callContract(String $contract_name, String $method_name, Object $params, String $amount_asset, Boolean $broadcast);
function callContract(String $contract_name, String $method_name, Object $params, String $amount_asset, Boolean $broadcast, String $fee_paying_asset);
// create smart contract method
function createContract(String $contract_name, String $code, Object $abi, String $vm_type, String $vm_version, Boolean $broadcast);
function createContract(String $contract_name, String $code, Object $abi, String $vm_type, String $vm_version, Boolean $broadcast, String $fee_paying_asset);
// update smart contract method
function updateContract(String $contract_name, String $newOwner, String $code, Object $abi, Boolean $broadcast);
function updateContract(String $contract_name, String $newOwner, String $code, Object $abi, Boolean $broadcast, String $fee_paying_asset);
//get contract table by contract_name
function getContractTable(String $contract_name)
//get contract abi by contract_name
Expand Down
44 changes: 36 additions & 8 deletions src/GXClient.php
Expand Up @@ -281,15 +281,14 @@ function detectTransaction($blockHeight, $callback)
* @param $broadcast
* @return mixed
*/
function transfer($to, $memo, $amount_asset, $broadcast = false)
function transfer($to, $memo, $amount_asset, $broadcast = false, $fee_paying_asset = "GXC")
{
$memo_private = $this->private_key;
$isMemoProvider = false;
// Check PrivateKey isValid
if (!Ecc::isValidPrivate($memo_private)) {
throw new \Exception("Not a Valid PrivateKey");
}

// if memo is function, it can receive fromAccount and toAccount, and should return a full memo object
if (gettype($memo) === "function") {
$isMemoProvider = true;
Expand All @@ -308,13 +307,17 @@ function transfer($to, $memo, $amount_asset, $broadcast = false)

$toAcc = $this->getAccount($to);
$assetInfo = $this->getAsset($asset);
$fee_asset = $this->getAsset($fee_paying_asset);

if (!$toAcc) {
throw new \Exception("Account {$to} not exist");
}
if (!$assetInfo) {
throw new \Exception("Asset {$asset} not exist");
}
if (!$fee_asset) {
throw new \Exception("Asset {$fee_paying_asset} not exist");
}
$amount = [
"amount" => $this->_accMult($amount, pow(10, $assetInfo['precision'])),
"asset_id" => $assetInfo['id']
Expand Down Expand Up @@ -369,7 +372,7 @@ function transfer($to, $memo, $amount_asset, $broadcast = false)
$tr->add_operation($tr->get_type_operation("transfer", [
'fee' => [
'amount' => 0,
'asset_id' => $amount['asset_id']
'asset_id' => $fee_asset['id']
],
'from' => $fromAcc['id'],
'to' => $toAcc['id'],
Expand Down Expand Up @@ -433,11 +436,21 @@ function getTableObjects($contract_name, $table_name, $start = 0, $limit = 100)
* @param $broadcast
* @return mixed
*/
function createContract($contract_name, $code, $abi, $vm_type = "0", $vm_version = "0", $broadcast = false)
function createContract($contract_name, $code, $abi, $vm_type = "0", $vm_version = "0", $broadcast = false, $fee_paying_asset = "GXC")
{
$this->_connect();

$fee_asset = $this->getAsset($fee_paying_asset);
if (!$fee_asset) {
throw new \Exception("Asset {$fee_paying_asset} not exist");
}

$tr = $this->_createTransaction();
$tr->add_operation($tr->get_type_operation("create_contract", [
'fee' => [
'amount' => 0,
'asset_id' => $fee_asset['id']
],
'name' => $contract_name,
'account' => $this->account_id,
'vm_type' => $vm_type,
Expand All @@ -457,15 +470,25 @@ function createContract($contract_name, $code, $abi, $vm_type = "0", $vm_version
* @param $broadcast
* @return mixed
*/
function updateContract($contract_name, $newOwner = null, $code, $abi, $broadcast = false)
function updateContract($contract_name, $newOwner = null, $code, $abi, $broadcast = false, $fee_paying_asset = "GXC")
{
$this->_connect();

$fee_asset = $this->getAsset($fee_paying_asset);
if (!$fee_asset) {
throw new \Exception("Asset {$fee_paying_asset} not exist");
}

$results[0] = $this->getAccount($contract_name);
if ($newOwner) {
$results[1] = $this->getAccount($newOwner);
}
$tr = $this->_createTransaction();
$opt = [
'fee' => [
'amount' => 0,
'asset_id' => $fee_asset['id']
],
'owner' => $this->account_id,
'contract' => $results[0]['id'],
'code' => $code,
Expand All @@ -487,7 +510,7 @@ function updateContract($contract_name, $newOwner = null, $code, $abi, $broadcas
* @param $broadcast {Boolean} - Broadcast the transaction or just return a serialized transaction
* @return mixed
*/
function callContract($contract_name, $method_name, $params, $amount_asset, $broadcast = false)
function callContract($contract_name, $method_name, $params, $amount_asset, $broadcast = false, $fee_paying_asset = "GXC")
{
$this->_connect();
if ($amount_asset) {
Expand All @@ -500,10 +523,15 @@ function callContract($contract_name, $method_name, $params, $amount_asset, $bro

$acc = $this->getAccount($contract_name);
$assetInfo = $this->getAsset($asset);
$fee_asset = $this->getAsset($fee_paying_asset);

if (!$assetInfo) {
throw new \Exception("Asset {$asset} not exist");
}
if (!$fee_asset) {
throw new \Exception("Asset {$fee_paying_asset} not exist");
}

$amount = [
'amount' => $this->_accMult($amount, pow(10, $assetInfo['precision'])),
'asset_id' => $assetInfo['id']
Expand All @@ -519,7 +547,7 @@ function callContract($contract_name, $method_name, $params, $amount_asset, $bro
$opts = [
"fee" => [
"amount" => 0,
"asset_id" => $amount['asset_id']
"asset_id" => $fee_asset['id']
],
"account" => $this->account_id,
"contract_id" => $acc['id'],
Expand Down Expand Up @@ -562,7 +590,7 @@ function vote($accounts, $fee_paying_asset = "GXC", $broadcast = false)
throw new \Exception("account_id {$this->account_id} not exist");
}
if (!$fee_asset) {
throw new \Exception("asset {$fee_paying_asset} not exist");
throw new \Exception("Asset {$fee_paying_asset} not exist");
}
$new_options = [
'memo_key' => $acc['options']['memo_key'],
Expand Down

0 comments on commit 5820c67

Please sign in to comment.