Skip to content

Commit

Permalink
Merge pull request #336 from yusufidimaina9989/master
Browse files Browse the repository at this point in the history
Replace Numbers with their constants value
  • Loading branch information
zhfnjust authored Apr 1, 2024
2 parents ed8070a + 68de77d commit 2e7ebea
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 35 deletions.
4 changes: 3 additions & 1 deletion src/contracts/bsv20Auction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
slice,
StatefulNext,
pubKey2Addr,
Constants,
} from 'scrypt-ts'
import { BSV20V2, OrdiMethodCallOptions } from 'scrypt-ord'

Expand Down Expand Up @@ -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'
)

Expand Down
11 changes: 9 additions & 2 deletions src/contracts/bsv20BuyLimitOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
assert,
len,
byteString2Int,
Constants,
} from 'scrypt-ts'
import { RabinPubKey, RabinSig, RabinVerifier } from 'scrypt-ts-lib'

Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/contracts/bsv20BuyOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
pubKey2Addr,
assert,
len,
Constants,
} from 'scrypt-ts'
import { RabinPubKey, RabinSig, RabinVerifier } from 'scrypt-ts-lib'

Expand Down Expand Up @@ -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'
)

Expand Down
31 changes: 25 additions & 6 deletions src/contracts/bsv20CouponBond.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
assert,
ByteString,
byteString2Int,
Constants,
fill,
FixedArray,
hash256,
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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'
)

Expand All @@ -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')
Expand All @@ -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'
)

Expand All @@ -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')
Expand Down
13 changes: 10 additions & 3 deletions src/contracts/bsv20ForwardContract.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { assert } from 'console'
import { BSV20V2 } from 'scrypt-ord'
import {
ByteString,
Expand All @@ -11,6 +10,8 @@ import {
Sig,
slice,
Utils,
assert,
Constants,
} from 'scrypt-ts'
import { RabinPubKey, RabinSig, RabinVerifier } from 'scrypt-ts-lib'

Expand Down Expand Up @@ -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')
Expand Down
35 changes: 28 additions & 7 deletions src/contracts/bsv20LendingPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
assert,
ByteString,
byteString2Int,
Constants,
fill,
FixedArray,
hash256,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions src/contracts/bsv20LendingPoolCollateral.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
assert,
byteString2Int,
Constants,
hash256,
method,
prop,
Expand All @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions src/contracts/bsv20Loan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
assert,
ByteString,
byteString2Int,
Constants,
hash256,
method,
prop,
Expand Down Expand Up @@ -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
Expand Down
21 changes: 17 additions & 4 deletions src/contracts/bsv20LoanMultiple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
assert,
ByteString,
byteString2Int,
Constants,
fill,
FixedArray,
hash256,
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down
11 changes: 9 additions & 2 deletions src/contracts/bsv20PutOption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
assert,
ByteString,
byteString2Int,
Constants,
hash256,
method,
prop,
Expand Down Expand Up @@ -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')
Expand Down
Loading

0 comments on commit 2e7ebea

Please sign in to comment.