Skip to content

Commit

Permalink
Fixes regression (#2527)
Browse files Browse the repository at this point in the history
  • Loading branch information
comountainclimber authored Sep 22, 2023
1 parent e54bd7b commit 7ca9e4f
Show file tree
Hide file tree
Showing 5 changed files with 513 additions and 88 deletions.
14 changes: 9 additions & 5 deletions app/actions/nftGalleryActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ export type NftGalleryItem = {

export type NftGalleryResults = {
results: NftGalleryItem[],
count: number,
page: number,
hasMore: boolean,
}

const DEFAULT_NFT_GALLERY_RESULTS = (previousResults?: NftGalleryItem[]) => ({
results: previousResults ?? [],
page: 0,
count: 0,
hasMore: false,
})

export async function parseGhostMarketResults({
Expand All @@ -49,9 +49,9 @@ export async function parseGhostMarketResults({
`https://api.ghostmarket.io/api/v2/assets?chain=n3&owners[]=${address}&size=${SIZE}&page=${page}&getTotal=true`,
)

const { assets, total: count } = response?.data
const { assets, next } = response?.data

if (!assets || !assets.length || !count)
if (!assets || !assets.length)
return DEFAULT_NFT_GALLERY_RESULTS(previousResults)

const results = assets.map(
Expand All @@ -70,7 +70,11 @@ export async function parseGhostMarketResults({
}),
)

return { results: previousResults.concat(results), page, count }
return {
results: previousResults.concat(results),
page,
hasMore: !!next && assets.length === SIZE,
}
} catch (e) {
console.error('An error occurred fetching data for NFT gallery', { e })
return DEFAULT_NFT_GALLERY_RESULTS(previousResults)
Expand Down
179 changes: 99 additions & 80 deletions app/components/Modals/TransferNftModal/TransferNftModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { FormattedMessage } from 'react-intl'
import { wallet as n3Wallet } from '@cityofzion/neon-js'
import { BSNeo3 } from '@cityofzion/bs-neo3'

import { NeonInvoker } from '@cityofzion/neon-invoker'
import { NFT } from '../../../containers/NftGallery/NftGallery'
import Button from '../../Button'
import SelectInput from '../../Inputs/SelectInput'
Expand All @@ -13,6 +12,8 @@ import N3Fees from '../../Send/N3Fees'
import BaseModal from '../BaseModal'
import styles from './TransferNftModal.scss'
import { getNode, getRPCEndpoint } from '../../../actions/nodeStorageActions'
import N3Helper from '../../../util/N3Helper'
import { convertToArbitraryDecimals } from '../../../core/formatters'
import { addPendingTransaction } from '../../../actions/pendingTransactionActions'
import { useContactsContext } from '../../../context/contacts/ContactsContext'
import { MODAL_TYPES } from '../../../core/constants'
Expand All @@ -30,10 +31,10 @@ type Props = {
address: string,
tokenId: string,
wif: string,
showSuccessNotification({ message: string }): any,
showErrorNotification({ message: string }): any,
showInfoNotification({ message: string, autoDismiss: number }): any,
hideNotification(id: string): any,
showSuccessNotification: ({ message: string }) => any,
showErrorNotification: ({ message: string }) => any,
showInfoNotification: ({ message: string }) => any,
hideNotification: (id: string) => void,
dispatch: any => any,
isHardwareLogin: boolean,
signingFunction: () => void,
Expand All @@ -51,14 +52,14 @@ export default function TransferNftModal(props: Props) {
address,
wif,
dispatch,
isHardwareLogin,
signingFunction,
showSuccessNotification,
showErrorNotification,
showInfoNotification,
hideNotification,
recipientAddressProp,
publicKey,
isHardwareLogin,
signingFunction,
} = props
function handleSubmit() {}

Expand All @@ -71,7 +72,8 @@ export default function TransferNftModal(props: Props) {
recipientAddressProp ?? '',
)
const [recipientAddressError, setRecipientAddressError] = useState('')
const [fees, setFees] = useState(DEFAULT_FEES)
const [gasFee, setGasFee] = useState(DEFAULT_FEES)
const [feesInitialized, setFeesInitialized] = useState(false)
const [sendButtonDisabled, setSendButtonDisabled] = useState(false)
const [loading, setLoading] = useState(true)
const { contacts } = useContactsContext()
Expand Down Expand Up @@ -158,54 +160,53 @@ export default function TransferNftModal(props: Props) {
async function transfer() {
try {
setLoading(true)
let rpcAddress = await getNode(net)
if (!rpcAddress) {
rpcAddress = await getRPCEndpoint(net)
let endpoint = await getNode(net)
if (!endpoint) {
endpoint = await getRPCEndpoint(net)
}
const account = new n3Wallet.Account(isHardwareLogin ? publicKey : wif)

const invoker = await NeonInvoker.init({
rpcAddress,
account,
signingCallback: signingFunction,
})

let notificationId

if (isHardwareLogin) {
notificationId = showInfoNotification({
message: 'Please sign the transaction on your hardware device',
autoDismiss: 0,
})
const testReq = {
params: {
request: {
method: 'multiInvoke',
params: {
invocations: [
{
scriptHash: contract,
operation: 'transfer',
args: [
{
type: 'Hash160',
value: recipientAddress,
},
{ type: 'ByteArray', value: tokenId },
{ type: 'Any', value: null },
],
},
],
signers: [{ scopes: 1 }],
},
},
},
}

const hash = await invoker.invokeFunction({
invocations: [
{
scriptHash: contract,
operation: 'transfer',
args: [
{
type: 'Hash160',
value: recipientAddress,
},
{ type: 'ByteArray', value: tokenId },
{ type: 'Any', value: null },
],
},
],
})
const results = await new N3Helper(endpoint, 0).rpcCall(
account,
testReq,
isHardwareLogin,
signingFunction,
showInfoNotification,
hideNotification,
)

if (notificationId) {
hideNotification(notificationId)
}
const { result } = results

dispatch(
addPendingTransaction.call({
address,
net,
tx: {
hash,
hash: result,
sendEntries: [
{ amount: 1, address, contractHash: contract, symbol: 'N/A' },
],
Expand All @@ -216,52 +217,67 @@ export default function TransferNftModal(props: Props) {
showSuccessNotification({
message: 'Transaction pending! Your NFT will be transferred shortly.',
})
setLoading(false)
hideModal()
} catch (e) {
hideModal()
showErrorNotification({
message: e.message,
})
} finally {
setLoading(false)
hideModal()
}
}

useEffect(
() => {
;(async () => {
let rpcAddress = await getNode(net)
if (!rpcAddress) {
rpcAddress = await getRPCEndpoint(net)
}
const account = new n3Wallet.Account(isHardwareLogin ? publicKey : wif)

const invoker = await NeonInvoker.init({ rpcAddress, account })
const { networkFee, systemFee } = await invoker.calculateFee({
invocations: [
{
scriptHash: contract,
operation: 'transfer',
args: [
useEffect(() => {
async function testInvoke() {
setLoading(true)
let endpoint = await getNode(net)
if (!endpoint) {
endpoint = await getRPCEndpoint(net)
}
const account = new n3Wallet.Account(address)
const testReq = {
params: {
request: {
method: 'testInvoke',
params: {
invocations: [
{
type: 'Hash160',
value: address,
scriptHash: contract,
operation: 'transfer',
args: [
{
type: 'Hash160',
value: address,
},
{ type: 'ByteArray', value: tokenId },
{ type: 'Any', value: null },
],
},
{ type: 'ByteArray', value: tokenId },
{ type: 'Any', value: null },
],
signers: [{ scopes: 1 }],
},
],
signers: [{ scopes: 1 }],
})

setFees({
networkFee,
systemFee,
})
})()
},
[address, tokenId, isHardwareLogin, publicKey, wif, net, contract],
)
},
},
}
const results = await new N3Helper(endpoint, 0).rpcCall(
account,
testReq,
isHardwareLogin,
signingFunction,
showInfoNotification,
hideNotification,
)
const fee = convertToArbitraryDecimals(results.result.gasconsumed)
setGasFee({
networkFee: fee,
systemFee: 0,
})
setFeesInitialized(true)
setLoading(false)
}
testInvoke()
}, [])

function createContactList(): Array<string> {
const filteredContacts = Object.keys(contacts).filter(contact =>
Expand Down Expand Up @@ -298,7 +314,10 @@ export default function TransferNftModal(props: Props) {
error={recipientAddressError}
/>

<N3Fees fees={fees} notEnoughGasCallback={toggleHasEnoughGas} />
<N3Fees
fees={loading && !feesInitialized ? DEFAULT_FEES : gasFee}
notEnoughGasCallback={toggleHasEnoughGas}
/>

<Button
className={styles.submitButton}
Expand Down
7 changes: 4 additions & 3 deletions app/containers/NftGallery/NftGallery.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ type Props = {
networkId: string,
net: string,
page: number,
count: number,
// count: number,
isWatchOnly: boolean,
showModal: (type: string, props: any) => any,
hasMore: boolean,
}

export function NFT({
Expand Down Expand Up @@ -118,7 +119,7 @@ export default function NFTGallery({
loading,
page,
fetchAddtionalNFTData,
count,
hasMore,
isWatchOnly,
showModal,
}: Props) {
Expand Down Expand Up @@ -206,7 +207,7 @@ export default function NFTGallery({
)
})}

{count !== results.length && (
{hasMore && (
<div className={styles.loadMoreButton}>
<Button
onClick={() =>
Expand Down
Loading

0 comments on commit 7ca9e4f

Please sign in to comment.