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

Added validation for token metadata #110

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 4 additions & 3 deletions as/cNFT/assembly/__tests__/index.cnft.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { Token } from '../models/persistent_tokens'
import { AccountId } from '../types'
import { nft_payout } from '../royalty_payout'
import { Bid } from '../models/market'
import { ONE_NEAR } from '../../../utils'
import {ONE_NEAR, randomInt} from '../../../utils'
import { nft_metadata_extra } from '../metadata'

const initContract = (): void => {
Expand All @@ -38,8 +38,9 @@ const mintToken = (accountId: AccountId): Token => {
VMContext.setAttached_deposit(ONE_NEAR)

const token_metadata = new TokenMetadata()
token_metadata.media = 'media'
token_metadata.extra = 'extra'
token_metadata.title = 'Sample Token'
token_metadata.description = 'Sample token description'
token_metadata.copies = <u8>randomInt(1, 10)

const token_royalty = new TokenRoyalty()
token_royalty.percentage = 2500
Expand Down
1 change: 1 addition & 0 deletions as/cNFT/assembly/mint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export function mint(
token_royalty: TokenRoyalty
): Token {
assert_not_paused()
assert(tokenMetadata.title && tokenMetadata.description && tokenMetadata.copies, 'Token should have valid metadata')

const contract_extra = storage.getSome<NFTContractExtra>(
PersistentNFTContractMetadata.STORAGE_KEY_EXTRA
Expand Down
6 changes: 6 additions & 0 deletions as/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,9 @@ export function royalty_to_payout(
u128.from(ONE_HUNDRED_PERCENT)
)
}


/** @hidden */
export function randomInt(min: number = 0, max: number = 10): number {
return Math.floor(Math.random() * (max - min + 1) + min)
}
16 changes: 8 additions & 8 deletions near-workspaces/__tests__/nft.ava.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
CONTRACT_EXTRA,
CONTRACT_METADATA,
get_random_token_metadata,
TOKEN_ROYALTY,
} from '../utils/dummyData'
import {
call_burn_design,
Expand Down Expand Up @@ -79,14 +80,13 @@ workspace.test(
})
test.log(`✓ Alice failed to mint with wrong deposit\n`)

/** @todo fix in the contract **/
// await test.throwsAsync(async () => {
// await call_mint(contract, alice, {
// tokenMetadata: {},
// token_royalty: TOKEN_ROYALTY
// })
// })
// test.log(`✓ Alice failed to mint without tokenMetadata\n`)
await test.throwsAsync(async () => {
await call_mint(contract, alice, {
tokenMetadata: {},
token_royalty: TOKEN_ROYALTY
})
})
test.log(`✓ Alice failed to mint without tokenMetadata\n`)

await test.throwsAsync(async () => {
await call_mint(contract, alice, {
Expand Down