diff --git a/src/contracts/bsv20Auction.ts b/src/contracts/bsv20Auction.ts index 401ec664..02776542 100644 --- a/src/contracts/bsv20Auction.ts +++ b/src/contracts/bsv20Auction.ts @@ -12,6 +12,7 @@ import { slice, StatefulNext, pubKey2Addr, + Constants, } from 'scrypt-ts' import { BSV20V2, OrdiMethodCallOptions } from 'scrypt-ord' @@ -105,7 +106,8 @@ export class BSV20Auction extends BSV20V2 { // Ensure the first input is spending the auctioned ordinal UTXO. assert( - slice(this.prevouts, 0n, 36n) == this.ordinalPrevout, + slice(this.prevouts, 0n, Constants.OutpointLen) == + this.ordinalPrevout, 'first input is not spending specified ordinal UTXO' ) diff --git a/src/contracts/bsv20BuyLimitOrder.ts b/src/contracts/bsv20BuyLimitOrder.ts index dbc1d50a..6665514f 100644 --- a/src/contracts/bsv20BuyLimitOrder.ts +++ b/src/contracts/bsv20BuyLimitOrder.ts @@ -13,6 +13,7 @@ import { assert, len, byteString2Int, + Constants, } from 'scrypt-ts' import { RabinPubKey, RabinSig, RabinVerifier } from 'scrypt-ts-lib' @@ -71,12 +72,18 @@ export class BSV20BuyLimitOrder extends BSV20V2 { // Check that we're unlocking the UTXO specified in the oracles message. assert( - slice(this.prevouts, 36n, 72n) == slice(oracleMsg, 0n, 36n), + slice( + this.prevouts, + Constants.OutpointLen, + Constants.OutpointLen * 2n + ) == slice(oracleMsg, 0n, Constants.OutpointLen), 'second input is not spending specified ordinal UTXO' ) // Get token amount held by the UTXO from oracle message. - const utxoTokenAmt = byteString2Int(slice(oracleMsg, 36n, 44n)) + const utxoTokenAmt = byteString2Int( + slice(oracleMsg, Constants.OutpointLen, 44n) + ) // Check token amount doesn't exceed total. const remainingToClear = this.tokenAmt - this.tokenAmtCleared diff --git a/src/contracts/bsv20BuyOrder.ts b/src/contracts/bsv20BuyOrder.ts index 5ccd4026..9a99ad51 100644 --- a/src/contracts/bsv20BuyOrder.ts +++ b/src/contracts/bsv20BuyOrder.ts @@ -12,6 +12,7 @@ import { pubKey2Addr, assert, len, + Constants, } from 'scrypt-ts' import { RabinPubKey, RabinSig, RabinVerifier } from 'scrypt-ts-lib' @@ -60,7 +61,11 @@ export class BSV20BuyOrder extends BSV20V2 { // Check that we're unlocking the UTXO specified in the oracles message. assert( - slice(this.prevouts, 36n, 72n) == slice(oracleMsg, 0n, 36n), + slice( + this.prevouts, + Constants.OutpointLen, + Constants.OutpointLen * 2n + ) == slice(oracleMsg, 0n, Constants.OutpointLen), 'second input is not spending specified ordinal UTXO' ) diff --git a/src/contracts/bsv20CouponBond.ts b/src/contracts/bsv20CouponBond.ts index 8c6e75dd..86a9193f 100644 --- a/src/contracts/bsv20CouponBond.ts +++ b/src/contracts/bsv20CouponBond.ts @@ -3,6 +3,7 @@ import { assert, ByteString, byteString2Int, + Constants, fill, FixedArray, hash256, @@ -101,12 +102,18 @@ export class Bsv20CouponBond extends BSV20V2 { // Check that we're unlocking the UTXO specified in the oracles message. assert( - slice(this.prevouts, 36n, 72n) == slice(oracleMsg, 0n, 36n), + slice( + this.prevouts, + Constants.OutpointLen, + Constants.OutpointLen * 2n + ) == slice(oracleMsg, 0n, Constants.OutpointLen), 'second input is not spending specified ordinal UTXO' ) // Get token amount held by the UTXO from oracle message. - const utxoTokenAmt = byteString2Int(slice(oracleMsg, 36n, 44n)) + const utxoTokenAmt = byteString2Int( + slice(oracleMsg, Constants.OutpointLen, 44n) + ) // Ensure token amount is equal to the face value. assert(utxoTokenAmt == this.faceValue, 'utxo token amount insufficient') @@ -152,7 +159,11 @@ export class Bsv20CouponBond extends BSV20V2 { // Check that we're unlocking the UTXO specified in the oracles message. assert( - slice(this.prevouts, 36n, 72n) == slice(oracleMsg, 0n, 36n), + slice( + this.prevouts, + Constants.OutpointLen, + Constants.OutpointLen * 2n + ) == slice(oracleMsg, 0n, Constants.OutpointLen), 'second input is not spending specified ordinal UTXO' ) @@ -173,7 +184,9 @@ export class Bsv20CouponBond extends BSV20V2 { } // Get token amount held by the UTXO from oracle message. - const utxoTokenAmt = byteString2Int(slice(oracleMsg, 36n, 44n)) + const utxoTokenAmt = byteString2Int( + slice(oracleMsg, Constants.OutpointLen, 44n) + ) // Ensure utxo token amount covers output token amount. assert(utxoTokenAmt >= totalAmt, 'utxo token amount insufficient') @@ -199,7 +212,11 @@ export class Bsv20CouponBond extends BSV20V2 { // Check that we're unlocking the UTXO specified in the oracles message. assert( - slice(this.prevouts, 36n, 72n) == slice(oracleMsg, 0n, 36n), + slice( + this.prevouts, + Constants.OutpointLen, + Constants.OutpointLen * 2n + ) == slice(oracleMsg, 0n, Constants.OutpointLen), 'second input is not spending specified ordinal UTXO' ) @@ -219,7 +236,9 @@ export class Bsv20CouponBond extends BSV20V2 { } // Get token amount held by the UTXO from oracle message. - const utxoTokenAmt = byteString2Int(slice(oracleMsg, 36n, 44n)) + const utxoTokenAmt = byteString2Int( + slice(oracleMsg, Constants.OutpointLen, 44n) + ) // Ensure utxo token amount covers output token amount. assert(utxoTokenAmt >= totalAmt, 'utxo token amount insufficient') diff --git a/src/contracts/bsv20ForwardContract.ts b/src/contracts/bsv20ForwardContract.ts index 004ed026..44742f0e 100644 --- a/src/contracts/bsv20ForwardContract.ts +++ b/src/contracts/bsv20ForwardContract.ts @@ -1,4 +1,3 @@ -import { assert } from 'console' import { BSV20V2 } from 'scrypt-ord' import { ByteString, @@ -11,6 +10,8 @@ import { Sig, slice, Utils, + assert, + Constants, } from 'scrypt-ts' import { RabinPubKey, RabinSig, RabinVerifier } from 'scrypt-ts-lib' @@ -120,12 +121,18 @@ export class Bsv20ForwardContract extends BSV20V2 { // Check that we're unlocking the UTXO specified in the oracles message. assert( - slice(this.prevouts, 36n, 72n) == slice(oracleMsg, 0n, 36n), + slice( + this.prevouts, + Constants.OutpointLen, + Constants.OutpointLen * 2n + ) == slice(oracleMsg, 0n, Constants.OutpointLen), 'second input is not spending specified ordinal UTXO' ) // Get token amount held by the UTXO from oracle message. - const utxoTokenAmt = byteString2Int(slice(oracleMsg, 36n, 44n)) + const utxoTokenAmt = byteString2Int( + slice(oracleMsg, Constants.OutpointLen, 44n) + ) // Check token amount is correct. assert(utxoTokenAmt == this.amt, 'invalid token amount') diff --git a/src/contracts/bsv20LendingPool.ts b/src/contracts/bsv20LendingPool.ts index 4d52faf9..e87fe60b 100644 --- a/src/contracts/bsv20LendingPool.ts +++ b/src/contracts/bsv20LendingPool.ts @@ -3,6 +3,7 @@ import { assert, ByteString, byteString2Int, + Constants, fill, FixedArray, hash256, @@ -138,9 +139,23 @@ export class Bsv20LendingPool extends BSV20V2 { // Make sure second input spends collateral holding contract. const prevTxId = this.ctx.utxo.outpoint.txid - const prevoutCollateralContract = slice(this.prevouts, 36n, 72n) - assert(slice(prevoutCollateralContract, 0n, 32n) == prevTxId) - assert(byteString2Int(slice(prevoutCollateralContract, 32n, 36n)) == 1n) + const prevoutCollateralContract = slice( + this.prevouts, + Constants.OutpointLen, + Constants.OutpointLen * 2n + ) + assert( + slice(prevoutCollateralContract, 0n, Constants.TxIdLen) == prevTxId + ) + assert( + byteString2Int( + slice( + prevoutCollateralContract, + Constants.TxIdLen, + Constants.OutpointLen + ) + ) == 1n + ) // Check merkle proof. const prevTxid = Sha256(this.ctx.utxo.outpoint.txid) @@ -173,12 +188,15 @@ export class Bsv20LendingPool extends BSV20V2 { // Check that we're unlocking the UTXO specified in the oracles message. assert( - slice(this.prevouts, 72n, 108n) == slice(oracleMsg, 0n, 36n), + slice(this.prevouts, Constants.OutpointLen * 2n, 108n) == + slice(oracleMsg, 0n, Constants.OutpointLen), 'third input is not spending specified ordinal UTXO' ) // Get token amount held by the UTXO from oracle message. - const utxoTokenAmt = byteString2Int(slice(oracleMsg, 36n, 44n)) + const utxoTokenAmt = byteString2Int( + slice(oracleMsg, Constants.OutpointLen, 44n) + ) // Check token amount is correct. assert(utxoTokenAmt == amt, 'invalid token amount') @@ -322,12 +340,15 @@ export class Bsv20LendingPool extends BSV20V2 { // Check that we're unlocking the UTXO specified in the oracles message. assert( - slice(this.prevouts, 72n, 108n) == slice(oracleMsg, 0n, 36n), + slice(this.prevouts, Constants.OutpointLen * 2n, 108n) == + slice(oracleMsg, 0n, Constants.OutpointLen), 'third input is not spending specified ordinal UTXO' ) // Get token amount held by the UTXO from oracle message. - const utxoTokenAmt = byteString2Int(slice(oracleMsg, 36n, 44n)) + const utxoTokenAmt = byteString2Int( + slice(oracleMsg, Constants.OutpointLen, 44n) + ) // Check token amount is correct. const interest = (borrower.amt * this.interestRate) / 100n diff --git a/src/contracts/bsv20LendingPoolCollateral.ts b/src/contracts/bsv20LendingPoolCollateral.ts index 8b1ffbb4..b1a5177a 100644 --- a/src/contracts/bsv20LendingPoolCollateral.ts +++ b/src/contracts/bsv20LendingPoolCollateral.ts @@ -1,6 +1,7 @@ import { assert, byteString2Int, + Constants, hash256, method, prop, @@ -25,9 +26,13 @@ export class Bsv20LendingPoolCollateral extends SmartContract { // Make sure first input spends main contract. const prevTxId = this.ctx.utxo.outpoint.txid - const prevoutContract = slice(this.prevouts, 0n, 36n) - assert(slice(prevoutContract, 0n, 32n) == prevTxId) - assert(byteString2Int(slice(prevoutContract, 32n, 36n)) == 0n) + const prevoutContract = slice(this.prevouts, 0n, Constants.OutpointLen) + assert(slice(prevoutContract, 0n, Constants.TxIdLen) == prevTxId) + assert( + byteString2Int( + slice(prevoutContract, Constants.TxIdLen, Constants.OutpointLen) + ) == 0n + ) // Propagate contract. const output = this.buildStateOutput(this.amt) diff --git a/src/contracts/bsv20Loan.ts b/src/contracts/bsv20Loan.ts index cbd9a0e8..83acc05b 100644 --- a/src/contracts/bsv20Loan.ts +++ b/src/contracts/bsv20Loan.ts @@ -3,6 +3,7 @@ import { assert, ByteString, byteString2Int, + Constants, hash256, method, prop, @@ -104,12 +105,18 @@ export class Bsv20Loan extends BSV20V2 { // Check that we're unlocking the UTXO specified in the oracles message. assert( - slice(this.prevouts, 36n, 72n) == slice(oracleMsg, 0n, 36n), + slice( + this.prevouts, + Constants.OutpointLen, + Constants.OutpointLen * 2n + ) == slice(oracleMsg, 0n, Constants.OutpointLen), 'second input is not spending specified ordinal UTXO' ) // Get token amount held by the UTXO from oracle message. - const utxoTokenAmt = byteString2Int(slice(oracleMsg, 36n, 44n)) + const utxoTokenAmt = byteString2Int( + slice(oracleMsg, Constants.OutpointLen, 44n) + ) // Check token amount is correct. const interest = (this.tokenAmt * this.interestRate) / 100n diff --git a/src/contracts/bsv20LoanMultiple.ts b/src/contracts/bsv20LoanMultiple.ts index 702dc9fb..f91878dc 100644 --- a/src/contracts/bsv20LoanMultiple.ts +++ b/src/contracts/bsv20LoanMultiple.ts @@ -3,6 +3,7 @@ import { assert, ByteString, byteString2Int, + Constants, fill, FixedArray, hash256, @@ -204,12 +205,18 @@ export class Bsv20LoanMultiple extends BSV20V2 { // Check that we're unlocking the UTXO specified in the oracles message. assert( - slice(this.prevouts, 36n, 72n) == slice(oracleMsg, 0n, 36n), + slice( + this.prevouts, + Constants.OutpointLen, + Constants.OutpointLen * 2n + ) == slice(oracleMsg, 0n, Constants.OutpointLen), 'second input is not spending specified ordinal UTXO' ) // Get token amount held by the UTXO from oracle message. - const utxoTokenAmt = byteString2Int(slice(oracleMsg, 36n, 44n)) + const utxoTokenAmt = byteString2Int( + slice(oracleMsg, Constants.OutpointLen, 44n) + ) // Check token amount is correct. assert(utxoTokenAmt == borrower.amt, 'invalid token amount') @@ -256,12 +263,18 @@ export class Bsv20LoanMultiple extends BSV20V2 { // Check that we're unlocking the UTXO specified in the oracles message. assert( - slice(this.prevouts, 36n, 72n) == slice(oracleMsg, 0n, 36n), + slice( + this.prevouts, + Constants.OutpointLen, + Constants.OutpointLen * 2n + ) == slice(oracleMsg, 0n, Constants.OutpointLen), 'second input is not spending specified ordinal UTXO' ) // Get token amount held by the UTXO from oracle message. - const utxoTokenAmt = byteString2Int(slice(oracleMsg, 36n, 44n)) + const utxoTokenAmt = byteString2Int( + slice(oracleMsg, Constants.OutpointLen, 44n) + ) // Check token amount is correct. assert(utxoTokenAmt == borrower.amt, 'invalid token amount') diff --git a/src/contracts/bsv20PutOption.ts b/src/contracts/bsv20PutOption.ts index 0e65468b..a820cebe 100644 --- a/src/contracts/bsv20PutOption.ts +++ b/src/contracts/bsv20PutOption.ts @@ -3,6 +3,7 @@ import { assert, ByteString, byteString2Int, + Constants, hash256, method, prop, @@ -80,12 +81,18 @@ export class Bsv20PutOption extends BSV20V2 { // Check that we're unlocking the UTXO specified in the oracles message. assert( - slice(this.prevouts, 36n, 72n) == slice(oracleMsg, 0n, 36n), + slice( + this.prevouts, + Constants.OutpointLen, + Constants.OutpointLen * 2n + ) == slice(oracleMsg, 0n, Constants.OutpointLen), 'second input is not spending specified ordinal UTXO' ) // Get token amount held by the UTXO from oracle message. - const utxoTokenAmt = byteString2Int(slice(oracleMsg, 36n, 44n)) + const utxoTokenAmt = byteString2Int( + slice(oracleMsg, Constants.OutpointLen, 44n) + ) // Check token amount is correct. assert(utxoTokenAmt == this.tokenAmt, 'invalid token amount') diff --git a/src/contracts/bsv20ZeroCouponBond.ts b/src/contracts/bsv20ZeroCouponBond.ts index 0c7b381b..d13005b4 100644 --- a/src/contracts/bsv20ZeroCouponBond.ts +++ b/src/contracts/bsv20ZeroCouponBond.ts @@ -3,6 +3,7 @@ import { assert, ByteString, byteString2Int, + Constants, fill, FixedArray, hash256, @@ -101,12 +102,18 @@ export class Bsv20ZeroCouponBond extends BSV20V2 { // Check that we're unlocking the UTXO specified in the oracles message. assert( - slice(this.prevouts, 36n, 72n) == slice(oracleMsg, 0n, 36n), + slice( + this.prevouts, + Constants.OutpointLen, + Constants.OutpointLen * 2n + ) == slice(oracleMsg, 0n, Constants.OutpointLen), 'second input is not spending specified ordinal UTXO' ) // Get token amount held by the UTXO from oracle message. - const utxoTokenAmt = byteString2Int(slice(oracleMsg, 36n, 44n)) + const utxoTokenAmt = byteString2Int( + slice(oracleMsg, Constants.OutpointLen, 44n) + ) // Ensure token amount is equal to the purchase price. assert( @@ -154,7 +161,11 @@ export class Bsv20ZeroCouponBond extends BSV20V2 { // Check that we're unlocking the UTXO specified in the oracles message. assert( - slice(this.prevouts, 36n, 72n) == slice(oracleMsg, 0n, 36n), + slice( + this.prevouts, + Constants.OutpointLen, + Constants.OutpointLen * 2n + ) == slice(oracleMsg, 0n, Constants.OutpointLen), 'second input is not spending specified ordinal UTXO' ) @@ -174,7 +185,9 @@ export class Bsv20ZeroCouponBond extends BSV20V2 { } // Get token amount held by the UTXO from oracle message. - const utxoTokenAmt = byteString2Int(slice(oracleMsg, 36n, 44n)) + const utxoTokenAmt = byteString2Int( + slice(oracleMsg, Constants.OutpointLen, 44n) + ) // Ensure utxo token amount covers output token amount. assert(utxoTokenAmt >= totalAmt, 'utxo token amount insufficient')