All files / src kit.ts

95.45% Statements 21/22
80% Branches 12/15
100% Functions 4/4
95.45% Lines 21/22

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 7548x 1x 1x 1x                                 1x   1x     98x     98x 98x           98x 1x   97x       98x 2x 4x       98x 1x                     307x 307x 307x                     1x  
import type {ABICacheInterface} from '@wharfkit/abicache'
import {ABICache} from '@wharfkit/abicache'
import {ABI, ABIDef, APIClient, Name, NameType} from '@wharfkit/antelope'
import {Contract} from './contract'
 
export interface ContractKitArgs {
    client: APIClient
}
 
export interface ABIDefinition {
    name: NameType
    abi: ABIDef
}
 
export interface ContractKitOptions {
    abiCache?: ABICacheInterface
    abis?: ABIDefinition[]
    debug?: boolean
}
 
const defaultContractKitOptions: ContractKitOptions = {}
 
export class ContractKit {
    readonly abiCache: ABICacheInterface
    readonly client: APIClient
    readonly debug: boolean = false
 
    constructor(args: ContractKitArgs, options: ContractKitOptions = defaultContractKitOptions) {
        if (args.client) {
            this.client = args.client
        } else E{
            throw new Error('A `client` must be passed when initializing the ContractKit.')
        }
 
        // Use either the specified cache or create one
        if (options.abiCache) {
            this.abiCache = options.abiCache
        } else {
            this.abiCache = new ABICache(this.client)
        }
 
        // If any ABIs are provided during construction, inject them into the cache
        if (options.abis) {
            options.abis.forEach(({name, abi}) =>
                this.abiCache.setAbi(Name.from(name), ABI.from(abi))
            )
        }
 
        if (options.debug) {
            this.debug = options.debug
        }
    }
 
    /**
     * Load a contract by name from an API endpoint
     *
     * @param contract The name of the contract to load
     * @returns
     */
    async load(contract: NameType): Promise<Contract> {
        const account = Name.from(contract)
        const abiDef = await this.abiCache.getAbi(account)
        return new Contract(
            {
                abi: ABI.from(abiDef),
                account,
                client: this.client,
            },
            {
                debug: this.debug,
            }
        )
    }
}