Skip to content

Commit

Permalink
Add Typescript type hinting for all CAP-35 operations. (#407)
Browse files Browse the repository at this point in the history
Co-authored-by: Jun Luo <[email protected]>
  • Loading branch information
Shaptic and overcat authored Apr 7, 2021
1 parent c687cf9 commit d29f60d
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 18 deletions.
13 changes: 11 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,30 @@

## Unreleased

### Update

- The Typescript definitions have been updated to support CAP-35 ([#407](https://github.com/stellar/js-stellar-base/pull/407)).

- The `AllowTrust` flag type definitions (`TrustLineFlags`) have been renamed to match the XDR directly, since SetTrustLineFlags supercedes the now-deprecated `AllowTrustOp` ([#407](https://github.com/stellar/js-stellar-base/pull/407)).


## [v5.0.0](https://github.com/stellar/js-stellar-base/compare/v4.0.3..v5.0.0)

### Add
- Introduced new CAP-35 operations, `ClawbackOp` and `ClawbackClaimableBalanceOp` ([#397](https://github.com/stellar/js-stellar-base/pull/397/)).

- Introduced new CAP-35 operations, `ClawbackOp`, `ClawbackClaimableBalanceOp`, and `SetTrustLineFlagsOp` ([#397](https://github.com/stellar/js-stellar-base/pull/397/)).

### Update

- Add an additional parameter check to `claimClaimableBalance` to fail faster ([#390](https://github.com/stellar/js-stellar-base/pull/390)).

- The XDR & TS definitions have been updated to support CAP-35 ([#394](https://github.com/stellar/js-stellar-base/pull/394)).
- The XDR definitions have been updated to support CAP-35 ([#394](https://github.com/stellar/js-stellar-base/pull/394)).

### Breaking

- `AllowTrustOpAsset` has been renamed to `AssetCode` ([#394](https://github.com/stellar/js-stellar-base/pull/394))


### Deprecated

- `AllowTrustOp` is now a deprecated operation.
Expand Down
12 changes: 6 additions & 6 deletions src/operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ export const AuthImmutableFlag = 1 << 2;
* * `{@link Operation.bumpSequence}`
* * `{@link Operation.createClaimableBalance}`
* * `{@link Operation.claimClaimableBalance}`
* * `{@link Operation.clawbackClaimableBalance}`
* * `{@link Operation.beginSponsoringFutureReserves}`
* * `{@link Operation.endSponsoringFutureReserves}`
* * `{@link Operation.revokeAccountSponsorship}`
Expand All @@ -72,6 +71,7 @@ export const AuthImmutableFlag = 1 << 2;
* * `{@link Operation.revokeClaimableBalanceSponsorship}`
* * `{@link Operation.revokeSignerSponsorship}`
* * `{@link Operation.clawback}`
* * `{@link Operation.clawbackClaimableBalance}`
* * `{@link Operation.setTrustLineFlags}`
*
* @class Operation
Expand Down Expand Up @@ -276,11 +276,6 @@ export class Operation {
result.balanceId = attrs.toXDR('hex');
break;
}
case 'clawbackClaimableBalance': {
result.type = 'clawbackClaimableBalance';
result.balanceId = attrs.toXDR('hex');
break;
}
case 'beginSponsoringFutureReserves': {
result.type = 'beginSponsoringFutureReserves';
result.sponsoredId = accountIdtoAddress(attrs.sponsoredId());
Expand All @@ -301,6 +296,11 @@ export class Operation {
result.asset = Asset.fromOperation(attrs.asset());
break;
}
case 'clawbackClaimableBalance': {
result.type = 'clawbackClaimableBalance';
result.balanceId = attrs.toXDR('hex');
break;
}
case 'setTrustLineFlags': {
result.type = 'setTrustLineFlags';
result.asset = Asset.fromOperation(attrs.asset());
Expand Down
16 changes: 9 additions & 7 deletions src/operations/set_trustline_flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ import { Keypair } from '../keypair';
* @param {string} opts.trustor - the account whose trustline this is
* @param {Asset} opts.asset - the asset on the trustline
* @param {object} opts.flags - the set of flags to modify
* @param {bool} opts.flags.authorized - authorize account to perform transactions
* with its credit
* @param {bool} opts.flags.authorizedToMaintainLiabilities - authorize
* account to maintain and reduce liabilities for its credit
* @param {bool} opts.flags.clawbackEnabled - stop claimable balances on this
* trustlines from having clawbacks enabled (this flag can only be set to
* false!)
*
* @param {bool} [opts.flags.authorized] - authorize account to perform
* transactions with its credit
* @param {bool} [opts.flags.authorizedToMaintainLiabilities] - authorize
* account to maintain and reduce liabilities for its credit
* @param {bool} [opts.flags.clawbackEnabled] - stop claimable balances on
* this trustlines from having clawbacks enabled (this flag can only be set
* to false!)
* @param {string} [opts.source] - The source account for the operation.
* Defaults to the transaction's source account.
*
* @note You must include at least one flag.
*
* @return {xdr.SetTrustLineFlagsOp}
*
* @link xdr.AccountFlags
Expand Down
64 changes: 61 additions & 3 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ export namespace OperationType {
type BeginSponsoringFutureReserves = 'beginSponsoringFutureReserves';
type EndSponsoringFutureReserves = 'endSponsoringFutureReserves';
type RevokeSponsorship = 'revokeSponsorship';
type Clawback = 'clawback';
type ClawbackClaimableBalance = 'clawbackClaimableBalance';
type SetTrustLineFlags = 'setTrustLineFlags';
}
export type OperationType =
| OperationType.CreateAccount
Expand All @@ -259,7 +262,10 @@ export type OperationType =
| OperationType.ClaimClaimableBalance
| OperationType.BeginSponsoringFutureReserves
| OperationType.EndSponsoringFutureReserves
| OperationType.RevokeSponsorship;
| OperationType.RevokeSponsorship
| OperationType.Clawback
| OperationType.ClawbackClaimableBalance
| OperationType.SetTrustLineFlags;

export namespace OperationOptions {
interface BaseOptions {
Expand Down Expand Up @@ -373,6 +379,23 @@ export namespace OperationOptions {
account: string;
signer: SignerKeyOptions;
}
interface Clawback extends BaseOptions {
asset: Asset;
amount: string;
from: string;
}
interface ClawbackClaimableBalance extends BaseOptions {
balanceId: string;
}
interface SetTrustLineFlags extends BaseOptions {
trustor: string;
asset: Asset;
flags: {
authorized?: boolean;
authorizedToMaintainLiabilities?: boolean;
clawbackEnabled?: boolean;
};
}
}
export type OperationOptions =
| OperationOptions.CreateAccount
Expand All @@ -397,7 +420,10 @@ export type OperationOptions =
| OperationOptions.RevokeOfferSponsorship
| OperationOptions.RevokeDataSponsorship
| OperationOptions.RevokeClaimableBalanceSponsorship
| OperationOptions.RevokeSignerSponsorship;
| OperationOptions.RevokeSignerSponsorship
| OperationOptions.Clawback
| OperationOptions.ClawbackClaimableBalance
| OperationOptions.SetTrustLineFlags;

export namespace Operation {
interface BaseOperation<T extends OperationType = OperationType> {
Expand Down Expand Up @@ -622,6 +648,35 @@ export namespace Operation {
options: OperationOptions.RevokeSignerSponsorship
): xdr.Operation<RevokeSignerSponsorship>;

interface Clawback extends BaseOperation<OperationType.Clawback> {
asset: Asset;
amount: string;
from: string;
}
function clawback(
options: OperationOptions.Clawback
): xdr.Operation<Clawback>;

interface ClawbackClaimableBalance extends BaseOperation<OperationType.ClawbackClaimableBalance> {
balanceId: string;
}
function clawbackClaimableBalance(
options: OperationOptions.ClawbackClaimableBalance
): xdr.Operation<ClawbackClaimableBalance>;

interface SetTrustLineFlags extends BaseOperation<OperationType.SetTrustLineFlags> {
trustor: string;
asset: Asset;
flags: {
authorized?: boolean;
authorizedToMaintainLiabilities?: boolean;
clawbackEnabled?: boolean;
};
}
function setTrustLineFlags(
options: OperationOptions.SetTrustLineFlags
): xdr.Operation<SetTrustLineFlags>;

function fromXDRObject<T extends Operation = Operation>(
xdrOperation: xdr.Operation<T>
): T;
Expand Down Expand Up @@ -650,7 +705,10 @@ export type Operation =
| Operation.RevokeOfferSponsorship
| Operation.RevokeDataSponsorship
| Operation.RevokeClaimableBalanceSponsorship
| Operation.RevokeSignerSponsorship;
| Operation.RevokeSignerSponsorship
| Operation.Clawback
| Operation.ClawbackClaimableBalance
| Operation.SetTrustLineFlags;

export namespace StrKey {
function encodeEd25519PublicKey(data: Buffer): string;
Expand Down
49 changes: 49 additions & 0 deletions types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,34 @@ const transaction = new StellarSdk.TransactionBuilder(account, {
preAuthTx: "da0d57da7d4850e7fc10d2a9d0ebc731f7afb40574c03395b17d49149b91f5be"
}
})
).addOperation(
StellarSdk.Operation.clawback({
from: account.accountId(),
amount: "1000",
asset: usd,
})
).addOperation(
StellarSdk.Operation.clawbackClaimableBalance({
balanceId: "00000000da0d57da7d4850e7fc10d2a9d0ebc731f7afb40574c03395b17d49149b91f5be",
})
).addOperation(
StellarSdk.Operation.setTrustLineFlags({
trustor: account.accountId(),
asset: usd,
flags: {
authorized: true,
authorizedToMaintainLiabilities: true,
clawbackEnabled: true,
},
})
).addOperation(
StellarSdk.Operation.setTrustLineFlags({
trustor: account.accountId(),
asset: usd,
flags: {
authorized: true,
},
})
).addMemo(new StellarSdk.Memo(StellarSdk.MemoText, 'memo'))
.setTimeout(5)
.build(); // $ExpectType () => Transaction<Memo<MemoType>, Operation[]>
Expand Down Expand Up @@ -213,3 +241,24 @@ const claimant = new StellarSdk.Claimant(sourceKey.publicKey()); // $ExpectType
claimant.toXDRObject(); // $ExpectType Claimant
claimant.destination; // $ExpectType string
claimant.predicate; // $ExpectType ClaimPredicate

const claw = StellarSdk.xdr.ClawbackOp.fromXDR(
// tslint:disable:max-line-length
'AAAAAAAAABMAAAABVVNEAAAAAADNTrgPO19O0EsnYjSc333yWGLKEVxLyu1kfKjCKOz9ewAAAADFTYDKyTn2O0DVUEycHKfvsnFWj91TVl0ut1kwg5nLigAAAAJUC+QA',
'base64'
);
claw; // $ExpectType ClawbackOp

const clawCb = StellarSdk.xdr.ClawbackClaimableBalanceOp.fromXDR(
// tslint:disable:max-line-length
'AAAAAAAAABUAAAAAxU2Aysk59jtA1VBMnByn77JxVo/dU1ZdLrdZMIOZy4oAAAABVVNEAAAAAADNTrgPO19O0EsnYjSc333yWGLKEVxLyu1kfKjCKOz9ewAAAAAAAAAH',
'base64'
);
clawCb; // $ExpectType ClawbackClaimableBalanceOp

const trust = StellarSdk.xdr.SetTrustLineFlagsOp.fromXDR(
// tslint:disable:max-line-length
'AAAAAAAAABUAAAAAF1frB6QZRDTYW4dheEA3ZZLCjSWs9eQgzsyvqdUy2rgAAAABVVNEAAAAAADNTrgPO19O0EsnYjSc333yWGLKEVxLyu1kfKjCKOz9ewAAAAAAAAAB',
'base64'
);
trust; // $ExpectType SetTrustLineFlagsOp

0 comments on commit d29f60d

Please sign in to comment.