-
Notifications
You must be signed in to change notification settings - Fork 24
feat: integrate clarity-abitype #61
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,12 @@ | ||
| import { | ||
| AuthType, | ||
| bufferCVFromString, | ||
| ClarityType, | ||
| contractPrincipalCV, | ||
| cvToString, | ||
| listCV, | ||
| noneCV, | ||
| Pc, | ||
| PostConditionMode, | ||
| principalCV, | ||
| PrincipalCV, | ||
| someCV, | ||
| standardPrincipalCV, | ||
| trueCV, | ||
|
|
@@ -41,8 +38,9 @@ export type Row = { | |
| stx: string; | ||
| memo?: string; | ||
| error?: string; | ||
| toCV?: PrincipalCV; | ||
| toCV?: string; | ||
| }; | ||
|
|
||
| const addrToCV = (addr: string) => { | ||
| const toParts = addr.split('.'); | ||
| if (toParts.length === 1) { | ||
|
|
@@ -52,28 +50,17 @@ const addrToCV = (addr: string) => { | |
| } | ||
| }; | ||
|
|
||
| const addToCVValues = async <T extends Row>(parts: T[]) => { | ||
| return Promise.all( | ||
| parts.map(async p => { | ||
| if (p.to === '') { | ||
| return p; | ||
| } | ||
| try { | ||
| return { ...p, toCV: addrToCV(p.to) }; | ||
| } catch (e) { | ||
| try { | ||
| const owner = await getNameInfo(toAscii(p.to)); | ||
| if (owner.type === ClarityType.OptionalSome) { | ||
| return { ...p, toCV: owner.value.value.owner }; | ||
| } else { | ||
| return { ...p, error: `No address for ${p.to}` }; | ||
| } | ||
| } catch (e2) { | ||
| return { ...p, error: `${p.to} not found` }; | ||
| } | ||
| } | ||
| }) | ||
| ); | ||
| const resolveRecipient = async (recipient: string): Promise<string> => { | ||
| try { | ||
| c32addressDecode(recipient); | ||
| return recipient; | ||
| } catch (e) { | ||
| const owner = await getNameInfo(toAscii(recipient)); | ||
| if (owner?.owner) { | ||
| return owner.owner; | ||
| } | ||
| throw new Error(`No address for ${recipient}`); | ||
| } | ||
|
Comment on lines
+58
to
+63
|
||
| }; | ||
|
|
||
| function nonEmptyPart(p: Row) { | ||
|
|
@@ -168,7 +155,7 @@ export function SendManyInputContainer({ | |
| setNamesResolved(!!p.toCV); | ||
| return ( | ||
| <Fragment key={index}> | ||
| {p.error || (p.toCV ? <Address addr={cvToString(p.toCV)} /> : '...')}:{' '} | ||
| {p.error || (p.toCV ? <Address addr={p.toCV} /> : '...')}:{' '} | ||
| <Amount amount={p.ustx} asset={asset} /> <br /> | ||
| <br /> | ||
| </Fragment> | ||
|
|
@@ -371,21 +358,36 @@ export function SendManyInputContainer({ | |
| let { parts, total, hasMemos } = getPartsFromRows( | ||
| useAssetForFees ? cloneAndAddFees(rows) : rows | ||
| ); | ||
| const updatedParts = await addToCVValues(parts); | ||
| let invalidNames = updatedParts.filter(r => !!r.error); | ||
| if (invalidNames.length > 0) { | ||
| updatePreview({ parts: updatedParts, total, hasMemos }); | ||
|
|
||
| const resolvedPartsWithErrors = await Promise.all( | ||
| parts.map(async p => { | ||
| if (p.to === '') { | ||
| return p; | ||
| } | ||
| try { | ||
| return { ...p, toCV: await resolveRecipient(p.to) }; | ||
| } catch (e) { | ||
| return { ...p, error: `${p.to} not found` }; | ||
| } | ||
| }) | ||
| ); | ||
|
|
||
| const errors = resolvedPartsWithErrors.filter(r => !!r.error); | ||
| if (errors.length > 0) { | ||
| updatePreview({ parts: resolvedPartsWithErrors, total, hasMemos }); | ||
| setLoading(false); | ||
| setStatus('Please verify receivers'); | ||
| return; | ||
| } | ||
|
|
||
| if (!namesResolved) { | ||
| updatePreview({ parts: updatedParts, total, hasMemos }); | ||
| updatePreview({ parts: resolvedPartsWithErrors, total, hasMemos }); | ||
| setLoading(false); | ||
| setNamesResolved(true); | ||
| return; | ||
| } | ||
| const nonEmptyParts = updatedParts.filter(nonEmptyPart); | ||
|
|
||
| const nonEmptyParts = resolvedPartsWithErrors.filter(nonEmptyPart); | ||
| console.log(nonEmptyParts[0]); | ||
| const firstMemo = | ||
| nonEmptyParts.length > 0 && nonEmptyParts[0].memo ? nonEmptyParts[0].memo.trim() : ''; | ||
|
|
@@ -406,13 +408,13 @@ export function SendManyInputContainer({ | |
| nonEmptyParts.map(p => { | ||
| return hasMemos | ||
| ? tupleCV({ | ||
| to: p.toCV!, | ||
| to: addrToCV(p.toCV!), | ||
| ustx: uintCV(p.ustx), | ||
| memo: bufferCVFromString( | ||
| firstMemoForAll ? firstMemo : p.memo ? p.memo.trim() : '' | ||
| ), | ||
| }) | ||
| : tupleCV({ to: p.toCV!, ustx: uintCV(p.ustx) }); | ||
| : tupleCV({ to: addrToCV(p.toCV!), ustx: uintCV(p.ustx) }); | ||
| }) | ||
| ), | ||
| ], | ||
|
|
@@ -431,7 +433,7 @@ export function SendManyInputContainer({ | |
| functionArgs: [ | ||
| nonEmptyParts.map(p => { | ||
| return tupleCV({ | ||
| to: p.toCV!, | ||
| to: addrToCV(p.toCV!), | ||
| 'xbtc-in-sats': uintCV(p.ustx), | ||
| memo: bufferCVFromString( | ||
| hasMemos ? (firstMemoForAll ? firstMemo : p.memo ? p.memo.trim() : '') : '' | ||
|
|
@@ -464,7 +466,7 @@ export function SendManyInputContainer({ | |
| listCV( | ||
| nonEmptyParts.map(p => { | ||
| return tupleCV({ | ||
| to: p.toCV!, | ||
| to: addrToCV(p.toCV!), | ||
| sender: principalCV(ownerStxAddress), | ||
| amount: uintCV(p.ustx), | ||
| memo: hasMemos | ||
|
|
@@ -503,7 +505,7 @@ export function SendManyInputContainer({ | |
| listCV( | ||
| nonEmptyParts.map(p => { | ||
| return tupleCV({ | ||
| to: p.toCV!, | ||
| to: addrToCV(p.toCV!), | ||
| amount: uintCV(p.ustx), | ||
| memo: hasMemos | ||
| ? firstMemoForAll | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing error handling for the async function call. If
getNameFromAddressthrows an error or rejects, the promise rejection will be unhandled. Consider adding a.catch()handler to thefn(addr)call to gracefully handle errors (e.g., network failures, invalid addresses). The error handler should silently fail or log the error without crashing the component, since the component already falls back to displaying the short address when no name is found.