修改badge中nftid的生成规则, 改由合约自己生成
This commit is contained in:
parent
00ce8374ae
commit
4e2cefbba4
29056
src/abis/BEBadge.json
29056
src/abis/BEBadge.json
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -40,20 +40,21 @@ export class DistributorReactor {
|
|||||||
async mintNft({
|
async mintNft({
|
||||||
address,
|
address,
|
||||||
to,
|
to,
|
||||||
nftList,
|
count,
|
||||||
encodeABI = false,
|
encodeABI = false,
|
||||||
}: {
|
}: {
|
||||||
address?: string
|
address?: string
|
||||||
to: string
|
to: string
|
||||||
nftList: string[]
|
count: number
|
||||||
encodeABI?: boolean
|
encodeABI?: boolean
|
||||||
}) {
|
}) {
|
||||||
const contract = address ? new this.web3.eth.Contract(abi, address, { from: this.account.address }) : this.contract
|
const contract = address ? new this.web3.eth.Contract(abi, address, { from: this.account.address }) : this.contract
|
||||||
|
const countNft = count + ''
|
||||||
if (encodeABI) {
|
if (encodeABI) {
|
||||||
return contract.methods.mintToUser(to, nftList).encodeABI()
|
return contract.methods.mintToUser(to, countNft).encodeABI()
|
||||||
}
|
}
|
||||||
let gas = await contract.methods.mintToUser(to, nftList).estimateGas({ from: this.account.address })
|
let gas = await contract.methods.mintToUser(to, countNft).estimateGas({ from: this.account.address })
|
||||||
let res = await contract.methods.mintToUser(to, nftList).send({ gas: gas | 0 })
|
let res = await contract.methods.mintToUser(to, countNft).send({ gas: gas | 0 })
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
3
src/config/nfttypes.json
Normal file
3
src/config/nfttypes.json
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"0xfa44C759f0D51e749ba591a79b3f7F16a4d41CEC": 104
|
||||||
|
}
|
35
src/models/IDCounter.ts
Normal file
35
src/models/IDCounter.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import { getModelForClass, index, modelOptions, prop } from '@typegoose/typegoose'
|
||||||
|
import { dbconn } from '../decorators/dbconn'
|
||||||
|
|
||||||
|
@dbconn()
|
||||||
|
@modelOptions({ schemaOptions: { collection: 'id_counter', _id: false } })
|
||||||
|
class IDCounterClass {
|
||||||
|
@prop()
|
||||||
|
public _id: string
|
||||||
|
@prop({ default: 1 })
|
||||||
|
public seq: number
|
||||||
|
static nextCount() {
|
||||||
|
return IDCounter.findOneAndUpdate(
|
||||||
|
{ _id: 'userShortId' },
|
||||||
|
{ $inc: { seq: 1 } },
|
||||||
|
{
|
||||||
|
upsert: true,
|
||||||
|
new: true,
|
||||||
|
setDefaultsOnInsert: true,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
static nextID(id: string) {
|
||||||
|
return IDCounter.findOneAndUpdate(
|
||||||
|
{ _id: id },
|
||||||
|
{ $inc: { seq: 1 } },
|
||||||
|
{
|
||||||
|
upsert: true,
|
||||||
|
new: true,
|
||||||
|
setDefaultsOnInsert: true,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const IDCounter = getModelForClass(IDCounterClass, { existingConnection: IDCounterClass['db'] })
|
34
src/utils/nft.util.ts
Normal file
34
src/utils/nft.util.ts
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import { IDCounter } from 'models/IDCounter'
|
||||||
|
|
||||||
|
export const ONE_DAY = 24 * 60 * 60 * 1000
|
||||||
|
export const NFT_BEGIN_DAY = new Date(2023, 4, 8)
|
||||||
|
|
||||||
|
export const NFT_TYPE = require('config/nfttypes.json')
|
||||||
|
|
||||||
|
export const MINT_CHANNEL = {
|
||||||
|
claim: '01', // 2022购买用户claim
|
||||||
|
}
|
||||||
|
|
||||||
|
// calc days between two Date
|
||||||
|
export function daysBetween(date1: Date, date2: Date) {
|
||||||
|
// hours*minutes*seconds*milliseconds
|
||||||
|
const diffInMs = Math.abs(date1.getTime() - date2.getTime())
|
||||||
|
const diffInDays = Math.round(diffInMs / ONE_DAY)
|
||||||
|
return diffInDays
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成nft的tokenid
|
||||||
|
* 规则:
|
||||||
|
* 100 9999 00 0000001
|
||||||
|
* NFT类型 当前日期至开始日期的天数 渠道编号 当前类型的序号
|
||||||
|
*/
|
||||||
|
export async function generateNftID(nfttype: number, channel: number) {
|
||||||
|
const days = daysBetween(new Date(), NFT_BEGIN_DAY)
|
||||||
|
const dayKey = (days + '').padStart(4, '0')
|
||||||
|
const channelKey = (channel + '').padStart(2, '0')
|
||||||
|
const idkey = nfttype + dayKey + channelKey
|
||||||
|
const idobj = await IDCounter.nextID(idkey)
|
||||||
|
const val = (idobj.seq + '').padStart(7, '0')
|
||||||
|
return idkey + val
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user