一、创建快照
async createShot() {
console.log('创建检查点:')
const checkpointQueue = await this.assets.checkpoints.create()
const checkpoint = await checkpointQueue.run()
console.log('创建检查点成功', checkpoint)
},
二、查询股息列表
1、获取资产
async getAssets() {
this.assets = await this.api.assets.getAsset({
ticker: this.ticker
})
console.log('assets:', this.assets)
}
2、获取快照
async getShot() {
if (!this.assets) {
await this.getAssets()
}
const checkpointQueue = await this.assets.checkpoints.get()
console.log('checkpointQueue:', checkpointQueue.data)
const checkpointQueueList = checkpointQueue.data.sort((obj1, obj2) => {
// console.log(obj2.checkpoint.id.toString(), obj1.checkpoint.id.toString())
return parseInt(obj2.checkpoint.id.toString()) - parseInt(obj1.checkpoint.id.toString())
})
console.log('checkpointQueueList:', checkpointQueueList)
this.checkpointQueue = checkpointQueueList
},
3、通过快照Id获取股息
此方法获取的每个快照的股息唯一,查不全
async getDividend() {
if (this.checkpointQueue.length === 0) {
await this.getShot()
}
const dividendList = await Promise.all(this.checkpointQueue.map(async item => {
const dividend = await this.assets.corporateActions.distributions.getOne({
id: item.checkpoint.id
})
return dividend
}))
console.log('dividendList:', dividendList)
},
4、获取所有股息
async getDividendAll() {
if (this.checkpointQueue.length === 0) {
await this.getShot()
}
const dividend = await this.assets.corporateActions.distributions.get()
console.log('所有股息:dividend:', dividend)
this.dividendList = dividend
},
5、通过股息获取快照
先查询当前资产的所有股息,然后通过股息去查快照,然后再根据快照ID把股息整合到前面查询到的所有快照上。
async dividendGetShot() {
if (this.dividendList.length === 0) {
await this.getDividendAll()
}
const arr = await Promise.all(this.dividendList.map(async item => {
const obj = item.distribution.checkpoint()
return obj
}))
console.log('股息查询快照:', arr)
},
三、提交股息
注意点:
查询当前TY001资产持有人,与查询当前快照时TY001资产持有人是有区别的
1、查询当前TY001资产持有人
async getTokenHold() {
console.log('资产持有人查询:')
// 方法1:直接查询资产持有人
if (!this.assets) {
await this.getAssets()
}
const arr = await this.assets.assetHolders.get()
console.log('资产持有人:', arr)
if (this.checkpointQueue.length === 0) {
await this.getShot()
}
},
2、查询当前快照时TY001资产持有人
async getTokenHold() {
console.log('资产持有人查询:')
// 方法2:查询当前快照时的资产持有人
const list = await this.checkpointQueue[0].checkpoint.allBalances()
console.log('当前快照时间点资产持有人:', list)
},
3、获取投资组合与支付货币列表及可用余额
选择支付股息的货币,同时需要知道其可用余额
async getAssetsPer() {
const signIden = await this.api.getSigningIdentity()
const getPortfolioList = await signIden.portfolios.getPortfolios()
this.portfolioList = getPortfolioList
console.log('投资组合列表:', getPortfolioList)
const arr = await Promise.all(getPortfolioList.map(async item => {
return await item.getAssetBalances()
}))
this.portfolioBalanceList = arr
console.log('投资组合资产列表:', arr)
},
4、提交股息
async submitFun() {
if (this.checkpointQueue.length === 0) {
await this.getShot()
}
if (this.portfolioList.length === 0){
await this.getAssetsPer()
}
// 申报日期
const declarationDate = this.checkpointQueue[0].createdAt
const declarationDateToString = declarationDate.toLocaleString()
const newDeclarationDate = new Date(declarationDateToString)
newDeclarationDate.setMinutes(newDeclarationDate.getMinutes() - 10)
// 支付日期
const paymentDate = new Date()
paymentDate.setMinutes(paymentDate.getMinutes() + 2)
// 过期日期
const expiryDate = new Date()
expiryDate.setMinutes(expiryDate.getMinutes() + 120)
// paymentDate
const obj = {
checkpoint: this.checkpointQueue[0].checkpoint, // 检查点
// checkpoint: new Date(Date.now() + 1000 * 60 * 2), // 检查点
originPortfolio: this.portfolioList[0], // optional, defaults to the CAA's default portfolio
currency: this.currency,
perShare: new BigNumber(1),
maxAmount: new BigNumber(165),
paymentDate, // 支付日期
expiryDate, // 过期日期
declarationDate: newDeclarationDate, // 申报日期
description: this.description,
targets: {
identities: ['0x1a3fb688e10f4a544ab627c74b4059133ac81cc8a09cb947dbb65ff55d56c975'],
treatment: TargetTreatment.Include
},
taxWithholdings: [
{
identity: ['0x1a3fb688e10f4a544ab627c74b4059133ac81cc8a09cb947dbb65ff55d56c975'],
percentage: new BigNumber(0)
}
]
}
console.log('股息入参:', obj)
const dividendActionQueue = await this.assets.corporateActions.distributions.configureDividendDistribution(obj)
const dividendAction = await dividendActionQueue.run()
console.log(dividendAction)
},
四、股息支付、认领、过期解锁
1、支付前确认是否有权在此分配中获得股息的所有身份
async pay(i) {
const distribution = this.corpActionList[i].distribution
const participants = await distribution.getParticipants()
const arr = participants.map(item => (item.identity.did))
console.log('pay 入参:', participants, arr)
if (arr.length === 0) {
console.log('没有检索到有权在此分配中获得股息的所有身份,请确定是否创建分发检查点')
return
}
console.log('支付股息。。。')
const paymentQ = await this.distribution.pay({
targets: participants.map(item => (item.identity.did))
})
await paymentQ.run()
console.log('股息支付')
}
2、认领
async accept(i) {
// const participants = await distribution.getParticipants()
// console.log('pay 入参:', participants)
// 支付
const claim = await this.dividendList[i].distribution.claim()
const d = await claim.run()
console.log('接受成功', d)
},
五、状态
details: {
fundsReclaimed: false
remainingFunds: BigNumber
}
fundsReclaimed:只有当过期了,解锁资金了,才为true,其它状态都为false
remainingFunds:金额会随着支付或者认领而逐渐减少,最终为0
六、问题点:
1、当设置的最大金额多于认领或支付的最大金额,过期时间为无期,股息资金剩余的部分好像无法拿出来
2、当获取Polymesh数据渲染页面,带入复杂数据类型,比如(identity)。当账户切换的时候,页面会报错。当去掉此复杂数据类型的时候,切换账户正常。