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

spl-memo on delegate dc #431

Merged
merged 3 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"@rnmapbox/maps": "^10.0.7-rc.0",
"@shopify/restyle": "1.8.0",
"@solana/spl-account-compression": "0.1.4",
"@solana/spl-memo": "^0.2.3",
"@solana/spl-token": "0.3.6",
"@solana/spl-token-registry": "^0.2.4574",
"@solana/wallet-adapter-react": "^0.15.33",
Expand Down
89 changes: 89 additions & 0 deletions src/components/MemoInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { BoxProps } from '@shopify/restyle'
import React, { memo, useMemo } from 'react'
import { useTranslation } from 'react-i18next'
import { Theme } from '@theme/theme'
import Box from './Box'
import Text from './Text'
import TextInput from './TextInput'

export const DEFAULT_MEMO = 'AAAAAAAAAAA='
export const MEMO_MAX_BYTES = 8
export const encodeMemoString = (utf8Input: string | undefined) => {
if (!utf8Input) return undefined
const buff = Buffer.from(utf8Input, 'utf8')
return buff.toString('base64')
}

export const decodeMemoString = (base64String: string | undefined | null) => {
if (!base64String) return ''
const buff = Buffer.from(base64String, 'base64')
return buff.toString('utf8')
}

export const getMemoBytesLeft = (base64Memo?: string) => {
if (!base64Memo)
return { numBytes: MEMO_MAX_BYTES, valid: true, bytesUsed: 0 }
const buff = Buffer.from(base64Memo, 'base64')
const size = buff.byteLength
return {
bytesRemaining: MEMO_MAX_BYTES - size,
valid: size <= MEMO_MAX_BYTES,
bytesUsed: size,
}
}

export const getMemoStrValid = (memoStr?: string) => {
const base64Memo = encodeMemoString(memoStr)
if (!base64Memo) {
return true
}
const buff = Buffer.from(base64Memo, 'base64')
const size = buff.byteLength
return size <= MEMO_MAX_BYTES
}

export const useMemoValid = (txnMemo?: string) => {
return useMemo(() => {
const base64Memo = encodeMemoString(txnMemo)
return getMemoBytesLeft(base64Memo)
}, [txnMemo])
}

type Props = {
onChangeText?: ((text: string) => void) | undefined
value?: string | undefined
} & BoxProps<Theme>
const MemoInput = ({ onChangeText, value, ...boxProps }: Props) => {
const { t } = useTranslation()
const { bytesUsed, valid } = useMemoValid(value)

return (
<Box
justifyContent="center"
flexDirection="row"
alignItems="center"
{...boxProps}
>
<TextInput
flex={1}
variant="transparent"
textInputProps={{
placeholder: t('payment.enterMemo'),
onChangeText,
value,
editable: !!onChangeText,
returnKeyType: 'done',
}}
/>
<Box position="absolute" top={0} right={0}>
<Text variant="body3" color={valid ? 'white' : 'error'} marginRight="m">
{t('payment.memoBytes', {
used: bytesUsed,
total: MEMO_MAX_BYTES,
})}
</Text>
</Box>
</Box>
)
}
export default memo(MemoInput)
Loading
Loading