o1-labs / docs2

Docs website for the Mina Protocol.
https://docs.minaprotocol.com
87 stars 130 forks source link

`TokenContract` instead of `this.token` #890

Closed Pfed-prog closed 6 months ago

Pfed-prog commented 7 months ago

Not really sure how to utilize this.token. Could not find relevant information on how to implement the required functionalities.

https://docs.minaprotocol.com/zkapps/o1js-reference/classes/TokenContract

image image

Possibly affected tutorial: https://docs.minaprotocol.com/zkapps/tutorials/advanced-account-updates

mitschabaude commented 7 months ago

Not really sure how to utilize this.token. Could not find relevant information on how to implement the required functionalities.

I don't get what you're asking tbh. What you mean with "how to implement the required functionalities"? Implement what? What's the use case?

Regarding TokenContract, it's an abstract base class, like SmartContract. You don't instantiate new TokenContract() directly -- write your own token contract which extends TokenContract

Re this.token, it now lives on this.internal on TokenContract. Mainly because it's only supposed to be used by token contracts -- for minting, sending, burning

Pfed-prog commented 7 months ago

Absolutely, thank you very much. I am just not sure how to use minting or other methods within already created contracts. Because my editor says it is going to be deprecated.

mitschabaude commented 7 months ago

I am just not sure how to use minting or other methods within already created contracts. Because my editor says it is going to be deprecated.

Change your token contract to extend TokenContract instead of SmartContract. Use this.internal instead of this.token.

It will ask you to implement the abstract approveBase() method. As a first step, you can just refuse to actually implement it:

async approveBase(_: AccountUpdateForest) {
  throw Error('not implemented");
}

then check if your app still works and report back here if it doesn't!

Pfed-prog commented 6 months ago

Implemented empty method to extendTokenContract

@method approveBase() {}

@method transfer() {}

Instead of transfer, implemented

@method public transferNFT(
    item: NFT,
    newOwner: PublicKey,
    keyWitness: MerkleMapWitness,
    adminSignature: Signature
  ): Bool {
    this.verifyAdminItemSignature(item, adminSignature);
    const sender: PublicKey = this.verifyTreeLeaf(item, keyWitness);
    item.changeOwner(newOwner);
    const itemHash: Field = item.hash();
    const [rootAfter] = keyWitness.computeRootAndKey(itemHash);
    this.internal.send({
      from: sender,
      to: newOwner,
      amount: UInt64.from(1_000_000_000),
    });
    this.emitEvent('transferred-nft', itemHash);
    this.emitEvent('updated-merkle-key', item.id);
    this.updateRoot(rootAfter);
    return Bool(true);
  }