Skip to content

Commit

Permalink
TEALScript -> PuyaTS
Browse files Browse the repository at this point in the history
  • Loading branch information
joe-p committed Nov 12, 2024
1 parent bb44d33 commit 2dc18fc
Showing 1 changed file with 53 additions and 27 deletions.
80 changes: 53 additions & 27 deletions tealscript_contracts/kitchen-sink-tealscript.algo.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,96 @@
import { Contract } from "@algorandfoundation/tealscript";
import {
abimethod,
Account,
Application,
assert,
assertMatch,
BigUint,
biguint,
Box,
BoxMap,
BoxRef,
bytes,
Bytes,
Contract,
Global,
GlobalState,
gtxn,
LocalState,
Txn,
Uint64,
uint64,
} from "@algorandfoundation/algorand-typescript";
import {
DynamicArray,
UintN,
} from "@algorandfoundation/algorand-typescript/arc4";

export class KitchenSinkContract extends Contract {
globalInt = GlobalStateKey<uint64>();
globalString = GlobalStateKey<string>({ key: "customKey" });
globalInt = GlobalState({ initialValue: Uint64(4) });
globalString = GlobalState<string>({ key: "customKey" });

localBigInt = LocalStateKey<uint<512>>();
localBigInt = LocalState<biguint>();

boxOfArray = BoxKey<uint64[]>({ key: "b" });
boxMap = BoxMap<Address, bytes>({ prefix: "" });
boxRef = BoxKey<bytes>({ key: "FF" });
boxOfArray = Box<DynamicArray<UintN<64>>>({ key: "b" });
boxMap = BoxMap<Account, bytes>({ keyPrefix: "" });
boxRef = BoxRef({ key: Bytes.fromHex("FF") });

useState(a: uint64, b: string, c: uint64) {
this.globalInt.value *= a;
if (this.globalString.exists) {
if (this.globalString.hasValue) {
this.globalString.value += b;
} else {
this.globalString.value = b;
}
if (this.txn.sender.isOptedInToApp(this.app.id)) {
this.localBigInt(this.txn.sender).value = <uint<512>>(c * a);
if (Txn.sender.isOptedIn(Global.currentApplicationId)) {
this.localBigInt(Txn.sender).value = BigUint(c) * BigUint(a);
}
}

createApplication() {
this.globalInt.value = 4;
this.globalInt.value = this.app.id;
@abimethod({ onCreate: "require", allowActions: "NoOp" })
createApp() {
this.globalInt.value = Global.currentApplicationId.id;
}

optInToApplication() {}
@abimethod({ allowActions: ["OptIn"] })
optIn() {}

addToBox(x: uint64) {
if (!this.boxOfArray.exists) {
this.boxOfArray.value = [x];
this.boxOfArray.value = new DynamicArray(new UintN<64>(x));
} else {
this.boxOfArray.value.push(x);
this.boxOfArray.value.push(new UintN<64>(x));
}
}

addToBoxMap(x: string) {
this.boxMap(this.txn.sender).value = x;
this.boxMap.set(Txn.sender, Bytes(x));
}

insertIntoBoxRef(content: bytes, offset: uint64, boxSize: uint64) {
assert(offset + content.length < boxSize);
if (this.boxRef.exists) {
this.boxRef.create(boxSize);
} else if (this.boxRef.size !== boxSize) {
this.boxRef.create({ size: boxSize });
} else if (this.boxRef.length !== boxSize) {
this.boxRef.resize(boxSize);
}
this.boxRef.splice(offset, offset + content.length, content);
}

sayHello(name: string, a: uint64): string {
return this.getHello() + name + itob(a);
return `${this.getHello()} ${name} ${Bytes(a)}`;
}

checkTransaction(pay: PayTxn) {
verifyPayTxn(pay, {
amount: { greaterThan: 1000, lessThan: 2000 },
lastValid: { greaterThan: globals.round },
sender: this.txn.sender,
receiver: this.app.address,
checkTransaction(pay: gtxn.PaymentTxn) {
assertMatch(pay, {
amount: { between: [1000, 2000] },
lastValid: { greaterThan: Global.round },
sender: Txn.sender,
receiver: Global.currentApplicationId.address,
});
}

private getHello(): string {
private getHello() {
return "Hello";
}
}

0 comments on commit 2dc18fc

Please sign in to comment.