import sjcl from "@tinyanvil/sjcl";
import { has as loHas } from "lodash-es";
import { handleError } from "@services/error";
export default async function makePayment(e: Event) {
let instructions = await this.setPrompt("{Amount} {Asset} {Destination}");
instructions = instructions.split(" ");
if (!/xlm/gi.test(instructions[1]))
instructions[3] = await this.setPrompt(
`Who issues the ${instructions[1]} asset?`,
"Enter ME to refer to yourself",
const pincode = await this.setPrompt("Enter your keystore pincode");
if (!instructions || !pincode) return;
const keypair = Keypair.fromSecret(
sjcl.decrypt(pincode, this.account.keystore),
if (/me/gi.test(instructions[3])) instructions[3] = keypair.publicKey();
this.loading = { ...this.loading, pay: true };
.accountId(keypair.publicKey())
.then(({ sequence }) => {
const account = new Account(keypair.publicKey(), sequence);
const transaction = new TransactionBuilder(account, {
networkPassphrase: Networks.TESTNET,
destination: instructions[2],
? new Asset(instructions[1], instructions[3])
transaction.sign(keypair);
return this.server.submitTransaction(transaction).catch((err) => {
// Paying an account which doesn't exist, create it instead
loHas(err, "response.data.extras.result_codes.operations") &&
err.response.data.status === 400 &&
err.response.data.extras.result_codes.operations.indexOf(
const transaction = new TransactionBuilder(account, {
networkPassphrase: Networks.TESTNET,
Operation.createAccount({
destination: instructions[2],
startingBalance: instructions[0],
transaction.sign(keypair);
return this.server.submitTransaction(transaction);
.then((res) => console.log(res))
this.loading = { ...this.loading, pay: false };
this.error = handleError(err);