-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0fda7b5
commit 4137c7a
Showing
29 changed files
with
3,881 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { BaseContract } from './base-contract' | ||
import { NoImplementation } from './impl/errors' | ||
import { LogicSig } from './logic-sig' | ||
import { bytes, uint64 } from './primitives' | ||
import { Account } from './reference' | ||
import { ConstructorFor, DeliberateAny } from './typescript-helpers' | ||
|
||
/** | ||
* Provides compiled programs and state allocation values for a Contract. Created by calling `compile(ExampleContractType)` | ||
*/ | ||
export type CompiledContract = { | ||
/** | ||
* Approval program pages for a contract, after template variables have been replaced and compiled to AVM bytecode | ||
*/ | ||
approvalProgram: [bytes, bytes] | ||
/** | ||
* Clear state program pages for a contract, after template variables have been replaced and compiled to AVM bytecode | ||
*/ | ||
clearStateProgram: [bytes, bytes] | ||
/** | ||
* By default, provides extra program pages required based on approval and clear state program size, can be overridden when calling `compile(ExampleContractType, { extraProgramPages: ... })` | ||
*/ | ||
extraProgramPages: uint64 | ||
/** | ||
* By default, provides global num uints based on contract state totals, can be overridden when calling `compile(ExampleContractType, { globalUints: ... })` | ||
*/ | ||
globalUints: uint64 | ||
/** | ||
* By default, provides global num bytes based on contract state totals, can be overridden when calling `compile(ExampleContractType, { globalBytes: ... })` | ||
*/ | ||
globalBytes: uint64 | ||
/** | ||
* By default, provides local num uints based on contract state totals, can be overridden when calling `compile(ExampleContractType, { localUints: ... })` | ||
*/ | ||
localUints: uint64 | ||
/** | ||
* By default, provides local num bytes based on contract state totals, can be overridden when calling `compile(ExampleContractType, { localBytes: ... })` | ||
*/ | ||
localBytes: uint64 | ||
} | ||
|
||
/** | ||
* Provides account for a Logic Signature. Created by calling `compile(LogicSigType)` | ||
*/ | ||
export type CompiledLogicSig = { | ||
/** | ||
* Address of a logic sig program, after template variables have been replaced and compiled to AVM bytecode | ||
*/ | ||
account: Account | ||
} | ||
|
||
/** | ||
* Options for compiling a contract | ||
*/ | ||
type CompileContractOptions = { | ||
/** | ||
* Number of extra program pages, defaults to minimum required for contract | ||
*/ | ||
extraProgramPages?: uint64 | ||
/** | ||
* Number of global uint64s, defaults to value defined for contract | ||
*/ | ||
globalUints?: uint64 | ||
/** | ||
* Number of global bytes, defaults to value defined for contract | ||
*/ | ||
globalBytes?: uint64 | ||
/** | ||
* Number of local uint64s, defaults to value defined for contract | ||
*/ | ||
localUints?: uint64 | ||
/** | ||
* Number of local bytes, defaults to value defined for contract | ||
*/ | ||
localBytes?: uint64 | ||
/** | ||
* Template variables to substitute into the contract, key should be without the prefix, must evaluate to a compile time constant | ||
* and match the type of the template var declaration | ||
*/ | ||
templateVars?: Record<string, DeliberateAny> | ||
/** | ||
* Prefix to add to provided template vars, defaults to the prefix supplied on command line (which defaults to TMPL_) | ||
*/ | ||
templateVarsPrefix?: string | ||
} | ||
|
||
/** | ||
* Options for compiling a logic signature | ||
*/ | ||
type CompileLogicSigOptions = { | ||
/** | ||
* Template variables to substitute into the contract, key should be without the prefix, must evaluate to a compile time constant | ||
* and match the type of the template var declaration | ||
*/ | ||
templateVars?: Record<string, DeliberateAny> | ||
/** | ||
* Prefix to add to provided template vars, defaults to the prefix supplied on command line (which defaults to TMPL_) | ||
*/ | ||
templateVarsPrefix?: string | ||
} | ||
|
||
/** | ||
* Compile a contract and return the resulting byte code for approval and clear state programs. | ||
* @param contract The contract class to compile | ||
* @param options Options for compiling the contract | ||
*/ | ||
export function compile(contract: ConstructorFor<BaseContract>, options?: CompileContractOptions): CompiledContract | ||
/** | ||
* Compile a logic signature and return an account ready for signing transactions. | ||
* @param logicSig The logic sig class to compile | ||
* @param options Options for compiling the logic sig | ||
*/ | ||
export function compile(logicSig: ConstructorFor<LogicSig>, options?: CompileLogicSigOptions): CompiledLogicSig | ||
export function compile(artefact: ConstructorFor<BaseContract> | ConstructorFor<LogicSig>): CompiledLogicSig | CompiledContract { | ||
throw new NoImplementation() | ||
} |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import { NoImplementation } from './impl/errors' | ||
|
||
export function TemplateVar<T>(variableName: string, prefix = 'TMPL_'): T { | ||
throw new Error('TODO') | ||
throw NoImplementation | ||
} |
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
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,53 @@ | ||
import { ContractReference, LogicSigReference } from '../../../awst/models' | ||
import { nodeFactory } from '../../../awst/node-factory' | ||
import type { SourceLocation } from '../../../awst/source-location' | ||
import { invariant } from '../../../util' | ||
import type { PType } from '../../ptypes' | ||
import { compiledContractType, compileFunctionType, ContractClassPType, LogicSigPType } from '../../ptypes' | ||
import { instanceEb } from '../../type-registry' | ||
import type { NodeBuilder } from '../index' | ||
import { FunctionBuilder } from '../index' | ||
import { parseFunctionArgs } from '../util/arg-parsing' | ||
|
||
export class CompileFunctionBuilder extends FunctionBuilder { | ||
readonly ptype = compileFunctionType | ||
|
||
call(args: ReadonlyArray<NodeBuilder>, typeArgs: ReadonlyArray<PType>, sourceLocation: SourceLocation): NodeBuilder { | ||
const { | ||
args: [contractOrSig, options], | ||
} = parseFunctionArgs({ | ||
args, | ||
typeArgs, | ||
genericTypeArgs: 0, | ||
callLocation: sourceLocation, | ||
funcName: this.typeDescription, | ||
argSpec: (a) => [a.required(ContractClassPType, LogicSigPType), a.optional()], | ||
}) | ||
|
||
if (contractOrSig.ptype instanceof ContractClassPType) { | ||
return instanceEb( | ||
nodeFactory.compiledContract({ | ||
contract: ContractReference.fromPType(contractOrSig.ptype), | ||
allocationOverrides: new Map(), | ||
prefix: null, | ||
templateVariables: new Map(), | ||
wtype: compiledContractType.wtype, | ||
sourceLocation, | ||
}), | ||
compiledContractType, | ||
) | ||
} else { | ||
invariant(contractOrSig.ptype instanceof LogicSigPType, 'ptype must be LogicSigPType') | ||
return instanceEb( | ||
nodeFactory.compiledLogicSig({ | ||
logicSig: LogicSigReference.fromPType(contractOrSig.ptype), | ||
prefix: null, | ||
templateVariables: new Map(), | ||
wtype: compiledContractType.wtype, | ||
sourceLocation, | ||
}), | ||
compiledContractType, | ||
) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.