Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement getContractMapEntry function #1461

Merged
merged 1 commit into from
Mar 17, 2023

Conversation

zone117x
Copy link
Member

@zone117x zone117x commented Mar 17, 2023

This PR was published to npm with the version 6.2.2-pr.3c85dab.0
e.g. npm install @stacks/common@6.2.2-pr.3c85dab.0 --save-exact

Implement getContractMapEntry function for the POST /v2/map_entry/[Stacks Address]/[Contract Name]/[Map Name] RPC endpoint

This makes the endpoint less cumbersome to use by handling the POST body clarity serialization.

Example usage (untyped):

import { getContractMapEntry, principalCV } from '@stacks/transactions';

const result = await getContractMapEntry({
  contractAddress: 'SPSCWDV3RKV5ZRN1FQD84YE1NQFEDJ9R1F4DYQ11',
  contractName: 'newyorkcitycoin-core-v2',
  mapName: 'UserIds',
  mapKey: principalCV('SP25V8V2QQ2K8N3JAS15Z14W4YW7ABFDZHK5ZPGW7'),
});

console.log(result);
// > {type: 1, value: 60n}

Example usage (typed):

import { getContractMapEntry, principalCV, UIntCV, ClarityType } from '@stacks/transactions';

const result = await getContractMapEntry<UIntCV>({
  contractAddress: 'SPSCWDV3RKV5ZRN1FQD84YE1NQFEDJ9R1F4DYQ11',
  contractName: 'newyorkcitycoin-core-v2',
  mapName: 'UserIds',
  mapKey: principalCV('SP25V8V2QQ2K8N3JAS15Z14W4YW7ABFDZHK5ZPGW7'),
});

if (result.type === ClarityType.OptionalNone) {
  throw new Error('Not found!');
}

// Type is narrowed to `UIntCV`
console.log(result.value);
// > 60n

This function can be improved later on with a flag to enable more checking. For example, it could validate the contract principal and map name are valid with an ABI fetch, then validate that the map Clarity value type matches the type from the ABI. Right now, the RPC endpoint returns none for most of these conditions.

@zone117x zone117x requested a review from janniks March 17, 2023 15:51
@vercel
Copy link

vercel bot commented Mar 17, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
stacksjs-docs ✅ Ready (Inspect) Visit Preview Mar 17, 2023 at 3:51PM (UTC)

@codecov
Copy link

codecov bot commented Mar 17, 2023

Codecov Report

Merging #1461 (3c85dab) into master (9d5b7fb) will increase coverage by 0.03%.
The diff coverage is 79.16%.

@@            Coverage Diff             @@
##           master    #1461      +/-   ##
==========================================
+ Coverage   65.68%   65.72%   +0.03%     
==========================================
  Files         116      116              
  Lines        8509     8533      +24     
  Branches     1892     1872      -20     
==========================================
+ Hits         5589     5608      +19     
- Misses       2792     2797       +5     
  Partials      128      128              
Impacted Files Coverage Δ
packages/network/src/network.ts 57.14% <50.00%> (-0.15%) ⬇️
packages/transactions/src/builders.ts 77.88% <81.81%> (+0.22%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Collaborator

@janniks janniks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@zone117x zone117x merged commit 7031ead into master Mar 17, 2023
@zone117x zone117x deleted the feat/get-contract-map-entry branch March 17, 2023 16:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants