Hello, I want to transfer assets instead of ADA’s using cardano serialisation lib. I am facing too much difficulty in finding out the soution to transaction creation in cardano using CSL. The main factor is I am beginner in this niche.
Here is my CSL script…
async function main() {
// Takes one string as a parameter and returns an object representing the address
const user_address = CardanoWasm.Address.from_bech32("YOUR_ADDRESS");
const RECEIVER_ADDRESS = CardanoWasm.Address.from_bech32("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)
.pool_deposit(CardanoWasm.BigNum.from_str('500000000'))
.key_deposit(CardanoWasm.BigNum.from_str('2000000'))
.max_value_size(4000)
.max_tx_size(8000)
.coins_per_utxo_word(CardanoWasm.BigNum.from_str('34482'))
.build();
// private key changes DONE
// transaction hash
// index must be changes that is in tx_hash
// below the value will changed that the integer is converted to the string just by .string method
try {
const txBuilder = CardanoWasm.TransactionBuilder.new(txBuilderCfg);
const prvKey = CardanoWasm.PrivateKey.from_bech32("YOUR_PRIVATE_KEY");
const multiAsset = CardanoWasm.MultiAsset.new();
const assetsValue = CardanoWasm.Value.new(
CardanoWasm.BigNum.from_str("300000")
);
const assets = CardanoWasm.Assets.new();
const unit = "USING_BLOCKFROST_TO_GET_UINT";
// substring name and policy id
let policyid = unit.substr(0, 56);
let name = unit.substr(56,unit.length).toString();
let quantity = "1";
// add asset with asset quantity
assets.insert(
CardanoWasm.AssetName.new(Buffer.from(name, "hex")),
CardanoWasm.BigNum.from_str(quantity) // How much to send
);
// add it to the multi asset
multiAsset.insert(
CardanoWasm.ScriptHash.from_bytes(Buffer.from(policyid, "hex")), // PolicyID
assets
);
assetsValue.set_multiasset(multiAsset);
// insert them in the add_input
txBuilder.add_input(
prvKey.to_public().hash(),
CardanoWasm.TransactionInput.new(
CardanoWasm.TransactionHash.from_bytes(Buffer.from("TRANSACTION_HASH", "hex")), // tx hash
0 // index
),
assetsValue
);
// add output
txBuilder.add_output(
CardanoWasm.TransactionOutput.new(
RECEIVER_ADDRESS,
CardanoWasm.Value.new_with_assets(
CardanoWasm.BigNum.from_str("1000000"),
multiAsset
)
)
);
const shelleyOutputAddress = CardanoWasm.Address.from_bech32("YOUR_ADDRESS");
// pointer address
const shelleyChangeAddress = CardanoWasm.Address.from_bech32("YOUR_ADDRESS");
// set the time to live - the absolute slot value before the tx becomes invalid
const latestBlock = await API.blocksLatest();
// set the time to live - the absolute slot value before the tx becomes invalid
txBuilder.set_ttl(latestBlock.slot + 7200);
// calculate the min fee required and send any change to an address
txBuilder.add_change_if_needed(user_address);
// once the transaction is ready, we build it to get the tx body without witnesses
const txBody = txBuilder.build();
const txHash = CardanoWasm.hash_transaction(txBody);
const witnesses = CardanoWasm.TransactionWitnessSet.new();
// add keyhash witnesses
const vkeyWitnesses = CardanoWasm.Vkeywitnesses.new();
const vkeyWitness = CardanoWasm.make_vkey_witness(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();
The error I am facing is
Using CSL I have created a script on how to transfer ADA’s. I have successfully created a script of transfering assets using multiAsset object. Please take a look at my script. I am getting an Error
“Error: expected instance of Address
at _assertClass (D:\cardano\Cardano-PolicyID\node_modules@emurgo\cardano-serialization-lib-nodejs\cardano_serialization_lib.js:196:15)
at TransactionBuilder.add_input (D:\cardano\Cardano-PolicyID\node_modules@emurgo\cardano-serialization-lib-nodejs\cardano_serialization_lib.js:10946:9)
at main (D:\cardano\Cardano-PolicyID\CSLTransferAssets.js:86:11)
at Object. (D:\cardano\Cardano-PolicyID\CSLTransferAssets.js:178:1)”
…