wharfkit / contract

Access table data, create actions, and retrieve types for Antelope-based Smart Contracts
https://wharfkit.com
Other
1 stars 1 forks source link

Override `args` in code generated contract #44

Closed aaroncox closed 11 months ago

aaroncox commented 11 months ago

I'd like to change Omit here to something that allows us to override the generated contract parameters.

https://github.com/wharfkit/contract/blob/6c85e09859e7bae3f859c7f53c4151fcead3e53c/src/codegen/contract.ts#L36-L45

The above creates this constructor:

https://github.com/wharfkit/contract/blob/960b1aef111fb17324d6d53c14fc639bc93c79c1/test/data/contracts/mock-rewards.ts#L37-L43

This prevents the developer from passing in either a abi or account when constructing the generated contract instance.

I found a way we can do this, by using this nifty generic type:

export type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>

This generic type makes it so whatever values you pass in as the parameters become optional, while the remaining args stay required.

Using: PartialBy<ContractArgs, 'abi' | 'account'>

Creates an interface that looks like this:

interface {
  client: APIClient // Still required
  abi?: ABI // Now optional
  account? NameType // Now optional
}

The proposed constructor using this would then be:

constructor(args: PartialBy<ContractArgs, 'abi' | 'account'>) {
    super({
        client: args.client,
        abi: args.abi || abi,
        account: args.name || Name.from('rewards.gm'),
    })
}

And the end result is that the generated contract could be used in any of these configurations:

new Contract({client})
new Contract({client, abi})
new Contract({client, account})
new Contract({client, abi, account})

@dafuga Assigning you for visibilities sake, but either of us can get this implemented 💯

dafuga commented 11 months ago

Sounds great! Will go ahead and close this since it was implemented in your last PR.