stacks-archive / stacks-transactions-js

The JavaScript library for generating Stacks 2.0 transactions
19 stars 17 forks source link

Use network object in tx builder options #65

Closed yknl closed 4 years ago

yknl commented 4 years ago

This PR adds the network options object to transaction builder functions. Networks are constants that will determine the transaction version, chain ID and API URLs. This simplifies the builder options and provides defaults. This PR also moves the broadcast() function out of the StacksTransaction class and into a separate function in the tx builders. This is so that users can use a network object to specify broadcast API URLs. This PR is based off of the fee estimate PR: #45

Usage:

const network = new StacksTestnet();
const options = {
  network: network
};

const transaction = await makeSTXTokenTransfer(
  recipientAddress,
  amount,
  secretKey,
  options
);

broadcastTransaction(transaction, network);

Creating a custom network

const myNetwork = {
  version: TransactionVersion.Mainnet;
  chainId: ChainID.Mainnet;
  coreApiUrl = 'https://mystacksnetworknode.com';
  broadcastApiUrl = `${this.coreApiUrl}/v2/transactions`;
  transferFeeEstimateApiUrl = `${this.coreApiUrl}/v2/fees/transfer`;
}

Resolves #64

Type of Change

Does this introduce a breaking change?

Yes, the options object for transaction builder functions have been changed.

TokenTransferOptions ContractDeployOptions ContractCallOptions

The StacksTransaction class no longer has a broadcast() method. It has been moved to a separate function broadcastTransaction(). And it now takes a transaction and network as arguments.

Are documentation updates required?

The readme examples have been updated with this PR.

Testing information

Provide context on how tests should be performed. Transactions should continue to be generated and broadcasted correctly when using the new options objects for transaction builders.

Checklist