diff --git a/packages/sdk/src/algebraic_type.ts b/packages/sdk/src/algebraic_type.ts index d38cf95..9d85b19 100644 --- a/packages/sdk/src/algebraic_type.ts +++ b/packages/sdk/src/algebraic_type.ts @@ -1,8 +1,8 @@ import { Address } from './address'; -import { ProductValue, SumValue, type ValueAdapter } from './algebraic_value'; import type BinaryReader from './binary_reader'; import type BinaryWriter from './binary_writer'; import { Identity } from './identity'; +import ScheduleAt from './schedule_at'; /** * A variant of a sum type. @@ -353,10 +353,7 @@ export class AlgebraicType { ]); } static createScheduleAtType(): AlgebraicType { - return AlgebraicType.createSumType([ - new SumTypeVariant('Interval', AlgebraicType.createTimeDurationType()), - new SumTypeVariant('Time', AlgebraicType.createTimestampType()), - ]); + return ScheduleAt.getAlgebraicType(); } static createTimestampType(): AlgebraicType { return AlgebraicType.createProductType([ @@ -422,6 +419,17 @@ export class AlgebraicType { return this.#isBytesNewtype('__address__'); } + isScheduleAt(): boolean { + return ( + this.isSumType() && + this.sum.variants.length === 2 && + this.sum.variants[0].name === 'Interval' && + this.sum.variants[0].algebraicType.type === Type.U64 && + this.sum.variants[1].name === 'Time' && + this.sum.variants[1].algebraicType.type === Type.U64 + ); + } + isTimestamp(): boolean { return this.#isI64Newtype('__timestamp_micros_since_unix_epoch__'); } diff --git a/packages/sdk/src/algebraic_value.ts b/packages/sdk/src/algebraic_value.ts index ca41c03..3de9731 100644 --- a/packages/sdk/src/algebraic_value.ts +++ b/packages/sdk/src/algebraic_value.ts @@ -2,6 +2,7 @@ import { Address } from './address'; import { AlgebraicType, ProductType, SumType } from './algebraic_type'; import BinaryReader from './binary_reader'; import { Identity } from './identity'; +import { ScheduleAt } from './schedule_at'; export interface ReducerArgsAdapter { next: () => ValueAdapter; @@ -314,6 +315,10 @@ export class AlgebraicValue { asAddress(): Address { return new Address(this.asField(0).asBigInt()); } + + asScheduleAt(): ScheduleAt { + return ScheduleAt.fromValue(this); + } } export interface ParseableType { diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts index 0b40e58..c892fba 100644 --- a/packages/sdk/src/index.ts +++ b/packages/sdk/src/index.ts @@ -1,6 +1,6 @@ // Should be at the top as other modules depend on it export * from './db_connection_impl.ts'; - +export * from './schedule_at'; export * from './address.ts'; export * from './client_cache.ts'; export * from './identity.ts'; diff --git a/packages/sdk/src/schedule_at.ts b/packages/sdk/src/schedule_at.ts new file mode 100644 index 0000000..e31dd69 --- /dev/null +++ b/packages/sdk/src/schedule_at.ts @@ -0,0 +1,45 @@ +import { AlgebraicType, SumTypeVariant } from './algebraic_type'; +import type { AlgebraicValue } from './algebraic_value'; + +export namespace ScheduleAt { + export function getAlgebraicType(): AlgebraicType { + return AlgebraicType.createSumType([ + new SumTypeVariant('Interval', AlgebraicType.createU64Type()), + new SumTypeVariant('Time', AlgebraicType.createU64Type()), + ]); + } + + export function serialize(value: ScheduleAt): object { + switch (value.tag) { + case 'Interval': + return { Interval: value.value }; + case 'Time': + return { Time: value.value }; + default: + throw 'unreachable'; + } + } + + export type Interval = { tag: 'Interval'; value: BigInt }; + export const Interval = (value: BigInt): Interval => ({ + tag: 'Interval', + value, + }); + export type Time = { tag: 'Time'; value: BigInt }; + export const Time = (value: BigInt): Time => ({ tag: 'Time', value }); + + export function fromValue(value: AlgebraicValue): ScheduleAt { + let sumValue = value.asSumValue(); + switch (sumValue.tag) { + case 0: + return { tag: 'Interval', value: sumValue.value.asBigInt() }; + case 1: + return { tag: 'Time', value: sumValue.value.asBigInt() }; + default: + throw 'unreachable'; + } + } +} + +export type ScheduleAt = ScheduleAt.Interval | ScheduleAt.Time; +export default ScheduleAt;