Skip to content

Commit

Permalink
Support for BumpSequenceOperation and result
Browse files Browse the repository at this point in the history
  • Loading branch information
zulucrypto committed Sep 22, 2018
1 parent 8941f89 commit 7395221
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 0 deletions.
24 changes: 24 additions & 0 deletions examples/bump-sequence.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* Adds a new signer to an existing account
*/


require '../vendor/autoload.php';

use \ZuluCrypto\StellarSdk\Keypair;
use \ZuluCrypto\StellarSdk\Server;
use \phpseclib\Math\BigInteger;


$server = Server::testNet();

// GCP6IHMHWRCF5TQ4ZP6TVIRNDZD56W42F42VHYWMVDGDAND75YGAHHBQ
$currentAccount = Keypair::newFromSeed('SCEDMZ7DUEOUGRQWEXHXEXISQ2NAWI5IDXRHYWT2FHTYLIQOSUK5FX2E');


// Submit to the network
$server->buildTransaction($currentAccount->getPublicKey())
->bumpSequenceTo(new BigInteger('47061756253569030'))
->submit($currentAccount->getSecret());
48 changes: 48 additions & 0 deletions src/Model/BumpSequenceOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php


namespace ZuluCrypto\StellarSdk\Model;


use phpseclib\Math\BigInteger;
use ZuluCrypto\StellarSdk\XdrModel\Operation\BumpSequenceOp;

class BumpSequenceOperation extends Operation
{
/**
* @var BigInteger
*/
protected $bumpTo;

/**
* @param array $rawData
* @return BumpSequenceOp
*/
public static function fromRawResponseData($rawData)
{
$object = new BumpSequenceOp($rawData['id'], $rawData['type']);

$object->loadFromRawResponseData($rawData);

return $object;
}

/**
* @param $id
* @param $type
*/
public function __construct($id, $type)
{
parent::__construct($id, Operation::TYPE_BUMP_SEQUENCE);
}

/**
* @param $rawData
*/
public function loadFromRawResponseData($rawData)
{
parent::loadFromRawResponseData($rawData);

$this->bumpTo = new BigInteger($rawData['bump_to']);
}
}
6 changes: 6 additions & 0 deletions src/Model/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace ZuluCrypto\StellarSdk\Model;

use ZuluCrypto\StellarSdk\XdrModel\Operation\BumpSequenceOp;


/**
* See: https://www.stellar.org/developers/horizon/reference/resources/operation.html
Expand All @@ -20,6 +22,7 @@ class Operation extends RestApiModel
const TYPE_ACCOUNT_MERGE = 'account_merge';
const TYPE_INFLATION = 'inflation';
const TYPE_MANAGE_DATA = 'manage_data';
const TYPE_BUMP_SEQUENCE = 'bump_sequence';

/**
* Operation ID on the Stellar network
Expand Down Expand Up @@ -83,6 +86,9 @@ public static function fromRawResponseData($rawData)
case Operation::TYPE_INFLATION:
$object = new InflationOperation($rawData['id'], $rawData['type']);
break;
case Operation::TYPE_BUMP_SEQUENCE:
$object = new BumpSequenceOp($rawData['id'], $rawData['type']);
break;
}

$object->loadFromRawResponseData($rawData);
Expand Down
41 changes: 41 additions & 0 deletions src/XdrModel/BumpSequenceResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php


namespace ZuluCrypto\StellarSdk\XdrModel;


use ZuluCrypto\StellarSdk\Xdr\XdrBuffer;

class BumpSequenceResult extends OperationResult
{
// https://github.com/stellar/stellar-core/blob/master/src/xdr/Stellar-transaction.x#L675
const BAD_SEQ = 'bump_sequence_bad_seq'; // "bumpTo" is not within bounds

/**
* @deprecated Do not call this method directly. Instead, use OperationResult::fromXdr
*
* @param XdrBuffer $xdr
* @return OperationResult|PaymentResult
* @throws \ErrorException
*/
public static function fromXdr(XdrBuffer $xdr)
{
$model = new BumpSequenceResult();

$rawErrorCode = $xdr->readInteger();
$errorCodeMap = [
'0' => 'success',
'-1' => static::BAD_SEQ,
];
if (!isset($errorCodeMap[$rawErrorCode])) {
throw new \ErrorException(sprintf('Unknown error code %s', $rawErrorCode));
}

// Do not store the "success" error code
if ($errorCodeMap[$rawErrorCode] !== 'success') {
$model->errorCode = $errorCodeMap[$rawErrorCode];
}

return $model;
}
}
3 changes: 3 additions & 0 deletions src/XdrModel/OperationResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public static function fromXdr(XdrBuffer $xdr)
case Operation::TYPE_MANAGE_DATA:
return ManageDataResult::fromXdr($xdr);
break;
case Operation::TYPE_BUMP_SEQUENCE:
return BumpSequenceResult::fromXdr($xdr);
break;
default:
throw new \ErrorException(sprintf('Unknown operation type: %s', $opType));
}
Expand Down

0 comments on commit 7395221

Please sign in to comment.