babbage-scrypt-helpers
v1.0.33
Published
Tools for deploying sCrypt contracts with Babbage SDK
Downloads
21
Readme
babbage-scrypt-helpers
Tools for deploying sCrypt contracts with Babbage SDK
The code is hosted on GitHub and the package is available through NPM.
Installation
npm i babbage-scrypt-helpers
Example Usage
import { deployContract, listContracts, redeemContract } from 'babbage-scrypt-helpers'
import Demo from './src/contracts/Demo.ts'
import { sha256, byteString } from 'scrypt-ts'
// Compile and deploy the Demo contract
await Demo.compile()
const instance = new Demo(sha256(toByteString('hello world', true)))
const deployTX = await deployContract(
instance, // Contract instance
1000, // Number of satoshis
'Deploy a smart contract', // Description
'tests' // Basket where you want to keep the deployed UTXO
)
console.log('deployed', deployTX.txid)
// List the basket and provide a hydrator to transform the locking script into a contract
const contracts = await listContracts('tests', (lockingScript: string) => {
return Demo.fromLockingScript(lockingScript) as Demo
})
console.log('listed', contracts)
// Redeem a contract
const redeemTX = await redeemContract(
contracts[0], // Contract instance to redeem
// A function that takes the contract and you call a public method to redeem it
(self: SmartContract): void => {
(self as Demo).unlock(toByteString('hello world', true))
},
'redeem a smart contract' // A description
)
console.log('REDEEMED!!', redeemTX.txid)
API
Variables
| | | --- | | deployContract | | listContracts | | redeemContract |
Variable: deployContract
deployContract = async (instance: SmartContract, satoshis: number, description: string, basket?: string, metadata?: string, acceptDelayedBroadcast = false): Promise<CreateActionResult> => {
return await createAction({
description,
outputs: [
{
script: instance.lockingScript.toHex(),
satoshis,
basket,
customInstructions: metadata,
},
],
acceptDelayedBroadcast
});
}
Variable: listContracts
listContracts = async <T extends SmartContract>(basket: string, contractHydrator: (lockingScript: string) => T): Promise<ListResult<T>[]> => {
const outputs = await getTransactionOutputs({
basket,
spendable: true,
includeEnvelope: true,
includeCustomInstructions: true,
});
const contracts: ListResult<T>[] = [];
for (let i = 0; i < outputs.length; i++) {
contracts.push({
...outputs[i],
contract: contractHydrator(outputs[i].outputScript),
});
}
return contracts;
}
Variable: redeemContract
redeemContract = async (listResult: ListResult<SmartContract>, redeemTransformer: (self: SmartContract) => void, description: string, customLockTime?: number, customSequenceNumber = 4294967295, outputs?: CreateActionOutput[]): Promise<CreateActionResult> => {
return await createAction({
inputs: {
[listResult.txid]: {
...verifyTruthy(listResult.envelope),
outputsToRedeem: [
{
index: listResult.vout,
unlockingScript: (await listResult.contract
.getUnlockingScript(redeemTransformer))
.toHex(),
sequenceNumber: customSequenceNumber
}
]
}
},
description,
lockTime: customLockTime,
outputs,
});
}
License
The license for the code in this repository is the Open BSV License.