Add ownership check to flp (#516)

* Assert ownership of ATAs

* Some better checks in punch
This commit is contained in:
Jordan Prince 2021-09-26 18:31:56 -05:00 committed by GitHub
parent 048ddd6bac
commit 23372a130a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 21 deletions

View File

@ -949,9 +949,8 @@ program
)
)[0];
const fairLaunchLotteryBitmap = ( //@ts-ignore
await getFairLaunchLotteryBitmap(fairLaunchObj.tokenMint)
)[0];
const fairLaunchLotteryBitmap = //@ts-ignore
(await getFairLaunchLotteryBitmap(fairLaunchObj.tokenMint))[0];
await adjustTicket({
amountNumber,
@ -1298,9 +1297,8 @@ program
)
)[0];
const fairLaunchLotteryBitmap = ( //@ts-ignore
await getFairLaunchLotteryBitmap(fairLaunchObj.tokenMint)
)[0];
const fairLaunchLotteryBitmap = //@ts-ignore
(await getFairLaunchLotteryBitmap(fairLaunchObj.tokenMint))[0];
const ticket = await anchorProgram.account.fairLaunchTicket.fetch(
fairLaunchTicket,
@ -1327,19 +1325,31 @@ program
adjustMantissa: false,
});
}
const buyerTokenAccount = await punchTicket({
puncher: walletKeyPair.publicKey,
payer: walletKeyPair,
anchorProgram,
fairLaunchTicket,
fairLaunch,
fairLaunchLotteryBitmap,
fairLaunchObj,
});
console.log(
`Punched ticket and placed token in new account ${buyerTokenAccount.toBase58()}.`,
);
let tries = 0;
try {
const buyerTokenAccount = await punchTicket({
puncher: walletKeyPair.publicKey,
payer: walletKeyPair,
anchorProgram,
fairLaunchTicket,
fairLaunch,
fairLaunchLotteryBitmap,
fairLaunchObj,
});
console.log(
`Punched ticket and placed token in new account ${buyerTokenAccount.toBase58()}.`,
);
} catch (e) {
if (tries > 3) {
throw e;
} else {
tries++;
}
console.log('Ticket failed to punch, trying one more time');
await sleep(1000);
}
});
program
@ -1424,9 +1434,8 @@ program
const fairLaunchObj = await anchorProgram.account.fairLaunch.fetch(
fairLaunchKey,
);
const fairLaunchLotteryBitmap = ( //@ts-ignore
await getFairLaunchLotteryBitmap(fairLaunchObj.tokenMint)
)[0];
const fairLaunchLotteryBitmap = //@ts-ignore
(await getFairLaunchLotteryBitmap(fairLaunchObj.tokenMint))[0];
await anchorProgram.rpc.startPhaseThree({
accounts: {

View File

@ -337,6 +337,10 @@ pub mod fair_launch {
}
}
if buyer_token_account.owner != *buyer.key {
return Err(ErrorCode::AccountOwnerShouldBeBuyer.into());
}
assert_owned_by(treasury_mint_info, &token_program.key)?;
assert_owned_by(buyer_token_account_info, &token_program.key)?;
@ -554,6 +558,10 @@ pub mod fair_launch {
}
}
if buyer_token_account.owner != *buyer.key {
return Err(ErrorCode::AccountOwnerShouldBeBuyer.into());
}
let signer_seeds = [
PREFIX.as_bytes(),
fair_launch.token_mint.as_ref(),
@ -695,6 +703,10 @@ pub mod fair_launch {
return Err(ErrorCode::AccountShouldHaveNoDelegates.into());
}
if buyer_token.owner != fair_launch_ticket.buyer {
return Err(ErrorCode::AccountOwnerShouldBeBuyer.into());
}
fair_launch.number_tickets_punched = fair_launch
.number_tickets_punched
.checked_add(1)
@ -789,6 +801,10 @@ pub mod fair_launch {
return Err(ErrorCode::AccountShouldHaveNoDelegates.into());
}
if authority_token_account.owner != fair_launch.authority {
return Err(ErrorCode::AccountOwnerShouldBeAuthority.into());
}
if fair_launch.treasury_snapshot.is_none() {
fair_launch.treasury_snapshot = Some(treasury_account.amount)
}
@ -918,6 +934,10 @@ pub mod fair_launch {
return Err(ErrorCode::AccountShouldHaveNoDelegates.into());
}
if buyer_payment_account.owner != *buyer.key {
return Err(ErrorCode::AccountOwnerShouldBeBuyer.into());
}
if fair_launch.treasury_snapshot.is_none() {
fair_launch.treasury_snapshot = Some(treasury_account.amount)
}
@ -1604,4 +1624,8 @@ pub enum ErrorCode {
LotteryDurationHasntEndedYet,
#[msg("Fair launch ticket and fair launch key mismatch")]
FairLaunchMismatch,
#[msg("Account owner should be buyer")]
AccountOwnerShouldBeBuyer,
#[msg("Account owner should be fair launch authority")]
AccountOwnerShouldBeAuthority,
}