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: createFromApi support multiple token identifiers or default value #32

Merged
merged 7 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Publish

on:
workflow_dispatch:

permissions:
contents: write

jobs:
publish-npm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 16
registry-url: https://registry.npmjs.org/

- name: Create release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
RELEASE_TAG=v$(node -p "require('./package.json').version")
gh release create $RELEASE_TAG --target=$GITHUB_SHA --title="$RELEASE_TAG"

- run: npm ci
- run: npm test

- name: Publish to npmjs
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
run: npm publish --access=public
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@itheum/sdk-mx-data-nft",
"version": "1.0.0.beta.1",
"version": "1.0.0",
"description": "SDK for Itheum's Data NFT Technology on MultiversX Blockchain",
"main": "out/index.js",
"types": "out/index.d.js",
Expand Down
18 changes: 11 additions & 7 deletions src/datanft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,19 @@ export class DataNft {
* Creates a DataNft calling the API and also decoding the attributes.
*
* Not useful for creating an array of DataNft, because it calls the API every single time.
* @param nonce Token nonce
* @param tokenIdentifier the Data NFT-FT token identifier (default = `DATA-NFT-FT` token identifier based on the {@link EnvironmentsEnum})
* @param token Object should have a `nonce` property representing the token nonce. An optional `tokenIdentifier` property can be provided to specify the token identifier.
* If not provided, the default token identifier based on the {@link EnvironmentsEnum}
*/
static async createFromApi(
nonce: number,
tokenIdentifier = dataNftTokenIdentifier[this.env as EnvironmentsEnum]
): Promise<DataNft> {
static async createFromApi(token: {
nonce: number;
tokenIdentifier?: string;
}): Promise<DataNft> {
this.ensureNetworkConfigSet();
const identifier = createNftIdentifier(tokenIdentifier, nonce);
const identifier = createNftIdentifier(
token.tokenIdentifier ||
dataNftTokenIdentifier[this.env as EnvironmentsEnum],
token.nonce
);
const response = await fetch(`${this.apiConfiguration}/nfts/${identifier}`);
const data: NftType = await response.json();

Expand Down
7 changes: 5 additions & 2 deletions tests/datanft.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DataNft } from '../src';
describe('Data NFT test', () => {
test('#test not setting network config', async () => {
try {
await DataNft.createFromApi(62);
await DataNft.createFromApi({ nonce: 62 });
} catch (error) {
if (error instanceof Error) {
expect(error.message).toBe(
Expand Down Expand Up @@ -45,7 +45,10 @@ describe('Data NFT test', () => {

expect(typeof nonceToSign).toBe('string');

const nft = await DataNft.createFromApi(62, 'DATANFTFT3-d0978e');
const nft = await DataNft.createFromApi({
nonce: 62,
tokenIdentifier: 'DATANFTFT3-d0978e'
});

expect(nft).toBeInstanceOf(DataNft);
}, 10000);
Expand Down