Skip to content

Commit

Permalink
Merge pull request #808 from TokenScript/feat/outlet-ticket-remove
Browse files Browse the repository at this point in the history
feat: Add ticket delete method and various other improvements
  • Loading branch information
nicktaras authored Aug 27, 2023
2 parents 213d4e9 + e7cc143 commit 689748b
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,7 @@ export class Client {
) {
requiredParams(type, 'Event type is not defined')

if ((type === 'tokens' || type === 'tokens-selected') && callback) {
if (type === 'tokens-selected' && callback) {
this.readTokensFromUrl()
}

Expand Down
12 changes: 6 additions & 6 deletions src/client/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { BrowserDataInterface } from '../utils/support/isSupported'
import { WalletConnection } from '../wallet/Web3WalletProvider'
import { DecodedToken } from '../outlet/ticketStorage'
import { EasSchemaConfig } from '../outlet/interfaces'
import { TokenData } from './tokenStore'

export type SupportedBlockchainsParam = 'evm' | 'flow' | 'solana' | 'ultra'
export const SignatureSupportedBlockchainsParamList = ['evm', 'flow', 'solana', 'ultra']
Expand Down Expand Up @@ -117,7 +118,6 @@ export interface TokenNegotiatorEventsArgs {
'connected-wallet': EventSenderConnectedWallet
'disconnected-wallet': EventSenderDisconnectedWallet
'tokens-selected': EventSenderTokensSelected
tokens: EventSenderTokens
'tokens-loaded': EventSenderTokensLoaded
'network-change': string
error: EventSenderError
Expand Down Expand Up @@ -150,11 +150,11 @@ export interface EventSenderViewChanged {
}

export interface EventSenderTokensSelected {
selectedTokens: Object
}

export interface EventSenderTokens {
data: any[]
selectedTokens: {
[collectionId: string]: {
tokens: DecodedToken[] | TokenData[]
}
}
}

export interface EventSenderTokensLoaded {
Expand Down
12 changes: 8 additions & 4 deletions src/client/tokenStore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { OffChainTokenConfig, OnChainIssuer, OnChainTokenConfig, SolanaIssuerConfig, UltraIssuerConfig } from './interface'

import { logger } from '../utils'
import { DecodedToken } from '../outlet/ticketStorage'

interface IssuerLookup {
[collectionID: string]: TokenConfig & { timestamp: number }
Expand All @@ -10,14 +11,17 @@ interface TokenLookup {
[issuer: string]: { timestamp: number; tokens: TokenData[] | null }
}

interface TokenData {
export interface TokenData {
tokenId: string | number
walletAddress?: string
// TODO: add more common fields to this interface
[key: string]: any
}

type TokenConfig = OnChainTokenConfig | OffChainTokenConfig | SolanaIssuerConfig | UltraIssuerConfig

type SelectedTokens = { [collectionId: string]: { tokens: DecodedToken[] | TokenData[] } }

export class TokenStore {
public static LOCAL_STORAGE_KEY = 'tn-tokenStore'

Expand All @@ -28,7 +32,7 @@ export class TokenStore {
private tokenLookup: IssuerLookup = {}

// TODO: change to disabled tokens
private selectedTokens: any = {}
private selectedTokens: SelectedTokens = {}

constructor(
private autoEnableTokens: boolean,
Expand Down Expand Up @@ -173,7 +177,7 @@ export class TokenStore {
return null
}

public setTokens(issuer: string, tokens: TokenData[]) {
public setTokens(issuer: string, tokens: TokenData[] | DecodedToken[]) {
this.tokenData[issuer] = { timestamp: Date.now(), tokens }

this.saveTokenStore()
Expand All @@ -185,7 +189,7 @@ export class TokenStore {
return this.selectedTokens
}

public setSelectedTokens(selectedTokens: any) {
public setSelectedTokens(selectedTokens: SelectedTokens) {
this.selectedTokens = selectedTokens
}

Expand Down
3 changes: 1 addition & 2 deletions src/client/views/token-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ export class TokenList extends AbstractView {

this.viewContainer.querySelectorAll('.mobileToggle-tn').forEach((token: any, index: number) => {
if (index === 0) {
selectedTokens[token.dataset.key] = {}
selectedTokens[token.dataset.key]['tokens'] = []
selectedTokens[token.dataset.key] = { tokens: [] }
}

if (token.checked === true) {
Expand Down
2 changes: 1 addition & 1 deletion src/outlet/localOutlet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { MultiTokenAuthRequest, MultiTokenAuthResult, OutletInterface, OutletIss
import { OutletAction } from '../client/messaging'

export class LocalOutlet {
protected ticketStorage: TicketStorage
public readonly ticketStorage: TicketStorage

constructor(config: OutletInterface) {
this.ticketStorage = new TicketStorage(config)
Expand Down
20 changes: 19 additions & 1 deletion src/outlet/ticketStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export interface EasFieldData {
}

interface TicketStorageSchema {
[collectionHash: string]: StoredTicketRecord[]
[collectionHash: string]: Array<StoredTicketRecord>
}

export interface FilterInterface {
Expand Down Expand Up @@ -167,6 +167,24 @@ export class TicketStorage {
})
}

public deleteTicketByDecodedTokenOrId(collectionId: string, decodedTokenOrId: DecodedToken | string) {
const config = this.config.issuers.find((issuer) => issuer.collectionID === collectionId)
const hashes = createIssuerHashArray(config)
const tokenId = typeof decodedTokenOrId === 'string' ? decodedTokenOrId : decodedTokenOrId.tokenId

for (const hash of hashes) {
for (let i = 0; i < this.ticketCollections[hash].length; i++) {
if (tokenId === this.ticketCollections[hash][0].tokenId) {
this.ticketCollections[hash].splice(i, 1)
this.storeTickets()
return true
}
}
}

return false
}

/**
* Take in a request of collection hashes and return the returns, keyed by client provided ID
* @param request
Expand Down

0 comments on commit 689748b

Please sign in to comment.