Need some JavaScript help for marketplace buy transaction

Hello, so I have created a marketplace and got its ID on testnet API. I have created collections and listed NFTs.
Now, I’m trying to test buy feature with a different wallet address.
How can I trigger NFT buy feature with JavaScript?

const BuyNow = async () => {
  

  const payload = {
    	buyType: 1,
      id: '6f95048037a549be',
     currencyId: KLV,
    amount: 10000
  }

    try {
      const unsignedTx = await window.kleverWeb.buildTransaction([
      {
        payload,
        type: TransactionType.Buy,
      },
    ]);

    const signedTx = await window.kleverWeb.signTransaction(unsignedTx);
    const response = await window.kleverWeb.broadcastTransactions([signedTx]);
    console.log(response)
  } catch (error) {
    console.log(error)
  }



}

I’m unsure what to do with TransactionType.

So I want to be able to trigger MarketBuy transaction with JavaScript:

Please help.

Thanks

The biggest problem here is that type: TransactionType.Buy triggers Transfer instead of Buy and MarketBuy. It looks like there’s a bug? I’m using this to load KleverWeb:

import {
  web,
  TransactionType,
} from 'https://sdk.kleverscan.org/kleverchain-sdk-web-esm-1-0-x.js';

Checking the code, it looks like the correct way is TransactionType.BuyOrder, if you want you also can use directly the type: 17

1 Like

@Yuri thanks a lot! it worked! I was confused because documentation said it’s Buy, not BuyOrder. How can I get price of each NFT? it looks like the function throws invalid amount error when I enter wrong price.

Successful means that your transaction was broadcasted, but it does not guarantee that the transaction status is OK.

After the broadcast, your transaction goes to a pool (transactions pool) and then is selected and computed. This process can delay 4-12s (1 block or 3 blocks on epoch changes).

The error that you are receiving is AmountInvalid, checking the orderID, the listing amount was 3000000000000000 so the amount on the buy transaction is wrong

1 Like

You are not using the Transfer Contract, you are using the Buy Contract. So that’s why it’s showing 0TKLV on Klever Extension.

1 Like

Thanks @Yuri ! How can I use Transfer Contract ? would you please show me a sample code? I’m using below block of code:

import {
  web,
  TransactionType,
  Contract,
} from 'https://sdk.kleverscan.org/kleverchain-sdk-web-esm-1-0-x.js';

const BuyNow = async () => {
  

  const payload = {
       buyType: 1,
       id: '6f95048037a549be',
       currencyId: 'KLV',
       amount: 3000000000000000
  }

    try {
      const unsignedTx = await window.kleverWeb.buildTransaction([
      {
        payload,
        type: TransactionType.BuyOrder,
      }
    ]);

    const signedTx = await window.kleverWeb.signTransaction(unsignedTx);
    const response = await window.kleverWeb.broadcastTransactions([signedTx]);
    console.log(response)
  } catch (error) {
    console.log(error)
  }

}

Also, I’m curious if there’s a klever.finance API endpoint that will show price of my NFT token. I can’t find price of my NFT token after I list it on marketplace. I want to be able to pass in price of NFT token dynamically from API endpoint.

If you want to buy an NFT from a marketplace you can’t use the Transfer contract, but here is a sample:

const payload = {
       assetId: "KLV",
       toAddress: "address"
       amount: 30000
}

const unsignedTransaction = await window.kleverWeb.buildTransaction([
      {
        payload,
        type: TransactionType.Transfer,
      },
]);

I think you can check the prices using one of this endpoint:
https://api.mainnet.klever.finance/v1.0/marketplaces//collections
https://api.mainnet.klever.finance/v1.0/marketplaces//orders

1 Like

@Yuri , thanks for your response.
However, I still can’t see price from the endpoint you have mentioned.
Here’s my endpoint:
https://api.testnet.klever.finance/v1.0/marketplaces/175616c5b5c9e646/collections

How can I see price of each NFTs on the marketplace? /orders returns nothing.
https://api.testnet.klever.finance/v1.0/marketplaces/175616c5b5c9e646/orders

Try this one:
https://api.testnet.klever.finance/marketplaces/<marketplaceID>/collections/<nftID>

Example:
https://api.testnet.klever.finance/marketplaces/12345123124/collections/NFT-1234/1

1 Like

Thanks! yes, I can see the price from the API now. @Yuri

KLV22
@Yuri when I transfer an NFT asset directly on KleverScan, the transaction shows up well on Klever wallet extension as you can see on above image example. However, when I buy an NFT asset on marketplace through BuyOrder, NFT Receive transaction doesn’t show up on Klever extension. I can see that I have received an NFT on KleverScan but not on Klever wallet extension. Is there any way to show NFT ‘Receive’ transaction on Klever wallet too? Any advice would be appreciated. Thanks!

Since you’re on testnet, I suggest you check and confirm your transactions straight on KleverScan. It’s the best way to help and guide your development.

Don’t worry, on mainnet the transactions of all kinds of tokens will be on the kleverscan, extension, and wallet as well.

1 Like

Thanks @Duka So it’s because I’m on testnet.

1 Like

Perfect! BTW the extension team is already aware of this issue. It should be fixed in the next version.

1 Like