import { ContractSpec, Address } from 'stellar-sdk'; import { Buffer } from "buffer"; import { AssembledTransaction, Ok, Err } from './assembled-tx.js'; import type { u32, i32, u64, i64, u128, i128, u256, i256, Option, Typepoint, Duration, Error_, Result, } from './assembled-tx.js'; import type { ClassOptions, XDR_BASE64 } from './method-options.js'; export * from './assembled-tx.js'; export * from './method-options.js'; if (typeof window !== 'undefined') { //@ts-ignore Buffer exists window.Buffer = window.Buffer || Buffer; } export const networks = { testnet: { networkPassphrase: "Test SDF Network ; September 2015", contractId: "CADADRF7G2OYJCZUFSRULQDDWWNAODH7723DWSBGRPOBHELV2WAM74OZ", } } as const /** */ export const Errors = { } export class Contract { spec: ContractSpec; constructor(public readonly options: ClassOptions) { this.spec = new ContractSpec([ "AAAAAAAAAAAAAAAEcGxheQAAAAEAAAAAAAAADXBsYXllcl9jaG9pY2UAAAAAAAAGAAAAAQAAAAY=", "AAAAAAAAAAAAAAAQZGV0ZXJtaW5lX3dpbm5lcgAAAAIAAAAAAAAABnBsYXllcgAAAAAABgAAAAAAAAAIY29udHJhY3QAAAAGAAAAAQAAAAY=" ]); } private readonly parsers = { play: (result: XDR_BASE64): u64 => this.spec.funcResToNative("play", result), determineWinner: (result: XDR_BASE64): u64 => this.spec.funcResToNative("determine_winner", result) }; private txFromJSON = (json: string): AssembledTransaction => { const { method, ...tx } = JSON.parse(json) return AssembledTransaction.fromJSON( { ...this.options, method, parseResultXdr: this.parsers[method], }, tx, ); } public readonly fromJSON = { play: this.txFromJSON>, determineWinner: this.txFromJSON> } /** * Construct and simulate a play transaction. Returns an `AssembledTransaction` object which will have a `result` field containing the result of the simulation. If this transaction changes contract state, you will need to call `signAndSend()` on the returned object. */ play = async ({player_choice}: {player_choice: u64}, options: { /** * The fee to pay for the transaction. Default: 100. */ fee?: number, } = {}) => { return await AssembledTransaction.fromSimulation({ method: 'play', args: this.spec.funcArgsToScVals("play", {player_choice}), ...options, ...this.options, errorTypes: Errors, parseResultXdr: this.parsers['play'], }); } /** * Construct and simulate a determine_winner transaction. Returns an `AssembledTransaction` object which will have a `result` field containing the result of the simulation. If this transaction changes contract state, you will need to call `signAndSend()` on the returned object. */ determineWinner = async ({player, contract}: {player: u64, contract: u64}, options: { /** * The fee to pay for the transaction. Default: 100. */ fee?: number, } = {}) => { return await AssembledTransaction.fromSimulation({ method: 'determine_winner', args: this.spec.funcArgsToScVals("determine_winner", {player, contract}), ...options, ...this.options, errorTypes: Errors, parseResultXdr: this.parsers['determineWinner'], }); } }