Closed jafri closed 2 years ago
I think there is already an example:
Ok, It's more complicated than I think. Give me a moment to write a demo for you.
Here is the code. It can be compiled, but I have not tested it. Tell me if it does not work.
import {
print,
Contract,
action,
contract,
Name,
readActionData,
} from "as-chain";
@packer
class Transfer {
from!: Name
to!: Name
quantity!: Name
memo!: string
}
@packer
class NFTTransfer {
from!: Name
to!: Name
asset_ids!: u64[]
memo!: string
}
@contract("hello")
class MyContract extends Contract {
@action("transfer", notify=true)
transfer(): void {
let data = readActionData()
if (this.receiver == Name.fromString("atomicassets")) {
let t = new NFTTransfer()
t.unpack(data)
print(`${t.from} ${t.to} ${t.asset_ids}`)
} else {
let t = new Transfer()
t.unpack(data)
print(`${t.from} ${t.to} ${t.quantity}`)
}
}
}
@learnforpractice I am getting
ERROR TS2339: Property 'from' does not exist on type '~lib/array/Array<u8>'.
let data = readActionData()
let t = new NFTTransfer()
t.unpack(data)
if (data.from == this.receiver) {
if (t.from == this.receiver) {
Cool that works, just ugly warning from typescript
Yeah, the following code is much prettier. See 093fb8265b39002772eed7efe8b9471e442ff258
import {
unpackActionData,
} from "as-chain";
let t = unpackActionData<NFTTransfer>()
if (t.from == this.receiver) {
@learnforpractice last error here:
Can we change @action("transfer", notify=true)
to @action("transfer", { notify: true })
I think we need to keep the implementation simple enough.
@learnforpractice how about just @action("transfer", true)
then? We should aim to eliminate all errors in type-checking
Well, that reduces the readability. But for the reason of eliminating type-checking errors, I agree with the change.
Would you rather true or { notify: true }?
{ notify: true }
in a decorator is kind of wired. true
is fine I think.
@learnforpractice actually I saw something you did elsewhere with singleton,
lets do @action("transfer", notify)
Yeah, that's better.
Solved by e6eb2bd8db0803a597aed24f7d0ccf596490526a
I need a way to implement the following pattern
I saw the notify=true, but I think we need it to be selective to discern actions called, and cover * vs exact contract