import { deserialize, serialize, field, fixedArray, } from "@dao-xyz/borsh"; export class VestingConfig { @field({type: 'u64'}) startTime : bigint; @field({type: 'u64'}) endTime : bigint; @field({type: 'u64'}) totalAmount : bigint; @field({type: 'u64'}) period : bigint; @field({type: 'u8'}) transferableBySender : number; @field({type: 'u8'}) transferableByRecipient : number constructor(data: VestingConfig) { Object.assign(this, data) } static fromBytes(buffer) { return deserialize(buffer, VestingConfig); } toBytes() { return serialize(this); } } export class Vesting { @field({type: fixedArray('u8',8)}) magic:Uint8Array; @field({type: fixedArray('u8',32)}) mint:Uint8Array; @field({type: fixedArray('u8',32)}) sender:Uint8Array; @field({type: fixedArray('u8',32)}) recipient:Uint8Array; @field({type: fixedArray('u8',32)}) recipientTokens:Uint8Array; @field({type: fixedArray('u8',32)}) senderTokens:Uint8Array; @field({type: fixedArray('u8',32)}) escrowTokens:Uint8Array; @field({type: 'u64'}) withdrawnAmount : bigint; @field({type: 'u64'}) createdAt : bigint; @field({type: 'u64'}) canceledAt : bigint; @field({type: 'u64'}) lastWithdrawnAt :bigint; @field({type: VestingConfig}) config; constructor(data: Vesting) { Object.assign(this, data) } static fromBytes(buffer) { return deserialize(buffer, Vesting); } toBytes() { return serialize(this); } nextClaimDate() { const now = new Date().getTime() / 1000; const periodPassed = Math.floor( (now - Number(this.config.startTime)) / Number(this.config.period) ); const seconds = Number(this.config.startTime) + (periodPassed + 1) * Number(this.config.period); const res = new Date(seconds * 1000); return res; } periodAmount() { const numPeriods = Math.floor( (Number(this.config.endTime) - Number(this.config.startTime)) / Number(this.config.period) ); return Math.floor(Number(this.config.totalAmount) / numPeriods); } available() { const now = Math.floor(new Date().getTime() / 1000); if (Number(this.config.startTime) >= now) { return 0; } if (now >= Number(this.config.endTime)) { return ( Number(this.config.totalAmount - this.withdrawnAmount) ); } const periodAmount = this.periodAmount(); const periodPassed = Math.floor( (now - Number(this.config.startTime)) / Number(this.config.period) ); const res = Math.floor( (periodPassed * periodAmount) - Number(this.withdrawnAmount) ); return res; } }