2

I'm trying to swap NFT using such function:

async function swap(fa2: string, contractAddress: string, objkt_id: number, royalties: number, objkt_amount: number, xtz_per_objkt: number, owner_address: string, creator: string, privateKey: string) {

    try {
        tezos.setProvider({signer: await InMemorySigner.fromSecretKey(privateKey)});

        // Load the marketplace contract
        const marketplaceContract = await tezos.contract.at(contractAddress);


        // Call the swap entry point with the provided parameters
        const operation = await marketplaceContract.methods.swap(creator, objkt_amount, objkt_id, royalties, xtz_per_objkt).send();

        // Await confirmation
        const confirmation = await operation.confirmation();

        if (confirmation.completed) {
            console.log(`Successfully swapped NFT with token ID ${objkt_id}.`);
        } else {
            console.log('Failed to swap NFT.');
        }
    } catch (error) {
        console.error('Error swap NFT:', error);
        throw error;
    }
}

But it produces error TezosOperationError: FA2_NOT_OPERATOR

6
  • Which account is calling the function? By default only the token owner is the OPERATOR with permissions to transfer it.
    – ktorn
    Mar 25 at 14:41
  • @ktorn I think I'm a bit confused with terminology here. The function is called by account, who owns a token but is not a token creator. So my goal is to resell token using this API
    – Adam Levy
    Mar 25 at 14:49
  • Perhaps you need to temporarily add the contract as a valid operator. See tezos.stackexchange.com/questions/4095/…
    – ktorn
    Mar 25 at 18:11
  • 1
    That's the HEN v2 contract, you may want to refer to the original HEN UI code for reference: github.com/hicetnunc2000/hicetnunc/blob/… it seems like the update_operators method is on the objkts contract, rather than the marketplace contract.
    – ktorn
    Mar 26 at 3:17
  • 1
    Nice! That worked like a charm, thanks for help
    – Adam Levy
    Mar 26 at 13:25

1 Answer 1

2

Code example from https://github.com/hicetnunc2000/hicetnunc/blob/ebfbb55144a83b247b327388066f37a1e27328f8/src/context/HicetnuncContext.js#L185 worked in my case

My implementation:

async function swapv2(fa2Address, contractAddress, royalties, objkt_amount, xtz_per_objkt, objkt_id, creator, ownerAddress) {
    // If using proxy: both calls are made through this.state.proxyAddress:
    // const objktsAddress = this.state.proxyAddress || this.state.objkts;
    // const marketplaceAddress = this.state.proxyAddress || this.state.v2;
    // const ownerAddress = this.state.proxyAddress || from;

    let fa2 = await tezos.wallet.at(fa2Address)
    let marketplace = await tezos.wallet.at(contractAddress)

    let list = [
        {
            kind: OpKind.TRANSACTION,
            ...fa2.methods.update_operators([{
                add_operator: {
                    operator: contractAddress,
                    token_id: parseFloat(objkt_id),
                    owner: ownerAddress
                }
            }])
                .toTransferParams({amount: 0, mutez: true, storageLimit: 175})
        },
        {
            kind: OpKind.TRANSACTION,
            ...marketplace.methods.swap(creator, parseFloat(objkt_amount), parseFloat(objkt_id), parseFloat(royalties), parseFloat(xtz_per_objkt)).toTransferParams({
                amount: 0,
                mutez: false,
                storageLimit: 300
            })
        },
        {
            kind: OpKind.TRANSACTION,
            ...fa2.methods.update_operators([{
                remove_operator: {
                    operator: contractAddress,
                    token_id: parseFloat(objkt_id),
                    owner: ownerAddress
                }
            }])
                .toTransferParams({amount: 0, mutez: true, storageLimit: 175})
        },
    ]

    let batch = await tezos.wallet.batch(list);
    return await batch.send()
}
3
  • 1
    great! can you please edit your answer to include a code excerpt from the source (in case the link stops working) and accept your own answer.
    – ktorn
    Mar 26 at 14:47
  • Added code, it doesn't allow accepting my own answer yet
    – Adam Levy
    Mar 26 at 16:13
  • 1
    no worries. thanks for editing the question. the code will be useful for others in the future.
    – ktorn
    Mar 26 at 19:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.