From bfb21c7fd188c68e391271c4aefacc186f45d214 Mon Sep 17 00:00:00 2001 From: msinkec Date: Mon, 11 Dec 2023 11:39:17 +0100 Subject: [PATCH] Minor fixes for bsv-20 contracts. --- src/contracts/bsv20BuyLimitOrder.ts | 4 ++-- src/contracts/bsv20BuyOrder.ts | 4 ++-- src/contracts/bsv20CallOption.ts | 3 +++ src/contracts/bsv20CouponBond.ts | 17 ++++++++++------- src/contracts/bsv20Loan.ts | 8 ++++---- src/contracts/bsv20LoanMultiple.ts | 22 +++++++++++----------- src/contracts/bsv20LockToMint.ts | 2 +- src/contracts/bsv20Mint.ts | 2 +- src/contracts/bsv20PutOption.ts | 7 +++++-- src/contracts/bsv20SellLimitOrder.ts | 7 ------- src/contracts/bsv20ZeroCouponBond.ts | 13 ++++++++----- 11 files changed, 47 insertions(+), 42 deletions(-) diff --git a/src/contracts/bsv20BuyLimitOrder.ts b/src/contracts/bsv20BuyLimitOrder.ts index bd045409..dbc1d50a 100644 --- a/src/contracts/bsv20BuyLimitOrder.ts +++ b/src/contracts/bsv20BuyLimitOrder.ts @@ -71,8 +71,8 @@ export class BSV20BuyLimitOrder extends BSV20V2 { // Check that we're unlocking the UTXO specified in the oracles message. assert( - slice(this.prevouts, 0n, 36n) == slice(oracleMsg, 0n, 36n), - 'first input is not spending specified ordinal UTXO' + slice(this.prevouts, 36n, 72n) == slice(oracleMsg, 0n, 36n), + 'second input is not spending specified ordinal UTXO' ) // Get token amount held by the UTXO from oracle message. diff --git a/src/contracts/bsv20BuyOrder.ts b/src/contracts/bsv20BuyOrder.ts index 3026fc20..5ccd4026 100644 --- a/src/contracts/bsv20BuyOrder.ts +++ b/src/contracts/bsv20BuyOrder.ts @@ -60,8 +60,8 @@ export class BSV20BuyOrder extends BSV20V2 { // Check that we're unlocking the UTXO specified in the oracles message. assert( - slice(this.prevouts, 0n, 36n) == slice(oracleMsg, 0n, 36n), - 'first input is not spending specified ordinal UTXO' + slice(this.prevouts, 36n, 72n) == slice(oracleMsg, 0n, 36n), + 'second input is not spending specified ordinal UTXO' ) // Build transfer inscription. diff --git a/src/contracts/bsv20CallOption.ts b/src/contracts/bsv20CallOption.ts index a8212b61..0aa7694b 100644 --- a/src/contracts/bsv20CallOption.ts +++ b/src/contracts/bsv20CallOption.ts @@ -110,6 +110,9 @@ export class Bsv20CallOption extends BSV20V2 { // Check grantee sig. assert(this.checkSig(sigGrantee, this.grantee), 'invalid sig grantee') + // Check premium is a positive integer. + assert(premium > 0n, 'invalid premium value') + // Store premium value in property. this.premium = premium diff --git a/src/contracts/bsv20CouponBond.ts b/src/contracts/bsv20CouponBond.ts index 15436334..8c6e75dd 100644 --- a/src/contracts/bsv20CouponBond.ts +++ b/src/contracts/bsv20CouponBond.ts @@ -101,8 +101,8 @@ export class Bsv20CouponBond extends BSV20V2 { // Check that we're unlocking the UTXO specified in the oracles message. assert( - slice(this.prevouts, 0n, 36n) == slice(oracleMsg, 0n, 36n), - 'first input is not spending specified ordinal UTXO' + slice(this.prevouts, 36n, 72n) == slice(oracleMsg, 0n, 36n), + 'second input is not spending specified ordinal UTXO' ) // Get token amount held by the UTXO from oracle message. @@ -113,7 +113,7 @@ export class Bsv20CouponBond extends BSV20V2 { // Check slot index is empty. const investor = this.investors[Number(slotIdx)] - assert(investor.emptySlot == true, 'slot is not empty') + assert(investor.emptySlot, 'slot is not empty') // Add to investors array. this.investors[Number(slotIdx)] = { @@ -152,8 +152,8 @@ export class Bsv20CouponBond extends BSV20V2 { // Check that we're unlocking the UTXO specified in the oracles message. assert( - slice(this.prevouts, 0n, 36n) == slice(oracleMsg, 0n, 36n), - 'first input is not spending specified ordinal UTXO' + slice(this.prevouts, 36n, 72n) == slice(oracleMsg, 0n, 36n), + 'second input is not spending specified ordinal UTXO' ) // For each investor add an output that pays them interest. @@ -199,8 +199,8 @@ export class Bsv20CouponBond extends BSV20V2 { // Check that we're unlocking the UTXO specified in the oracles message. assert( - slice(this.prevouts, 0n, 36n) == slice(oracleMsg, 0n, 36n), - 'first input is not spending specified ordinal UTXO' + slice(this.prevouts, 36n, 72n) == slice(oracleMsg, 0n, 36n), + 'second input is not spending specified ordinal UTXO' ) // For each investor add an output that pays them the face value token amount. @@ -239,6 +239,9 @@ export class Bsv20CouponBond extends BSV20V2 { 'invalid sig investor' ) + // Check price value. + assert(price > 0n, 'invalid price value') + // Toggle forSale flag and set price. this.investors[Number(slotIdx)].forSale = true this.investors[Number(slotIdx)].price = price diff --git a/src/contracts/bsv20Loan.ts b/src/contracts/bsv20Loan.ts index b8136b52..cbd9a0e8 100644 --- a/src/contracts/bsv20Loan.ts +++ b/src/contracts/bsv20Loan.ts @@ -75,7 +75,7 @@ export class Bsv20Loan extends BSV20V2 { @method() public borrow() { // Check loan isn't taken yet. - assert(this.taken == false, 'loan already taken') + assert(!this.taken, 'loan already taken') this.taken = true // Pay borrower the principal, i.e. token amount locked in the contract. @@ -94,7 +94,7 @@ export class Bsv20Loan extends BSV20V2 { @method() public repay(oracleMsg: ByteString, oracleSig: RabinSig) { // Check loan is already taken. - assert(this.taken == true, 'loan not taken yet') + assert(this.taken, 'loan not taken yet') // Check oracle signature. assert( @@ -104,8 +104,8 @@ export class Bsv20Loan extends BSV20V2 { // Check that we're unlocking the UTXO specified in the oracles message. assert( - slice(this.prevouts, 0n, 36n) == slice(oracleMsg, 0n, 36n), - 'first input is not spending specified ordinal UTXO' + slice(this.prevouts, 36n, 72n) == slice(oracleMsg, 0n, 36n), + 'second input is not spending specified ordinal UTXO' ) // Get token amount held by the UTXO from oracle message. diff --git a/src/contracts/bsv20LoanMultiple.ts b/src/contracts/bsv20LoanMultiple.ts index 082c5392..292b14dd 100644 --- a/src/contracts/bsv20LoanMultiple.ts +++ b/src/contracts/bsv20LoanMultiple.ts @@ -101,7 +101,7 @@ export class Bsv20LoanMultiple extends BSV20V2 { ) { // Check slot index is empty. const borrower = this.borrowers[Number(slotIdx)] - assert(borrower.emptySlot == true, 'slot is not empty') + assert(borrower.emptySlot, 'slot is not empty') // Check borrower sig. assert( @@ -130,8 +130,8 @@ export class Bsv20LoanMultiple extends BSV20V2 { public cancelRequest(slotIdx: bigint, borrowerSig: Sig) { // Check slot index is not empty and not yet approved. const borrower = this.borrowers[Number(slotIdx)] - assert(borrower.emptySlot == false, 'slot is empty') - assert(borrower.approved == false, 'request was already approved') + assert(!borrower.emptySlot, 'slot is empty') + assert(!borrower.approved, 'request was already approved') // Check borrower sig. assert( @@ -169,8 +169,8 @@ export class Bsv20LoanMultiple extends BSV20V2 { ) { // Check slot index is not empty and not yet approved. const borrower = this.borrowers[Number(slotIdx)] - assert(borrower.emptySlot == false, 'slot is empty') - assert(borrower.approved == false, 'request was already approved') + assert(!borrower.emptySlot, 'slot is empty') + assert(!borrower.approved, 'request was already approved') // Check lender sig. assert(this.checkSig(lenderSig, this.lender), 'invalid sig lender') @@ -204,8 +204,8 @@ export class Bsv20LoanMultiple extends BSV20V2 { // Check that we're unlocking the UTXO specified in the oracles message. assert( - slice(this.prevouts, 0n, 36n) == slice(oracleMsg, 0n, 36n), - 'first input is not spending specified ordinal UTXO' + slice(this.prevouts, 36n, 72n) == slice(oracleMsg, 0n, 36n), + 'second input is not spending specified ordinal UTXO' ) // Get token amount held by the UTXO from oracle message. @@ -239,7 +239,7 @@ export class Bsv20LoanMultiple extends BSV20V2 { ) { // Check slot index is not empty and approved. const borrower = this.borrowers[Number(slotIdx)] - assert(borrower.emptySlot == false, 'slot is empty') + assert(!borrower.emptySlot, 'slot is empty') assert(borrower.approved, 'borrow request not approved') // Check borrower sig. @@ -256,8 +256,8 @@ export class Bsv20LoanMultiple extends BSV20V2 { // Check that we're unlocking the UTXO specified in the oracles message. assert( - slice(this.prevouts, 0n, 36n) == slice(oracleMsg, 0n, 36n), - 'first input is not spending specified ordinal UTXO' + slice(this.prevouts, 36n, 72n) == slice(oracleMsg, 0n, 36n), + 'second input is not spending specified ordinal UTXO' ) // Get token amount held by the UTXO from oracle message. @@ -294,7 +294,7 @@ export class Bsv20LoanMultiple extends BSV20V2 { public foreclose(slotIdx: bigint, lenderSig: Sig) { // Check slot index is not empty and approved. const borrower = this.borrowers[Number(slotIdx)] - assert(borrower.emptySlot == false, 'slot is empty') + assert(!borrower.emptySlot, 'slot is empty') assert(borrower.approved, 'borrow request not approved') // Check lender sig. diff --git a/src/contracts/bsv20LockToMint.ts b/src/contracts/bsv20LockToMint.ts index 10f5f07b..c3e9c6a8 100644 --- a/src/contracts/bsv20LockToMint.ts +++ b/src/contracts/bsv20LockToMint.ts @@ -53,7 +53,7 @@ export class BSV20LockToMint extends BSV20V2 { let outputs = toByteString('') let transferAmt = amount - if (this.supply - transferAmt > 0n) { + if (this.supply > transferAmt) { // If there are still tokens left, then update supply and // build state output inscribed with leftover tokens. this.supply -= transferAmt diff --git a/src/contracts/bsv20Mint.ts b/src/contracts/bsv20Mint.ts index cd97bcac..b19926d7 100644 --- a/src/contracts/bsv20Mint.ts +++ b/src/contracts/bsv20Mint.ts @@ -58,7 +58,7 @@ export class BSV20Mint extends BSV20V2 { let outputs = toByteString('') let transferAmt = amount - if (this.supply - transferAmt > 0n) { + if (this.supply > transferAmt) { // If there are still tokens left, then update supply and // build state output inscribed with leftover tokens. this.supply -= transferAmt diff --git a/src/contracts/bsv20PutOption.ts b/src/contracts/bsv20PutOption.ts index 842d48e7..0e65468b 100644 --- a/src/contracts/bsv20PutOption.ts +++ b/src/contracts/bsv20PutOption.ts @@ -80,8 +80,8 @@ export class Bsv20PutOption extends BSV20V2 { // Check that we're unlocking the UTXO specified in the oracles message. assert( - slice(this.prevouts, 0n, 36n) == slice(oracleMsg, 0n, 36n), - 'first input is not spending specified ordinal UTXO' + slice(this.prevouts, 36n, 72n) == slice(oracleMsg, 0n, 36n), + 'second input is not spending specified ordinal UTXO' ) // Get token amount held by the UTXO from oracle message. @@ -140,6 +140,9 @@ export class Bsv20PutOption extends BSV20V2 { // Check grantee sig. assert(this.checkSig(sigGrantee, this.grantee), 'invalid sig grantee') + // Check premium value. + assert(premium > 0n, 'invalid premium value') + // Store premium value in property. this.premium = premium diff --git a/src/contracts/bsv20SellLimitOrder.ts b/src/contracts/bsv20SellLimitOrder.ts index a9a64551..97093841 100644 --- a/src/contracts/bsv20SellLimitOrder.ts +++ b/src/contracts/bsv20SellLimitOrder.ts @@ -66,13 +66,6 @@ export class BSV20SellLimitOrder extends BSV20V2 { // Update cleared amount. this.tokenAmtSold += amount - // Build transfer inscription. - const transferInscription = BSV20V2.createTransferInsciption( - this.id, - amount - ) - const transferInscriptionLen = len(transferInscription) - // Fist output is the contract itself, holding the remaining tokens. // If no tokens are remaining, then terminate the contract const tokensRemaining = this.tokenAmt - this.tokenAmtSold diff --git a/src/contracts/bsv20ZeroCouponBond.ts b/src/contracts/bsv20ZeroCouponBond.ts index 12971893..0c7b381b 100644 --- a/src/contracts/bsv20ZeroCouponBond.ts +++ b/src/contracts/bsv20ZeroCouponBond.ts @@ -101,8 +101,8 @@ export class Bsv20ZeroCouponBond extends BSV20V2 { // Check that we're unlocking the UTXO specified in the oracles message. assert( - slice(this.prevouts, 0n, 36n) == slice(oracleMsg, 0n, 36n), - 'first input is not spending specified ordinal UTXO' + slice(this.prevouts, 36n, 72n) == slice(oracleMsg, 0n, 36n), + 'second input is not spending specified ordinal UTXO' ) // Get token amount held by the UTXO from oracle message. @@ -116,7 +116,7 @@ export class Bsv20ZeroCouponBond extends BSV20V2 { // Check slot index is empty. const investor = this.investors[Number(slotIdx)] - assert(investor.emptySlot == true, 'slot is not empty') + assert(investor.emptySlot, 'slot is not empty') // Add to investors array. this.investors[Number(slotIdx)] = { @@ -154,8 +154,8 @@ export class Bsv20ZeroCouponBond extends BSV20V2 { // Check that we're unlocking the UTXO specified in the oracles message. assert( - slice(this.prevouts, 0n, 36n) == slice(oracleMsg, 0n, 36n), - 'first input is not spending specified ordinal UTXO' + slice(this.prevouts, 36n, 72n) == slice(oracleMsg, 0n, 36n), + 'second input is not spending specified ordinal UTXO' ) // For each investor add an output that pays them the face value token amount. @@ -194,6 +194,9 @@ export class Bsv20ZeroCouponBond extends BSV20V2 { 'invalid sig investor' ) + // Check price value. + assert(price > 0n, 'price invalid value') + // Toggle forSale flag and set price. this.investors[Number(slotIdx)].forSale = true this.investors[Number(slotIdx)].price = price