From 85e74d4284b78d743c19f1186bbbfcd4d4944d9d Mon Sep 17 00:00:00 2001 From: Yusuf Idi Maina <120538505+yusufidimaina9989@users.noreply.github.com> Date: Wed, 27 Mar 2024 13:49:32 +0100 Subject: [PATCH 01/11] Replace Numbers with their constants value --- src/contracts/bsv20LendingPool.ts | 35 ++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) 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 From faa6fa5a1708e750db119623326463e4b99dd8ff Mon Sep 17 00:00:00 2001 From: Yusuf Idi Maina <120538505+yusufidimaina9989@users.noreply.github.com> Date: Fri, 29 Mar 2024 18:13:27 +0100 Subject: [PATCH 02/11] Update bsv20Auction.ts --- src/contracts/bsv20Auction.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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' ) From 8548b4ffee6b269b363caa1eda4398c4c584d97b Mon Sep 17 00:00:00 2001 From: Yusuf Idi Maina <120538505+yusufidimaina9989@users.noreply.github.com> Date: Fri, 29 Mar 2024 18:16:00 +0100 Subject: [PATCH 03/11] Update bsv20BuyLimitOrder.ts --- src/contracts/bsv20BuyLimitOrder.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) 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 From 76a6eef1d0a1981c7ab1d6021a550b6e3e2a8204 Mon Sep 17 00:00:00 2001 From: Yusuf Idi Maina <120538505+yusufidimaina9989@users.noreply.github.com> Date: Fri, 29 Mar 2024 18:18:19 +0100 Subject: [PATCH 04/11] Update bsv20BuyOrder.ts --- src/contracts/bsv20BuyOrder.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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' ) From 34436cb3bdbc19b8a3092ae67f315ff7d63b9dac Mon Sep 17 00:00:00 2001 From: Yusuf Idi Maina <120538505+yusufidimaina9989@users.noreply.github.com> Date: Fri, 29 Mar 2024 18:25:06 +0100 Subject: [PATCH 05/11] Update bsv20CouponBond.ts --- src/contracts/bsv20CouponBond.ts | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) 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') From 9d6133e5b5afa2c08803563bc6d8b8e99ea1b25b Mon Sep 17 00:00:00 2001 From: Yusuf Idi Maina <120538505+yusufidimaina9989@users.noreply.github.com> Date: Sun, 31 Mar 2024 12:54:49 +0100 Subject: [PATCH 06/11] Update bsv20ForwardContract.ts --- src/contracts/bsv20ForwardContract.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) 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') From 2a44cced4beb6d6506fb719ed7e1000795f75f33 Mon Sep 17 00:00:00 2001 From: Yusuf Idi Maina <120538505+yusufidimaina9989@users.noreply.github.com> Date: Sun, 31 Mar 2024 12:57:01 +0100 Subject: [PATCH 07/11] Update bsv20LendingPoolCollateral.ts --- src/contracts/bsv20LendingPoolCollateral.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) 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) From 5b104fbc500089bc32dd6e7b65097a7c647a3532 Mon Sep 17 00:00:00 2001 From: Yusuf Idi Maina <120538505+yusufidimaina9989@users.noreply.github.com> Date: Sun, 31 Mar 2024 12:59:34 +0100 Subject: [PATCH 08/11] Update bsv20Loan.ts --- src/contracts/bsv20Loan.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) 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 From f9627d9883fe463e728f50b19d70a1ec28ae7f15 Mon Sep 17 00:00:00 2001 From: Yusuf Idi Maina <120538505+yusufidimaina9989@users.noreply.github.com> Date: Sun, 31 Mar 2024 13:04:33 +0100 Subject: [PATCH 09/11] Update bsv20LoanMultiple.ts --- src/contracts/bsv20LoanMultiple.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) 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') From f2b8be204e5f3b9d7d655ae43999540bffe53865 Mon Sep 17 00:00:00 2001 From: Yusuf Idi Maina <120538505+yusufidimaina9989@users.noreply.github.com> Date: Sun, 31 Mar 2024 13:16:15 +0100 Subject: [PATCH 10/11] Update bsv20PutOption.ts --- src/contracts/bsv20PutOption.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) 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') From 68de77d91136f9905a939b3c2f5e29539126230d Mon Sep 17 00:00:00 2001 From: Yusuf Idi Maina <120538505+yusufidimaina9989@users.noreply.github.com> Date: Sun, 31 Mar 2024 13:45:57 +0100 Subject: [PATCH 11/11] Update bsv20ZeroCouponBond.ts --- src/contracts/bsv20ZeroCouponBond.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) 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')