stacks-archive / stacks-transactions-js

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

Validate Contract-Call Payload with ABI #77

Closed reedrosenbluth closed 4 years ago

reedrosenbluth commented 4 years ago

Description

This PR augments the makeContractCall transaction builder such that it fetches the contracts ABI (either from the mainne or testnet) and uses the ABI to validate that the contract-call payload's function arguments match the contract specification.

Issue: #3

Type of Change

Checklist

codecov[bot] commented 4 years ago

Codecov Report

Merging #77 into master will decrease coverage by 0.95%. The diff coverage is 77.95%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #77      +/-   ##
==========================================
- Coverage   84.86%   83.91%   -0.96%     
==========================================
  Files          26       27       +1     
  Lines        1434     1666     +232     
  Branches      262      350      +88     
==========================================
+ Hits         1217     1398     +181     
- Misses        215      266      +51     
  Partials        2        2              
Impacted Files Coverage Δ
src/contract-abi.ts 73.25% <73.25%> (ø)
src/builders.ts 72.81% <75.00%> (+1.30%) :arrow_up:
src/clarity/clarityValue.ts 100.00% <100.00%> (ø)
src/clarity/index.ts 100.00% <100.00%> (ø)
src/clarity/types/tupleCV.ts 87.50% <100.00%> (ø)
src/index.ts 100.00% <100.00%> (ø)
src/network.ts 100.00% <100.00%> (ø)

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 138edce...a282f7d. Read the comment docs.

yknl commented 4 years ago

Let's also export the contract-abi.ts file. We need ClarityAbi and other interfaces for argument processing outside of the lib.

Also came across this when I tried to use the ABI below (taken from unit tests) as an argument to a function that expects the ClarityAbiFunction type.

{
    "name": "set-value",
    "access": "public",
    "args": [
      { "name": "key", "type": { "buffer": { "length": 3 } } },
      { "name": "value", "type": { "buffer": { "length": 3 } } }
    ],
    "outputs": { "type": { "response": { "ok": "bool", "error": "none" } } }
  }

Results in this error:

Argument of type '{ name: string; access: string; args: { name: string; type: { buffer: { length: number; }; }; }[]; outputs: { type: { response: { ok: string; error: string; }; }; }; }' is not assignable to parameter of type 'ClarityAbiFunction'.
  Types of property 'access' are incompatible.
    Type 'string' is not assignable to type '"public" | "private" | "read_only"'.
zone117x commented 4 years ago

@yknl Have you tried adding ... as ClarityAbiFunction or ... as const to the value you're passing? I think that might be required, otherwise the type definitions would have to be relaxed enough to allow any string for the access field and any other literal type unions

yknl commented 4 years ago

Yea that works, I was hoping there was a way to make it cleaner.