Skip to content

Commit

Permalink
initialize security associated account for escrow by default
Browse files Browse the repository at this point in the history
  • Loading branch information
makarychev committed Jul 19, 2024
1 parent 4b7e71c commit 6ef22d9
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 36 deletions.
28 changes: 27 additions & 1 deletion app/src/idl/transfer_restrictions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1465,6 +1465,26 @@ export type TransferRestrictions = {
]
}
},
{
"name": "escrowSecurityAssociatedAccount",
"writable": true,
"pda": {
"seeds": [
{
"kind": "const",
"value": [
115,
97,
97
]
},
{
"kind": "account",
"path": "escrowAccount"
}
]
}
},
{
"name": "mint"
},
Expand All @@ -1484,6 +1504,10 @@ export type TransferRestrictions = {
"name": "payer",
"writable": true,
"signer": true
},
{
"name": "systemProgram",
"address": "11111111111111111111111111111111"
}
],
"args": []
Expand Down Expand Up @@ -2015,7 +2039,9 @@ export type TransferRestrictions = {
},
{
"name": "holder",
"type": "pubkey"
"type": {
"option": "pubkey"
}
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub const SECURITY_ASSOCIATED_ACCOUNT_PREFIX: &str = "saa"; // security associat
#[derive(Default, InitSpace)]
pub struct SecurityAssociatedAccount {
pub group: u64,
pub holder: Pubkey,
pub holder: Option<Pubkey>,
}

#[derive(Accounts)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use std::str::FromStr;
use access_control::{self, AccessControl, WalletRole};
use anchor_lang::prelude::*;
use anchor_spl::{
token_2022::{self, ID as TOKEN_2022_PROGRAM_ID},
token_interface::{Mint, TokenAccount},
};
use std::str::FromStr;

use crate::{TransferRestrictionData, TRANSFER_RESTRICTION_DATA_PREFIX};
use crate::{
common::DISCRIMINATOR_LEN, SecurityAssociatedAccount, TransferRestrictionData,
SECURITY_ASSOCIATED_ACCOUNT_PREFIX, TRANSFER_RESTRICTION_DATA_PREFIX,
};

const TOKENLOCK_ID: &str = "7CN3iHcRimZRa97M38cyMQAF68ecQYDqHfCUgBeSARG2";
#[derive(Accounts)]
Expand All @@ -21,6 +24,15 @@ pub struct SetLockupEscrowAccount<'info> {
)]
pub transfer_restriction_data: Account<'info, TransferRestrictionData>,

#[account(init, payer = payer, space = DISCRIMINATOR_LEN + SecurityAssociatedAccount::INIT_SPACE,
seeds = [
SECURITY_ASSOCIATED_ACCOUNT_PREFIX.as_bytes(),
&escrow_account.key().to_bytes(),
],
bump,
)]
pub escrow_security_associated_account: Account<'info, SecurityAssociatedAccount>,

#[account(
mint::token_program = TOKEN_2022_PROGRAM_ID,
)]
Expand Down Expand Up @@ -49,4 +61,6 @@ pub struct SetLockupEscrowAccount<'info> {
pub tokenlock_account: AccountInfo<'info>,
#[account(mut)]
pub payer: Signer<'info>,

pub system_program: Program<'info, System>,
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,24 @@ pub struct UpdateWalletGroup<'info> {
#[account(mut,
seeds = [
TRANSFER_RESTRICTION_HOLDER_GROUP_PREFIX.as_bytes(),
&security_associated_account.holder.to_bytes(),
&security_associated_account.holder.unwrap().to_bytes(),
&security_associated_account.group.to_le_bytes(),
],
bump,
constraint = holder_group_current.group == security_associated_account.group,
constraint = holder_group_current.group == transfer_restriction_group_current.id,
constraint = holder_group_current.holder == security_associated_account.holder,
constraint = holder_group_current.holder == security_associated_account.holder.unwrap(),
)]
pub holder_group_current: Account<'info, HolderGroup>,
#[account(mut,
seeds = [
TRANSFER_RESTRICTION_HOLDER_GROUP_PREFIX.as_bytes(),
&security_associated_account.holder.to_bytes(),
&security_associated_account.holder.unwrap().to_bytes(),
&transfer_restriction_group_new.id.to_le_bytes(),
],
bump,
constraint = holder_group_new.group == transfer_restriction_group_new.id,
constraint = holder_group_new.holder == security_associated_account.holder,
constraint = holder_group_new.holder == security_associated_account.holder.unwrap(),
)]
pub holder_group_new: Account<'info, HolderGroup>,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn initialize_security_associated_account(
let security_associated_account = &mut ctx.accounts.security_associated_account;

security_associated_account.group = ctx.accounts.group.id;
security_associated_account.holder = *ctx.accounts.holder.to_account_info().key;
security_associated_account.holder = Some(ctx.accounts.holder.key());

let holder_group = &mut ctx.accounts.holder_group;
holder_group.current_wallets_count = holder_group.current_wallets_count.checked_add(1).unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ pub fn set_lockup_escrow_account(
let transfer_restriction_data = &mut ctx.accounts.transfer_restriction_data;
transfer_restriction_data.lockup_escrow_account = Some(escrow_account);

let escrow_security_associated_token_account = &mut ctx.accounts.escrow_security_associated_account;
escrow_security_associated_token_account.group = 0;
escrow_security_associated_token_account.holder = None;

Ok(())
}

Expand Down
3 changes: 3 additions & 0 deletions tests/helpers/transfer-restrictions_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,16 +489,19 @@ export class TransferRestrictionsHelper {
authorityWalletRolePubkey: PublicKey,
payer: Keypair
): Promise<string> {
const [escrowSecurityAssociatedAccountPubkey] = this.securityAssociatedAccountPDA(lockupEscrowAccountPubkey);
return this.program.methods
.setLockupEscrowAccount()
.accountsStrict({
transferRestrictionData: this.transferRestrictionDataPubkey,
escrowSecurityAssociatedAccount: escrowSecurityAssociatedAccountPubkey,
mint: this.mintPubkey,
accessControlAccount: this.accessControlPubkey,
authorityWalletRole: authorityWalletRolePubkey,
escrowAccount: lockupEscrowAccountPubkey,
tokenlockAccount: tokenlockAccountPubkey,
payer: payer.publicKey,
systemProgram: SystemProgram.programId,
})
.signers([payer])
.rpc({ commitment: this.confirmOptions });
Expand Down
13 changes: 0 additions & 13 deletions tests/lockup-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,19 +512,6 @@ describe("token lockup", () => {
)[0],
testEnvironment.walletsAdmin
);
await testEnvironment.transferRestrictionsHelper.initializeSecurityAssociatedAccount(
testEnvironment.transferRestrictionsHelper.groupPDA(recipientGroupId)[0],
testEnvironment.transferRestrictionsHelper.holderPDA(
recipientHolderId
)[0],
recipientHolderGroupPubkey,
escrowOwnerPubkey,
escrowAccount,
testEnvironment.accessControlHelper.walletRolePDA(
testEnvironment.walletsAdmin.publicKey
)[0],
testEnvironment.walletsAdmin
);
// Initialize Transfer Restrictions Rule
const tsNow = await getNowTs(testEnvironment.connection);
const lockedUntil = new anchor.BN(tsNow);
Expand Down
17 changes: 3 additions & 14 deletions tests/tokenlock/100-timelocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe("TokenLockup stress test", () => {
);
const maxReleaseDelay = new anchor.BN(346896000);
const minTimelockAmount = new anchor.BN(100);
const initializeTokenlockSignature = await initializeTokenlock(
await initializeTokenlock(
tokenlockProgram,
maxReleaseDelay,
minTimelockAmount,
Expand Down Expand Up @@ -272,20 +272,9 @@ describe("TokenLockup stress test", () => {
walletCTokenAccount,
authorityWalletRole,
testEnvironment.walletsAdmin,
)

await testEnvironment.transferRestrictionsHelper.initializeSecurityAssociatedAccount(
groupPubkey,
holderPubkey,
holderGroupPubkey,
escrowOwnerPubkey,
escrowAccount,
authorityWalletRole,
testEnvironment.walletsAdmin,
)
);

const transferAmount = new anchor.BN(unlockedBalance);

const withdrawTxSignature = await withdraw(
testEnvironment.connection,
transferAmount,
Expand All @@ -298,7 +287,7 @@ describe("TokenLockup stress test", () => {
walletCTokenAccount,
testEnvironment.transferRestrictionsHelper,
walletC
)
);
console.log("Transfer Transaction Signature", withdrawTxSignature);
tokenlockData = await tokenlockProgram.account.tokenLockData.fetch(
tokenlockDataPubkey
Expand Down

0 comments on commit 6ef22d9

Please sign in to comment.