Skip to content

Commit

Permalink
Set Trust Line Flags operation
Browse files Browse the repository at this point in the history
  • Loading branch information
quietbits committed Jun 4, 2024
1 parent 407d03b commit 6c00e6b
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -686,9 +686,7 @@ export const Operations = () => {
<option value="clawback_claimable_balance">
Clawback Claimable Balance
</option>
<option value="set_trust_line_flags" disabled>
Set Trust Line Flags
</option>
<option value="set_trust_line_flags">Set Trust Line Flags</option>
<option value="liquidity_pool_deposit">Liquidity Pool Deposit</option>
<option value="liquidity_pool_withdraw">
Liquidity Pool Withdraw
Expand Down
32 changes: 27 additions & 5 deletions src/app/(sidebar)/transaction/build/components/TransactionXdr.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { Routes } from "@/constants/routes";
import {
OPERATION_CLEAR_FLAGS,
OPERATION_SET_FLAGS,
OPERATION_TRUSTLINE_CLEAR_FLAGS,
OPERATION_TRUSTLINE_SET_FLAGS,
} from "@/constants/settings";

import {
Expand Down Expand Up @@ -169,9 +171,17 @@ export const TransactionXdr = () => {
}, [] as any[]);
};

const flagTotal = (val: string[], operations: OptionFlag[]) => {
const flagTotal = (
val: string[],
operations: OptionFlag[],
op?: string,
) => {
const total = optionsFlagDetails(operations, val).total;

if (op === "set_trust_line_flags") {
return BigInt(total);
}

return total > 0 ? BigInt(total) : null;
};

Expand Down Expand Up @@ -223,7 +233,7 @@ export const TransactionXdr = () => {
};
};

const getXdrVal = (key: string, val: any) => {
const getXdrVal = (key: string, val: any, op?: string) => {
switch (key) {
// Amount
case "amount":
Expand Down Expand Up @@ -267,9 +277,21 @@ export const TransactionXdr = () => {
return formatAssetMultiValue(val);
// Flags
case "clear_flags":
return flagTotal(val, OPERATION_CLEAR_FLAGS);
return flagTotal(
val,
op === "set_trust_line_flags"
? OPERATION_TRUSTLINE_CLEAR_FLAGS
: OPERATION_CLEAR_FLAGS,
op,
);
case "set_flags":
return flagTotal(val, OPERATION_SET_FLAGS);
return flagTotal(
val,
op === "set_trust_line_flags"
? OPERATION_TRUSTLINE_SET_FLAGS
: OPERATION_SET_FLAGS,
op,
);
// Signer
case "signer":
return formatSignerValue(val);
Expand Down Expand Up @@ -298,7 +320,7 @@ export const TransactionXdr = () => {
}

return Object.entries(params).reduce((res, [key, val]) => {
res[key] = getXdrVal(key, val);
res[key] = getXdrVal(key, val, opType);

return res;
}, {} as AnyObject);
Expand Down
6 changes: 5 additions & 1 deletion src/components/FormElements/FlagFieldPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface FlagFieldProps {
label?: string | React.ReactNode;
labelSuffix?: string | React.ReactNode;
onChange: (value: string[]) => void;
options: OptionFlag[];
options?: OptionFlag[];
infoLink?: string;
infoText?: string | React.ReactNode;
note?: React.ReactNode;
Expand All @@ -26,6 +26,10 @@ export const FlagFieldPicker = ({
infoText,
note,
}: FlagFieldProps) => {
if (!options || options.length === 0) {
return null;
}

const details = optionsFlagDetails(options, selectedOptions);

return (
Expand Down
8 changes: 2 additions & 6 deletions src/components/formComponentTemplateTxnOps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ import { SignerPicker } from "@/components/FormElements/SignerPicker";
import { AuthorizePicker } from "@/components/FormElements/AuthorizePicker";
import { NumberFractionPicker } from "@/components/FormElements/NumberFractionPicker";

import {
OPERATION_CLEAR_FLAGS,
OPERATION_SET_FLAGS,
} from "@/constants/settings";
import { validate } from "@/validate";
import {
AnyObject,
Expand Down Expand Up @@ -233,7 +229,7 @@ export const formComponentTemplateTxnOps = ({
</>
}
onChange={templ.onChange}
options={OPERATION_CLEAR_FLAGS}
options={custom?.options}
/>
),
validate: null,
Expand Down Expand Up @@ -536,7 +532,7 @@ export const formComponentTemplateTxnOps = ({
</>
}
onChange={templ.onChange}
options={OPERATION_SET_FLAGS}
options={custom?.options}
/>
),
validate: null,
Expand Down
31 changes: 31 additions & 0 deletions src/constants/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,34 @@ export const OPERATION_CLEAR_FLAGS = [
value: 8,
},
];

export const OPERATION_TRUSTLINE_SET_FLAGS = [
{
id: "trustline-set-authorized",
label: "Authorized",
value: 1,
},
{
id: "trustline-set-liabilites",
label: "Authorized to maintain liabilites",
value: 2,
},
];

export const OPERATION_TRUSTLINE_CLEAR_FLAGS = [
{
id: "trustline-clear-authorized",
label: "Authorized",
value: 1,
},
{
id: "trustline-clear-liabilites",
label: "Authorized to maintain liabilites",
value: 2,
},
{
id: "trustline-clear-clawback",
label: "Clawback enabled",
value: 4,
},
];
31 changes: 31 additions & 0 deletions src/constants/transactionOperations.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { SdsLink } from "@/components/SdsLink";
import {
OPERATION_CLEAR_FLAGS,
OPERATION_SET_FLAGS,
OPERATION_TRUSTLINE_CLEAR_FLAGS,
OPERATION_TRUSTLINE_SET_FLAGS,
} from "@/constants/settings";
import { AnyObject } from "@/types/types";

type TransactionOperation = {
Expand Down Expand Up @@ -220,6 +226,12 @@ export const TRANSACTION_OPERATIONS: { [key: string]: TransactionOperation } = {
),
showWarning: true,
},
set_flags: {
options: OPERATION_SET_FLAGS,
},
clear_flags: {
options: OPERATION_CLEAR_FLAGS,
},
},
},
change_trust: {
Expand Down Expand Up @@ -323,6 +335,25 @@ export const TRANSACTION_OPERATIONS: { [key: string]: TransactionOperation } = {
params: ["balance_id"],
requiredParams: ["balance_id"],
},
set_trust_line_flags: {
label: "Set Trust Line Flags",
description: "Creates a trustline flag configuring operation.",
docsUrl:
"https://developers.stellar.org/docs/learn/fundamentals/list-of-operations#set-trustline-flags",
params: ["asset", "trustor", "set_flags", "clear_flags"],
requiredParams: ["asset", "trustor"],
custom: {
asset: {
includeNative: false,
},
set_flags: {
options: OPERATION_TRUSTLINE_SET_FLAGS,
},
clear_flags: {
options: OPERATION_TRUSTLINE_CLEAR_FLAGS,
},
},
},
liquidity_pool_deposit: {
label: "Liquidity Pool Deposit",
description: "Deposits assets into a liquidity pool.",
Expand Down

0 comments on commit 6c00e6b

Please sign in to comment.