Skip to content

Commit

Permalink
Add premium mechanism.
Browse files Browse the repository at this point in the history
  • Loading branch information
msinkec committed Nov 21, 2023
1 parent 582dbf2 commit 5a24caf
Showing 1 changed file with 42 additions and 4 deletions.
46 changes: 42 additions & 4 deletions src/contracts/bsv20Option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ export class Bsv20Option extends BSV20V2 {
@prop()
expirationTime: bigint

@prop(true)
forSale: boolean

@prop(true)
premium: bigint

constructor(
id: ByteString,
sym: ByteString,
Expand All @@ -36,7 +42,9 @@ export class Bsv20Option extends BSV20V2 {
grantee: PubKey,
tokenAmt: bigint,
strikePrice: bigint,
expirationTime: bigint
expirationTime: bigint,
forSale: boolean,
premium: bigint
) {
super(id, sym, max, dec)
this.init(...arguments)
Expand All @@ -46,6 +54,8 @@ export class Bsv20Option extends BSV20V2 {
this.tokenAmt = tokenAmt
this.strikePrice = strikePrice
this.expirationTime = expirationTime
this.forSale = forSale
this.premium = premium
}

@method()
Expand All @@ -71,13 +81,41 @@ export class Bsv20Option extends BSV20V2 {
}

@method()
public transfer(sigGrantee: Sig, newGrantee: PubKey) {
// Check grantee sig.
assert(this.checkSig(sigGrantee, this.grantee), 'invalid sig grantee')
public buy(newGrantee: PubKey) {
// Check if option is up for sale.
assert(this.forSale, 'option is not up for sale')

// Set new grantee.
const prevGrantee = this.grantee
this.grantee = newGrantee

// Toggle for sale flag.
this.forSale = false

// Propagate contract.
let outputs = this.buildStateOutputFT(this.tokenAmt)

// Make sure premium is payed to previous grantee / holder.
outputs += Utils.buildAddressOutput(
pubKey2Addr(prevGrantee),
this.premium
)

outputs += this.buildChangeOutput()
assert(hash256(outputs) == this.ctx.hashOutputs, 'hashOutputs mismatch')
}

@method()
public listForSale(sigGrantee: Sig, premium: bigint) {
// Check grantee sig.
assert(this.checkSig(sigGrantee, this.grantee), 'invalid sig grantee')

// Store premium value in property.
this.premium = premium

// Toggle for sale flag.
this.forSale = true

// Propagate contract.
let outputs = this.buildStateOutputFT(this.tokenAmt)
outputs += this.buildChangeOutput()
Expand Down

0 comments on commit 5a24caf

Please sign in to comment.