-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Build Transaction] Added operations (#843)
* Operation type description + docs URL * Payment operation * Manage buy and sell operations * Create Passive Sell Offer operation
Showing
14 changed files
with
709 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
389 changes: 251 additions & 138 deletions
389
src/app/(sidebar)/transaction/build/components/Operations.tsx
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
import { JSX } from "react"; | ||
|
||
import { PubKeyPicker } from "@/components/FormElements/PubKeyPicker"; | ||
import { TextPicker } from "@/components/FormElements/TextPicker"; | ||
import { AssetPicker } from "@/components/FormElements/AssetPicker"; | ||
import { PositiveIntPicker } from "@/components/FormElements/PositiveIntPicker"; | ||
|
||
import { validate } from "@/validate"; | ||
import { AnyObject, AssetObjectValue, JsonAsset } from "@/types/types"; | ||
|
||
// Types | ||
type TemplateRenderProps = { | ||
value: string | undefined; | ||
error: string | undefined; | ||
onChange: (val: any) => void; | ||
isRequired?: boolean; | ||
}; | ||
|
||
type TemplateRenderAssetProps = { | ||
value: JsonAsset | undefined; | ||
error: { code: string | undefined; issuer: string | undefined } | undefined; | ||
onChange: (asset: AssetObjectValue | undefined) => void; | ||
isRequired?: boolean; | ||
}; | ||
|
||
type FormComponentTemplateTxnOpsProps = { | ||
render: (...args: any[]) => JSX.Element; | ||
validate: ((...args: any[]) => any) | null; | ||
}; | ||
|
||
const assetPickerValue = ( | ||
value: JsonAsset | undefined, | ||
): AssetObjectValue | undefined => { | ||
if (!value) { | ||
return undefined; | ||
} | ||
|
||
if (value === "native") { | ||
return { type: "native", code: "", issuer: "" }; | ||
} | ||
|
||
const type = Object.keys(value)[0] as keyof typeof value; | ||
const val = value[type] as { | ||
asset_code: string; | ||
issuer: string; | ||
}; | ||
|
||
return { | ||
type, | ||
code: val.asset_code, | ||
issuer: val.issuer, | ||
}; | ||
}; | ||
|
||
export const formComponentTemplateTxnOps = ({ | ||
param, | ||
opType, | ||
index, | ||
custom, | ||
}: { | ||
param: string; | ||
opType: string; | ||
index: number; | ||
custom?: AnyObject; | ||
}): FormComponentTemplateTxnOpsProps | null => { | ||
const id = `${index}-${opType}-${param}`; | ||
|
||
switch (param) { | ||
case "amount": | ||
case "buy_amount": | ||
return { | ||
render: (templ: TemplateRenderProps) => ( | ||
<TextPicker | ||
key={id} | ||
id={id} | ||
label={custom?.label || "Amount"} | ||
labelSuffix={!templ.isRequired ? "optional" : undefined} | ||
value={templ.value || ""} | ||
error={templ.error} | ||
onChange={templ.onChange} | ||
note={custom?.note} | ||
/> | ||
), | ||
validate: validate.amount, | ||
}; | ||
case "asset": | ||
return { | ||
render: (templ: TemplateRenderAssetProps) => ( | ||
<AssetPicker | ||
key={id} | ||
assetInput="alphanumeric" | ||
id={id} | ||
label="Asset" | ||
labelSuffix={!templ.isRequired ? "optional" : undefined} | ||
value={assetPickerValue(templ.value)} | ||
error={templ.error} | ||
includeNative | ||
onChange={templ.onChange} | ||
/> | ||
), | ||
validate: validate.assetJson, | ||
}; | ||
case "buying": | ||
return { | ||
render: (templ: TemplateRenderAssetProps) => ( | ||
<AssetPicker | ||
key={id} | ||
assetInput="alphanumeric" | ||
id={id} | ||
label="Buying" | ||
labelSuffix={!templ.isRequired ? "optional" : undefined} | ||
value={assetPickerValue(templ.value)} | ||
error={templ.error} | ||
includeNative | ||
onChange={templ.onChange} | ||
/> | ||
), | ||
validate: validate.assetJson, | ||
}; | ||
case "destination": | ||
return { | ||
render: (templ: TemplateRenderProps) => ( | ||
<PubKeyPicker | ||
key={id} | ||
id={id} | ||
label="Destination" | ||
labelSuffix={!templ.isRequired ? "optional" : undefined} | ||
value={templ.value || ""} | ||
error={templ.error} | ||
onChange={templ.onChange} | ||
/> | ||
), | ||
validate: validate.publicKey, | ||
}; | ||
case "offer_id": | ||
return { | ||
render: (templ: TemplateRenderProps) => ( | ||
<PositiveIntPicker | ||
key={id} | ||
id={id} | ||
label={custom?.label || "Offer ID"} | ||
labelSuffix={!templ.isRequired ? "optional" : undefined} | ||
value={templ.value || ""} | ||
error={templ.error} | ||
onChange={templ.onChange} | ||
note={custom?.note} | ||
/> | ||
), | ||
validate: validate.positiveInt, | ||
}; | ||
case "price": | ||
return { | ||
render: (templ: TemplateRenderProps) => ( | ||
<TextPicker | ||
key={id} | ||
id={id} | ||
label={custom?.label || "Price"} | ||
labelSuffix={!templ.isRequired ? "optional" : undefined} | ||
value={templ.value || ""} | ||
error={templ.error} | ||
onChange={templ.onChange} | ||
/> | ||
), | ||
validate: validate.positiveNumber, | ||
}; | ||
case "selling": | ||
return { | ||
render: (templ: TemplateRenderAssetProps) => ( | ||
<AssetPicker | ||
key={id} | ||
assetInput="alphanumeric" | ||
id={id} | ||
label="Selling" | ||
labelSuffix={!templ.isRequired ? "optional" : undefined} | ||
value={assetPickerValue(templ.value)} | ||
error={templ.error} | ||
includeNative | ||
onChange={templ.onChange} | ||
/> | ||
), | ||
validate: validate.assetJson, | ||
}; | ||
case "source_account": | ||
return { | ||
render: (templ: TemplateRenderProps) => ( | ||
<PubKeyPicker | ||
key={id} | ||
id={id} | ||
label="Source Account" | ||
labelSuffix={!templ.isRequired ? "optional" : undefined} | ||
value={templ.value || ""} | ||
error={templ.error} | ||
onChange={templ.onChange} | ||
/> | ||
), | ||
validate: validate.publicKey, | ||
}; | ||
case "starting_balance": | ||
return { | ||
render: (templ: TemplateRenderProps) => ( | ||
<TextPicker | ||
key={id} | ||
id={id} | ||
label="Starting Balance" | ||
labelSuffix={!templ.isRequired ? "optional" : undefined} | ||
value={templ.value || ""} | ||
error={templ.error} | ||
onChange={templ.onChange} | ||
/> | ||
), | ||
validate: validate.amount, | ||
}; | ||
default: | ||
return null; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// This content is copied from js-stellar-base (src/util/continued_fraction.js) | ||
import BigNumber from "bignumber.js"; | ||
|
||
const MAX_INT = ((1 << 31) >>> 0) - 1; | ||
|
||
export function best_r(rawNumber: string | number | BigNumber) { | ||
let number = new BigNumber(rawNumber); | ||
let a; | ||
let f; | ||
const fractions = [ | ||
[new BigNumber(0), new BigNumber(1)], | ||
[new BigNumber(1), new BigNumber(0)], | ||
]; | ||
let i = 2; | ||
|
||
// eslint-disable-next-line no-constant-condition | ||
while (true) { | ||
if (number.gt(MAX_INT)) { | ||
break; | ||
} | ||
a = number.integerValue(BigNumber.ROUND_FLOOR); | ||
f = number.minus(a); | ||
const h = a.times(fractions[i - 1][0]).plus(fractions[i - 2][0]); | ||
const k = a.times(fractions[i - 1][1]).plus(fractions[i - 2][1]); | ||
if (h.gt(MAX_INT) || k.gt(MAX_INT)) { | ||
break; | ||
} | ||
fractions.push([h, k]); | ||
if (f.eq(0)) { | ||
break; | ||
} | ||
number = new BigNumber(1).div(f); | ||
i += 1; | ||
} | ||
const [n, d] = fractions[fractions.length - 1]; | ||
|
||
if (n.isZero() || d.isZero()) { | ||
throw new Error("Couldn't find approximation"); | ||
} | ||
|
||
return [n.toNumber(), d.toNumber()]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// XDR helpers from js-stellar-base | ||
import { xdr } from "@stellar/stellar-sdk"; | ||
import BigNumber from "bignumber.js"; | ||
import { best_r } from "./fraction"; | ||
|
||
const ONE = 10000000; | ||
|
||
function toXDRAmount(value: string) { | ||
// Using BigNumber to handle decimal point values | ||
return BigInt(new BigNumber(value).times(ONE).toString()); | ||
} | ||
|
||
function fromXDRAmount(value: string) { | ||
return new BigNumber(value).div(ONE).toFixed(7); | ||
} | ||
|
||
function toXDRPrice(price: string) { | ||
const approx = best_r(price); | ||
|
||
return { | ||
n: parseInt(approx[0].toString(), 10), | ||
d: parseInt(approx[1].toString(), 10), | ||
}; | ||
} | ||
|
||
function fromXDRPrice(price: xdr.Price) { | ||
const n = new BigNumber(price.n()); | ||
return n.div(new BigNumber(price.d())).toString(); | ||
} | ||
|
||
export const xdrUtils = { | ||
toAmount: toXDRAmount, | ||
fromAmount: fromXDRAmount, | ||
toPrice: toXDRPrice, | ||
fromPrice: fromXDRPrice, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { isEmptyObject } from "@/helpers/isEmptyObject"; | ||
import { JsonAsset } from "@/types/types"; | ||
import { assetCode } from "./assetCode"; | ||
import { publicKey } from "./publicKey"; | ||
|
||
// Validate asset in XDR or JSON format | ||
export const assetJson = (asset: JsonAsset | undefined) => { | ||
if (!asset || asset === "native") { | ||
return false; | ||
} | ||
|
||
const type = Object.keys(asset)[0] as keyof typeof asset; | ||
const values = asset[type] as { | ||
asset_code: string; | ||
issuer: string; | ||
}; | ||
|
||
const invalid = Object.entries({ | ||
code: assetCode(values.asset_code || "", type), | ||
issuer: publicKey(values.issuer || ""), | ||
}).reduce((res, cur) => { | ||
const [key, value] = cur; | ||
|
||
if (value) { | ||
return { ...res, [key]: value }; | ||
} | ||
|
||
return res; | ||
}, {}); | ||
|
||
return isEmptyObject(invalid) ? false : invalid; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export const positiveNumber = (value: string) => { | ||
if (value.toString().charAt(0) === "-") { | ||
return "Expected a positive number or zero."; | ||
} else if (!value.toString().match(/^[0-9]*(\.[0-9]+){0,1}$/g)) { | ||
return "Expected a positive number with a period for the decimal point."; | ||
} | ||
|
||
return false; | ||
}; |