Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions packages/paima-sdk/config/src/schema/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,24 @@ export class ConfigSchema<
}) as any;
};
}

export function createSchema<
Base extends ConfigSchema<ObjectLike, ObjectLike>,
Required extends ObjectLike,
Optional extends ObjectLike,
>(
params: {
base: Base;
required: Required;
optional: Optional;
},
) {
const config = params.base.cloneMerge({
required: params.required,
optional: params.optional,
});
const optionalProps = config.allProperties(false);
const allProps = config.allProperties(true);

return { optionalProps, allProps };
}
8 changes: 8 additions & 0 deletions packages/paima-sdk/test/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@paima/test",
"version": "0.3.0",
"exports": "./src/mod.ts",
"imports": {
"@sinclair/typebox": "npm:@sinclair/typebox@^0.34.30"
}
}
38 changes: 38 additions & 0 deletions packages/paima-sdk/test/src/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { ConfigPrimitivePayloadType, ConfigPrimitiveType } from "@paima/config";
import { type TIntersect, type TObject, type TSchema } from "@sinclair/typebox";
import type { BlockNumber } from "@paima/utils";

export interface EvmPrimitive<Data extends PrimitiveData> {
data: Data;
fetchData: () => void; // TODO
createViews: () => void;
createScheduledData: (
paima_block_height: BlockNumber,
payload: Data["payloadType"],
) => void;
}

export type BasePrimitivePayloadType = {
type: ConfigPrimitivePayloadType;
payload: TObject;
};
export abstract class BasePrimitive<
Data extends { payloadType: BasePrimitivePayloadType },
> {
abstract fetchData(): void; // TODO
createViews(): void {}
abstract createScheduledData(
paima_block_height: BlockNumber,
payload: Data["payloadType"],
): void;
}

export interface PrimitiveData {
readonly payloadType: BasePrimitivePayloadType;
readonly schema: {
required: TIntersect<any>;
optional: TIntersect<any>;
};
readonly primitiveType: ConfigPrimitiveType;
readonly transitions: Record<string, [string, TSchema][]>;
}
83 changes: 83 additions & 0 deletions packages/paima-sdk/test/src/builtin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import {
ConfigPrimitivePayloadType,
ConfigPrimitiveType,
createSchema,
PrimitiveConfigBaseEvm,
} from "@paima/config";
import { pickAll } from "@paima/utils";
import { TypeboxHelpers } from "@paima/utils";
import { type Static, Type } from "@sinclair/typebox";
import type { BlockNumber } from "@paima/utils";
import { Value } from "@sinclair/typebox/value";
import { BasePrimitive } from "./base.ts";

const payloadType = {
type: ConfigPrimitivePayloadType.Transfer,
payload: Type.Object({
from: TypeboxHelpers.Evm.Address,
to: TypeboxHelpers.Evm.Address,
value: TypeboxHelpers.Uint256,
}),
};
const PrimitiveDataErc20 = {
payloadType,
schema: createSchema({
base: PrimitiveConfigBaseEvm,
required: Type.Object({
type: Type.Literal(ConfigPrimitiveType.EvmRpcERC20),
contractAddress: TypeboxHelpers.Evm.Address,
}),
optional: Type.Object({
scheduledPrefix: Type.String(),
}),
}),
primitiveType: ConfigPrimitiveType.EvmRpcERC20,
transitions: {
transferScheduledPrefix: pickAll(["from", "to", "value"]).from(
payloadType.payload,
),
},
};

type DataType = typeof PrimitiveDataErc20;
class Erc20Primitive extends BasePrimitive<DataType> {
config: Static<typeof PrimitiveDataErc20.schema.allProps>;
data: DataType;

constructor(
props: Static<typeof PrimitiveDataErc20.schema.optionalProps>,
) {
super();
this.data = PrimitiveDataErc20;
this.config = Value.Default(
PrimitiveDataErc20.schema.allProps,
props,
) as typeof this.config;
}
override createViews = () => {
// TODO: views for erc20
};
fetchData = () => {
};

createScheduledData(
paima_block_height: BlockNumber,
payload: DataType["payloadType"],
): void {
console.log(paima_block_height, payload);
// yield* World.resolve(insertPrimitiveAccounting, {
// primitive_name: name,
// paima_block_height: paima_block_height,
// payload_type: ConfigPrimitiveAccountingPayloadType.Transfer,
// payload: clearBigInts(payload) satisfies PayloadOf<
// typeof PrimitiveEvmRpcErc20TransferAccounting
// >,
// });
}
}

declare module "@paima/test" {
interface SomeGlobalNamespace {
Erc20Primitive: typeof Erc20Primitive;
}
}
1 change: 1 addition & 0 deletions packages/paima-sdk/test/src/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type { RegisteredComponents } from "./builtin.ts";
7 changes: 7 additions & 0 deletions packages/paima-sdk/test2/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@paima/test2",
"version": "0.3.0",
"exports": "./src/mod.ts",
"imports": {
}
}
5 changes: 5 additions & 0 deletions packages/paima-sdk/test2/src/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { SomeGlobalNamespace } from "@paima/test";

type ComponentName = keyof SomeGlobalNamespace;

const foo: ComponentName = "Erc20Primitive";
9 changes: 9 additions & 0 deletions packages/paima-sdk/utils/src/types/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,12 @@ export function keysOf<Key extends number | string>(
): Key[] {
return Object.keys(obj) as Key[];
}

export function clearBigInts<T>(value: T): T {
return JSON.parse(
JSON.stringify(
value,
(_, v) => typeof v === "bigint" ? v.toString() : v,
),
);
}