From 92de36e1899ccd56d506c6a30d76aa6f9d5c698a Mon Sep 17 00:00:00 2001 From: msinkec Date: Wed, 25 Oct 2023 13:07:48 +0200 Subject: [PATCH] Update BSV-20 buy order to use scrypt-ord package. --- src/contracts/bsv20BuyOrder.ts | 48 ++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/contracts/bsv20BuyOrder.ts b/src/contracts/bsv20BuyOrder.ts index b3e08c39..3eb4d414 100644 --- a/src/contracts/bsv20BuyOrder.ts +++ b/src/contracts/bsv20BuyOrder.ts @@ -1,9 +1,9 @@ +import { BSV20V2 } from 'scrypt-ord' import { ByteString, PubKey, Addr, Sig, - SmartContract, Utils, hash256, method, @@ -11,24 +11,14 @@ import { 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. @@ -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 } @@ -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(