-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cli): Support initializers not named constructor in cli (#5397)
Adds a new `--initializer` option to CLI deploy for specifying the name of the initializer function, otherwise falls back to fetching the default initializer (based on name, visibility, param length, etc). Fixes issue reported by @signorecello.
- Loading branch information
1 parent
1381064
commit 85f14c5
Showing
6 changed files
with
217 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { ContractArtifact, FunctionArtifact, FunctionType, getDefaultInitializer, getInitializer } from './abi.js'; | ||
|
||
describe('abi', () => { | ||
describe('getDefaultInitializer', () => { | ||
it('does not return non initializer functions', () => { | ||
const contract = { functions: [{ isInitializer: false }] } as ContractArtifact; | ||
expect(getDefaultInitializer(contract)).toBeUndefined(); | ||
}); | ||
|
||
it('returns the single initializer in a contract', () => { | ||
const contract = { | ||
functions: [ | ||
{ name: 'non-init', isInitializer: false }, | ||
{ name: 'init', isInitializer: true }, | ||
], | ||
} as ContractArtifact; | ||
expect(getDefaultInitializer(contract)?.name).toEqual('init'); | ||
}); | ||
|
||
it('prefers functions based on name', () => { | ||
const contract = { | ||
functions: [ | ||
{ name: 'foo', isInitializer: true }, | ||
{ name: 'constructor', isInitializer: true }, | ||
], | ||
} as ContractArtifact; | ||
expect(getDefaultInitializer(contract)?.name).toEqual('constructor'); | ||
}); | ||
|
||
it('prefers functions based on parameter length', () => { | ||
const contract = { | ||
functions: [ | ||
{ name: 'foo', parameters: [{}], isInitializer: true }, | ||
{ name: 'bar', parameters: [], isInitializer: true }, | ||
], | ||
} as ContractArtifact; | ||
expect(getDefaultInitializer(contract)?.name).toEqual('bar'); | ||
}); | ||
|
||
it('prefers functions based on type', () => { | ||
const contract = { | ||
functions: [ | ||
{ name: 'foo', isInitializer: true, functionType: FunctionType.OPEN }, | ||
{ name: 'bar', isInitializer: true, functionType: FunctionType.SECRET }, | ||
], | ||
} as ContractArtifact; | ||
expect(getDefaultInitializer(contract)?.name).toEqual('bar'); | ||
}); | ||
|
||
it('returns an initializer if there is any', () => { | ||
const contract = { | ||
functions: [ | ||
{ name: 'foo', isInitializer: true }, | ||
{ name: 'bar', isInitializer: true }, | ||
], | ||
} as ContractArtifact; | ||
expect(getDefaultInitializer(contract)?.name).toBeDefined(); | ||
}); | ||
}); | ||
|
||
describe('getInitializer', () => { | ||
it('returns initializer based on name', () => { | ||
const contract = { | ||
functions: [ | ||
{ name: 'foo', isInitializer: true }, | ||
{ name: 'bar', isInitializer: true }, | ||
], | ||
} as ContractArtifact; | ||
expect(getInitializer(contract, 'bar')?.name).toEqual('bar'); | ||
}); | ||
|
||
it('fails if named initializer not found', () => { | ||
const contract = { | ||
functions: [ | ||
{ name: 'foo', isInitializer: true }, | ||
{ name: 'bar', isInitializer: true }, | ||
], | ||
} as ContractArtifact; | ||
expect(() => getInitializer(contract, 'baz')).toThrow(); | ||
}); | ||
|
||
it('fails if named initializer not an initializer', () => { | ||
const contract = { | ||
functions: [ | ||
{ name: 'foo', isInitializer: true }, | ||
{ name: 'bar', isInitializer: false }, | ||
], | ||
} as ContractArtifact; | ||
expect(() => getInitializer(contract, 'bar')).toThrow(); | ||
}); | ||
|
||
it('falls back to default initializer on undefined argument', () => { | ||
const contract = { | ||
functions: [ | ||
{ name: 'foo', isInitializer: true }, | ||
{ name: 'initializer', isInitializer: true }, | ||
], | ||
} as ContractArtifact; | ||
expect(getInitializer(contract, undefined)?.name).toEqual('initializer'); | ||
}); | ||
|
||
it('passes artifact through', () => { | ||
const contract = {} as ContractArtifact; | ||
const artifact = { name: 'foo', isInitializer: true } as FunctionArtifact; | ||
expect(getInitializer(contract, artifact)?.name).toEqual('foo'); | ||
}); | ||
|
||
it('validates artifact is initializer', () => { | ||
const contract = {} as ContractArtifact; | ||
const artifact = { name: 'foo', isInitializer: false } as FunctionArtifact; | ||
expect(() => getInitializer(contract, artifact)).toThrow(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters