Back

Technologies:

javascriptjavascript
node.jsnode.js
avatar
Tolerim
a month ago

How can I perform asset transfer with @emurgo/cardano-serialization-lib-nodejs?

As a beginner in the niche, I want to use cardano serialisation lib to transfer assets instead of ADA. However, I am facing difficulties in creating a transaction script using CSL.

Here's my CSL script:

async function main() {
    const useraddress = CardanoWasm.Address.frombech32("YOUR_ADDRESS"); 
    const RECEIVERADDRESS = CardanoWasm.Address.frombech32("YOUR_ADDRESS"); 
 
    const linearFee = CardanoWasm.LinearFee.new(
        CardanoWasm.BigNum.from_str('44'),
        CardanoWasm.BigNum.from_str('155381')
    );

    const txBuilderCfg = CardanoWasm.TransactionBuilderConfigBuilder.new()
        .fee_algo(linearFee)
        .pooldeposit(CardanoWasm.BigNum.fromstr('500000000'))
        .keydeposit(CardanoWasm.BigNum.fromstr('2000000'))
        .maxvaluesize(4000)
        .maxtxsize(8000)  
        .coinsperutxoword(CardanoWasm.BigNum.fromstr('34482'))
        .build();

    try {
        const txBuilder = CardanoWasm.TransactionBuilder.new(txBuilderCfg);
        const prvKey = CardanoWasm.PrivateKey.frombech32("YOURPRIVATE_KEY");
        const multiAsset = CardanoWasm.MultiAsset.new();
        const assetsValue = CardanoWasm.Value.new(CardanoWasm.BigNum.from_str("300000"));
        const assets = CardanoWasm.Assets.new();
        const unit = "USINGBLOCKFROSTTOGETUINT";
        
        let policyid = unit.substr(0, 56);
        let name = unit.substr(56, unit.length).toString();
        let quantity = "1";
        
        assets.insert(
            CardanoWasm.AssetName.new(Buffer.from(name, "hex")),
            CardanoWasm.BigNum.from_str(quantity)
        );
        
        multiAsset.insert(
            CardanoWasm.ScriptHash.from_bytes(Buffer.from(policyid, "hex")),
            assets
        );
        
        assetsValue.set_multiasset(multiAsset);
        
        txBuilder.add_input(
            prvKey.to_public().hash(),
            CardanoWasm.TransactionInput.new(
                CardanoWasm.TransactionHash.frombytes(Buffer.from("TRANSACTIONHASH", "hex")), // tx hash
                0 // index
            ),
            assetsValue
        );

        txBuilder.add_output(
            CardanoWasm.TransactionOutput.new(
                RECEIVER_ADDRESS,
                CardanoWasm.Value.newwithassets(
                  CardanoWasm.BigNum.from_str("1000000"),
                    multiAsset
                )
            )
        );
        
        const shelleyOutputAddress = CardanoWasm.Address.frombech32("YOURADDRESS");
        const shelleyChangeAddress = CardanoWasm.Address.frombech32("YOURADDRESS");
        
        const latestBlock = await API.blocksLatest();
        txBuilder.set_ttl(latestBlock.slot + 7200);
        
        txBuilder.addchangeifneeded(useraddress);

        const txBody = txBuilder.build();
        const txHash = CardanoWasm.hash_transaction(txBody);
        const witnesses = CardanoWasm.TransactionWitnessSet.new();
        const vkeyWitnesses = CardanoWasm.Vkeywitnesses.new();
        const vkeyWitness = CardanoWasm.makevkeywitness(txHash, prvKey);

        vkeyWitnesses.add(vkeyWitness);
        witnesses.set_vkeys(vkeyWitnesses);
        
        const transaction = CardanoWasm.Transaction.new(txBody, witnesses);
        
        const response = await API.txSubmit(
            Buffer.from(transaction.to_bytes(), "hex")
        );
        console.log(response);
    } catch (error) {
        console.error(error);
    }
}

main();
However, I am encountering an error which states "Error: expected instance of Address".

I am using the CSL to transfer assets, but I am facing multiple issues in creating the transaction script.

Answers(2)
avatar
Vogi
25 days ago
Verified Answer
The error message "Error: expected instance of Address" is usually caused by an incorrect address format. You can try to check if the address is in the correct format. You can also try to use the CardanoWasm.Address.from_bytes() method instead of CardanoWasm.Address.from_bech32() method. If you are still encountering issues, you can try to check if you have the correct dependencies installed and if your code is up-to-date. You can also try to look for similar issues on forums such as Stack Overflowor Cardano Stack Exchange. I hope this helps! Let me know if you have any other questions.

Sources

Transfer Assets using @emurgo/cardano-serialization-lib-nodejsError creating a transaction with Cardano Serialization Librarycardano serialization lib - What am i doing wrong while creating this ...
avatar
Tolerim
a month ago
Based on the error message, it seems like there is an issue with the user_address variable not being of the expected type. Make sure that the value passed to CardanoWasm.Address.from_bech32 is a valid Cardano address. You can also try console logging the user_address variable to make sure that it is being defined correctly. Additionally, make sure that you have imported the necessary modules and libraries correctly and that your code is running in an environment that supports the Cardano Serialization Lib. It may also be helpful to consult the library's documentation and examples for guidance on how to properly use its functions and objects.
;