stacks-archive / stacks-transactions-js

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

feat: add callReadOnlyFunction #87

Closed aulneau closed 4 years ago

aulneau commented 4 years ago

Description

Yay, my first contribution to this library! When implementing makeContractCall within the explorers sandbox, I ran into some examples of read only calls. I realized this method doesn't apply, took a look at how @hstove had implemented it, and worked on this new method called callReadOnlyFunction.

  1. Motivation for change:
    • We need some ability for folks to call read only functions.
  2. What was changed
    • I added the following methods callReadOnlyFunction, cvToHex. I also fixed some typos and missing jsdoc declarations.
  3. How does this impact application developers
    • This method will allow developers who are interacting with contracts to easily call a known read only contract function.

4. Link to relevant issues and documentation

  1. Provide examples of use cases with code samples and applicable acceptance criteria
    
    const value = await callReadOnlyFunction({
    senderAddress: identity?.address as string,
    contractAddress,
    contractName,
    functionArgs,
    functionName: func.name,
    network: net, // testnet
    });

const parseReadOnlyResponse = ({ result }: ReadOnlyResponse) => { const hex = result.slice(2); const bufferCv = Buffer.from(hex, 'hex'); const clarityValue = deserializeCV(bufferCv); return cvToString(clarityValue); };

const result = parseReadOnlyResponse(value);

setResult(result);



## Type of Change
- [x] New feature
- [ ] Bug fix
- [ ] API reference/documentation update
- [ ] Other

## Does this introduce a breaking change?
I don't think so.

## Are documentation updates required?
Yes

## Checklist
- [x] Code is commented where needed
- [x] Unit test coverage for new or modified code paths
- [x] `npm run test` passes
- [x] Changelog is updated
- [x] Tag 1 of @yknl, @zone117x, @reedrosenbluth for review
friedger commented 4 years ago

Fixed #89

codecov[bot] commented 4 years ago

Codecov Report

Merging #87 into master will increase coverage by 0.15%. The diff coverage is 93.75%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #87      +/-   ##
==========================================
+ Coverage   84.09%   84.25%   +0.15%     
==========================================
  Files          27       27              
  Lines        1748     1778      +30     
  Branches      385      387       +2     
==========================================
+ Hits         1470     1498      +28     
- Misses        276      278       +2     
  Partials        2        2              
Impacted Files Coverage Δ
src/contract-abi.ts 72.76% <77.77%> (+0.08%) :arrow_up:
src/builders.ts 74.31% <100.00%> (+1.23%) :arrow_up:
src/network.ts 100.00% <100.00%> (ø)
src/utils.ts 76.71% <100.00%> (+3.27%) :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 ab8f6d8...1203f92. Read the comment docs.