Skip to content

Commit

Permalink
Merge pull request #335 from michavie/endpoint-owner-only
Browse files Browse the repository at this point in the history
Add Endpoint definition Owner-Only Support
  • Loading branch information
andreibancioiu authored Oct 6, 2023
2 parents ece6a7a + 8018b05 commit 60ace12
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
19 changes: 19 additions & 0 deletions src/smartcontracts/typesystem/endpoint.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { assert } from "chai";
import { EndpointDefinition } from "./endpoint";


describe("test endpoint", () => {
it('should handle an only-owner modifier', async () => {
const actual = EndpointDefinition.fromJSON({
name: 'foo',
ownerOnly: true,
mutability: 'payable',
payableInTokens: [],
inputs: [],
outputs: [],
})

assert.isTrue(actual.modifiers.ownerOnly)
assert.isTrue(actual.modifiers.isOwnerOnly())
})
});
12 changes: 10 additions & 2 deletions src/smartcontracts/typesystem/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,21 @@ export class EndpointDefinition {

static fromJSON(json: {
name: string,
ownerOnly?: boolean
mutability: string,
payableInTokens: string[],
inputs: any[],
outputs: any[]
}): EndpointDefinition {
json.name = json.name == null ? NamePlaceholder : json.name;
json.ownerOnly = json.ownerOnly || false;
json.payableInTokens = json.payableInTokens || [];
json.inputs = json.inputs || [];
json.outputs = json.outputs || [];

let input = json.inputs.map(param => EndpointParameterDefinition.fromJSON(param));
let output = json.outputs.map(param => EndpointParameterDefinition.fromJSON(param));
let modifiers = new EndpointModifiers(json.mutability, json.payableInTokens);
let modifiers = new EndpointModifiers(json.mutability, json.payableInTokens, json.ownerOnly);

return new EndpointDefinition(json.name, input, output, modifiers);
}
Expand All @@ -44,10 +46,12 @@ export class EndpointDefinition {
export class EndpointModifiers {
readonly mutability: string;
readonly payableInTokens: string[];
readonly ownerOnly: boolean;

constructor(mutability: string, payableInTokens: string[]) {
constructor(mutability: string, payableInTokens: string[], ownerOnly?: boolean) {
this.mutability = mutability || "";
this.payableInTokens = payableInTokens || [];
this.ownerOnly = ownerOnly || false;
}

isPayableInEGLD(): boolean {
Expand Down Expand Up @@ -77,6 +81,10 @@ export class EndpointModifiers {
isReadonly() {
return this.mutability == "readonly";
}

isOwnerOnly() {
return this.ownerOnly;
}
}

export class EndpointParameterDefinition {
Expand Down

0 comments on commit 60ace12

Please sign in to comment.