Skip to content

Commit

Permalink
feat: add payload type assertions (#1395)
Browse files Browse the repository at this point in the history
Adds assertion methods (i.e. type predicates) for the Payload type: `isTokenTransferPayload`, `isContractCallPayload`, `isSmartContractPayload`, `isPoisonPayload`, `isCoinbasePayload` — Thanks to @edu-stx
  • Loading branch information
edu-stx authored Nov 23, 2022
1 parent a2a7128 commit cdd32e9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/transactions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export {
PoisonPayload,
CoinbasePayload,
serializePayload,
isTokenTransferPayload,
isContractCallPayload,
isSmartContractPayload,
isPoisonPayload,
isCoinbasePayload,
} from './payload';

export {
Expand Down
16 changes: 16 additions & 0 deletions packages/transactions/src/payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ export type Payload =
| PoisonPayload
| CoinbasePayload;

export function isTokenTransferPayload(p: Payload): p is TokenTransferPayload {
return p.payloadType === PayloadType.TokenTransfer;
}
export function isContractCallPayload(p: Payload): p is ContractCallPayload {
return p.payloadType === PayloadType.ContractCall;
}
export function isSmartContractPayload(p: Payload): p is SmartContractPayload {
return p.payloadType === PayloadType.SmartContract;
}
export function isPoisonPayload(p: Payload): p is PoisonPayload {
return p.payloadType === PayloadType.PoisonMicroblock;
}
export function isCoinbasePayload(p: Payload): p is CoinbasePayload {
return p.payloadType === PayloadType.Coinbase;
}

export interface TokenTransferPayload {
readonly type: StacksMessageType.Payload;
readonly payloadType: PayloadType.TokenTransfer;
Expand Down

1 comment on commit cdd32e9

@vercel
Copy link

@vercel vercel bot commented on cdd32e9 Nov 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.