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:
// SendTxconstsrbKeypair=Keypair.fromSecret(privateKey);consttransaction=TransactionBuilder.fromXDR(xdrTx,mainnet.sorobanNetworkPassphrase);transaction.sign(srbKeypair);constsignedTx=transaction.toXDR();constrestoreXdrTx=awaitsdk.utils.srb.simulateAndCheckRestoreTxRequiredSoroban(signedTx, fromAddress);if (restoreXdrTx) {restoreXdrTx.sign(srbKeypair);constsignedRestoreXdrTx=restoreXdrTx.toXDR();constsentRestoreXdrTx=awaitsdk.utils.srb.sendTransactionSoroban(signedRestoreXdrTx);constconfirmRestoreXdrTx=awaitsdk.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}` ); } elseif (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 sequencesconstxdrTx2= (awaitsdk.bridge.rawTxBuilder.send(sendParams)) asstring;consttransaction2=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:
constsent=awaitsdk.utils.srb.sendTransactionSoroban(signedTx);constconfirm=awaitsdk.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}` );} elseif (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 SRBconstdestinationTokenSBR= sourceToken; // simulate destination is srbconstbalanceLine=awaitsdk.utils.srb.getBalanceLine(fromAddress,destinationTokenSBR.tokenAddress);console.log(`BalanceLine:`, balanceLine);constnotEnoughBalanceLine=!balanceLine ||Big(balanceLine.balance).add(amount).gt(Big(balanceLine.limit));if (notEnoughBalanceLine) {constxdrTx=awaitsdk.utils.srb.buildChangeTrustLineXdrTx({ sender: fromAddress, tokenAddress:destinationTokenSBR.tokenAddress,// limit: "1000000", });//SignTxconstkeypair=StellarKeypair.fromSecret(privateKey);consttransaction=StellarTransactionBuilder.fromXDR(xdrTx,mainnet.sorobanNetworkPassphrase);transaction.sign(keypair);constsignedTrustLineTx=transaction.toXDR();constsubmit=awaitsdk.utils.srb.submitTransactionStellar(signedTrustLineTx);console.log("Submitted change trust tx. Hash:",submit.hash);}