Skip to content

Commit c20bce6

Browse files
authored
Merge pull request #311 from algorandfoundation/feat/new_event_logger
feat: new event logger
2 parents 9518873 + a89acf7 commit c20bce6

File tree

5 files changed

+93
-58
lines changed

5 files changed

+93
-58
lines changed

src/lib/compiler.ts

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ type TypeInfo =
7070
length: number;
7171
};
7272

73+
type Event = {
74+
name: string;
75+
args: { name: string; type: TypeInfo; desc: string }[];
76+
desc: string;
77+
argTupleType: TypeInfo;
78+
};
79+
7380
function getConstantInitializer(node: ts.Node): ts.Node | undefined {
7481
if (!node.isKind(ts.SyntaxKind.Identifier)) return undefined;
7582
const definitionNode = node.getDefinitionNodes().at(-1);
@@ -1719,7 +1726,7 @@ export default class Compiler {
17191726

17201727
private disableTypeScript: boolean;
17211728

1722-
private events: Record<string, TypeInfo[]> = {};
1729+
private events: Record<string, Event> = {};
17231730

17241731
constructor(content: string, className: string, project: Project, options?: CompilerOptions) {
17251732
this.project = project;
@@ -4720,11 +4727,32 @@ export default class Compiler {
47204727
}
47214728

47224729
const initTypeArgs = init.getTypeArguments();
4723-
if (!initTypeArgs[0].isKind(ts.SyntaxKind.TupleType))
4724-
throw Error('EventLogger type argument must be a tuple of types');
4730+
if (!initTypeArgs[0].isKind(ts.SyntaxKind.TypeLiteral)) {
4731+
throw Error(`EventLogger type argument must be a type literal`);
4732+
}
47254733

4726-
this.events[node.getNameNode().getText()] =
4727-
initTypeArgs[0].getElements().map((t) => getTypeInfo(t.getType())) || [];
4734+
this.events[node.getNameNode().getText()] = {
4735+
name: node.getNameNode().getText(),
4736+
args: [],
4737+
desc: node
4738+
.getJsDocs()
4739+
.map((d) => d.getCommentText())
4740+
.join(''),
4741+
argTupleType: getTypeInfo(initTypeArgs[0].getType()),
4742+
};
4743+
4744+
const event = this.events[node.getNameNode().getText()];
4745+
4746+
initTypeArgs[0].getProperties().forEach((p) => {
4747+
const desc = p
4748+
.getJsDocs()
4749+
.map((d) => d.getCommentText())
4750+
.join();
4751+
4752+
const name = p.getName();
4753+
const type = getTypeInfo(p.getType());
4754+
event.args.push({ name, type, desc });
4755+
});
47284756
} else if (init.isKind(ts.SyntaxKind.CallExpression) && init.getExpression().getText() === 'ScratchSlot') {
47294757
if (init.getTypeArguments()?.length !== 1) throw Error('ScratchSlot must have one type argument ');
47304758

@@ -4913,16 +4941,15 @@ export default class Compiler {
49134941
// If this is an event
49144942
if (chain[0].isKind(ts.SyntaxKind.PropertyAccessExpression) && this.events[chain[0].getNameNode().getText()]) {
49154943
const name = chain[0].getNameNode().getText();
4916-
const types = this.events[name];
4917-
const typesTuple: TypeInfo = { kind: 'tuple', elements: types };
4944+
const { argTupleType } = this.events[name];
49184945

49194946
if (!chain[1].isKind(ts.SyntaxKind.PropertyAccessExpression) || !chain[2].isKind(ts.SyntaxKind.CallExpression))
49204947
throw Error(`Unsupported ${chain[1].getKindName()} ${chain[1].getText()}`);
49214948

49224949
if (chain[1].getNameNode().getText() !== 'log')
49234950
throw Error(`Unsupported event method ${chain[1].getNameNode().getText()}`);
49244951

4925-
const argTypes = typeInfoToABIString(typesTuple)
4952+
const argTypes = typeInfoToABIString(argTupleType)
49264953
.replace(/asset/g, 'uint64')
49274954
.replace(/account/g, 'address')
49284955
.replace(/application/g, 'uint64');
@@ -4931,9 +4958,9 @@ export default class Compiler {
49314958

49324959
const selector = sha512_256(Buffer.from(signature)).slice(0, 8);
49334960

4934-
this.typeHint = typesTuple;
4961+
this.typeHint = argTupleType;
49354962
this.pushVoid(chain[2], `byte 0x${selector} // ${signature}`);
4936-
this.processArrayElements(chain[2].getArguments(), chain[2]);
4963+
this.processNode(chain[2].getArguments()[0]);
49374964
this.pushVoid(chain[2], 'concat');
49384965

49394966
this.pushVoid(chain[2], 'log');

0 commit comments

Comments
 (0)