stacks-archive / stacks-transactions-js

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

feat: parseToCV function #90

Closed yknl closed 4 years ago

yknl commented 4 years ago

This PR adds a parseToCV function that converts string input to the appropriate Clarity value based on the Clarity ABI type specified by the contract function. This is useful for converting user input from UI into args for contract function calls dynamically.

Example:

// First we need to fetch the ABI of the contract
getAbi(
  contractAddress,
  contractName,
  txNetwork
).then((abi) => {
  // Filter the function signature that we need
  const filtered = abi.functions.filter(fn => fn.name === functionName);    
  const expectedArgs = filtered[0].args;
  const argsCV = []

  // Use parseToCV() to convert to Clarity value
  for (let i = 0; i < expectedArgs.length; i++) {
    const expectedArg = expectedArgs[i];
    const userInput = userInputs[expectedArg.name];
    argsCV.push(parseToCV(userInput, expectedArg.type));
  }
})

Type of Change

Does this introduce a breaking change?

No

Are documentation updates required?

No

Checklist

codecov[bot] commented 4 years ago

Codecov Report

Merging #90 into master will decrease coverage by 0.34%. The diff coverage is 61.53%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #90      +/-   ##
==========================================
- Coverage   84.43%   84.09%   -0.35%     
==========================================
  Files          27       27              
  Lines        1722     1748      +26     
  Branches      371      385      +14     
==========================================
+ Hits         1454     1470      +16     
- Misses        266      276      +10     
  Partials        2        2              
Impacted Files Coverage Δ
src/contract-abi.ts 72.68% <61.53%> (-1.62%) :arrow_down:

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 f5fd7c0...801a9a4. Read the comment docs.