uuosio / ascdk

MIT License
12 stars 9 forks source link

return values not working as expected #86

Open boid-com opened 7 months ago

boid-com commented 7 months ago

@table("payrolls")
export class Payroll extends Table {
  constructor(
    /** unique id of this payroll */
    public id:u64 = 0,
    /** the total amount that will be paid out by this payroll */
    public total:Asset = new Asset(),
    /** the total amount that has been paid by this payroll, when finished will be equal to `total` */
    public paid:Asset = new Asset(),
    /** when payments should start being available to claim */
    public startTime:TimePointSec = new TimePointSec(),
    /** when the total amount should be released to the reciver */
    public finishTime:TimePointSec = new TimePointSec(),
    /** the last time this payroll was paid */
    public lastPayout:TimePointSec = new TimePointSec(),
    /** the minimum amount of time the reciver needs to wait in between claming funds from this payroll */
    public minClaimFrequencySec:u32 = 0,
    /** the chain account that will be able to withdraw the funds */
    public receiverAccount:Name = new Name(),
    /** the account holding funds for thie payroll, if this is an external account then the payroll contract will need to have authority to withdraw funds from it. */
    public treasuryAccount:Name = new Name(),
    /** when paused the payroll can't be claimed */
    public paused:bool = false
  ) {
    super()
  }

  @primary
  get primary():u64 {
    return this.id
  }
}

  @action("payroll.rm")
  payrollRm(payrollId:u64):Payroll {
    requireAuth(this.receiver)
    const existing = this.payrollsT.requireGet(payrollId, "payroll not found")
    this.payrollsT.remove(existing)
    return existing
  }

I'm getting compile errors when trying to return a class structure from an action result. I get similar errors if it's just a packer class (not a table) and I also get an error if I try to return a class that does not derive from packer. Returning basic types like boolean seems to work.

WARNING AS201: Conversion from type 'usize' to 'i32' will require an explicit cast when switching between 32/64-bit.

             size += ret_value.getSize();
learnforpractice commented 7 months ago

Well, an action is not meant to return a value. The following link may help improve your understanding of writing contracts in AssemblyScript https://learnforpractice.github.io/ascdk-book

boid-com commented 7 months ago

action return values is a feature now supported by antelope in the c++ CDT, I thought this feature was available? https://github.com/pinax-network/action-return-value

learnforpractice commented 7 months ago

Use setActionReturnValue to set the return value of an action.

https://github.com/uuosio/ascdk/blob/3a47b0ea170e69da4a0aaf7ca30aa4abdfa80916/as-packages/chain/assembly/action.ts#L59C17-L59C37

boid-com commented 7 months ago

@learnforpractice can you explain how to use setActionReturnValue or show an example? I'm confused about the actual usage.

learnforpractice commented 7 months ago
import {
    Name,
    check,
    Encoder,
    setActionReturnValue,
    Contract,
    printString,
} from "asm-chain";

@packer
class MyData {
    constructor(
        public name: string
    ){}
}

@contract
class MyContract extends Contract{
    @action("test")
    test(): void {
        let data = new MyData("Hello,World");
        setActionReturnValue(Encoder.pack(data));
    }
}
boid-com commented 6 months ago

I'm getting this error from the Encoder.pack() Argument of type 'MyData' is not assignable to parameter of type 'Packer'. Type 'MyData' is missing the following properties from type 'Packer': pack, unpack, getSizets(2345)

image

learnforpractice commented 6 months ago

Part of the code is generated by the compiler, so ignore such warnings if the code can be compiled.

boid-com commented 6 months ago

Is it possible to improve that? it's not very clear as a developer.

learnforpractice commented 6 months ago

Yes, you can implement pack, unpack, and getSize manually. Also, You can take the generated code at target directory as a reference.