Chained APIs
Using Chained APIs
Chained APIs 简体中文版
When deploying and calling a contract, it is often necessary to calculate the transaction fee and the change output. Without knowing the size of the unlocking script, it is difficult to calculate both. scryptlib extends the bsv library and provides a set of chained APIs to simplify building transactions in these cases.
Deploy Contract
The following is a function that deploys any type of contracts.
async function deployContract(contract, amount) {
const address = privateKey.toAddress()
const tx = new bsv.Transaction()
tx.from(await fetchUtxos(address)) // Add UTXOs/bitcoins that are locked into the contract and pay for miner fees. In practice, wallets only add enough UTXOs, not all UTXOs as done here for ease of exposition.
.addOutput(new bsv.Transaction.Output({
script: contract.lockingScript, // Deploy the contract to the 0-th output
satoshis: amount,
}))
.change(address) // Add change output
.sign(privateKey) // Sign inputs. Only apply to P2PKH inputs.
await sendTx(tx) // Broadcast transaction
return tx
}
Call contract
There are two cases when calling a public function of a contract: 1. The function takes a parameter of type SigHashPreimage 2. No SigHashPreimage parameter.
No SigHashPreimage
SigHashPreimageIf the public function of the contract being called does not contain parameter of type SigHashPreimage, it is relatively simple to build a transaction calling it. Generally, you can build a transaction according to the following steps:
Create an input from a previous transaction where the contract is located by
createInputFromPrevTx(tx, outputIndex)and add it to the transaction.Use
change(address)to add a change output.Use
setInputScript(inputIndex, (tx, output) => bsv.Script)to set the unlocking script for the input added in step 1.Call
seal()to finalize the transaction, and automatically calculate the correct transaction fee and change balance.
SigHashPreimage
SigHashPreimageIf the public function of the contract contains parameters of type SigHashPreimage, it is more complicated to build the transaction calling it. This is because unlocking script containing SigHashPreimage affects the calculation of transaction fee and thus the satoshis amount in the output. In turn, satoshis affects the calculation of SigHashPreimage. The chained APIs provided by scryptlib hides the details of processing these inter-dependent calculations. You only need to build the transaction in the following way:
1. Transaction fees paid by the bitcoins locked in the contract:
In this case, no separate change output is included. The balances of all outputs are unknown before the transaction is built. Transaction fees will affect the calculation of all output balances.
Create an input from the transaction where the contract is located by
createInputFromPrevTx(tx, outputIndex)and add it to the transaction.Use
setOutput(outputIndex, (tx) => bsv.Transaction.Output)to add one or more outputs. The output balance usually needs to subtract transaction fee;javascript const newAmount = amount - tx.getEstimateFee();Use
setInputScript(inputIndex, (tx, output) => bsv.Script)to set the unlocking script for the input added in step 1. The unlocking parameter usually contains a parameter that specifies the new balance of the contract and its calculation is the same as the previous step.Call
seal()to finalize the transaction and automatically calculate the correct transaction fee and change amount.
2. Transaction fees paid by adding other inputs:
In this case, a separate change output is included. The balance of other outputs can generally be calculated before the transaction is built. Transaction fees only affect the calculation of change output.
Create an input from the transaction where the contract is located by
createInputFromPrevTx(tx, outputIndex)and add it to the transactionUse
from([utxo])to add inputs used to pay transaction fee.Use
addOutput()to add one or more outputs (add according to the contract business logic).Use
change(address)to add a change output.Use
setInputScript(inputIndex, (tx, output) => bsv.Script)to set the unlocking script for the input added in step 1. The unlocking parameters usually contains a parameter that specifies the change amount, which can be obtained throughtx.getChangeAmount().Use
sign(privateKey)to sign all inputs added to pay transaction fee.Call
seal()to finalize the transaction.
Extended APIs list
setInputScript
Set input unlocking script
1. inputIndex Input index
2. (tx, output) => bsv.Script A callback function returns the unlocking script of the input
setOutput
Add output to the specified index
1. outputIndex Output index
2. (tx) => bsv.Transaction.Output A callback function returns the output
setLockTime
Set the transaction's nLockTime
1. nLockTime
setInputSequence
Set the sequenceNumber
1. inputIndex Input index
2. sequenceNumber
seal
Seal transactions. Transactions after sealing can no longer be modified
-
getChangeAmount
Get the balance of the change output
-
getEstimateFee
Estimate transaction fee based on transaction size and fee rate
-
checkFeeRate
Check whether the transaction fee meets the fee rate
1. feePerKb fee rate, satoshis per KB
prevouts
returns the serialization of all input outpoints
-
Last updated