Skip to content
This repository has been archived by the owner on Dec 18, 2023. It is now read-only.

Commit

Permalink
Calculate rate based on duration
Browse files Browse the repository at this point in the history
  • Loading branch information
irshadnilam committed Jun 21, 2022
1 parent d9239ca commit 2de5b8e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 25 deletions.
31 changes: 17 additions & 14 deletions src/msp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
TOKEN_PROGRAM_ID,
} from '@solana/spl-token';
import { BN, Program } from '@project-serum/anchor';
import * as anchor from '@project-serum/anchor';

import { Msp } from './msp_idl_001';

Expand All @@ -37,6 +36,8 @@ import {
TreasuryType,
} from './types';
import { Category } from './types';
import { StreamTemplate } from './types';
import { TimeUnit } from './types';
import {
createProgram,
createWrapSolInstructions,
Expand All @@ -49,12 +50,11 @@ import {
listStreamsCached,
} from './utils';
import { getStreamTemplate } from './utils';
import { findStreamTemplateAddress } from './utils';
import { Constants, WARNING_TYPES } from './constants';
import { LATEST_IDL_FILE_VERSION } from './constants';
import { Beneficiary, listTreasuries, StreamBeneficiary } from '.';
import { u64Number } from './u64n';
import { StreamTemplate } from './types';
import { findStreamTemplateAddress } from './utils';

/**
* API class with functions to interact with the Money Streaming Program using Solana Web3 JS API
Expand Down Expand Up @@ -970,12 +970,15 @@ export class MSP {
type: TreasuryType,
solFeePayedByTreasury: boolean,
treasuryAssociatedTokenMint: PublicKey,
rateIntervalInSeconds: number,
duration: number,
durationUnit: TimeUnit,
startUtc?: Date,
cliffVestAmount?: number,
cliffVestPercent?: number,
cliffVestPercent = 0,
feePayedByTreasurer?: boolean,
): Promise<[Transaction, PublicKey]> {
// convert duration to seconds
const rateIntervalInSeconds: number = durationUnit as number;

const slot = await this.connection.getSlot(
(this.commitment as Commitment) || 'finalized',
);
Expand Down Expand Up @@ -1053,7 +1056,7 @@ export class MSP {
LATEST_IDL_FILE_VERSION,
new BN(startUtcInSeconds),
new BN(rateIntervalInSeconds),
new BN(cliffVestAmount as number),
new BN(duration),
new BN(cliffVestPercentValue),
feePayedByTreasurer ?? false,
{
Expand Down Expand Up @@ -1086,9 +1089,8 @@ export class MSP {
treasury: PublicKey,
beneficiary: PublicKey,
treasuryAssociatedTokenMint: PublicKey,
rateAmount: number,
allocationAssigned: number,
startUtc?: Date,
streamName = '',
): Promise<[Transaction, PublicKey]> {
if (treasurer.equals(beneficiary)) {
throw Error('Beneficiary can not be the same Treasurer');
Expand Down Expand Up @@ -1116,6 +1118,11 @@ export class MSP {
throw Error("Stream template doesn't exist");
}

// Calculate rate amount
const rateAmount =
(allocationAssigned * (1 - templateInfo.cliffVestPercent / 1_000_000)) /
templateInfo.durationNumberOfUnits;

// Get the treasury token account
const treasuryToken = await Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID,
Expand All @@ -1133,16 +1140,12 @@ export class MSP {
true,
);

const now = new Date();
const startDate =
startUtc && startUtc.getTime() >= now.getTime() ? startUtc : now;
const startUtcInSeconds = parseInt((startDate.getTime() / 1000).toString());
const streamAccount = Keypair.generate();

// Create Stream
const tx = this.program.transaction.createStreamWithTemplate(
LATEST_IDL_FILE_VERSION,
new BN(startUtcInSeconds),
streamName,
new BN(rateAmount),
new BN(allocationAssigned),
{
Expand Down
16 changes: 12 additions & 4 deletions src/msp_idl_001.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ export type Msp = {
"type": "u64"
},
{
"name": "cliffVestAmountUnits",
"name": "durationNumberOfUnits",
"type": "u64"
},
{
Expand Down Expand Up @@ -1149,13 +1149,17 @@ export type Msp = {
"type": "u64"
},
{
"name": "cliffVestAmountUnits",
"name": "cliffVestPercent",
"type": "u64"
},
{
"name": "rateIntervalInSeconds",
"type": "u64"
},
{
"name": "durationNumberOfUnits",
"type": "u64"
},
{
"name": "feePayedByTreasurer",
"type": "bool"
Expand Down Expand Up @@ -2023,7 +2027,7 @@ export const IDL: Msp = {
"type": "u64"
},
{
"name": "cliffVestAmountUnits",
"name": "durationNumberOfUnits",
"type": "u64"
},
{
Expand Down Expand Up @@ -2922,13 +2926,17 @@ export const IDL: Msp = {
"type": "u64"
},
{
"name": "cliffVestAmountUnits",
"name": "cliffVestPercent",
"type": "u64"
},
{
"name": "rateIntervalInSeconds",
"type": "u64"
},
{
"name": "durationNumberOfUnits",
"type": "u64"
},
{
"name": "feePayedByTreasurer",
"type": "bool"
Expand Down
17 changes: 15 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,10 @@ export type StreamTemplate = {
id: PublicKey | string;
version: number;
bump: number;
rateIntervalInSeconds: number;
startUtc: Date | string;
cliffVestAmount: number;
cliffVestPercent: number;
rateIntervalInSeconds: number;
durationNumberOfUnits: number;
feePayedByTreasurer: boolean;
};

Expand Down Expand Up @@ -217,3 +218,15 @@ export enum Category {
default = 0,
vesting = 1,
}

// Preferred Time Unit
export enum TimeUnit {
Second = 0,
Minute = 60,
Hour = 3600,
Day = 86400,
Week = 604800,
Month = 2629750,
Year = 31557000,
}

6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
TOKEN_PROGRAM_ID,
} from '@solana/spl-token';
import * as anchor from '@project-serum/anchor';
import { TimeUnit } from './types';

String.prototype.toPublicKey = function (): PublicKey {
return new PublicKey(this.toString());
Expand Down Expand Up @@ -1239,15 +1240,14 @@ const parseStreamTemplateData = (
id: friendly ? address.toBase58() : address,
version: template.version,
bump: template.bump,
durationNumberOfUnits: template.durationNumberOfUnits.toNumber(),
rateIntervalInSeconds: friendly
? template.rateIntervalInSeconds.toNumber()
: template.rateIntervalInSeconds,
startUtc: !friendly
? new Date(template.startUtcInSeconds * 1000).toString()
: new Date(template.startUtcInSeconds * 1000),
cliffVestAmount: friendly
? template.cliffVestAmountUnits.toNumber()
: template.cliffVestAmountUnits,
cliffVestPercent: template.cliffVestPercent,
feePayedByTreasurer: template.feePayedByTreasurer,
} as StreamTemplate;
};
Expand Down
6 changes: 4 additions & 2 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Constants, MSP, TreasuryType } from '../src';
import { NATIVE_MINT } from '@solana/spl-token';
import {PublicKey} from "@solana/web3.js";
import {Transaction} from "@solana/web3.js";
import {TimeUnit} from "../src/types";

const endpoint = 'http://localhost:8899';
// deploy msp locally
Expand Down Expand Up @@ -52,7 +53,8 @@ describe('Tests creating a vesting treasury\n', async () => {
TreasuryType.Open,
false,
Constants.SOL_MINT,
3600,
12,
TimeUnit.Month,
new Date(),
);
await sendAndConfirmTransaction(connection, createVestingTreasuryTx, [user1Wallet], { commitment: 'confirmed' });
Expand Down Expand Up @@ -84,8 +86,8 @@ describe('Tests creating a vesting treasury\n', async () => {
treasury,
user2Wallet.publicKey,
NATIVE_MINT,
LAMPORTS_PER_SOL,
100 * LAMPORTS_PER_SOL,
'test_stream',
);
createStreamTx.partialSign(user1Wallet);
const createStreamTxSerialized = createStreamTx.serialize({
Expand Down

0 comments on commit 2de5b8e

Please sign in to comment.