Skip to content

Commit

Permalink
feat: make typegen factories an object instance (#2359)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedsalk authored May 21, 2024
1 parent 7c08593 commit 6d1db46
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 67 deletions.
5 changes: 5 additions & 0 deletions .changeset/giant-worms-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/abi-typegen": patch
---

feat: make `typegen` factories an object instance
20 changes: 9 additions & 11 deletions packages/abi-typegen/src/templates/contract/factory.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,31 @@ const _abi = {{abiJsonString}};

const _storageSlots: StorageSlot[] = {{storageSlotsJsonString}};

export class {{capitalizedName}}__factory {
static readonly abi = _abi;
export const {{capitalizedName}}__factory = {
abi: _abi,

static readonly storageSlots = _storageSlots;
storageSlots: _storageSlots,

static createInterface(): {{capitalizedName}}Interface {
createInterface(): {{capitalizedName}}Interface {
return new Interface(_abi) as unknown as {{capitalizedName}}Interface
}
},

static connect(
connect(
id: string | AbstractAddress,
accountOrProvider: Account | Provider
): {{capitalizedName}} {
return new Contract(id, _abi, accountOrProvider) as unknown as {{capitalizedName}}
}
},

static async deployContract(
async deployContract(
bytecode: BytesLike,
wallet: Account,
options: DeployContractOptions = {}
): Promise<{{capitalizedName}}> {
const factory = new ContractFactory(bytecode, _abi, wallet);

const { storageSlots } = {{capitalizedName}}__factory;

const contract = await factory.deployContract({
storageSlots,
storageSlots: _storageSlots,
...options,
});

Expand Down
14 changes: 6 additions & 8 deletions packages/abi-typegen/src/templates/predicate/factory.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,16 @@ const _abi = {{abiJsonString}}

const _bin = '{{hexlifiedBinString}}'

export class {{capitalizedName}}__factory {
export const {{capitalizedName}}__factory = {

static readonly abi = _abi
static readonly bin = _bin;
abi: _abi,
bin: _bin,

static createInstance(provider: Provider, predicateData?: {{capitalizedName}}Inputs, configurables?: {{capitalizedName}}Configurables) {

const { abi, bin } = {{capitalizedName}}__factory
createInstance(provider: Provider, predicateData?: {{capitalizedName}}Inputs, configurables?: {{capitalizedName}}Configurables) {

const predicate = new Predicate<{{capitalizedName}}Inputs>({
bytecode: bin,
abi,
bytecode: _bin,
abi: _abi,
provider,
inputData: predicateData,
configurableConstants: configurables,
Expand Down
12 changes: 5 additions & 7 deletions packages/abi-typegen/src/templates/script/factory.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,17 @@ const _abi = {{abiJsonString}}

const _bin = '{{hexlifiedBinString}}'

export class {{capitalizedName}}__factory {
export const {{capitalizedName}}__factory = {

static readonly abi = _abi
static readonly bin = _bin
abi: _abi,
bin: _bin,

static createInstance(wallet: Account) {

const { abi, bin } = {{capitalizedName}}__factory
createInstance(wallet: Account) {

const script = new Script<
{{capitalizedName}}Inputs,
{{capitalizedName}}Output
>(bin, abi, wallet);
>(_bin, _abi, wallet);

return script;

Expand Down
20 changes: 9 additions & 11 deletions packages/abi-typegen/test/fixtures/templates/contract/factory.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -59,33 +59,31 @@ const _abi = {

const _storageSlots: StorageSlot[] = [];

export class MyContractAbi__factory {
static readonly abi = _abi;
export const MyContractAbi__factory = {
abi: _abi,

static readonly storageSlots = _storageSlots;
storageSlots: _storageSlots,

static createInterface(): MyContractAbiInterface {
createInterface(): MyContractAbiInterface {
return new Interface(_abi) as unknown as MyContractAbiInterface
}
},

static connect(
connect(
id: string | AbstractAddress,
accountOrProvider: Account | Provider
): MyContractAbi {
return new Contract(id, _abi, accountOrProvider) as unknown as MyContractAbi
}
},

static async deployContract(
async deployContract(
bytecode: BytesLike,
wallet: Account,
options: DeployContractOptions = {}
): Promise<MyContractAbi> {
const factory = new ContractFactory(bytecode, _abi, wallet);

const { storageSlots } = MyContractAbi__factory;

const contract = await factory.deployContract({
storageSlots,
storageSlots: _storageSlots,
...options,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,16 @@ const _abi = {

const _bin = '0x000'

export class MyPredicateAbi__factory {
export const MyPredicateAbi__factory = {

static readonly abi = _abi
static readonly bin = _bin;
abi: _abi,
bin: _bin,

static createInstance(provider: Provider, predicateData?: MyPredicateAbiInputs, configurables?: MyPredicateAbiConfigurables) {

const { abi, bin } = MyPredicateAbi__factory
createInstance(provider: Provider, predicateData?: MyPredicateAbiInputs, configurables?: MyPredicateAbiConfigurables) {

const predicate = new Predicate<MyPredicateAbiInputs>({
bytecode: bin,
abi,
bytecode: _bin,
abi: _abi,
provider,
inputData: predicateData,
configurableConstants: configurables,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,16 @@ const _abi = {

const _bin = '0x000'

export class MyPredicateAbi__factory {
export const MyPredicateAbi__factory = {

static readonly abi = _abi
static readonly bin = _bin;
abi: _abi,
bin: _bin,

static createInstance(provider: Provider, predicateData?: MyPredicateAbiInputs, configurables?: MyPredicateAbiConfigurables) {

const { abi, bin } = MyPredicateAbi__factory
createInstance(provider: Provider, predicateData?: MyPredicateAbiInputs, configurables?: MyPredicateAbiConfigurables) {

const predicate = new Predicate<MyPredicateAbiInputs>({
bytecode: bin,
abi,
bytecode: _bin,
abi: _abi,
provider,
inputData: predicateData,
configurableConstants: configurables,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,17 @@ const _abi = {

const _bin = '0x000'

export class MyScriptAbi__factory {
export const MyScriptAbi__factory = {

static readonly abi = _abi
static readonly bin = _bin
abi: _abi,
bin: _bin,

static createInstance(wallet: Account) {

const { abi, bin } = MyScriptAbi__factory
createInstance(wallet: Account) {

const script = new Script<
MyScriptAbiInputs,
MyScriptAbiOutput
>(bin, abi, wallet);
>(_bin, _abi, wallet);

return script;

Expand Down
12 changes: 5 additions & 7 deletions packages/abi-typegen/test/fixtures/templates/script/factory.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,17 @@ const _abi = {

const _bin = '0x000'

export class MyScriptAbi__factory {
export const MyScriptAbi__factory = {

static readonly abi = _abi
static readonly bin = _bin
abi: _abi,
bin: _bin,

static createInstance(wallet: Account) {

const { abi, bin } = MyScriptAbi__factory
createInstance(wallet: Account) {

const script = new Script<
MyScriptAbiInputs,
MyScriptAbiOutput
>(bin, abi, wallet);
>(_bin, _abi, wallet);

return script;

Expand Down

0 comments on commit 6d1db46

Please sign in to comment.