All files / src/tmp contract.ts

25% Statements 5/20
0% Branches 0/8
0% Functions 0/5
25% Lines 5/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 921x                                         1x       1x       1x 1x                                                                                                                          
import {
    ABISerializableObject,
    Action,
    AnyAction,
    Checksum256,
    Name,
    NameType,
} from '@greymass/eosio'
 
// stubs for session kit
interface Session {
    transact(SessionTransactArgs: any): Promise<SessionTransactResult>
}
interface SessionTransactArgs {
    actions: AnyAction[]
}
export interface SessionTransactResult {
    id: Checksum256
}
 
// TODO: move this to core
export function isABISerializableObject(value: any): value is ABISerializableObject {
    return value.constructor && typeof value.constructor.abiName === 'string'
}
 
export class Contract {
    /** Account where contract is deployed. */
    static account: Name
 
    private static _shared: Contract | null = null
    private static _session: Session | null = null
 
    /** Shared instance of the contract. */
    static shared<T extends {new ()}>(this: T): InstanceType<T> {
        const self = this as unknown as typeof Contract
        if (!self._shared) {
            self._shared = new self()
        }
        return self._shared as InstanceType<T>
    }
 
    /** Account where contract is deployed. */
    get account() {
        return (this.constructor as typeof Contract).account
    }
 
    /** Call a contract action. */
    async call(
        name: NameType,
        data: ABISerializableObject | {[key: string]: any}
    ): Promise<SessionTransactResult> {
        let action: Action
        if (isABISerializableObject(data)) {
            action = Action.from({
                account: this.account,
                name,
                authorization: [],
                data,
            })
 
            return {id: Checksum256.from('random_id')}
        } else {
            // TODO: here we need to fetch the ABI and construct the action
            throw new Error('Not implemented')
        }
        // TODO: resolve session and transact
        throw new Error('Not implemented')
    }
 
    /** Generate a contract action. */
 
    async getAction(
        name: NameType,
        data: ABISerializableObject | {[key: string]: any}
    ): Promise<Action> {
        let action: Action
        if (isABISerializableObject(data)) {
            action = Action.from({
                account: this.account,
                name,
                authorization: [],
                data,
            })
        } else {
            // TODO: here we need to fetch the ABI and construct the action
            throw new Error('Not implemented')
        }
        // TODO: resolve session and transact
        throw new Error('Not implemented')
    }
}