2019-03-14 22:11:16 +00:00
|
|
|
import { action, field, relation, json } from '@nozbe/watermelondb/decorators'
|
2019-01-23 17:40:11 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb'
|
2019-01-21 22:32:42 +00:00
|
|
|
import { LiquidModel } from '../utils/models'
|
|
|
|
|
2019-03-14 22:11:16 +00:00
|
|
|
const sanitizeValues = json => json
|
2019-01-21 22:32:42 +00:00
|
|
|
export default class Pledge extends LiquidModel {
|
|
|
|
static table = 'pledges'
|
|
|
|
static associations = {
|
2019-01-22 19:16:58 +00:00
|
|
|
profiles: { type: 'belongs_to', key: 'profile_id' },
|
2019-01-21 22:32:42 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 22:11:16 +00:00
|
|
|
@field('id_pledge') idPledge
|
2019-01-21 22:32:42 +00:00
|
|
|
@field('owner_id') owner
|
|
|
|
@field('amount') amount
|
|
|
|
@field('token') token
|
|
|
|
@field('commit_time') commitTime
|
|
|
|
@field('n_delegates') nDelegates
|
|
|
|
@field('intended_project') intendedProject
|
|
|
|
@field('pledge_state') pledgeState
|
2019-02-05 18:41:17 +00:00
|
|
|
@field('block_number') blockNumber
|
2019-01-22 19:16:58 +00:00
|
|
|
@relation('profiles', 'profile_id') profile
|
2019-03-14 22:11:16 +00:00
|
|
|
@json('delegates', sanitizeValues) delegates
|
2019-01-21 22:32:42 +00:00
|
|
|
|
2019-01-23 17:40:11 +00:00
|
|
|
@action async transferTo(to, amount) {
|
|
|
|
const toPledgeQuery = await this.collections.get('pledges').query(
|
|
|
|
Q.where('pledge_id', to)
|
|
|
|
).fetch()
|
|
|
|
const toPledge = toPledgeQuery[0]
|
|
|
|
await this.batch(
|
|
|
|
this.prepareUpdate(pledge => {
|
|
|
|
pledge.amount = (BigInt(pledge.amount) - BigInt(amount)).toString()
|
|
|
|
}),
|
|
|
|
toPledge.prepareUpdate(pledge => {
|
|
|
|
pledge.amount = (BigInt(pledge.amount) + BigInt(amount)).toString()
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
2019-02-05 18:41:17 +00:00
|
|
|
|
|
|
|
@action async updateFields(newPledge) {
|
|
|
|
const { amount, nDelegates, pledgeState, blockNumber } = newPledge
|
|
|
|
this.prepareUpdate(pledge => {
|
|
|
|
pledge.amount = amount
|
|
|
|
pledge.nDelegates = Number(nDelegates)
|
|
|
|
pledge.pledgeState = pledgeState
|
|
|
|
pledge.blockNumber = blockNumber
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
2019-01-21 22:32:42 +00:00
|
|
|
}
|