修改badge中nftid的生成规则, 改由合约自己生成

This commit is contained in:
zhl 2023-04-23 11:36:31 +08:00
parent 00ce8374ae
commit 4e2cefbba4
7 changed files with 37463 additions and 10270 deletions

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

View File

@ -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
View File

@ -0,0 +1,3 @@
{
"0xfa44C759f0D51e749ba591a79b3f7F16a4d41CEC": 104
}

35
src/models/IDCounter.ts Normal file
View 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
View 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
}