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

return values not working as expected #86

Open
boid-com opened this issue Feb 19, 2024 · 9 comments
Open

return values not working as expected #86

boid-com opened this issue Feb 19, 2024 · 9 comments

Comments

@boid-com
Copy link

@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
Copy link
Contributor

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
Copy link
Author

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
Copy link
Contributor

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
Copy link
Author

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

@learnforpractice
Copy link
Contributor

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
Copy link
Author

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
Copy link
Contributor

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

@boid-com
Copy link
Author

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

@learnforpractice
Copy link
Contributor

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

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

2 participants