-
Notifications
You must be signed in to change notification settings - Fork 1
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
Comments
state as it's own objectimport { 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;
}
} |
function that initializes in scopeimport { 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;
}
};
}; |
capabilities
|
decorators and function that initializes in scopeimport { 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;
}
};
}; |
decorators for classesimport { 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);
}
} |
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
state on the class itself
The text was updated successfully, but these errors were encountered: