Skip to content

Commit

Permalink
Add Contract and ActionWrapper helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
jafri committed Feb 22, 2022
1 parent 66b8f92 commit 9393cab
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 163 deletions.
46 changes: 46 additions & 0 deletions as-packages/chain/assembly/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { PermissionLevel, Action } from "./action"
import { MultiIndexValue } from "./mi"
import { Name } from "./name"

export class ActionWrapperAct {
constructor(
public action: Name,
public contract: Name,
public permissionLevel: PermissionLevel
){}

send <T extends MultiIndexValue>(data: T): void {
const permissions = [this.permissionLevel]
const action = new Action(permissions, this.contract, this.action, data.pack())
action.send()
}
}

export class ActionWrapper {
public action: Name
constructor(action: string){
this.action = Name.fromString(action)
}

/**
* Create an action with act given contract, actor, and permission
* @param {Name} contract - The name of the contract
* @param {Name} actor - The name of the account that is executing the contract.
* @param {string} permission - The permission that the actor must have to execute the contract.
* @returns An instance of the Act class.
*/
act (
contract: Name,
permissionLevel: PermissionLevel
): ActionWrapperAct {
return new ActionWrapperAct(this.action, contract, permissionLevel)
}
}

export class Contract {
constructor(
public receiver: Name,
public firstReceiver: Name,
public action: Name
) {}
}
2 changes: 2 additions & 0 deletions as-packages/chain/assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export {

export {MultiIndex, MultiIndexValue, SAME_PAYER} from "./mi";

export {Contract, ActionWrapper} from "./helpers"

export {
getSender,
readActionData,
Expand Down
9 changes: 2 additions & 7 deletions as-packages/chain/tests/testaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
printString,
check,
getSender,
Contract,
} from "as-chain";

@packer
Expand All @@ -17,13 +18,7 @@ class MyData {
}

@contract("hello")
class MyContract {
constructor(
public receiver: Name,
public firstReceiver: Name,
public action: Name) {
}

class MyContract extends Contract {
@action("testgencode")
testGenCode(
a1: string,
Expand Down
14 changes: 2 additions & 12 deletions as-packages/chain/tests/testasset.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
import { Name, Asset, Symbol, check, isValid, printString, contract, action } from "as-chain";
import { Asset, Symbol, check, isValid, printString, contract, action, Contract } from "as-chain";

@contract("hello")
class MyContract {
receiver: Name;
firstReceiver: Name;
action: Name

constructor(receiver: Name, firstReceiver: Name, action: Name) {
this.receiver = receiver;
this.firstReceiver = firstReceiver;
this.action = action;
}

class MyContract extends Contract{
stringToU64(s: string): u64 {
let value: u64 = 0;
for (let i=0; i<s.length; i++) {
Expand Down
13 changes: 2 additions & 11 deletions as-packages/chain/tests/testcrypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {

print,
check,
Contract,
} from "as-chain";

@packer
Expand All @@ -40,17 +41,7 @@ class TestClass<T extends u32> {
}

@contract("hello")
class MyContract {
receiver: Name;
firstReceiver: Name;
action: Name

constructor(receiver: Name, firstReceiver: Name, action: Name) {
this.receiver = receiver;
this.firstReceiver = firstReceiver;
this.action = action;
}

class MyContract extends Contract {
@action("test")
Test(message: string, digest: Checksum256, sig: Signature, pub: PublicKey): void {
// message:'hello,world'
Expand Down
13 changes: 2 additions & 11 deletions as-packages/chain/tests/testmi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
secondary,
action,
Float128,
Contract,
} from "as-chain";

@table("mydata")
Expand Down Expand Up @@ -89,17 +90,7 @@ class MyData {
}

@contract("hello")
class MyContract {
receiver: Name;
firstReceiver: Name;
action: Name

constructor(receiver: Name, firstReceiver: Name, action: Name) {
this.receiver = receiver;
this.firstReceiver = firstReceiver;
this.action = action;
}

class MyContract extends Contract{
@action("testmi")
testmi(): void {
let mi = MyData.new(this.receiver, this.receiver);
Expand Down
14 changes: 2 additions & 12 deletions as-packages/chain/tests/testprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,11 @@ import {
printqf,
printn,
Float128,
Name,
Contract,
} from "as-chain";

@contract("hello")
class MyContract {
receiver: Name;
firstReceiver: Name;
action: Name

constructor(receiver: Name, firstReceiver: Name, action: Name) {
this.receiver = receiver;
this.firstReceiver = firstReceiver;
this.action = action;
}

class MyContract extends Contract{
@action("test")
testPrint(
a1: Float128
Expand Down
10 changes: 2 additions & 8 deletions as-packages/chain/tests/testpublickey.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Name, PublicKey, packer, action, contract, printString, printHex, check } from "as-chain";
import { PublicKey, packer, action, contract, printString, printHex, check, Contract } from "as-chain";

@packer
class MyData {
Expand All @@ -8,13 +8,7 @@ class MyData {
}

@contract("hello")
class MyContract {
constructor(
public receiver: Name,
public firstReceiver: Name,
public action: Name) {
}

class MyContract extends Contract {
hexToBytes(hex: string): u8[] {
let bytes = new Array<u8>();
for (let c = 0; c < hex.length; c += 2) {
Expand Down
13 changes: 2 additions & 11 deletions as-packages/chain/tests/testserializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
Signature,
TimePoint,
TimePointSec,
Contract,
} from "as-chain";

@packer
Expand All @@ -36,17 +37,7 @@ class TestClass<T extends u32> {
}

@contract("hello")
class MyContract {
receiver: Name;
firstReceiver: Name;
action: Name

constructor(receiver: Name, firstReceiver: Name, action: Name) {
this.receiver = receiver;
this.firstReceiver = firstReceiver;
this.action = action;
}

class MyContract extends Contract{
@action("test1")
testEncodeDecode(): void {
{
Expand Down
15 changes: 3 additions & 12 deletions as-packages/chain/tests/testsystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,12 @@ import {
check,
TimePoint,
TimePointSec,
print
print,
Contract
} from "as-chain";

@contract("hello")
class MyContract {
receiver: Name;
firstReceiver: Name;
action: Name

constructor(receiver: Name, firstReceiver: Name, action: Name) {
this.receiver = receiver;
this.firstReceiver = firstReceiver;
this.action = action;
}

class MyContract extends Contract{
@action("test")
testSerializer(
a1: TimePoint,
Expand Down
14 changes: 2 additions & 12 deletions as-packages/chain/tests/testtable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Name, Asset, MultiIndex, primary, contract, table, action } from "as-chain";
import { Asset, Contract, primary, contract, table, action } from "as-chain";

@table("mytable")
class MyTable {
Expand All @@ -15,17 +15,7 @@ class MyTable {
}

@contract("hello")
class MyContract {
receiver: Name;
firstReceiver: Name;
action: Name

constructor(receiver: Name, firstReceiver: Name, action: Name) {
this.receiver = receiver;
this.firstReceiver = firstReceiver;
this.action = action;
}

class MyContract extends Contract{
@action("testtable")
testTable(): void {
let mi = MyTable.new(this.receiver, this.receiver);
Expand Down
6 changes: 2 additions & 4 deletions examples/eosio.token/eosio.token.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { Name, Asset, Symbol, check, requireAuth, MultiIndex, hasAuth, isAccount, requireRecipient, contract, action, SAME_PAYER } from 'as-chain'
import { Name, Asset, Symbol, check, requireAuth, MultiIndex, hasAuth, isAccount, requireRecipient, contract, action, SAME_PAYER, Contract } from 'as-chain'
import { Account, Stat, currency_stats, account } from './tables';

@contract("eosio.token")
class TokenContract {
constructor(public receiver: Name, public firstReceiver: Name, public action: Name) {}

class TokenContract extends Contract {
getStatTable(sym: Symbol): MultiIndex<currency_stats> {
return Stat.new(this.receiver, new Name(sym.code()));
}
Expand Down
26 changes: 8 additions & 18 deletions examples/inlineaction/inlineaction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Name, Action, PermissionLevel, printString, action, contract, packer } from "as-chain"
import { Contract, PermissionLevel, printString, action, contract, packer, ActionWrapper } from "as-chain"

@packer
class MyData {
Expand All @@ -7,29 +7,19 @@ class MyData {
){}
}

@contract("hello")
class MyContract {
constructor(
public receiver: Name,
public firstReceiver: Name,
public action: Name) {
}

@contract("inlineaction")
class InlineAction extends Contract {
@action("saygoodbye")
sayGoodbye(name: string): void {
printString(`+++goodbye, ${name}\n`)
}

@action("sayhello")
sayHello(name: string): void {
let hello = new MyData('alice');
let a = new Action(
[new PermissionLevel(this.receiver, Name.fromString("active"))],
this.receiver,
Name.fromString("saygoodbye"),
hello.pack(),
);
a.send();
const actionData = new MyData('alice');
const actionWrapper = new ActionWrapper("saygoodbye")
const action = actionWrapper.act(this.receiver, new PermissionLevel(this.receiver))
action.send(actionData)
printString(`hello, ${name}\n`)
}
}
}
Loading

0 comments on commit 9393cab

Please sign in to comment.