Skip to content

Commit

Permalink
Merge pull request #24 from Mintbase/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
microchipgnu authored Apr 21, 2021
2 parents f10915f + 14c24a6 commit 673cc7d
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class API {
break
default:
this.apiBaseUrl = config.apiBaseUrl || API_BASE_NEAR_TESTNET
this.chainName = config.chain
this.chainName = config.chain || Chain.near
break
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/browser-index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/** @hidden @module */

import 'regenerator-runtime/runtime'

export * from './common-index'
Expand Down
40 changes: 38 additions & 2 deletions src/common-index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,38 @@
export { API } from './api'
export { Wallet } from './wallet'
/** @hidden @module */
import { API } from './api'
import { Wallet } from './wallet'
import {
Chain,
Network,
MintbaseAPIConfig,
WalletLoginProps,
Split,
Royalties,
Token,
List,
Account,
Visibility,
DisplayType,
Attribute,
MetadataField,
MintMetadata,
} from './types'

export {
API,
Wallet,
Chain,
Network,
MintbaseAPIConfig,
WalletLoginProps,
Split,
Royalties,
Token,
List,
Account,
Visibility,
DisplayType,
Attribute,
MetadataField,
MintMetadata,
}
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,5 @@ export const CLOUD_STORAGE_CONFIG = {
projectId: process.env.MINTBASEJS_FIREBASE_PROJECT_ID,
storageBucket: process.env.MINTBASEJS_FIRSTORE_BUCKET_URL,
}

export const DEFAULT_ROYALY_PERCENT = 1000
7 changes: 2 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import 'regenerator-runtime/runtime'
/** @ignore @module */

import { API } from './api'
import { Wallet } from './wallet'

export { API, Wallet }
export * from './common-index'
1 change: 1 addition & 0 deletions src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const FETCH_MARKETPLACE = `
}
}
`

export const GET_LATEST_LIST = `
query getListedItemsTree($groupId: String!) {
list(where: { groupId: { _eq: $groupId } }, limit: 1) {
Expand Down
51 changes: 38 additions & 13 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export enum Chain {
enum Chain {
near = 'near',
}

export enum Network {
enum Network {
main = 'main',
testnet = 'testnet',
}

export interface List {
interface List {
acceptedOfferId?: string
autoTransfer?: boolean
ownerId?: string
Expand All @@ -29,37 +29,45 @@ export interface List {
* @param networkName `Network` type to use. Defaults to `Network.testnet` (testnet)
* @param apiBaseUrl The API base url
*/
export interface MintbaseAPIConfig {
chain: Chain
interface MintbaseAPIConfig {
chain?: Chain
networkName?: Network
apiBaseUrl?: string
apiKey?: string
}

export interface WalletLoginProps {
interface WalletLoginProps {
requestSignIn?: boolean
contractAddress?: string
accountId?: string
privateKey?: string // useful for node environment
}

export interface Token {
interface Token {
id: string
ownerId: string
storeId: string
thingId: string
}

export interface Account {
interface Split {
[account: string]: number
}

interface Royalties {
[account: string]: number
}

interface Account {
id: string
}

export enum Visibility {
enum Visibility {
nsfw = 'nsfw',
safe = 'safe',
}

export enum DisplayType {
enum DisplayType {
boostNumber = 'boost_number',
boostPercentage = 'boost_percentage',
number = 'number',
Expand All @@ -74,13 +82,13 @@ export enum DisplayType {
longitude = 'longitude',
}

export interface Attribute {
interface Attribute {
trait_type: string
display_type?: DisplayType
value: string | number
}

export enum MetadataField {
enum MetadataField {
Id = 'id',
Name = 'name',
Description = 'description',
Expand Down Expand Up @@ -115,7 +123,7 @@ export enum MetadataField {
Royalty_perc = 'royalty_perc',
}

export interface MintMetadata {
interface MintMetadata {
[MetadataField.Id]?: string
[MetadataField.Name]: string
[MetadataField.Description]: string
Expand Down Expand Up @@ -146,3 +154,20 @@ export interface MintMetadata {
[MetadataField.Royalty]: { [key: string]: number } | null
[MetadataField.Royalty_perc]: number | null
}

export {
Chain,
Network,
MintbaseAPIConfig,
WalletLoginProps,
Split,
Royalties,
Token,
List,
Account,
Visibility,
DisplayType,
Attribute,
MetadataField,
MintMetadata,
}
129 changes: 118 additions & 11 deletions src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import {
MintbaseAPIConfig,
WalletLoginProps,
Network,
Token,
Split,
Royalties,
} from './types'

import {
Expand All @@ -31,6 +32,7 @@ import {
MARKET_ACCOUNT,
STORE_CONTRACT_VIEW_METHODS,
STORE_CONTRACT_CALL_METHODS,
DEFAULT_ROYALY_PERCENT,
} from './constants'
import { Minter } from './minter'

Expand Down Expand Up @@ -237,8 +239,8 @@ export class Wallet {

/**
* Transfer one or more tokens.
* @param contractName The contract name to transfer tokens from.
* @param tokenIds The mapping of transfers, defined by: [[accountName1, tokenId1], [accountName2, tokenId2]]
* @param contractName The contract name to transfer tokens from.
*/

// TODO: need more checks on the tokenIds
Expand All @@ -264,6 +266,32 @@ export class Wallet {
await contract.batch_transfer({ token_ids: tokenIds }, MAX_GAS, YOCTO)
}

/**
* Burn one or more tokens.
* @param contractName The contract name to burn tokens from.
* @param tokenIds The mapping of burns, defined by: [[accountName1, tokenId1], [accountName2, tokenId2]]
*/

// TODO: need more checks on the tokenIds
public async burn(tokenIds: number[], contractName: string): Promise<void> {
const account = this.activeWallet?.account()
const accountId = this.activeWallet?.account().accountId
const MAX_GAS = new BN('300000000000000')
const YOCTO = new BN('1')

if (!account || !accountId) throw new Error('Account is undefined.')

if (!contractName) throw new Error('No contract was provided.')

const contract = new Contract(account, contractName, {
viewMethods: STORE_CONTRACT_VIEW_METHODS,
changeMethods: STORE_CONTRACT_CALL_METHODS,
})

// @ts-ignore: method does not exist on Contract type
await contract.batch_burn({ token_ids: tokenIds }, MAX_GAS, YOCTO)
}

/**
* List an item for sale in the market.
* @param tokenId The token id list.
Expand All @@ -274,8 +302,7 @@ export class Wallet {
public async batchList(
tokenId: string[],
storeId: string,
price: string,
splits?: { accountId: string; split: number }[]
price: string
): Promise<void> {
const account = this.activeWallet?.account()
const accountId = this.activeWallet?.account().accountId
Expand Down Expand Up @@ -324,8 +351,7 @@ export class Wallet {
public async list(
tokenId: string,
storeId: string,
price: string,
splits?: { accountId: string; split: number }[]
price: string
): Promise<void> {
const account = this.activeWallet?.account()
const accountId = this.activeWallet?.account().accountId
Expand Down Expand Up @@ -447,9 +473,9 @@ export class Wallet {
public async mint(
amount: number,
contractName: string,
royalties: any,
splits: any,
category: string
royalties?: Royalties,
splits?: Split,
category?: string
): Promise<void> {
const account = this.activeWallet?.account()
const accountId = this.activeWallet?.account().accountId
Expand All @@ -473,10 +499,10 @@ export class Wallet {
metadata: {
reference: metadataId,
},
num_to_mint: amount, //numToMint,
num_to_mint: amount,
royalty_args: !royalties
? null
: { split_between: royalties, percentage: 1000 },
: { split_between: royalties, percentage: DEFAULT_ROYALY_PERCENT },
split_owners: splits || null,

// TODO: check if category is lowercase
Expand All @@ -487,6 +513,87 @@ export class Wallet {
await contract.mint_tokens(obj, MAX_GAS, ZERO)
}

/**
* Mint more pieces of tokens of a thing.
* @param amount The number of tokens to mint.
* @param id The thing id
* @param splits The contract in which tokens will be minted.
*/
public async mintMore(
amount: number,
id: string,
splits?: Split
): Promise<void> {
const account = this.activeWallet?.account()
const accountId = this.activeWallet?.account().accountId
const MAX_GAS = new BN('300000000000000')
const ZERO = new BN('0')

if (!account || !accountId) throw new Error('Account is undefined.')

const thingResult = (await this.api.custom(
`query GET_THING_BY_ID($id: String!) {
thing(where: {id: {_eq: $id}}) {
metaId
storeId
memo
tokens {
royaltyPercent
royaltys {
id
account
percent
}
}
}
}
`,
{ id: id }
)) as {
metaId: string
storeId: string
memo: string
tokens: {
royaltyPercent: string
royaltys: { account: string; percent: string }[]
}[]
}[]

if (thingResult.length === 0) throw new Error('Thing does not exist.')
if (thingResult[0].tokens.length === 0)
throw new Error('Thing does not have tokens.')

const thing = thingResult[0]
const contractName = thing.storeId
const memo = thing.memo
const metaId = thing.metaId
const token = thing.tokens[0]

const contract = new Contract(account, contractName, {
viewMethods: STORE_CONTRACT_VIEW_METHODS,
changeMethods: STORE_CONTRACT_CALL_METHODS,
})

const obj = {
owner_id: accountId,
metadata: {
reference: metaId,
},
num_to_mint: amount,
royalty_args: {
split_between: token.royaltys,
percentage: token.royaltyPercent,
},
split_owners: splits || null,

// TODO: check if category is lowercase
category: memo,
}

// @ts-ignore: method does not exist on Contract type
await contract.mint_tokens(obj, MAX_GAS, ZERO)
}

public async setSessionKeyPair(
accountId: string,
privateKey: string
Expand Down

0 comments on commit 673cc7d

Please sign in to comment.