Skip to content

Commit a2f2f46

Browse files
authored
Merge pull request #291 from kodadot/fix/isAddress
⚡ proper way to address address :D
2 parents 3de49cb + efe6c5d commit a2f2f46

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

src/mappings/nfts/setAttribute.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { getOrFail as get } from '@kodadot1/metasquid/entity'
22
import { CollectionEntity, NFTEntity } from '../../model'
33
import { unwrap } from '../utils/extract'
44
import { Context, isNFT } from '../utils/types'
5-
import { addressOf, unHex } from '../utils/helper'
5+
import { addressOf, isAddress, unHex } from '../utils/helper'
66
import { getAttributeEvent } from './getters'
77
import { attributeFrom, tokenIdOf } from './types'
88

@@ -37,8 +37,11 @@ export async function handleAttributeSet(context: Context): Promise<void> {
3737
try {
3838
final.recipient = final.recipient || addressOf(event.value as string)
3939
} catch (error) {
40-
console.log(error)
41-
final.recipient = '' // final.recipient ?? (event.value as string)
40+
const human = unHex(event.value)
41+
final.recipient = isAddress(human) ? human : ''
42+
if (final.recipient === '') {
43+
console.log(error)
44+
}
4245
}
4346
}
4447

src/mappings/utils/helper.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@ export function addressOf(address: Uint8Array | string): string {
5151
return ss58.codec(codec).encode(value)
5252
}
5353

54+
export function isAddress(value: Optional<string>): value is string {
55+
if (!value) {
56+
return false
57+
}
58+
59+
try {
60+
ss58.decode(value)
61+
return true
62+
} catch {
63+
return false
64+
}
65+
}
66+
5467
/**
5568
* Decode a hex value
5669
* @param value - the value to decode

0 commit comments

Comments
 (0)