import { GearApi, BaseGearProgram, HexString } from '@gear-js/api'; import { TypeRegistry } from '@polkadot/types'; import { TransactionBuilder, QueryBuilder, getServiceNamePrefix, getFnNamePrefix, ZERO_ADDRESS, ActorId, NonZeroU32, H160, NonZeroU8, } from 'sails-js'; export class SailsProgram { public readonly registry: TypeRegistry; public readonly pingPong: PingPong; public readonly counter: Counter; public readonly dog: Dog; public readonly references: References; public readonly thisThat: ThisThat; public readonly valueFee: ValueFee; private _program?: BaseGearProgram; constructor( public api: GearApi, programId?: `0x${string}`, ) { const types: Record = { ReferenceCount: '(u32)', DoThatParam: { p1: 'u32', p2: '[u8;32]', p3: 'ManyVariants' }, ManyVariants: { _enum: { One: 'Null', Two: 'u32', Three: 'Option', Four: { a: 'u32', b: 'Option' }, Five: '(String, H256)', Six: '(u32)', }, }, ManyVariantsReply: { _enum: ['One', 'Two', 'Three', 'Four', 'Five', 'Six'] }, TupleStruct: '(bool)', }; this.registry = new TypeRegistry(); this.registry.setKnownTypes({ types }); this.registry.register(types); if (programId) { this._program = new BaseGearProgram(programId, api); } this.pingPong = new PingPong(this); this.counter = new Counter(this); this.dog = new Dog(this); this.references = new References(this); this.thisThat = new ThisThat(this); this.valueFee = new ValueFee(this); } public get programId(): `0x${string}` { if (!this._program) throw new Error(`Program ID is not set`); return this._program.id; } /** * Program constructor (called once at the very beginning of the program lifetime) */ defaultCtorFromCode(code: Uint8Array | Buffer | HexString): TransactionBuilder { const builder = new TransactionBuilder( this.api, this.registry, 'upload_program', null, 'Default', null, null, 'String', code, async (programId) => { this._program = await BaseGearProgram.new(programId, this.api); }, ); return builder; } /** * Program constructor (called once at the very beginning of the program lifetime) */ defaultCtorFromCodeId(codeId: `0x${string}`) { const builder = new TransactionBuilder( this.api, this.registry, 'create_program', null, 'Default', null, null, 'String', codeId, async (programId) => { this._program = await BaseGearProgram.new(programId, this.api); }, ); return builder; } /** * Another program constructor (called once at the very beginning of the program lifetime) */ newCtorFromCode( code: Uint8Array | Buffer | HexString, counter: number | null, dog_position: [number, number] | null, ): TransactionBuilder { const builder = new TransactionBuilder( this.api, this.registry, 'upload_program', null, 'New', [counter, dog_position], '(Option, Option<(i32, i32)>)', 'String', code, async (programId) => { this._program = await BaseGearProgram.new(programId, this.api); }, ); return builder; } /** * Another program constructor (called once at the very beginning of the program lifetime) */ newCtorFromCodeId(codeId: `0x${string}`, counter: number | null, dog_position: [number, number] | null) { const builder = new TransactionBuilder( this.api, this.registry, 'create_program', null, 'New', [counter, dog_position], '(Option, Option<(i32, i32)>)', 'String', codeId, async (programId) => { this._program = await BaseGearProgram.new(programId, this.api); }, ); return builder; } } export class PingPong { constructor(private _program: SailsProgram) {} public ping(input: string): TransactionBuilder<{ ok: string } | { err: string }> { if (!this._program.programId) throw new Error('Program ID is not set'); return new TransactionBuilder<{ ok: string } | { err: string }>( this._program.api, this._program.registry, 'send_message', 'PingPong', 'Ping', input, 'String', 'Result', this._program.programId, ); } } export class Counter { constructor(private _program: SailsProgram) {} /** * Add a value to the counter */ public add(value: number): TransactionBuilder { if (!this._program.programId) throw new Error('Program ID is not set'); return new TransactionBuilder( this._program.api, this._program.registry, 'send_message', 'Counter', 'Add', value, 'u32', 'u32', this._program.programId, ); } /** * Subtract a value from the counter */ public sub(value: number): TransactionBuilder { if (!this._program.programId) throw new Error('Program ID is not set'); return new TransactionBuilder( this._program.api, this._program.registry, 'send_message', 'Counter', 'Sub', value, 'u32', 'u32', this._program.programId, ); } /** * Get the current value */ public value(): QueryBuilder { return new QueryBuilder( this._program.api, this._program.registry, this._program.programId, 'Counter', 'Value', null, null, 'u32', ); } /** * Emitted when a new value is added to the counter */ public subscribeToAddedEvent(callback: (data: number) => void | Promise): Promise<() => void> { return this._program.api.gearEvents.subscribeToGearEvent('UserMessageSent', ({ data: { message } }) => { if (!message.source.eq(this._program.programId) || !message.destination.eq(ZERO_ADDRESS)) { return; } const payload = message.payload.toHex(); if (getServiceNamePrefix(payload) === 'Counter' && getFnNamePrefix(payload) === 'Added') { callback( this._program.registry .createType('(String, String, u32)', message.payload)[2] .toNumber() as unknown as number, ); } }); } /** * Emitted when a value is subtracted from the counter */ public subscribeToSubtractedEvent(callback: (data: number) => void | Promise): Promise<() => void> { return this._program.api.gearEvents.subscribeToGearEvent('UserMessageSent', ({ data: { message } }) => { if (!message.source.eq(this._program.programId) || !message.destination.eq(ZERO_ADDRESS)) { return; } const payload = message.payload.toHex(); if (getServiceNamePrefix(payload) === 'Counter' && getFnNamePrefix(payload) === 'Subtracted') { callback( this._program.registry .createType('(String, String, u32)', message.payload)[2] .toNumber() as unknown as number, ); } }); } } export class Dog { constructor(private _program: SailsProgram) {} public makeSound(): TransactionBuilder { if (!this._program.programId) throw new Error('Program ID is not set'); return new TransactionBuilder( this._program.api, this._program.registry, 'send_message', 'Dog', 'MakeSound', null, null, 'String', this._program.programId, ); } public walk(dx: number, dy: number): TransactionBuilder { if (!this._program.programId) throw new Error('Program ID is not set'); return new TransactionBuilder( this._program.api, this._program.registry, 'send_message', 'Dog', 'Walk', [dx, dy], '(i32, i32)', 'Null', this._program.programId, ); } public avgWeight(): QueryBuilder { return new QueryBuilder( this._program.api, this._program.registry, this._program.programId, 'Dog', 'AvgWeight', null, null, 'u32', ); } public position(): QueryBuilder<[number, number]> { return new QueryBuilder<[number, number]>( this._program.api, this._program.registry, this._program.programId, 'Dog', 'Position', null, null, '(i32, i32)', ); } public subscribeToBarkedEvent(callback: (data: null) => void | Promise): Promise<() => void> { return this._program.api.gearEvents.subscribeToGearEvent('UserMessageSent', ({ data: { message } }) => { if (!message.source.eq(this._program.programId) || !message.destination.eq(ZERO_ADDRESS)) { return; } const payload = message.payload.toHex(); if (getServiceNamePrefix(payload) === 'Dog' && getFnNamePrefix(payload) === 'Barked') { callback(null); } }); } public subscribeToWalkedEvent( callback: (data: { from: [number, number]; to: [number, number] }) => void | Promise, ): Promise<() => void> { return this._program.api.gearEvents.subscribeToGearEvent('UserMessageSent', ({ data: { message } }) => { if (!message.source.eq(this._program.programId) || !message.destination.eq(ZERO_ADDRESS)) { return; } const payload = message.payload.toHex(); if (getServiceNamePrefix(payload) === 'Dog' && getFnNamePrefix(payload) === 'Walked') { callback( this._program.registry .createType('(String, String, {"from":"(i32, i32)","to":"(i32, i32)"})', message.payload)[2] .toJSON() as unknown as { from: [number, number]; to: [number, number] }, ); } }); } } export class References { constructor(private _program: SailsProgram) {} public add(v: number): TransactionBuilder { if (!this._program.programId) throw new Error('Program ID is not set'); return new TransactionBuilder( this._program.api, this._program.registry, 'send_message', 'References', 'Add', v, 'u32', 'u32', this._program.programId, ); } public addByte(byte: number): TransactionBuilder<`0x${string}`> { if (!this._program.programId) throw new Error('Program ID is not set'); return new TransactionBuilder<`0x${string}`>( this._program.api, this._program.registry, 'send_message', 'References', 'AddByte', byte, 'u8', 'Vec', this._program.programId, ); } public guessNum($number: number): TransactionBuilder<{ ok: string } | { err: string }> { if (!this._program.programId) throw new Error('Program ID is not set'); return new TransactionBuilder<{ ok: string } | { err: string }>( this._program.api, this._program.registry, 'send_message', 'References', 'GuessNum', $number, 'u8', 'Result', this._program.programId, ); } public incr(): TransactionBuilder { if (!this._program.programId) throw new Error('Program ID is not set'); return new TransactionBuilder( this._program.api, this._program.registry, 'send_message', 'References', 'Incr', null, null, 'ReferenceCount', this._program.programId, ); } public setNum($number: number): TransactionBuilder<{ ok: null } | { err: string }> { if (!this._program.programId) throw new Error('Program ID is not set'); return new TransactionBuilder<{ ok: null } | { err: string }>( this._program.api, this._program.registry, 'send_message', 'References', 'SetNum', $number, 'u8', 'Result', this._program.programId, ); } public baked(): QueryBuilder { return new QueryBuilder( this._program.api, this._program.registry, this._program.programId, 'References', 'Baked', null, null, 'String', ); } public lastByte(): QueryBuilder { return new QueryBuilder( this._program.api, this._program.registry, this._program.programId, 'References', 'LastByte', null, null, 'Option', ); } public message(): QueryBuilder { return new QueryBuilder( this._program.api, this._program.registry, this._program.programId, 'References', 'Message', null, null, 'Option', ); } } export class ThisThat { constructor(private _program: SailsProgram) {} public doThat( param: DoThatParam, ): TransactionBuilder<{ ok: [ActorId, NonZeroU32, ManyVariantsReply] } | { err: [string] }> { if (!this._program.programId) throw new Error('Program ID is not set'); return new TransactionBuilder<{ ok: [ActorId, NonZeroU32, ManyVariantsReply] } | { err: [string] }>( this._program.api, this._program.registry, 'send_message', 'ThisThat', 'DoThat', param, 'DoThatParam', 'Result<([u8;32], u32, ManyVariantsReply), (String)>', this._program.programId, ); } public doThis( p1: number, p2: string, p3: [H160 | null, NonZeroU8], p4: TupleStruct, ): TransactionBuilder<[string, number]> { if (!this._program.programId) throw new Error('Program ID is not set'); return new TransactionBuilder<[string, number]>( this._program.api, this._program.registry, 'send_message', 'ThisThat', 'DoThis', [p1, p2, p3, p4], '(u32, String, (Option, u8), TupleStruct)', '(String, u32)', this._program.programId, ); } public noop(): TransactionBuilder { if (!this._program.programId) throw new Error('Program ID is not set'); return new TransactionBuilder( this._program.api, this._program.registry, 'send_message', 'ThisThat', 'Noop', null, null, 'Null', this._program.programId, ); } public that(): QueryBuilder<{ ok: string } | { err: string }> { return new QueryBuilder<{ ok: string } | { err: string }>( this._program.api, this._program.registry, this._program.programId, 'ThisThat', 'That', null, null, 'Result', ); } public this(): QueryBuilder { return new QueryBuilder( this._program.api, this._program.registry, this._program.programId, 'ThisThat', 'This', null, null, 'u32', ); } } export class ValueFee { constructor(private _program: SailsProgram) {} /** * Return flag if fee taken and remain value, * using special type `CommandReply` */ public doSomethingAndTakeFee(): TransactionBuilder { if (!this._program.programId) throw new Error('Program ID is not set'); return new TransactionBuilder( this._program.api, this._program.registry, 'send_message', 'ValueFee', 'DoSomethingAndTakeFee', null, null, 'bool', this._program.programId, ); } public subscribeToWithheldEvent(callback: (data: bigint) => void | Promise): Promise<() => void> { return this._program.api.gearEvents.subscribeToGearEvent('UserMessageSent', ({ data: { message } }) => { if (!message.source.eq(this._program.programId) || !message.destination.eq(ZERO_ADDRESS)) { return; } const payload = message.payload.toHex(); if (getServiceNamePrefix(payload) === 'ValueFee' && getFnNamePrefix(payload) === 'Withheld') { callback( this._program.registry .createType('(String, String, u128)', message.payload)[2] .toBigInt() as unknown as bigint, ); } }); } }