I am creating Hash Time Lock Contract Template using javasdk.The Contract was created.but transaction is not happening … I am following this link Hash Time Lock Contract Template With Java | Algorand Developer Portal . I followed same code but transaction is not happening. why???
Which steps of the tutorial did you do?
What output did you see?
I am following above tutorial … using purestake version 2
That tutorial uses V1 of the api and should be updated to V2. If the contract is not funded you will get an overspend error. Here is the updated v2 example. You will need to change it to use purestake.
package com.algorand.algosdk.teal;
import com.algorand.algosdk.account.Account;
import com.algorand.algosdk.v2.client.common.AlgodClient;
import com.algorand.algosdk.v2.client.common.Response;
import com.algorand.algosdk.v2.client.model.PostTransactionsResponse;
import com.algorand.algosdk.v2.client.model.TransactionParametersResponse;
import com.algorand.algosdk.crypto.Address;
import com.algorand.algosdk.crypto.LogicsigSignature;
import com.algorand.algosdk.templates.ContractTemplate;
import com.algorand.algosdk.templates.HTLC;
import com.algorand.algosdk.transaction.SignedTransaction;
import com.algorand.algosdk.transaction.Transaction;
import com.algorand.algosdk.util.Encoder;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.util.ArrayList;
/**
* Sign and Submit a transaction example using HTLC
*
*/
public class SubmitHtlcTransaction
{
public static void main(String args[]) throws Exception {
// Setup conection parameters
final String ALGOD_API_ADDR = "http://localhost";
final int ALGOD_PORT = 8080;
final String ALGOD_API_TOKEN = "f1dee49e36a82face92fdb21cd3d340a1b369925cd12f3ee7371378f1665b9b1";
AlgodClient client = new AlgodClient(ALGOD_API_ADDR, ALGOD_PORT, ALGOD_API_TOKEN);
BigInteger expiryRound = BigInteger.valueOf(0);
TransactionParametersResponse params = null;
try {
Response < TransactionParametersResponse > resp = client.TransactionParams().execute();
if (!resp.isSuccessful()) {
throw new Exception(resp.message());
}
params = resp.body();
// Get suggested parameters from the node
} catch (Exception e) {
System.err.println("Exception when calling algod#transactionParams");
e.printStackTrace();
}
// Inputs to the Template
int maxFee = 2000;
Address owner = new Address("726KBOYUJJNE5J5UHCSGQGWIBZWKCBN4WYD7YVSTEXEVNFPWUIJ7TAEOPM");
Address receiver = new Address("42NJMHTPFVPXVSDGA6JGKUV6TARV5UZTMPFIREMLXHETRKIVW34QFSDFRE");
String hashFn = "sha256";
String hashImg = "QzYhq9JlYbn2QdOMrhyxVlNtNjeyvyJc/I8d8VAGfGc=";
// Instantiate the templates
ContractTemplate result = HTLC.MakeHTLC(owner, receiver, hashFn, hashImg, expiryRound.intValue(), maxFee);
// Get the program and parameters and use them to create an lsig
// For the contract account to be used in a transaction
// In this example 'hero wisdom green split loop element vote belt'
// hashed with sha256 will produce our image hash
// This is the passcode for the HTLC
// This example does not have parameters but if it did
// you could do the following
ArrayList<byte[]> pargs = new ArrayList<byte[]>();
byte[] arg1 = "hero wisdom green split loop element vote belt".getBytes();
// byte[] arg2 = {4, 5, 6};
pargs.add(arg1);
System.out.println("program: " + result.program.toString());
//lsig = new LogicsigSignature(program, args);
LogicsigSignature lsig = new LogicsigSignature(result.program, pargs);
// Print out the contract address
// if you are debugging this complete example
// be sure to add a break point after you have the contract address
// before submitting the transaction, so you can fund it using the dispenser
System.out.println("Contract address: " + lsig.toAddress().toString());
// Create a Transaction
Address zeroAddress = new Address("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAY5HFKQ");
Transaction txn = Transaction.PaymentTransactionBuilder()
.sender(lsig.toAddress())
.amount(0)
.receiver(zeroAddress)
.suggestedParams(params)
.closeRemainderTo(receiver)
.build();
try{
// Create a signed transaction with logicSig
SignedTransaction stx = Account.signLogicsigTransaction(lsig, txn);
byte[] encodedTxBytes = Encoder.encodeToMsgPack(stx);
String FILEPATH = "./htlc.txn";
File file = new File(FILEPATH);
OutputStream os = new FileOutputStream(file);
// Starts writing the bytes in it
os.write(encodedTxBytes);
// Close the file
os.close();
// Submit the transaction to the network
Response < PostTransactionsResponse > rawtxresponse = client.RawTransaction().rawtxn(encodedTxBytes).execute();
if (!rawtxresponse.isSuccessful()) {
throw new Exception(rawtxresponse.message());
}
String id = rawtxresponse.body().txId;
System.out.println("Successfully sent tx with ID: " + id);
} catch (Exception e) {
// This is generally expected, but should give us an informative error message.
System.err.println("Exception when calling algod#rawTransaction: " + e.getLocalizedMessage());
}
}
}
thanks … contract is working