Simulate the transaction before sending it. If some data is archived on Stellar, you have to restore it before sending the transaction. The simulation method can return a restore transaction, in this case, you have to send it first:
// SendTx
const srbKeypair = Keypair.fromSecret(privateKey);
const transaction = TransactionBuilder.fromXDR(xdrTx, mainnet.sorobanNetworkPassphrase);
transaction.sign(srbKeypair);
const signedTx = transaction.toXDR();
const restoreXdrTx = await sdk.utils.srb.simulateAndCheckRestoreTxRequiredSoroban(signedTx, fromAddress);
if (restoreXdrTx) {
restoreXdrTx.sign(srbKeypair);
const signedRestoreXdrTx = restoreXdrTx.toXDR();
const sentRestoreXdrTx = await sdk.utils.srb.sendTransactionSoroban(signedRestoreXdrTx);
const confirmRestoreXdrTx = await sdk.utils.srb.confirmTx(sentRestoreXdrTx.hash);
if (confirmRestoreXdrTx.status === SorobanRpc.Api.GetTransactionStatus.NOT_FOUND) {
console.log(
`Waited for Restore transaction to complete, but it did not. ` +
`Check the transaction status manually. ` +
`Hash: ${sentRestoreXdrTx.hash}`
);
} else if (confirmRestoreXdrTx.status === SorobanRpc.Api.GetTransactionStatus.FAILED) {
console.log(`Transaction Restore failed. Check the transaction manually.` + `Hash: ${sentRestoreXdrTx.hash}`);
} else {
console.log(`Transaction Restore Confirmed. Hash: ${sentRestoreXdrTx.hash}`);
}
//generate new tx with updated sequences
const xdrTx2 = (await sdk.bridge.rawTxBuilder.send(sendParams)) as string;
const transaction2 = TransactionBuilder.fromXDR(xdrTx2, mainnet.sorobanNetworkPassphrase);
transaction2.sign(srbKeypair);
signedTx = transaction2.toXDR();
}
Then you have to send the main transaction. Please note that if you send a restore transaction before, the transaction sequence will be changed, and you have to recompile it:
const sent = await sdk.utils.srb.sendTransactionSoroban(signedTx);
const confirm = await sdk.utils.srb.confirmTx(sent.hash);
if (confirm.status === SorobanRpc.Api.GetTransactionStatus.NOT_FOUND) {
console.log(
`Waited for transaction to complete, but it did not. ` +
`Check the transaction status manually. ` +
`Hash: ${sent.hash}`
);
} else if (confirm.status === SorobanRpc.Api.GetTransactionStatus.FAILED) {
console.log(`Transaction failed. Check the transaction manually.` + `Hash: ${sent.hash}`);
} else {
console.log(`Transaction Confirmed. Hash: ${sent.hash}`);
}
Finally, if you make a transfer to Stellar, you have to ensure that you have trustlines for the destination token. Use the example below to check for the trustline/create it:
//TrustLine check and Set up for destinationToken if it is SRB
const destinationTokenSBR = sourceToken; // simulate destination is srb
const balanceLine = await sdk.utils.srb.getBalanceLine(fromAddress, destinationTokenSBR.tokenAddress);
console.log(`BalanceLine:`, balanceLine);
const notEnoughBalanceLine = !balanceLine || Big(balanceLine.balance).add(amount).gt(Big(balanceLine.limit));
if (notEnoughBalanceLine) {
const xdrTx = await sdk.utils.srb.buildChangeTrustLineXdrTx({
sender: fromAddress,
tokenAddress: destinationTokenSBR.tokenAddress,
// limit: "1000000",
});
//SignTx
const keypair = StellarKeypair.fromSecret(privateKey);
const transaction = StellarTransactionBuilder.fromXDR(xdrTx, mainnet.sorobanNetworkPassphrase);
transaction.sign(keypair);
const signedTrustLineTx = transaction.toXDR();
const submit = await sdk.utils.srb.submitTransactionStellar(signedTrustLineTx);
console.log("Submitted change trust tx. Hash:", submit.hash);
}