Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Build Transaction] Create Claimable Balance operation #861

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 89 additions & 2 deletions src/app/(sidebar)/transaction/build/components/Operations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { sanitizeObject } from "@/helpers/sanitizeObject";
import { TRANSACTION_OPERATIONS } from "@/constants/transactionOperations";
import { useStore } from "@/store/useStore";
import {
AnyObject,
AssetObject,
AssetObjectValue,
AssetPoolShareObjectValue,
Expand Down Expand Up @@ -337,6 +338,72 @@ export const Operations = () => {
return false;
};

const isMissingClaimantFields = (
param: string,
value: AnyObject[] | undefined,
) => {
if (param === "claimants") {
if (!value || value.length === 0) {
return false;
}

let missing = false;

(value || []).forEach((val) => {
if (
!val.destination ||
!val.predicate ||
isEmptyObject(val.predicate)
) {
missing = true;
}

// Check only if nothing is missing yet
if (!missing) {
const missingPredicate = loopPredicate(val.predicate, []);

missing = Boolean(missingPredicate && missingPredicate?.length > 0);
}
});

return missing;
}

return false;
};

const loopPredicate = (
predicate: AnyObject = {},
missingArray: boolean[],
) => {
if (isEmptyObject(predicate)) {
missingArray.push(true);
}

Object.entries(predicate).forEach(([key, val]) => {
if (["relative", "absolute"].includes(key) && typeof val === "string") {
if (!val) {
missingArray.push(true);
}
}

if (Array.isArray(val)) {
val.forEach((v) => loopPredicate(v, missingArray));
} else if (typeof val === "object") {
if (isEmptyObject(val)) {
if (key !== "unconditional") {
missingArray.push(true);
}
} else {
loopPredicate(val, missingArray);
}
}
// }
quietbits marked this conversation as resolved.
Show resolved Hide resolved
});

return missingArray;
};

const validateOperationParam = ({
opIndex,
opParam,
Expand Down Expand Up @@ -436,6 +503,13 @@ export const Operations = () => {
opParamMissingFields = [...opParamMissingFields, opParam];
}

//==== Handle claimable balance claimants
const missingClaimantFields = isMissingClaimantFields(opParam, opValue);

if (missingClaimantFields && !opParamMissingFields.includes(opParam)) {
opParamMissingFields = [...opParamMissingFields, opParam];
}

return {
operationType: opType,
error: opParamErrorFields,
Expand Down Expand Up @@ -699,7 +773,6 @@ export const Operations = () => {
) : null
}
>
{/* TODO: remove disabled attribute when operation is implemented */}
<option value="">Select operation type</option>
<option value="create_account">Create Account</option>
<option value="payment">Payment</option>
Expand All @@ -720,7 +793,7 @@ export const Operations = () => {
<option value="account_merge">Account Merge</option>
<option value="manage_data">Manage Data</option>
<option value="bump_sequence">Bump Sequence</option>
<option value="create_claimable_balance" disabled>
<option value="create_claimable_balance">
Create Claimable Balance
</option>
<option value="claim_claimable_balance">
Expand Down Expand Up @@ -834,6 +907,20 @@ export const Operations = () => {
});
},
});
case "claimants":
return component.render({
...baseProps,
onChange: (
claimants: AnyObject[] | undefined,
) => {
handleOperationParamChange({
opIndex: idx,
opParam: input,
opValue: claimants,
opType: op.operation_type,
});
},
});
case "line":
return component.render({
...baseProps,
Expand Down
72 changes: 72 additions & 0 deletions src/app/(sidebar)/transaction/build/components/TransactionXdr.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useEffect, useState } from "react";
import { Button } from "@stellar/design-system";
import { stringify } from "lossless-json";
import { StrKey, TransactionBuilder } from "@stellar/stellar-sdk";
import { set } from "lodash";
import * as StellarXdr from "@/helpers/StellarXdr";

import { SdsLink } from "@/components/SdsLink";
Expand Down Expand Up @@ -171,6 +172,62 @@ export const TransactionXdr = () => {
}, [] as any[]);
};

const formatPredicate = (predicate: AnyObject) => {
const loop = (
item: AnyObject,
result: AnyObject,
parent: string | undefined,
): AnyObject => {
const params = Object.entries(item);

const key = params[0][0];
const val = params[0][1];

const getPath = (parent: string | undefined) =>
`${parent ? `${parent}.` : ""}`;

switch (key) {
case "unconditional":
result[`${parent || ""}`] = "unconditional";
break;
case "conditional":
loop(val, result, parent);
break;
case "and":
case "or":
val.forEach((v: any, idx: number) =>
loop(v, result, `${getPath(parent)}${key}[${idx}]`),
);
break;
case "not":
loop(val, result, `${getPath(parent)}${key}`);
break;
case "time":
loop(val, result, parent);
break;
case "relative":
result[`${getPath(parent)}before_relative_time`] = BigInt(val);
break;
case "absolute":
result[`${getPath(parent)}before_absolute_time`] = BigInt(val);
break;
default:
// Do nothing
}

return result;
};

const formattedPredicate = loop(predicate, {}, undefined);

return Object.entries(formattedPredicate).reduce((res, entry) => {
const [path, value] = entry;
res = path ? set(res, path, value) : value;

return res;
}, {} as AnyObject);
};

const flagTotal = (
val: string[],
operations: OptionFlag[],
Expand Down Expand Up @@ -345,6 +402,21 @@ export const TransactionXdr = () => {
};
}

if (opType === "create_claimable_balance") {
return {
asset: formatAssetValue(params.asset),
amount: xdrUtils.toAmount(params.amount),
claimants: params.claimants.map((cl: AnyObject) => {
return {
claimant_type_v0: {
destination: cl.destination,
predicate: formatPredicate(cl.predicate),
},
};
}),
};
}

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

Expand Down
Loading