All files / src/plugins escrow.ts

96.15% Statements 25/26
100% Branches 0/0
83.33% Functions 5/6
96% Lines 24/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 9627x 27x                       27x 27x                               35x               4x 4x       4x     27x 12x 12x       12x     27x 12x 12x 12x     27x                           12x 12x 12x 12x 12x 12x                       12x      
export { Escrow, EscrowDeposit };
import {
  Field,
  method,
  DeployArgs,
  Permissions,
  SmartContract,
  UInt64,
  Signature,
  PublicKey,
  Poseidon,
  Struct,
} from "o1js";
import { MinaNFTNameServiceContract } from "../contract/names";
import { EscrowTransfer } from "../contract/escrow";
 
class EscrowDeposit extends Struct({
  data: EscrowTransfer,
  signature: Signature,
}) {
  constructor(args: { data: EscrowTransfer; signature: Signature }) {
    super(args);
  }
}
 
/**
 * class Escrow
 *
 */
class Escrow extends SmartContract {
  events = {
    deploy: Field,
    transfer: EscrowTransfer,
    deposit: EscrowTransfer,
    approveSale: EscrowTransfer,
  };
 
  async deploy(args: DeployArgs) {
    super.deploy(args);
    this.account.permissions.set({
      ...Permissions.default(),
      editState: Permissions.proof(),
    });
    this.emitEvent("deploy", Field(0));
  }
 
  @method async deposit(deposited: EscrowDeposit, buyer: PublicKey) {
    deposited.data.newOwner.assertEquals(Poseidon.hash(buyer.toFields()));
    deposited.data.tokenId.assertEquals(Field(0)); // should be MINA
    //const senderUpdate = AccountUpdate.create(buyer);
    //senderUpdate.requireSignature();
    //senderUpdate.send({ to: this.address, amount: deposited.data.price });
    this.emitEvent("deposit", deposited.data);
  }
 
  @method async approveSale(deposited: EscrowDeposit, seller: PublicKey) {
    deposited.data.oldOwner.assertEquals(Poseidon.hash(seller.toFields()));
    deposited.data.tokenId.assertEquals(Field(0)); // should be MINA
    this.emitEvent("approveSale", deposited.data);
  }
 
  @method async transfer(
    nft: PublicKey,
    nameService: PublicKey,
    data: EscrowTransfer,
    signature1: Signature,
    signature2: Signature,
    signature3: Signature,
    escrow1: PublicKey,
    escrow2: PublicKey,
    escrow3: PublicKey,
    amount: UInt64,
    seller: PublicKey,
    buyer: PublicKey
  ) {
    data.price.assertEquals(amount);
    data.tokenId.assertEquals(Field(0)); // should be MINA
    data.oldOwner.assertEquals(Poseidon.hash(seller.toFields()));
    data.newOwner.assertEquals(Poseidon.hash(buyer.toFields()));
    const minanft = new MinaNFTNameServiceContract(nameService);
    await minanft.escrowTransfer(
      nft,
      data,
      signature1,
      signature2,
      signature3,
      escrow1,
      escrow2,
      escrow3
    );
 
    //this.send({ to: seller, amount });
    this.emitEvent("transfer", data);
  }
}