I tried using with_plutus_data
const settings = {
protocolParams: {
linearFee: {
minFeeA: "44",
minFeeB: "155381",
},
minUtxo: "4310",
poolDeposit: "500000000",
keyDeposit: "2000000",
maxValSize: 5000,
maxTxSize: 16384,
priceMem: 0.0577,
priceStep: 0.0000721,
coinsPerUtxoByte: "4310",
}
};
const initTransactionBuilder = () => {
const { protocolParams } = settings;
const txBuilder = TransactionBuilder.new(
TransactionBuilderConfigBuilder.new()
.fee_algo(LinearFee.new(BigNum.from_str(protocolParams.linearFee.minFeeA), BigNum.from_str(protocolParams.linearFee.minFeeB)))
.pool_deposit(BigNum.from_str(protocolParams.poolDeposit))
.key_deposit(BigNum.from_str(protocolParams.keyDeposit))
.coins_per_utxo_byte(BigNum.from_str(protocolParams.coinsPerUtxoByte))
.max_value_size(protocolParams.maxValSize)
.max_tx_size(protocolParams.maxTxSize)
.prefer_pure_change(true)
.build()
);
return txBuilder
};
buildSendTokenToPlutusScript: async (walletApi, {
assetNameHex,
assetPolicyIdHex,
assetAmountToSend,
lovelaceToSend = 2000000
}) => {
const txBuilder = initTransactionBuilder();
const ScriptAddress = Address.from_bech32(settings.smartContract.addressScriptBech32);
const shelleyChangeAddress = Address.from_bech32(await walletApi.getChangeAddress());
let txOutputBuilder = TransactionOutputBuilder.new();
txOutputBuilder = txOutputBuilder.with_address(ScriptAddress);
const inlineDatum = PlutusData.new_integer(BigInt.from_str('12345'));
txOutputBuilder = txOutputBuilder.with_plutus_data(inlineDatum);
txOutputBuilder = txOutputBuilder.next();
let multiAsset = MultiAsset.new();
let assets = Assets.new()
assets.insert(
AssetName.new(Buffer.from(assetNameHex, "hex")), // Asset Name
BigNum.from_str(assetAmountToSend.toString()) // How much to send
);
multiAsset.insert(
ScriptHash.from_bytes(Buffer.from(assetPolicyIdHex, "hex")), // PolicyID
assets
);
txOutputBuilder = txOutputBuilder.with_coin_and_asset(BigNum.from_str(lovelaceToSend.toString()),multiAsset)
const txOutput = txOutputBuilder.build();
txBuilder.add_output(txOutput)
// Find the available UTXOs in the wallet and
// us them as Inputs
const { txOutputs: txUnspentOutputs } = await walletApi.getUtxos();
txBuilder.add_inputs_from(txUnspentOutputs, 3)
// calculate the min fee required and send any change to an address
txBuilder.add_change_if_needed(shelleyChangeAddress)
// once the transaction is ready, we build it to get the tx body without witnesses
const txBody = txBuilder.build();
// Tx witness
const transactionWitnessSet = TransactionWitnessSet.new();
const tx = Transaction.new(
txBody,
TransactionWitnessSet.from_bytes(transactionWitnessSet.to_bytes())
);
let txVkeyWitnesses = await walletApi.api.signTx(Buffer.from(tx.to_bytes(), "utf8").toString("hex"), true);
txVkeyWitnesses = TransactionWitnessSet.from_bytes(Buffer.from(txVkeyWitnesses, "hex"));
transactionWitnessSet.set_vkeys(txVkeyWitnesses.vkeys());
const signedTx = Transaction.new(
tx.body(),
transactionWitnessSet
);
const submittedTxHash = await walletApi.api.submitTx(Buffer.from(signedTx.to_bytes(), "utf8").toString("hex"));
return {
submittedTxHash: submittedTxHash,
transactionIdLocked: submittedTxHash,
lovelaceLocked: lovelaceToSend
};
},
Error:
"Inputs do not conform to this spec or are otherwise invalid."