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

Commit

Permalink
Generate stream address as PDAs
Browse files Browse the repository at this point in the history
  • Loading branch information
irshadnilam committed Aug 9, 2022
1 parent 8bd4cba commit 9a35d7d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 257 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mean-dao/msp",
"version": "2.8.3-alpha.1",
"version": "2.8.3-alpha.2",
"description": "MSP Typescript SDK",
"private": false,
"main": "lib/index.js",
Expand Down
271 changes: 15 additions & 256 deletions src/msp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -992,155 +992,16 @@ export class MSP {
startUtc && startUtc.getTime() >= now.getTime() ? startUtc : now;
const startUtcInSeconds = parseInt((startDate.getTime() / 1000).toString());

const streamAccount = Keypair.generate();
const createStreamAccountInstruction =
await this.program.account.stream.createInstruction(
streamAccount,
Constants.STREAM_SIZE,
);

// Create Stream
const tx = this.program.transaction.createStream(
LATEST_IDL_FILE_VERSION,
streamName,
new BN(startUtcInSeconds),
new BN(rateAmount as number),
new BN(rateIntervalInSeconds as number),
new BN(allocationAssigned),
new BN(cliffVestAmount as number),
new BN(cliffVestPercentValue),
feePayedByTreasurer ?? false,
{
accounts: {
payer: payer,
treasurer: treasurer,
treasury: treasury,
treasuryToken: treasuryToken,
associatedToken: treasuryAssociatedTokenMint,
beneficiary: beneficiary,
stream: streamAccount.publicKey,
feeTreasury: Constants.FEE_TREASURY,
feeTreasuryToken: feeTreasuryToken,
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
tokenProgram: TOKEN_PROGRAM_ID,
systemProgram: SystemProgram.programId,
rent: SYSVAR_RENT_PUBKEY,
},
preInstructions: [createStreamAccountInstruction],
signers: [streamAccount],
},
);

tx.feePayer = payer;
const { blockhash } = await this.connection.getRecentBlockhash(
(this.commitment as Commitment) || 'finalized',
);
tx.recentBlockhash = blockhash;
tx.partialSign(...[streamAccount]);

return [tx, streamAccount.publicKey];
}

/**
* This creates a stream account required for the stream.
* @param payer The address of the payer
*/
public async createPreRequiredStreamAccount(
payer: PublicKey,
): Promise<[Transaction, PublicKey]> {
const streamAccount = Keypair.generate();
const createStreamAccountInstruction =
await this.program.account.stream.createInstruction(
streamAccount,
Constants.STREAM_SIZE,
);
const tx = new Transaction();
tx.add(createStreamAccountInstruction);
tx.feePayer = payer;
const { blockhash } = await this.connection.getRecentBlockhash(
(this.commitment as Commitment) || 'finalized',
);
tx.recentBlockhash = blockhash;
tx.partialSign(...[streamAccount]);
return [tx, streamAccount.publicKey];
}

/**
* This one returns not only the transaction but also the keypair of the
* stream that will be created
*
* @param payer The address of the payer
* @param treasurer The address of the treasurer
* @param treasury The address of the treasury
* @param beneficiary The address of the beneficiary
* @param stream The stream to be created
* @param allocationAssigned The amount of allocation to be assigned to the stream
* @param streamName The name of the stream
* @param rateAmount The amount of tokens to be issued per rate interval
* @param rateIntervalInSeconds The rate interval in seconds
* @param startUtc The start date of the stream
* @param cliffVestAmount The amount of tokens to be vested at the cliff
* @param cliffVestPercent The percentage of the cliff vest to be vested
* @param feePayedByTreasurer Whether the fee is payed by the treasurer
*/
public async createStreamWithPreDefinedAccount(
payer: PublicKey,
treasurer: PublicKey,
treasury: PublicKey,
beneficiary: PublicKey,
stream: PublicKey,
streamName: string,
allocationAssigned: number,
rateAmount?: number,
rateIntervalInSeconds?: number,
startUtc?: Date,
cliffVestAmount?: number,
cliffVestPercent?: number,
feePayedByTreasurer?: boolean,
): Promise<Transaction> {
if (treasurer.equals(beneficiary)) {
throw Error('Beneficiary can not be the same Treasurer');
}

const treasuryInfo = await getTreasury(this.program, treasury);

if (!treasuryInfo) {
throw Error("Treasury doesn't exist");
}

const treasuryAssociatedTokenMint = new PublicKey(
treasuryInfo.associatedToken,
);

// Get the treasury token account
const treasuryToken = await Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
treasuryAssociatedTokenMint,
treasury,
true,
);

const feeTreasuryToken = await Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
treasuryAssociatedTokenMint,
Constants.FEE_TREASURY,
true,
const uniqueSeed = Keypair.generate().publicKey;
const [stream] = await PublicKey.findProgramAddress(
[treasury.toBuffer(), uniqueSeed.toBuffer()],
this.program.programId,
);

const cliffVestPercentValue = cliffVestPercent
? cliffVestPercent * Constants.CLIFF_PERCENT_NUMERATOR
: 0;
const now = new Date();
const startDate =
startUtc && startUtc.getTime() >= now.getTime() ? startUtc : now;
const startUtcInSeconds = parseInt((startDate.getTime() / 1000).toString());

// Create Stream
const tx = this.program.transaction.createStream(
LATEST_IDL_FILE_VERSION,
streamName,
uniqueSeed,
new BN(startUtcInSeconds),
new BN(rateAmount as number),
new BN(rateIntervalInSeconds as number),
Expand Down Expand Up @@ -1168,111 +1029,12 @@ export class MSP {
);

tx.feePayer = payer;
const { blockhash } = await this.connection.getLatestBlockhash(
(this.commitment as Commitment) || 'finalized',
);
tx.recentBlockhash = blockhash;
return tx;
}

/**
* This creates a stream with template
* @param payer The address of the payer
* @param treasurer The address of the treasurer
* @param treasury The address of the treasury
* @param beneficiary The address of the beneficiary
* @param stream The stream to be created
* @param allocationAssigned The amount of allocation to be assigned to the stream
* @param streamName The name of the stream
*
* @returns The transaction to create a stream with template
*/
public async createStreamWithTemplateWithPreDefinedAccount(
payer: PublicKey,
treasurer: PublicKey,
treasury: PublicKey,
beneficiary: PublicKey,
stream: PublicKey,
allocationAssigned: number,
streamName = '',
): Promise<Transaction> {
if (treasurer.equals(beneficiary)) {
throw Error('Beneficiary can not be the same Treasurer');
}

const treasuryInfo = await getTreasury(this.program, treasury);

if (!treasuryInfo) {
throw Error("Treasury doesn't exist");
}
const treasuryAssociatedTokenMint = new PublicKey(
treasuryInfo.associatedToken,
);
// Get the template
const [template] = await findStreamTemplateAddress(
treasury,
this.program.programId,
);
const templateInfo = await getStreamTemplate(this.program, template);
if (!templateInfo) {
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,
TOKEN_PROGRAM_ID,
treasuryAssociatedTokenMint,
treasury,
true,
);

const feeTreasuryToken = await Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
treasuryAssociatedTokenMint,
Constants.FEE_TREASURY,
true,
);

// Create Stream
const tx = this.program.transaction.createStreamWithTemplate(
LATEST_IDL_FILE_VERSION,
streamName,
new BN(rateAmount),
new BN(allocationAssigned),
{
accounts: {
payer: payer,
template,
treasurer: treasurer,
treasury: treasury,
treasuryToken: treasuryToken,
associatedToken: treasuryAssociatedTokenMint,
beneficiary: beneficiary,
stream,
feeTreasury: Constants.FEE_TREASURY,
feeTreasuryToken: feeTreasuryToken,
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
tokenProgram: TOKEN_PROGRAM_ID,
systemProgram: SystemProgram.programId,
rent: SYSVAR_RENT_PUBKEY,
},
},
);

tx.feePayer = payer;
const { blockhash } = await this.connection.getLatestBlockhash(
const { blockhash } = await this.connection.getRecentBlockhash(
(this.commitment as Commitment) || 'finalized',
);
tx.recentBlockhash = blockhash;

return tx;
return [tx, stream];
}

/**
Expand Down Expand Up @@ -1702,17 +1464,17 @@ export class MSP {
true,
);

const streamAccount = Keypair.generate();
const createStreamAccountInstruction =
await this.program.account.stream.createInstruction(
streamAccount,
Constants.STREAM_SIZE,
);
const uniqueSeed = Keypair.generate().publicKey;
const [stream] = await PublicKey.findProgramAddress(
[treasury.toBuffer(), uniqueSeed.toBuffer()],
this.program.programId,
);

// Create Stream
const tx = this.program.transaction.createStreamWithTemplate(
LATEST_IDL_FILE_VERSION,
streamName,
uniqueSeed,
new BN(rateAmount),
new BN(allocationAssigned),
{
Expand All @@ -1724,16 +1486,14 @@ export class MSP {
treasuryToken: treasuryToken,
associatedToken: treasuryAssociatedTokenMint,
beneficiary: beneficiary,
stream: streamAccount.publicKey,
stream,
feeTreasury: Constants.FEE_TREASURY,
feeTreasuryToken: feeTreasuryToken,
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
tokenProgram: TOKEN_PROGRAM_ID,
systemProgram: SystemProgram.programId,
rent: SYSVAR_RENT_PUBKEY,
},
preInstructions: [createStreamAccountInstruction],
signers: [streamAccount],
},
);

Expand All @@ -1742,9 +1502,8 @@ export class MSP {
(this.commitment as Commitment) || 'finalized',
);
tx.recentBlockhash = blockhash;
tx.partialSign(...[streamAccount]);

return [tx, streamAccount.publicKey];
return [tx, stream];
}

public async createStreams(
Expand Down
16 changes: 16 additions & 0 deletions src/msp_idl_005.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ export type Msp = {
"name": "name",
"type": "string"
},
{
"name": "uniqueSeed",
"type": "publicKey"
},
{
"name": "startUtc",
"type": "u64"
Expand Down Expand Up @@ -529,6 +533,10 @@ export type Msp = {
"name": "name",
"type": "string"
},
{
"name": "uniqueSeed",
"type": "publicKey"
},
{
"name": "rateAmountUnits",
"type": "u64"
Expand Down Expand Up @@ -2946,6 +2954,10 @@ export const IDL: Msp = {
"name": "name",
"type": "string"
},
{
"name": "uniqueSeed",
"type": "publicKey"
},
{
"name": "startUtc",
"type": "u64"
Expand Down Expand Up @@ -3296,6 +3308,10 @@ export const IDL: Msp = {
"name": "name",
"type": "string"
},
{
"name": "uniqueSeed",
"type": "publicKey"
},
{
"name": "rateAmountUnits",
"type": "u64"
Expand Down

0 comments on commit 9a35d7d

Please sign in to comment.