Skip to content

Commit

Permalink
Minor fixes for bsv-20 contracts.
Browse files Browse the repository at this point in the history
  • Loading branch information
msinkec committed Dec 11, 2023
1 parent d7d31df commit bfb21c7
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/contracts/bsv20BuyLimitOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/contracts/bsv20BuyOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions src/contracts/bsv20CallOption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 10 additions & 7 deletions src/contracts/bsv20CouponBond.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)] = {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/contracts/bsv20Loan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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(
Expand All @@ -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.
Expand Down
22 changes: 11 additions & 11 deletions src/contracts/bsv20LoanMultiple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/bsv20LockToMint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/bsv20Mint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions src/contracts/bsv20PutOption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down
7 changes: 0 additions & 7 deletions src/contracts/bsv20SellLimitOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions src/contracts/bsv20ZeroCouponBond.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)] = {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bfb21c7

Please sign in to comment.