Skip to content

Commit

Permalink
fix: unify rosetta operation schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcr committed May 5, 2023
1 parent 8ec29fb commit bf3216b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 51 deletions.
9 changes: 7 additions & 2 deletions components/client/typescript/src/schemas/bitcoin/payload.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Static, Type } from '@sinclair/typebox';
import { Nullable, BlockIdentifierSchema, TransactionIdentifierSchema } from '../common';
import {
Nullable,
BlockIdentifierSchema,
TransactionIdentifierSchema,
RosettaOperationSchema,
} from '../common';

export const BitcoinInscriptionRevealedSchema = Type.Object({
content_bytes: Type.String(),
Expand Down Expand Up @@ -49,7 +54,7 @@ export type BitcoinTransactionMetadata = Static<typeof BitcoinTransactionMetadat

export const BitcoinTransactionSchema = Type.Object({
transaction_identifier: TransactionIdentifierSchema,
operations: Type.Array(Type.Any()),
operations: Type.Array(RosettaOperationSchema),
metadata: BitcoinTransactionMetadataSchema,
});
export type BitcoinTransaction = Static<typeof BitcoinTransactionSchema>;
Expand Down
57 changes: 55 additions & 2 deletions components/client/typescript/src/schemas/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,63 @@ import { Static, TSchema, Type } from '@sinclair/typebox';

export const Nullable = <T extends TSchema>(type: T) => Type.Union([type, Type.Null()]);

export const OperationIdentifierSchema = Type.Object({
export const RosettaOperationIdentifierSchema = Type.Object({
index: Type.Integer(),
network_index: Type.Optional(Type.Integer()),
});
export type OperationIdentifier = Static<typeof OperationIdentifierSchema>;
export type RosettaOperationIdentifier = Static<typeof RosettaOperationIdentifierSchema>;

export const RosettaOperationTypeSchema = Type.Union([
Type.Literal('CREDIT'),
Type.Literal('DEBIT'),
Type.Literal('LOCK'),
]);
export type RosettaOperationType = Static<typeof RosettaOperationTypeSchema>;

export const RosettaOperationAccountSchema = Type.Object({
address: Type.String(),
sub_account: Type.Optional(Type.String()),
});
export type RosettaOperationAccount = Static<typeof RosettaOperationAccountSchema>;

export const RosettaOperationAmountSchema = Type.Object({
currency: Type.Object({
decimals: Type.Integer(),
symbol: Type.String(),
metadata: Type.Object({
asset_class_identifier: Type.String(),
asset_identifier: Nullable(Type.String()),
standard: Type.String(),
}),
}),
value: Type.Integer(),
});
export type RosettaOperationAmount = Static<typeof RosettaOperationAmountSchema>;

export const RosettaOperationPublicKeySchema = Type.Object({
hex_bytes: Type.Optional(Type.String()),
curve_type: Type.String(),
});
export type RosettaOperationPublicKey = Static<typeof RosettaOperationPublicKeySchema>;

export const RosettaOperationMetadataSchema = Type.Object({
public_key: Type.Optional(RosettaOperationPublicKeySchema),
code: Type.Optional(Type.String()),
method_name: Type.Optional(Type.String()),
args: Type.Optional(Type.String()),
});
export type RosettaOperationMetadata = Static<typeof RosettaOperationMetadataSchema>;

export const RosettaOperationSchema = Type.Object({
account: RosettaOperationAccountSchema,
amount: Type.Optional(RosettaOperationAmountSchema),
metadata: Type.Optional(RosettaOperationMetadataSchema),
operation_identifier: RosettaOperationIdentifierSchema,
related_operations: Type.Optional(Type.Array(RosettaOperationIdentifierSchema)),
status: Type.Optional(Type.Literal('SUCCESS')),
type: RosettaOperationTypeSchema,
});
export type RosettaOperation = Static<typeof RosettaOperationSchema>;

export const BlockIdentifierSchema = Type.Object({
index: Type.Integer(),
Expand Down
49 changes: 2 additions & 47 deletions components/client/typescript/src/schemas/stacks/payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Static, Type } from '@sinclair/typebox';
import {
BlockIdentifierSchema,
Nullable,
OperationIdentifierSchema,
RosettaOperationSchema,
TransactionIdentifierSchema,
} from '../common';
import { StacksTransactionEventSchema } from './tx_events';
Expand Down Expand Up @@ -53,54 +53,9 @@ export const StacksTransactionMetadataSchema = Type.Object({
});
export type StacksTransactionMetadata = Static<typeof StacksTransactionMetadataSchema>;

export const StacksOperationAccountSchema = Type.Object({
address: StacksPrincipalSchema,
sub_account: Type.Optional(StacksPrincipalSchema),
});
export type StacksOperationAccount = Static<typeof StacksOperationAccountSchema>;

export const StacksOperationAmountSchema = Type.Object({
currency: Type.Object({
decimals: Type.Integer(),
symbol: Type.String(),
metadata: Type.Object({
asset_class_identifier: Type.String(),
asset_identifier: Nullable(Type.String()),
standard: Type.String(),
}),
}),
value: Type.Integer(),
});
export type StacksOperationAmount = Static<typeof StacksOperationAmountSchema>;

export const StacksOperationPublicKeySchema = Type.Object({
hex_bytes: Type.Optional(Type.String()),
curve_type: Type.String(),
});
export type StacksOperationPublicKey = Static<typeof StacksOperationPublicKeySchema>;

export const StacksOperationMetadataSchema = Type.Object({
public_key: Type.Optional(StacksOperationPublicKeySchema),
code: Type.Optional(Type.String()),
method_name: Type.Optional(Type.String()),
args: Type.Optional(Type.String()),
});
export type StacksOperationMetadata = Static<typeof StacksOperationMetadataSchema>;

export const StacksOperationSchema = Type.Object({
account: StacksOperationAccountSchema,
amount: Type.Optional(StacksOperationAmountSchema),
metadata: Type.Optional(StacksOperationMetadataSchema),
operation_identifier: OperationIdentifierSchema,
related_operations: Type.Optional(Type.Array(OperationIdentifierSchema)),
status: Type.Optional(Type.Literal('SUCCESS')),
type: Type.Union([Type.Literal('CREDIT'), Type.Literal('DEBIT'), Type.Literal('LOCK')]),
});
export type StacksOperation = Static<typeof StacksOperationSchema>;

const StacksTransactionSchema = Type.Object({
transaction_identifier: TransactionIdentifierSchema,
operations: Type.Array(StacksOperationSchema),
operations: Type.Array(RosettaOperationSchema),
metadata: StacksTransactionMetadataSchema,
});
export type StacksTransaction = Static<typeof StacksTransactionSchema>;
Expand Down

0 comments on commit bf3216b

Please sign in to comment.