Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

contract syntax #11

Open
pyramation opened this issue Oct 26, 2024 · 6 comments
Open

contract syntax #11

pyramation opened this issue Oct 26, 2024 · 6 comments

Comments

@pyramation
Copy link
Contributor

pyramation commented Oct 26, 2024

state on the class itself

import { BigNumber } from "jsd-std";

export class Counter {
  private count: BigNumber;

  constructor(initialState: BigNumber) {
    this.count = initialState;
  }

  public increment(amount: BigNumber): void {
    this.count = this.count.add(amount);
  }

  public decrement(amount: BigNumber): void {
    if (this.count.lt(amount)) {
      throw new Error("Count cannot be negative");
    }
    this.count = this.count.sub(amount);
  }

  public getCount(): BigNumber {
    return this.count;
  }
}
@pyramation
Copy link
Contributor Author

state as it's own object

import { BigNumber } from "jsd-std";

export interface State {
    count: BigNumber;
}

export class Counter {
  private state: State;

  constructor(initialState: State) {
    this.state.count = initialState;
  }

  public increment(amount: BigNumber): void {
    this.state.count = this.state.count.add(amount);
  }

  public decrement(amount: BigNumber): void {
    if (this.state.count.lt(amount)) {
      throw new Error("Count cannot be negative");
    }
    this.state.count = this.state.count.sub(amount);
  }

  public getCount(): BigNumber {
    return this.state.count;
  }
}

@pyramation
Copy link
Contributor Author

function that initializes in scope

import { BigNumber } from "jsd-std";

interface State {
  count: BigNumber;
}

// Core contract logic
export const start = (initialCount: BigNumber) => {
  let state: State = {
    count: initialCount
  };

  return {
    getCount: () => state.count,
    
    increment: (amount: BigNumber) => {
      state.count = state.count.add(amount);
      return state.count;
    },
    
    decrement: (amount: BigNumber) => {
      if (state.count.lt(amount)) {
        throw new Error("Count cannot be negative");
      }
      state.count = state.count.sub(amount);
      return state.count;
    }
  };
};

@pyramation
Copy link
Contributor Author

capabilities

capabilities.json

{
  "public": ["getCount"],
  "admin": ["getCount", "increment"],
  "creator": ["getCount", "increment", "decrement"]
}

entrypoint.ts

import { BigNumber } from "jsd-std";

interface State {
  count: BigNumber;
}

// Core contract logic
export const start = (initialCount: BigNumber) => {
  let state: State = {
    count: initialCount
  };

  return {
    getCount: () => state.count,
    
    increment: (amount: BigNumber) => {
      state.count = state.count.add(amount);
      return state.count;
    },
    
    decrement: (amount: BigNumber) => {
      if (state.count.lt(amount)) {
        throw new Error("Count cannot be negative");
      }
      state.count = state.count.sub(amount);
      return state.count;
    }
  };
};

@pyramation
Copy link
Contributor Author

decorators and function that initializes in scope

import { BigNumber } from "jsd-std";

// Permission decorators
export const admin = (target: any, propertyKey: string, descriptor: PropertyDescriptor) => descriptor;
export const creator = (target: any, propertyKey: string, descriptor: PropertyDescriptor) => descriptor;

interface State {
  count: BigNumber;
}

export const start = (initialCount: BigNumber) => {
  let state: State = {
    count: initialCount
  };

  return {
    // Public by default (no decorator needed)
    getCount: () => state.count,
    
    // Only admin and creator can increment
    @admin
    increment: (amount: BigNumber) => {
      state.count = state.count.add(amount);
      return state.count;
    },
    
    // Only creator can decrement
    @creator
    decrement: (amount: BigNumber) => {
      if (state.count.lt(amount)) {
        throw new Error("Count cannot be negative");
      }
      state.count = state.count.sub(amount);
      return state.count;
    }
  };
};

@pyramation
Copy link
Contributor Author

decorators for classes

import { BigNumber } from "jsd-std";

// Permission decorators
export const admin = (target: any, propertyKey: string, descriptor: PropertyDescriptor) => descriptor;
export const creator = (target: any, propertyKey: string, descriptor: PropertyDescriptor) => descriptor;

export interface State {
    count: BigNumber;
}

export class Counter {
  private state: State;

  constructor(initialState: State) {
    this.state.count = initialState;
  }

  // Public by default (no decorator needed)
  public getCount(): BigNumber {
    return this.state.count;
  }

  // Only admin and creator can increment
  @admin
  public increment(amount: BigNumber): void {
    this.state.count = this.state.count.add(amount);
  }

  // Only creator can decrement
  @creator
  public decrement(amount: BigNumber): void {
    if (this.state.count.lt(amount)) {
      throw new Error("Count cannot be negative");
    }
    this.state.count = this.state.count.sub(amount);
  }
}

@pyramation
Copy link
Contributor Author

modifiers?

import { BigNumber } from "jsd-std";

// Types
type Address = string;

// Events
@event
interface CounterEvents {
  Increment(who: Address, amount: BigNumber);
  Decrement(who: Address, amount: BigNumber);
  Transfer(from: Address, to: Address, amount: BigNumber);
}

// Modifiers
@modifier
function nonZero(amount: BigNumber) {
  if (amount.lte(0)) throw new Error("Amount must be positive");
}

export interface State {
  count: BigNumber;
  owner: Address;
  admins: Map<Address, boolean>;
  balance: BigNumber;
}

export class Counter {
  private state: State;

  constructor(initialState: State) {
    this.state.count = initialState.count;
    this.state.owner = msg.sender; // assuming we have msg.sender
    this.state.admins = new Map();
    this.state.balance = BigNumber.from(0);
  }

  // View function (readonly)
  @view
  public getCount(): BigNumber {
    return this.state.count;
  }

  // Admin function with event emission
  @admin
  @nonZero
  public increment(amount: BigNumber): void {
    this.state.count = this.state.count.add(amount);
    emit.Increment(msg.sender, amount);
  }

  // Creator function with event emission
  @creator
  @nonZero
  public decrement(amount: BigNumber): void {
    if (this.state.count.lt(amount)) {
      throw new Error("Count cannot be negative");
    }
    this.state.count = this.state.count.sub(amount);
    emit.Decrement(msg.sender, amount);
  }

  // Payable function example
  @payable
  public deposit(): void {
    this.state.balance = this.state.balance.add(msg.value);
  }

  // Admin management
  @creator
  public addAdmin(admin: Address): void {
    this.state.admins.set(admin, true);
  }

  @creator
  public removeAdmin(admin: Address): void {
    this.state.admins.delete(admin);
  }

  // Balance withdrawal
  @creator
  public withdraw(amount: BigNumber): void {
    if (this.state.balance.lt(amount)) {
      throw new Error("Insufficient balance");
    }
    this.state.balance = this.state.balance.sub(amount);
    // Assuming we have transfer capability
    transfer(msg.sender, amount);
    emit.Transfer(this.address, msg.sender, amount);
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant