AnyHedgeManager

AnyHedgeManager

Class that manages AnyHedge contract operations, such as creation, validation, and payout of AnyHedge contracts.

Constructor

new AnyHedgeManager(configopt)

Source:
See:
Initializes an AnyHedge Manager using the specified config options. Note that the `networkProvider` and `electrumCluster` options are mutually exclusive and the `networkProvider` takes precedence. The default network provider automatically connects and disconnects between network requests, so if you need a persistent connection, please use a custom provider.
Examples
const anyHedgeManager = new AnyHedgeManager({ authenticationToken: '<token>' });
const config =
{
	authenticationToken: '<token>',
	serviceDomain: 'localhost',
	servicePort: 6572,
	serviceScheme: 'http',
	networkProvider: new ElectrumNetworkProvider('mainnet')
};
const anyHedgeManager = new AnyHedgeManager(config);
Parameters:
Name Type Attributes Description
config <optional>
config object containing configuration options for the AnyHedge Manager.
Properties
Name Type Attributes Description
authenticationToken <optional>
authentication token used to authenticate network requests to the settlement service
contractVersion <optional>
string denoting which AnyHedge contract version to use.
serviceDomain <optional>
fully qualified domain name for the settlement service provider.
servicePort <optional>
network port number for the settlement service provider.
serviceScheme <optional>
network scheme for the settlement service provider, either 'http' or 'https'.
electrumCluster <optional>
electrum cluster to use in BCH network operations.
networkProvider <optional>
network provider to use for BCH network operations.

Methods

(async) completeMutualRedemption(signedProposal1, signedProposal2, contractParameters)

Source:
Complete a mutual redemption by generating a valid transaction from both parties' signed proposals and broadcasting it. Both parties need to generate and sign the same transaction proposal using signMutualEarlyMaturation(), signMutualRefund() or signMutualArbitraryPayout().
Parameters:
Name Type Description
signedProposal1 transaction proposal signed by one of the two parties.
signedProposal2 transaction proposal signed by the other party.
contractParameters contract parameters for the relevant contract.
Throws:
  • if any proposal is unsigned.
    Type
    Error
  • if the transaction details of both proposals don't match.
    Type
    Error
  • if the redemption data lists of the proposals have different lengths.
    Type
    Error
  • if both proposals are signed by the same party.
    Type
    Error
  • if the generated transaction could not successfully be broadcasted.
    Type
    Error
Returns:
transaction ID of the broadcasted mutual redemption transaction.

(async) createContract(oraclePublicKey, hedgePublicKey, longPublicKey, hedgeUnits, startPrice, startBlockHeight, earliestLiquidationModifier, maturityModifier, highLiquidationPriceMultiplier, lowLiquidationPriceMultiplier)

Source:
Creates a new contract.
Parameters:
Name Type Description
oraclePublicKey compressed public key hex string for the oracle that the contract trusts for price messages.
hedgePublicKey compressed public key hex string for the hedge party.
longPublicKey compressed public key hex string for the long party.
hedgeUnits amount in units that the hedge party wants to protect against volatility.
startPrice starting price (units/BCH) of the contract.
startBlockHeight blockHeight at which the contract is considered to have been started at.
earliestLiquidationModifier minimum number of blocks from the starting height before the contract can be liquidated.
maturityModifier exact number of blocks from the starting height that the contract should mature at.
highLiquidationPriceMultiplier multiplier for the startPrice determining the upper liquidation price boundary.
lowLiquidationPriceMultiplier multiplier for the startPrice determining the lower liquidation price boundary.
Returns:
the contract parameters, metadata and hashes.

(async) createContractHashes(contractParameters)

Source:
Computes contract hashes from contract parameters
Parameters:
Name Type Description
contractParameters parameters of the contract
Returns:
the hashes corresponding to the passed parameters

(async) custodialMutualArbitraryPayout(hedgePrivateKeyWIF, longPrivateKeyWIF, transactionProposal, contractParameters)

Source:
Build and broadcast a custodial mutual redemption transaction with arbitrary transaction details.
Parameters:
Name Type Description
hedgePrivateKeyWIF hedge's private key WIF.
longPrivateKeyWIF long's private key WIF.
transactionProposal unsigned transaction proposal for the mutual redemption.
contractParameters contract parameters of the relevant contract.
Throws:
  • if any of the private key WIF strings is not valid.
    Type
    Error
  • if any of the private key WIFs does not belong to a party of the contract.
    Type
    Error
  • if the generated transaction could not successfully be broadcasted.
    Type
    Error
Returns:
transaction ID of the broadcasted mutual redemption transaction.

(async) custodialMutualEarlyMaturation(hedgePrivateKeyWIF, longPrivateKeyWIF, contractFunding, settlementPrice, contractParameters)

Source:
Build and broadcast a custodial mutual redemption transaction that mimics a maturation before the actual maturation block height.
Parameters:
Name Type Description
hedgePrivateKeyWIF hedge's private key WIF.
longPrivateKeyWIF long's private key WIF.
contractFunding the specific Contract Funding to use in the custodial early maturation.
settlementPrice price to use in settlement.
contractParameters contract parameters of the relevant contract.
Throws:
  • if any of the private key WIF strings is not valid.
    Type
    Error
  • if any of the private key WIFs does not belong to a party of the contract.
    Type
    Error
  • if the generated transaction could not successfully be broadcasted.
    Type
    Error
Returns:
transaction ID of the broadcasted mutual redemption transaction.

(async) custodialMutualRefund(hedgePrivateKeyWIF, longPrivateKeyWIF, contractFunding, contractParameters, contractMetadata, hedgeRefundAddressopt, longRefundAddressopt)

Source:
Build and broadcast a custodial mutual redemption transaction that refunds the contract's funds based on the provided contract metadata. Optionally allows you to provide separate refund addresses. If these are omitted, the mutual redemption public keys are used to receive the refunds.
Parameters:
Name Type Attributes Description
hedgePrivateKeyWIF hedge's private key WIF.
longPrivateKeyWIF long's private key WIF.
contractFunding the specific Contract Funding to use in the custodial refund.
contractParameters contract parameters of the relevant contract.
contractMetadata contract metadata of the relevant contract.
hedgeRefundAddress <optional>
hedge's address to receive the refund.
longRefundAddress <optional>
long's address to receive the refund.
Throws:
  • if any of the private key WIF strings is not valid.
    Type
    Error
  • if any of the private key WIFs does not belong to a party of the contract.
    Type
    Error
  • if the generated transaction could not successfully be broadcasted.
    Type
    Error
Returns:
transaction ID of the broadcasted mutual redemption transaction.

(async) getContractFundings(contractParameters)

Source:
Retrieve a list of all ContractFunding instances for a contract.
Parameters:
Name Type Description
contractParameters Contract parameters for the relevant contract.
Returns:
list of contract fundings for a contract.

(async) getContractStatus(contractAddress, privateKeyWIFopt)

Source:
Request the contract data and status of a contract with the settlement service.
Parameters:
Name Type Attributes Description
contractAddress address to retrieve status for
privateKeyWIF <optional>
private key WIF of one of the contract's parties.
Throws:
  • if no authentication token is provided or the authentication token is invalid.
    Type
    Error
  • if no private key WIF was provided *and* the authentication token is different than the one used for registration.
    Type
    Error
  • if an invalid WIF was provided.
    Type
    Error
  • if a private key WIF was provided that does not belong to either of the contract parties.
    Type
    Error
  • if no contract is registered at the settlement service for the given address.
    Type
    Error
  • if the API call is unsuccessful.
    Type
    Error
Returns:
the contract data and status of the contract

(async) getFeeAddress()

Source:
Request a new fee address from the settlement service.
Throws:
if no authentication token is provided or the authentication token is invalid.
Type
Error
Returns:
a new fee address that can be used with the register() call.

(async) liquidateContractFunding(oraclePublicKey, oracleMessage, oracleSignature, hedgePublicKey, longPublicKey, contractFunding, contractMetadata, contractParameters)

Source:
Liquidates a contract.
Parameters:
Name Type Description
oraclePublicKey compressed public key hex string of the oracle that provided the price message.
oracleMessage price message hex string to use for liquidation.
oracleSignature signature hex string for the price message.
hedgePublicKey compressed public key hex string of the hedge party.
longPublicKey compressed public key hex string of the long party.
contractFunding the specific Contract Funding to use in the payout.
contractMetadata contract metadata required to determine available satoshis.
contractParameters contract parameters required to simulate liquidation outcome.
Returns:
ContractSettlement object containing the details of the liquidation.

(async) matureContractFunding(oraclePublicKey, oracleMessage, oracleSignature, hedgePublicKey, longPublicKey, contractFunding, contractMetadata, contractParameters)

Source:
Matures a contract.
Parameters:
Name Type Description
oraclePublicKey compressed public key hex string of the oracle that provided the price message.
oracleMessage price message hex string to use for maturation.
oracleSignature signature hex string for the price message.
hedgePublicKey compressed public key hex string of the hedge party.
longPublicKey compressed public key hex string of the long party.
contractFunding the specific Contract Funding to use in the maturation.
contractMetadata contract metadata required to determine available satoshis.
contractParameters contract parameters required to simulate maturation outcome.
Returns:
ContractSettlement object containing the details of the maturation.

(async) parseSettlementTransaction(settlementTransactionHex)

Source:
Parse a settlement transaction to extract as much data as possible, ending up with partial ContractParameters, ContractSettlement and ContractFunding objects, depending on what data could be retrieved.
Parameters:
Name Type Description
settlementTransactionHex hex string for the settlement transaction
Throws:
  • when the passed transaction hex cannot be parsed by Libauth.
    Type
    Error
  • if the transaction does not have exactly one input.
    Type
    SettlementParseError
  • if the transaction does not have exactly two outputs.
    Type
    SettlementParseError
  • if the unlocking script does not include exactly 6 or 10 input parameters.
    Type
    SettlementParseError
  • if the redeem script does not match expectations for an AnyHedge contract.
    Type
    SettlementParseError
Returns:
partial ContractParameters, ContractSettlement, and ContractFunding objects. See examples/parse-settlement-transaction.js to inspect the data that this function returns.

(async) registerContractForSettlement(oraclePublicKey, hedgePublicKey, longPublicKey, hedgeUnits, startPrice, startBlockHeight, earliestLiquidationModifier, maturityModifier, highLiquidationPriceMultiplier, lowLiquidationPriceMultiplier, feeAddressopt, allowAddressReuseopt)

Source:
Register a new contract for external management.
Parameters:
Name Type Attributes Description
oraclePublicKey compressed public key hex string for the oracle that the contract trusts for price messages.
hedgePublicKey compressed public key hex string for the hedge party.
longPublicKey compressed public key hex string for the long party.
hedgeUnits amount in units that the hedge party wants to protect against volatility.
startPrice starting price (units/BCH) of the contract.
startBlockHeight blockHeight at which the contract is considered to have been started at.
earliestLiquidationModifier minimum number of blocks from the starting height before the contract can be liquidated.
maturityModifier exact number of blocks from the starting height that the contract should mature at.
highLiquidationPriceMultiplier multiplier for the startPrice determining the upper liquidation price boundary.
lowLiquidationPriceMultiplier multiplier for the startPrice determining the lower liquidation price boundary.
feeAddress <optional>
optional fee address acquired from the settlement service to use for this contract.
allowAddressReuse <optional>
optional flag to indicate if fee addresses should be allowed to be reused across contracts.
Throws:
if no authentication token is provided or the authentication token is invalid.
Type
Error
Returns:
contract information for the registered contract.

(async) requestAuthenticationToken(name)

Source:
Request an authentication token from the settlement service. This token is used to authenticate all requests to the settlement services. Only a single token needs to be generated per consuming application.
Parameters:
Name Type Description
name name to communicate an identity to the settlement service.
Throws:
if the request failed.
Type
Error
Returns:
a new authentication token

(async) settleContractFunding(oraclePublicKey, oracleMessage, oracleSignature, hedgePublicKey, longPublicKey, contractFunding, contractMetadata, contractParameters)

Source:
Settles a contract (this includes both maturation and liquidation).
Parameters:
Name Type Description
oraclePublicKey compressed public key hex string of the oracle that provided the price message.
oracleMessage price message hex string to use for settlement.
oracleSignature signature hex string for the price message.
hedgePublicKey compressed public key hex string of the hedge party.
longPublicKey compressed public key hex string of the long party.
contractFunding the specific Contract Funding to use in the settlement.
contractMetadata contract metadata required to determine available satoshis.
contractParameters contract parameters required to simulate settlement outcome.
Returns:
ContractSettlement object containing the details of the settlement.

(async) signMutualArbitraryPayout(privateKeyWIF, transactionProposal, contractParameters)

Source:
Sign a mutual redemption transaction proposal with arbitrary transaction details. Both parties need to call this function with the same transaction details. Both signed transaction proposals must then be passed into the completeMutualRedemption() function to broadcast the transaction.
Parameters:
Name Type Description
privateKeyWIF private key WIF of one of the contract's parties.
transactionProposal An unsigned proposal for a transaction.
contractParameters contract parameters for the relevant contract.
Throws:
  • if the private key WIF string is not valid.
    Type
    Error
  • if the private key WIF does not belong to a party of the contract.
    Type
    Error
  • if a valid transaction is generated during the preparation.
    Type
    Error
Returns:
updated transaction proposal with the resolved variables added in.

(async) signMutualEarlyMaturation(privateKeyWIF, contractFunding, settlementPrice, contractParameters)

Source:
Sign a mutual redemption transaction that mimics a maturation before the actual maturation block height. Both parties need to call this function with the same input and settlement price. Both signed transaction proposals must then be passed into the completeMutualRedemption() function to broadcast the transaction.
Parameters:
Name Type Description
privateKeyWIF private key WIF of one of the contract's parties.
contractFunding the specific Contract Funding to use in the mutual early maturation.
settlementPrice price to use in settlement.
contractParameters contract parameters of the relevant contract.
Throws:
  • if the private key WIF string is not valid.
    Type
    Error
  • if the private key WIF does not belong to a party of the contract.
    Type
    Error
Returns:
a signed settlement transaction proposal.

(async) signMutualRefund(privateKeyWIF, contractFunding, contractParameters, contractMetadata, hedgeRefundAddressopt, longRefundAddressopt)

Source:
Sign a mutual redemption transaction that refunds the contract's funds based on the provided contract metadata. Optionally allows you to provide separate refund addresses. If these are omitted, the mutual redemption public keys are used to receive the refunds. Both parties need to call this function with the same contract funding, contract metadata and refund addresses. Both signed transaction proposals must then be passed into the completeMutualRedemption() function to broadcast the transaction.
Parameters:
Name Type Attributes Description
privateKeyWIF private key WIF of one of the contract's parties.
contractFunding the specific Contract Funding to use in the mutual refund.
contractParameters contract parameters of the relevant contract.
contractMetadata contract metadata of the relevant contract.
hedgeRefundAddress <optional>
hedge's address to receive the refund.
longRefundAddress <optional>
long's address to receive the refund.
Throws:
  • if the private key WIF string is not valid.
    Type
    Error
  • if the private key WIF does not belong to a party of the contract.
    Type
    Error
Returns:
a signed refund transaction proposal.

(async) submitFundingTransaction(contractAddress, transactionHex, dependencyTransactionsopt)

Source:
Register a new contract for external management.
Parameters:
Name Type Attributes Description
contractAddress contract address to submit funding for.
transactionHex funding transaction as a hex-encoded string.
dependencyTransactions <optional>
list of transaction hex strings of transactions that the funding transaction depends on.
Throws:
  • if no authentication token is provided or the authentication token is invalid.
    Type
    Error
  • if the API call failed in any way (e.g. the transaction failed to broadcast).
    Type
    Error
Returns:
funding information for the registered contract.

(async) validateContract(contractAddress, oraclePublicKey, hedgePublicKey, longPublicKey, hedgeUnits, startPrice, startBlockHeight, earliestLiquidationModifier, maturityModifier, highLiquidationPriceMultiplier, lowLiquidationPriceMultiplier)

Source:
Validates that a given contract address matches specific contract parameters.
Parameters:
Name Type Description
contractAddress contract address encoded according to the cashaddr specification.
oraclePublicKey compressed public key hex string for the oracle that the contract trusts for price messages.
hedgePublicKey compressed public key hex string for the hedge party.
longPublicKey compressed public key hex string for the long party.
hedgeUnits amount in units that the hedge party wants to protect against volatility.
startPrice starting price (units/BCH) of the contract.
startBlockHeight blockHeight at which the contract is considered to have been started at.
earliestLiquidationModifier minimum number of blocks from the starting height before the contract can be liquidated.
maturityModifier exact number of blocks from the starting height that the contract should mature at.
highLiquidationPriceMultiplier multiplier for the startPrice determining the upper liquidation price boundary.
lowLiquidationPriceMultiplier multiplier for the startPrice determining the lower liquidation price boundary.
Returns:
true if the contract address and parameters match, otherwise false.