Skip to content

Commit

Permalink
Merge pull request #101 from Itheum/develop
Browse files Browse the repository at this point in the history
DEVELOP -> MAIN
  • Loading branch information
damienen authored Feb 22, 2024
2 parents 7f0588d + 456b1ad commit aea4511
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 31 deletions.
29 changes: 15 additions & 14 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
name: Publish

on:
push:
branches:
- main
workflow_dispatch:

permissions:
contents: write

jobs:
release:
name: Release
publish-npm:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v4
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: lts/*
registry-url: https://registry.npmjs.org/
- name: Install dependencies
run: npx ci
- name: Install semantic-release extra plugins
run: npm install --save-dev @semantic-release/changelog @semantic-release/git
- name: Lint
run: npm run lint:fix
- name: Test
run: npm run test --if-present
- name: Build
run: npm run build
- name: Release
- name: Create release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: npm config set access public && npx semantic-release
run: |
RELEASE_TAG=v$(node -p "require('./package.json').version")
gh release create $RELEASE_TAG --target=$GITHUB_SHA --title="$RELEASE_TAG" --generate-notes
- name: Publish to npmjs
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
run: npm publish --access=public
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@ This SDK is currently focused on interacting with the Itheum's Data NFT technolo
- work on typescript code in the `/src` folder
- handy tip: when developing locally, you can do integration tests by running `npm run prepare` and the `npm install --save ../sdk-mx-data-nft` in the host project

### Dev Environment

- create a '.husky' folder in the root of the project
Inside the folder:
- add a 'commit-msg' file with the following content:
```bash
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
npx --no-install commitlint --edit $1
```
- add a 'pre-commit' file with the following content:
```bash
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
npm test
```

### Dev Testing

- Only simple dev testing added. First **Build** as below and then run `npm run test` and work on the test.mjs file for live reload
Expand Down
11 changes: 3 additions & 8 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": "0.0.0-semantic-release",
"version": "2.6.3",
"description": "SDK for Itheum's Data NFT Technology on MultiversX Blockchain",
"main": "out/index.js",
"types": "out/index.d.js",
Expand Down
11 changes: 9 additions & 2 deletions src/datanft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import {
ErrNetworkConfig,
ErrTooManyItems
} from './errors';
import { NftType, ViewDataReturnType } from './interfaces';
import { DataNftType, NftType, ViewDataReturnType } from './interfaces';
import BigNumber from 'bignumber.js';

export class DataNft {
export class DataNft implements DataNftType {
readonly tokenIdentifier: string = '';
readonly nftImgUrl: string = '';
readonly dataPreview: string = '';
Expand Down Expand Up @@ -69,6 +69,13 @@ export class DataNft {
this.overrideDataMarshalChainId = override.chainId;
}

/**
* Update any attributes for DataNft
*
*/
updateDataNft(init: Partial<DataNft>) {
Object.assign(this, init);
}
/**
* Sets the network configuration for the DataNft class.
* @param env 'devnet' | 'mainnet' | 'testnet'
Expand Down
23 changes: 23 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import BigNumber from 'bignumber.js';

export interface NftType {
identifier: string;
collection: string;
Expand Down Expand Up @@ -44,6 +46,27 @@ export interface NftType {
}[];
}

export interface DataNftType {
readonly tokenIdentifier: string;
readonly nftImgUrl: string;
readonly dataPreview: string;
readonly dataStream: string;
readonly dataMarshal: string;
readonly tokenName: string;
readonly creator: string;
readonly creationTime: Date;
readonly supply: number | BigNumber.Value;
readonly description: string;
readonly title: string;
readonly royalties: number;
readonly nonce: number;
readonly collection: string;
readonly balance: number | BigNumber.Value;
readonly owner: string;
readonly overrideDataMarshal: string;
readonly overrideDataMarshalChainId: string;
}

export enum NftEnumType {
NonFungibleESDT = 'NonFungibleESDT',
SemiFungibleESDT = 'SemiFungibleESDT',
Expand Down
88 changes: 82 additions & 6 deletions src/marketplace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,24 @@ export class DataNftMarket {
* Retrieves an array of `Offer` objects in an arbitrary order.
* @param from first index
* @param to last index
* @param senderAddress the address of the sender (optional)
*/
async viewPagedOffers(from: number, to: number): Promise<Offer[]> {
const interaction = this.contract.methodsExplicit.viewPagedOffers([
async viewPagedOffers(
from: number,
to: number,
senderAddress?: string
): Promise<Offer[]> {
let interaction = this.contract.methodsExplicit.viewPagedOffers([
new U64Value(from),
new U64Value(to)
]);
if (senderAddress) {
interaction = this.contract.methodsExplicit.viewPagedOffersByAddress([
new U64Value(from),
new U64Value(to),
new AddressValue(new Address(senderAddress))
]);
}
const query = interaction.buildQuery();
const queryResponse = await this.networkProvider.queryContract(query);
const endpointDefinition = interaction.getEndpoint();
Expand All @@ -222,11 +234,37 @@ export class DataNftMarket {
}
}

/**
* Retrieves an `Offer` object based on the offer id
* @param offerId The id of the offer to be retrieved
*/
async viewOffer(offerId: number): Promise<Offer> {
let interaction = this.contract.methodsExplicit.viewOffer([
new U64Value(offerId)
]);

const query = interaction.buildQuery();
const queryResponse = await this.networkProvider.queryContract(query);
const endpointDefinition = interaction.getEndpoint();
const { firstValue, returnCode } = new ResultsParser().parseQueryResponse(
queryResponse,
endpointDefinition
);
if (returnCode.isSuccess()) {
const returnValue = firstValue?.valueOf();
const offer: Offer = parseOffer(returnValue);
return offer;
} else {
throw new ErrContractQuery('viewPagedOffers', returnCode.toString());
}
}

/**
* Retrieves an array of `Offer` objects.
*/
async viewOffers(): Promise<Offer[]> {
const interaction = this.contract.methodsExplicit.getOffers();
async viewOffers(offerIds: number[]): Promise<Offer[]> {
const input = offerIds.map((id) => new U64Value(id));
const interaction = this.contract.methodsExplicit.viewOffers(input);
const query = interaction.buildQuery();
const queryResponse = await this.networkProvider.queryContract(query);
const endpointDefinition = interaction.getEndpoint();
Expand Down Expand Up @@ -416,7 +454,7 @@ export class DataNftMarket {
* @param senderAddress the address of the sender
* @param offerId the id of the offer to be accepted
* @param amount the amount of tokens to be bought
* @param price the price of the offer (must include the buyer fee)
* @param price the price of the offer for the total amount to be bought (must include the buyer fee)
* @param paymentTokenIdentifier the identifier of the payment token (default = `ITHEUM` token identifier based on the {@link EnvironmentsEnum}))
*/
acceptOfferWithESDT(
Expand Down Expand Up @@ -447,11 +485,49 @@ export class DataNftMarket {
return acceptTx;
}

/**
* Creates a `acceptOffer` transaction with NFT/SFT tokens
* @param senderAddress the address of the sender
* @param offerId the id of the offer to be accepted
* @param amount the amount of tokens to be bought
* @param tokenIdentifier the identifier of the token for the payment
* @param nonce the nonce of the token for the payment
* @param paymentAmount the amount of the token for the payment
*/

acceptOfferWithNFT(
senderAddress: IAddress,
offerId: number,
amount: BigNumber.Value,
tokenIdentifier: string,
nonce: number,
paymentAmount: BigNumber.Value
): Transaction {
const offerEsdtTx = new Transaction({
value: 0,
data: new ContractCallPayloadBuilder()
.setFunction(new ContractFunction('ESDTNFTTransfer'))
.addArg(new TokenIdentifierValue(tokenIdentifier))
.addArg(new U64Value(nonce))
.addArg(new BigUIntValue(paymentAmount))
.addArg(new AddressValue(this.contract.getAddress()))
.addArg(new StringValue('acceptOffer'))
.addArg(new U64Value(offerId))
.addArg(new BigUIntValue(amount))
.build(),
receiver: senderAddress,
sender: senderAddress,
gasLimit: 20000000,
chainID: this.chainID
});
return offerEsdtTx;
}

/**
* Creates a `acceptOffer` transaction with EGLD
* @param senderAddress the address of the sender
* @param offerId the id of the offer to be accepted
* @param amount the amount of tokens to be bought
* @param amount the price of the offer for the total amount to be bought (must include the buyer fee)
* @param price the price of the offer (must include the buyer fee)
*/
acceptOfferWithEGLD(
Expand Down
8 changes: 8 additions & 0 deletions tests/datanft.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,13 @@ describe('Data NFT test', () => {

expect(dataNft.overrideDataMarshal).toBe('');
expect(dataNft.overrideDataMarshalChainId).toBe('');

dataNft.updateDataNft({
overrideDataMarshal: 'overrideUrl',
overrideDataMarshalChainId: 'D'
});

expect(dataNft.overrideDataMarshal).toBe('overrideUrl');
expect(dataNft.overrideDataMarshalChainId).toBe('D');
});
});

0 comments on commit aea4511

Please sign in to comment.