stacks-archive / stacks-transactions-js

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

feat: Sponsored transactions #99

Closed yknl closed 4 years ago

yknl commented 4 years ago

This PR will enable the construction of sponsored transactions where a second "sponsor" signer can pay the fees of a transaction signed by the origin.

To create a transaction that can be sponsored, set sponsored = true in the transaction builder options:

const txOptions = {
  contractAddress: 'SPBMRFRPPGCDE3F384WCJPK8PQJGZ8K9QKK7F59X',
  contractName: 'contract_name',
  functionName: 'contract_function',
  functionArgs: [bufferCVFromString('foo')],
  senderKey: 'b244296d5907de9864c0b0d51f98a13c52890be0404e83f273144cd5b9960eed01',
  sponsored: true,
};

const transaction = await makeContractCall(txOptions);
const serializedTx = transaction.serialize().toString('hex'); 

Next, pass the serialized transaction to the sponsoring party and use the sponsorTransaction builder function to complete the transaction.

// We need to deserialize the transaction first
const bufferReader = new BufferReader(Buffer.from(serializedTx));
const deserializedTx = deserializeTransaction(bufferReader);
const sponsorKey = 'b244296d5907de9864c0b0d51f98a13c52890be0404e83f273144cd5b9960eed01';
// The sponsor sets the fee
// If not set, the fee will be automatically estimated like a non-sponsored transaction
const fee = BigNum(1000);
const sponsorNonce = BigNum(12);

const sponsorOptions = {
  transaction: deserializedTx,
  sponsorPrivateKey: sponsorKey,
  fee,
  sponsorNonce,
}

const sponsoredTx = await sponsorTransaction(sponsorOptions);
broadcastTransaction(sponsoredTx, network);

https://github.com/blockstack/stacks-transactions-js/issues/7

Type of Change

Does this introduce a breaking change?

No

Are documentation updates required?

Yes

Checklist

codecov[bot] commented 4 years ago

Codecov Report

Merging #99 into master will decrease coverage by 1.39%. The diff coverage is 69.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #99      +/-   ##
==========================================
- Coverage   84.10%   82.70%   -1.40%     
==========================================
  Files          28       28              
  Lines        1812     2024     +212     
  Branches      367      415      +48     
==========================================
+ Hits         1524     1674     +150     
- Misses        286      346      +60     
- Partials        2        4       +2     
Impacted Files Coverage Δ
src/errors.ts 14.28% <14.28%> (ø)
src/signer.ts 73.21% <61.53%> (-4.06%) :arrow_down:
src/keys.ts 90.52% <70.00%> (-5.48%) :arrow_down:
src/authorization.ts 77.82% <74.11%> (+0.72%) :arrow_up:
src/transaction.ts 78.09% <77.27%> (-1.22%) :arrow_down:
src/builders.ts 77.06% <83.01%> (+0.92%) :arrow_up:
src/types.ts 83.18% <100.00%> (+0.15%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 9981f63...fa817f4. Read the comment docs.

CLAassistant commented 4 years ago

CLA assistant check
All committers have signed the CLA.

zone117x commented 4 years ago

@yknl could we get this merged and released before the refactor into the blockstack.js monorepo? Need it for use in https://github.com/blockstack/stacks-blockchain-api/pull/176