phplaravellaravel-5bitcoinblock-cipher

Getting error when hit blockcypher api laravel


Here is my code:

public static function test(){
    try{
        $apiContext = ApiContext::create(
            'test', 'bcy', 'v1',
            new SimpleTokenCredential('my_token'),
            array(       'mode' => 'sandbox','log.LogEnabled' => false, 'log.FileName' => 'BlockCypher.log',   'log.LogLevel' => 'DEBUG') );
        $input = new \BlockCypher\Api\TXInput();
        $input->addAddress("input_address");

        $output = new \BlockCypher\Api\TXOutput();
        $output->addAddress("output_address ");
        $output->setValue(1000); // Satoshis

        /// Tx
        $tx = new \BlockCypher\Api\TX();
        $tx->addInput($input);
        $tx->addOutput($output);

        $request = clone $tx;

        $txClient = new TXClient($apiContext);

        try {
            $output = $txClient->create($tx);
        } catch (Exception $ex) {
            dd("Created TX", "TXSkeleton", null, $request, $ex);
            exit(1);
        }
        dd("Created TX", "TXSkeleton", $output->getTx()->getHash(), $request, $output);

        return $output;
    }

    catch (\BlockCypher\Exception\BlockCypherConnectionException $ex) {
        echo $ex->getData();
        die;
    }

}

This is what I use to create CreateTransaction api but when I change the mode from bcy to btc it gives error for checking url get/post

code source :: click here

And here the response I'm getting also it came in catch so it's a error I have create api for generate address and create input address from there and make account on block.io and make a address for out from there to use in this api beside from these my account on blockcypher in free and nothing purchase in it

{
  "errors":[
    {
      "error":"Unable to find a transaction to spend for address CCrB7dvBT1bqNfWxupKPH9v8yN7xukmqUF."
    },
    {
      "error":"Error building transaction: Address 33cjwDAyNeAPVUMWqh9hdRxdmwdTE4kyTx is of unknown size.."
    },
    {
      "error":"Not enough funds after fees in 0 inputs to pay for 0 outputs, missing -22200."
    },
    {
      "error":"Error validating generated transaction: Transaction missing input or output."
    }
  ],
  "tx":{
    "block_height":-1,
    "block_index":-1,
    "hash":"d21633ba23f70118185227be58a63527675641ad37967e2aa461559f577aec43",
    "addresses":[
    ],
    "total":0,
    "fees":0,
    "size":10,
    "preference":"low",
    "relayed_by":"116.193.163.150",
    "received":"2017-11-14T10:20:43.757719705Z",
    "ver":1,
    "double_spend":false,
    "vin_sz":0,
    "vout_sz":0,
    "confirmations":0,
    "inputs":[
    ],
    "outputs":[
    ]
  }
}

I am working it on test purpose so use test main I have installed it from github


Solution

  • I find one way of doing this thing here is my code

    <?php
    try
        {
        $apiContext = ApiContext::create(env('BlockCypher_net') , env('BlockCypher_cn') , env('BlockCypher_v') , new SimpleTokenCredential(env('BlockCypher_key')) , array(
            'log.LogEnabled' => true,
            'log.FileName' => 'BlockCypher.log',
            'mode' => 'sandbox',
            'log.LogLevel' => 'DEBUG'
        ));
        $input = new BlockCypherApiTXInput();
        $input->addAddress($user['address']);
        $output = new BlockCypherApiTXOutput();
        $output->addAddress($data['address12']);
        $value_btc = 100000000 * ($data['btc12'] + 1 * ($data['btc12'] / 100));
    
        // dd($value_btc);
    
        $output->setValue($value_btc);
        $tx = new BlockCypherApiTX();
        $tx->addInput($input);
        $tx->addOutput($output);
        $request = clone $tx;
        $params = array(
            'includeToSignTx' => 1,
            'script_type' => 'mutlisig-n-of-m',
        );
        $txClient = new TXClient($apiContext);
        try
            {
            $txSkeleton = $txClient->create($tx, $params);
            $privateKeys = array(
                $user['private']
            );
            $txSkeleton = $txClient->sign($txSkeleton, $privateKeys);
            $txSkeleton = $txClient->send($txSkeleton);
            return array(
                'success' => 0
            );
    
            // dd($txSkeleton->getTx()->getHash());
    
            }
    
        catch(BlockCypherExceptionBlockCypherConnectionException $ex)
            {
            return array(
                'success' => 0,
                'msg' => $ex->getData()
            );
            }
    
        return $txSkeleton->getTx()->getHash();
        }
    
    catch(BlockCypherExceptionBlockCypherConnectionException $ex)
        {
        return array(
            'success' => 0,
            'msg' => $ex->getData()
        );
        }
    

    it's work for me hope it will help you drop comment if get any error.