cashscript
Advanced tools
Comparing version 0.8.0-next.3 to 0.8.0-next.4
export declare const VERSION_SIZE = 4; | ||
export declare const LOCKTIME_SIZE = 4; | ||
export declare const P2PKH_INPUT_SIZE: number; |
export const VERSION_SIZE = 4; | ||
export const LOCKTIME_SIZE = 4; | ||
export const P2PKH_INPUT_SIZE = 32 + 4 + 1 + 1 + 65 + 1 + 33 + 4; | ||
//# sourceMappingURL=constants.js.map |
@@ -7,2 +7,3 @@ import { hexToBin, binToHex, encodeTransaction, decodeTransaction, } from '@bitauth/libauth'; | ||
import SignatureTemplate from './SignatureTemplate.js'; | ||
import { P2PKH_INPUT_SIZE } from './constants.js'; | ||
const bip68 = await import('bip68'); | ||
@@ -190,3 +191,3 @@ export class Transaction { | ||
if (this.tokenChange) { | ||
const tokenChangeOutputs = createTokenChangeOutputs(tokenInputs, this.outputs, this.address); | ||
const tokenChangeOutputs = createFungibleTokenChangeOutputs(tokenInputs, this.outputs, this.address); | ||
this.outputs.push(...tokenChangeOutputs); | ||
@@ -286,3 +287,3 @@ } | ||
// Add one extra byte per input to over-estimate tx-in count | ||
const inputSize = getInputSize(placeholderScript) + 1; | ||
const contractInputSize = getInputSize(placeholderScript) + 1; | ||
// Note that we use the addPrecision function to add "decimal points" to BigInt numbers | ||
@@ -296,4 +297,6 @@ // Calculate amount to send and base fee (excluding additional fees per UTXO) | ||
// If inputs are already defined, the user provided the UTXOs and we perform no further UTXO selection | ||
if (!this.hardcodedFee) | ||
fee += addPrecision(this.inputs.length * inputSize * this.feePerByte); | ||
if (!this.hardcodedFee) { | ||
const totalInputSize = this.inputs.reduce((acc, input) => acc + (isSignableUtxo(input) ? P2PKH_INPUT_SIZE : contractInputSize), 0); | ||
fee += addPrecision(totalInputSize * this.feePerByte); | ||
} | ||
satsAvailable = addPrecision(this.inputs.reduce((acc, input) => acc + input.satoshis, 0n)); | ||
@@ -312,3 +315,3 @@ } | ||
if (!this.hardcodedFee) | ||
fee += addPrecision(inputSize * this.feePerByte); | ||
fee += addPrecision(contractInputSize * this.feePerByte); | ||
} | ||
@@ -321,3 +324,3 @@ for (const utxo of bchUtxos) { | ||
if (!this.hardcodedFee) | ||
fee += addPrecision(inputSize * this.feePerByte); | ||
fee += addPrecision(contractInputSize * this.feePerByte); | ||
} | ||
@@ -358,6 +361,5 @@ } | ||
const genesisUtxo = getTokenGenesisUtxo(utxos, tokenCategory); | ||
if (genesisUtxo) { | ||
if (genesisUtxo) | ||
return [genesisUtxo]; | ||
} | ||
const tokenUtxos = utxos.filter((utxo) => utxo.token?.category === tokenCategory); | ||
const tokenUtxos = utxos.filter((utxo) => utxo.token?.category === tokenCategory && utxo.token?.amount > 0n); | ||
// We sort the UTXOs mainly so there is consistent behaviour between network providers | ||
@@ -370,6 +372,6 @@ // even if they report UTXOs in a different order | ||
for (const utxo of tokenUtxos) { | ||
if (amountAvailable >= amountNeeded) | ||
break; | ||
selectedUtxos.push(utxo); | ||
amountAvailable += utxo.token.amount; | ||
if (amountAvailable >= amountNeeded) | ||
break; | ||
} | ||
@@ -385,10 +387,13 @@ if (amountAvailable < amountNeeded) { | ||
}; | ||
const createTokenChangeOutputs = (utxos, outputs, address) => { | ||
const createFungibleTokenChangeOutputs = (utxos, outputs, address) => { | ||
const tokenCategories = getTokenCategories(utxos); | ||
return tokenCategories.map((tokenCategory) => { | ||
const changeOutputs = tokenCategories.map((tokenCategory) => { | ||
const required = calculateTotalTokenAmount(outputs, tokenCategory); | ||
const available = calculateTotalTokenAmount(utxos, tokenCategory); | ||
const change = available - required; | ||
if (change === 0n) | ||
return undefined; | ||
return { to: address, amount: BigInt(1000), token: { category: tokenCategory, amount: change } }; | ||
}); | ||
return changeOutputs.filter((output) => output !== undefined); | ||
}; | ||
@@ -395,0 +400,0 @@ // Note: the below is a very simple implementation of a "decimal point" system for BigInt numbers |
{ | ||
"name": "cashscript", | ||
"version": "0.8.0-next.3", | ||
"version": "0.8.0-next.4", | ||
"description": "Easily write and interact with Bitcoin Cash contracts", | ||
@@ -47,3 +47,3 @@ "keywords": [ | ||
"@bitauth/libauth": "^2.0.0-alpha.8", | ||
"@cashscript/utils": "^0.8.0-next.3", | ||
"@cashscript/utils": "^0.8.0-next.4", | ||
"bip68": "^1.0.4", | ||
@@ -62,3 +62,3 @@ "bitcoin-rpc-promise-retry": "^1.3.0", | ||
}, | ||
"gitHead": "398ff2afab6d731f077002e6be021ed2f1996b4a" | ||
"gitHead": "f4c3f9c21cca98465e5cad919e64b4c6292e9a1f" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
74975
1534