Skip to content

Commit

Permalink
Update BSV-20 buy order to use scrypt-ord package.
Browse files Browse the repository at this point in the history
  • Loading branch information
msinkec committed Oct 25, 2023
1 parent 22a14d7 commit 92de36e
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions src/contracts/bsv20BuyOrder.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,24 @@
import { BSV20V2 } from 'scrypt-ord'
import {
ByteString,
PubKey,
Addr,
Sig,
SmartContract,
Utils,
hash256,
method,
prop,
slice,
pubKey2Addr,
assert,
len,
} from 'scrypt-ts'
import { RabinPubKey, RabinSig, RabinVerifier } from 'scrypt-ts-lib'

export class BSV20BuyOrder extends SmartContract {
// Inscription data of BSV-20 transfer we're
// looking to buy.
// This contains the ordered tokens ticker symbol and amount.
// Example:
// OP_FALSE OP_IF 6f7264 OP_TRUE application/bsv-20 OP_FALSE
// {
// "p": "bsv-20",
// "op": "transfer",
// "tick": "ordi",
// "amt": "1000"
// }
// OP_ENDIF
export class BSV20BuyOrder extends BSV20V2 {
// Amount of tokens we're buying.
@prop()
transferInscription: ByteString
readonly tokenAmt: bigint

// Public key of the oracle, that is used to verify the transfers
// genesis.
Expand All @@ -40,12 +30,17 @@ export class BSV20BuyOrder extends SmartContract {
buyer: PubKey

constructor(
transferInscription: ByteString,
id: ByteString,
max: bigint,
dec: bigint,
tokenAmt: bigint,
oraclePubKey: RabinPubKey,
buyer: PubKey
) {
super(...arguments)
this.transferInscription = transferInscription
super(id, max, dec)
this.init(...arguments)

this.tokenAmt = tokenAmt
this.oraclePubKey = oraclePubKey
this.buyer = buyer
}
Expand All @@ -68,16 +63,25 @@ export class BSV20BuyOrder extends SmartContract {
'first input is not spending specified ordinal UTXO'
)

// Build transfer inscription.
const transferInscription = BSV20V2.createTransferInsciption(
this.id,
this.tokenAmt
)
const transferInscriptionLen = len(transferInscription)

// Check that the ordinal UTXO contains the right inscription.
assert(
slice(oracleMsg, 36n) == this.transferInscription,
slice(oracleMsg, transferInscriptionLen) == transferInscription,
'unexpected inscription from oracle'
)

// Ensure the tokens ared being payed out to the buyer.
let outScript = Utils.buildPublicKeyHashScript(pubKey2Addr(this.buyer))
outScript += this.transferInscription
let outputs = Utils.buildOutput(outScript, 1n)
let outputs = BSV20V2.buildTransferOutput(
pubKey2Addr(this.buyer),
this.id,
this.tokenAmt
)

// Ensure the second output is paying the offer to the seller.
outputs += Utils.buildPublicKeyHashOutput(
Expand Down

0 comments on commit 92de36e

Please sign in to comment.