Skip to content

Commit

Permalink
fix: compatible with legacy red packet (#11399)
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleBill authored Feb 18, 2024
1 parent bbe352d commit cc3393c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
29 changes: 26 additions & 3 deletions packages/plugins/RedPacket/src/SiteAdaptor/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
import { createRenderWithMetadata, createTypedMessageMetadataReader } from '@masknet/typed-message-react'
import { RedPacketMetaKey, RedPacketNftMetaKey } from '../constants.js'
import { EVMChainResolver } from '@masknet/web3-providers'
import type { RedPacketJSONPayload, RedPacketNftJSONPayload } from '@masknet/web3-providers/types'
import schema from '../schema.json'
import { ChainId } from '@masknet/web3-shared-evm'
import { Ok, type Result } from 'ts-results-es'
import { RedPacketMetaKey, RedPacketNftMetaKey } from '../constants.js'
import schemaNtf from '../schema-nft.json'
import schema from '../schema.json'

export const reader = createTypedMessageMetadataReader<RedPacketJSONPayload>(RedPacketMetaKey, schema)
export function RedPacketMetadataReader(
metadata: ReadonlyMap<string, unknown> | undefined,
): Result<RedPacketJSONPayload, void> {
const result = reader(metadata)
if (result.isOk()) {
const payload = result.value
// Hard code for legacy RedPacket
if (!payload.token && payload.contract_version === 1 && payload.token_type === 0) {
const chainId = payload.network === 'Mainnet' ? ChainId.Mainnet : undefined
if (!chainId) return result

export const RedPacketMetadataReader = createTypedMessageMetadataReader<RedPacketJSONPayload>(RedPacketMetaKey, schema)
return Ok({
...payload,
token: EVMChainResolver.nativeCurrency(chainId),
})
}
return result
}
return result
}
export const renderWithRedPacketMetadata = createRenderWithMetadata(RedPacketMetadataReader)

export const RedPacketNftMetadataReader = createTypedMessageMetadataReader<RedPacketNftJSONPayload>(
Expand Down
4 changes: 4 additions & 0 deletions packages/web3-providers/src/RedPacket/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export interface RedPacketJSONPayload extends RedPacketBasic {
chainId?: ChainId
network?: string
token?: FungibleToken<ChainId, SchemaType>
/**
* For contract_version === 1, payload has no token but token_type
*/
token_type?: 0 | number
total_remaining?: string
}

Expand Down

0 comments on commit cc3393c

Please sign in to comment.