Skip to content

Commit

Permalink
Merge pull request #203 from cardinal-labs/jeremy/crd-307
Browse files Browse the repository at this point in the history
CRD-307
  • Loading branch information
jpbogle committed May 4, 2022
2 parents ccf13c9 + ac6120d commit d85784d
Show file tree
Hide file tree
Showing 17 changed files with 27 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub fn handler(ctx: Context<PayCtx>) -> Result<()> {
};
let cpi_program = ctx.accounts.token_program.to_account_info();
let cpi_context = CpiContext::new(cpi_program, cpi_accounts);
token::transfer(cpi_context, ctx.accounts.claim_approver.payment_amount.checked_sub(recipient_fee).expect("Add error"))?;
token::transfer(cpi_context, ctx.accounts.claim_approver.payment_amount.checked_sub(recipient_fee).expect("Sub error"))?;

let token_manager_key = ctx.accounts.token_manager.key();
let claim_approver_seeds = &[PAID_CLAIM_APPROVER_SEED.as_bytes(), token_manager_key.as_ref(), &[ctx.accounts.claim_approver.bump]];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,17 @@ pub fn handler(ctx: Context<ExtendExpirationCtx>, seconds_to_add: u64) -> Result
if time_invalidator.expiration != None {
expiration = time_invalidator.expiration.unwrap();
}
if ctx.accounts.token_manager.state == TokenManagerState::Issued as u8 {
expiration = Clock::get().unwrap().unix_timestamp;
}
let new_expiration = Some(expiration.checked_add(seconds_to_add as i64).expect("Addition error"));

if time_invalidator.max_expiration != None && new_expiration > time_invalidator.max_expiration {
return Err(error!(ErrorCode::InvalidExtendExpiration));
}

let provider_fee = time_invalidator
.extension_payment_amount
.expect("No extension amount")
.checked_mul(PROVIDER_FEE.checked_div(FEE_SCALE).expect("Division error"))
.expect("Multiplication error");
let recipient_fee = time_invalidator
.extension_payment_amount
.expect("No extension amount")
.checked_mul(RECIPIENT_FEE.checked_div(FEE_SCALE).expect("Division error"))
.expect("Multiplication error");
let provider_fee = price_to_pay.checked_mul(PROVIDER_FEE.checked_div(FEE_SCALE).expect("Division error")).expect("Multiplication error");
let recipient_fee = price_to_pay.checked_mul(RECIPIENT_FEE.checked_div(FEE_SCALE).expect("Division error")).expect("Multiplication error");
if provider_fee.checked_add(recipient_fee).expect("Add error") > 0 {
let cpi_accounts = Transfer {
from: ctx.accounts.payer_token_account.to_account_info(),
Expand All @@ -107,7 +102,7 @@ pub fn handler(ctx: Context<ExtendExpirationCtx>, seconds_to_add: u64) -> Result

let cpi_program = ctx.accounts.token_program.to_account_info();
let cpi_context = CpiContext::new(cpi_program, cpi_accounts);
token::transfer(cpi_context, price_to_pay)?;
token::transfer(cpi_context, price_to_pay.checked_sub(recipient_fee).expect("Sub error"))?;

time_invalidator.expiration = new_expiration;
Ok(())
Expand Down
4 changes: 1 addition & 3 deletions programs/cardinal-time-invalidator/src/instructions/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub struct InitIx {
pub collector: Pubkey,
pub payment_manager: Pubkey,
pub duration_seconds: Option<i64>,
pub expiration: Option<i64>,
pub extension_payment_amount: Option<u64>,
pub extension_duration_seconds: Option<u64>,
pub extension_payment_mint: Option<Pubkey>,
Expand Down Expand Up @@ -38,7 +37,7 @@ pub struct InitCtx<'info> {
}

pub fn handler(ctx: Context<InitCtx>, ix: InitIx) -> Result<()> {
if ix.duration_seconds == None && ix.expiration == None {
if ix.duration_seconds == None && ix.max_expiration == None {
return Err(error!(ErrorCode::InvalidInstruction));
} else if (ix.extension_payment_amount == None && ix.extension_duration_seconds != None) || (ix.extension_payment_amount != None && ix.extension_duration_seconds == None) {
return Err(error!(ErrorCode::InvalidInstruction));
Expand All @@ -52,7 +51,6 @@ pub fn handler(ctx: Context<InitCtx>, ix: InitIx) -> Result<()> {
time_invalidator.collector = ix.collector;
time_invalidator.payment_manager = ix.payment_manager;
time_invalidator.duration_seconds = ix.duration_seconds;
time_invalidator.expiration = ix.expiration;
time_invalidator.extension_payment_amount = ix.extension_payment_amount;
time_invalidator.extension_duration_seconds = ix.extension_duration_seconds;
time_invalidator.extension_payment_mint = ix.extension_payment_mint;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ pub struct InvalidateCtx<'info> {
token_manager: Box<Account<'info, TokenManager>>,

#[account(mut,
constraint = time_invalidator.expiration != None && Clock::get().unwrap().unix_timestamp >= time_invalidator.expiration.unwrap()
|| time_invalidator.max_expiration != None && Clock::get().unwrap().unix_timestamp >= time_invalidator.max_expiration.unwrap()
constraint = time_invalidator.max_expiration != None && Clock::get().unwrap().unix_timestamp >= time_invalidator.max_expiration.unwrap()
|| time_invalidator.expiration != None && token_manager.state == TokenManagerState::Claimed as u8 && Clock::get().unwrap().unix_timestamp >= time_invalidator.expiration.unwrap()
|| time_invalidator.expiration == None && token_manager.state == TokenManagerState::Claimed as u8 && Clock::get().unwrap().unix_timestamp >= token_manager.state_changed_at.checked_add(time_invalidator.duration_seconds.expect("No extension duration")).expect("Addition error")
@ ErrorCode::InvalidTimeInvalidator
)]
Expand Down
12 changes: 0 additions & 12 deletions src/idl/cardinal_time_invalidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,6 @@ export type CardinalTimeInvalidator = {
option: "i64";
};
},
{
name: "expiration";
type: {
option: "i64";
};
},
{
name: "extensionPaymentAmount";
type: {
Expand Down Expand Up @@ -620,12 +614,6 @@ export const IDL: CardinalTimeInvalidator = {
option: "i64",
},
},
{
name: "expiration",
type: {
option: "i64",
},
},
{
name: "extensionPaymentAmount",
type: {
Expand Down
5 changes: 0 additions & 5 deletions src/programs/timeInvalidator/instruction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { findTimeInvalidatorAddress } from "./pda";
export type TimeInvalidationParams = {
collector?: PublicKey;
paymentManager?: PublicKey;
expiration?: number;
durationSeconds?: number;
maxExpiration?: number;
extension?: {
Expand Down Expand Up @@ -57,10 +56,6 @@ export const init = async (
{
collector: timeInvalidation.collector || CRANK_KEY,
paymentManager: timeInvalidation.paymentManager || PAYMENT_MANAGER_KEY,
expiration:
timeInvalidation.expiration !== undefined
? new BN(timeInvalidation.expiration)
: null,
durationSeconds:
timeInvalidation.durationSeconds !== undefined
? new BN(timeInvalidation.durationSeconds)
Expand Down
8 changes: 6 additions & 2 deletions src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,12 @@ export const withInvalidate = async (
);
} else if (
timeInvalidatorData &&
((timeInvalidatorData.parsed.expiration &&
timeInvalidatorData.parsed.expiration.lte(new BN(Date.now() / 1000))) ||
((timeInvalidatorData.parsed.maxExpiration &&
timeInvalidatorData.parsed.maxExpiration.lte(
new BN(Date.now() / 1000)
)) ||
(timeInvalidatorData.parsed.expiration &&
timeInvalidatorData.parsed.expiration.lte(new BN(Date.now() / 1000))) ||
(!timeInvalidatorData.parsed.expiration &&
timeInvalidatorData.parsed.durationSeconds &&
tokenManagerData.parsed.stateChangedAt
Expand Down
4 changes: 2 additions & 2 deletions tests/createMultipleRentals.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe("Multiple rentals", () => {
paymentAmount: RENTAL_PAYMENT_AMONT,
paymentMint: paymentMint.publicKey,
},
timeInvalidation: { expiration: Date.now() / 1000 + 1 },
timeInvalidation: { maxExpiration: Date.now() / 1000 + 1 },
mint: rentalMint.publicKey,
issuerTokenAccountId: issuerTokenAccountId,
}
Expand Down Expand Up @@ -132,7 +132,7 @@ describe("Multiple rentals", () => {
paymentAmount: RENTAL_PAYMENT_AMONT,
paymentMint: paymentMint.publicKey,
},
timeInvalidation: { expiration: Date.now() / 1000 + 1 },
timeInvalidation: { maxExpiration: Date.now() / 1000 + 1 },
mint: rentalMint2.publicKey,
issuerTokenAccountId: issuerTokenAccountId2,
amount: new BN(1),
Expand Down
2 changes: 1 addition & 1 deletion tests/createRentalMasterEdition.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ describe("Master editions", () => {
paymentAmount: RENTAL_PAYMENT_AMONT,
paymentMint: paymentMint.publicKey,
},
timeInvalidation: { expiration: Date.now() / 1000 + 1 },
timeInvalidation: { maxExpiration: Date.now() / 1000 + 1 },
mint: rentalMint.publicKey,
issuerTokenAccountId: issuerTokenAccountId,
amount: new BN(1),
Expand Down
2 changes: 1 addition & 1 deletion tests/issueClaimPayerWithPayer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe("Issue payer invalidate", () => {
provider.connection,
provider.wallet,
{
timeInvalidation: { expiration: Date.now() / 1000 },
timeInvalidation: { maxExpiration: Date.now() / 1000 },
mint: rentalMint.publicKey,
issuerTokenAccountId: issuerTokenAccountId,
amount: new BN(1),
Expand Down
2 changes: 1 addition & 1 deletion tests/issueInvalidate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe("Issue Invalidate", () => {
provider.connection,
provider.wallet,
{
timeInvalidation: { expiration: Date.now() / 1000 },
timeInvalidation: { maxExpiration: Date.now() / 1000 },
mint: rentalMint.publicKey,
issuerTokenAccountId: issuerTokenAccountId,
amount: new BN(1),
Expand Down
2 changes: 1 addition & 1 deletion tests/issueInvalidateCloseMintManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe("Issue Claim Close Mint Manager", () => {
provider.connection,
provider.wallet,
{
timeInvalidation: { expiration: Date.now() / 1000 },
timeInvalidation: { maxExpiration: Date.now() / 1000 },
mint: rentalMint.publicKey,
issuerTokenAccountId: issuerTokenAccountId,
amount: new BN(1),
Expand Down
2 changes: 1 addition & 1 deletion tests/issueInvalidatePaidToken.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe("Issue Invalidate", () => {
paymentAmount: RENTAL_PAYMENT_AMONT,
paymentMint: paymentMint.publicKey,
},
timeInvalidation: { expiration: Date.now() / 1000 + 1 },
timeInvalidation: { maxExpiration: Date.now() / 1000 + 1 },
mint: rentalMint.publicKey,
issuerTokenAccountId: issuerTokenAccountId,
amount: new BN(1),
Expand Down
2 changes: 1 addition & 1 deletion tests/issueNoMintManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe("Issue no mint manager", () => {
provider.connection,
provider.wallet,
{
timeInvalidation: { expiration: Date.now() / 1000 },
timeInvalidation: { maxExpiration: Date.now() / 1000 },
mint: rentalMint.publicKey,
issuerTokenAccountId: issuerTokenAccountId,
}
Expand Down
2 changes: 1 addition & 1 deletion tests/issueUnissueClose.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe("Issue Unissue", () => {
collector: collector.publicKey,
},
timeInvalidation: {
expiration: Date.now() / 1000 + 1,
maxExpiration: Date.now() / 1000 + 1,
collector: collector.publicKey,
},
mint: rentalMint.publicKey,
Expand Down
2 changes: 1 addition & 1 deletion tests/timeInvalidation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe("Use without use invalidator", () => {
provider.connection,
provider.wallet,
{
timeInvalidation: { expiration: Date.now() / 1000 + 1 },
timeInvalidation: { maxExpiration: Date.now() / 1000 + 1 },
mint: rentalMint.publicKey,
issuerTokenAccountId: issuerTokenAccountId,
amount: new BN(1),
Expand Down
2 changes: 1 addition & 1 deletion tests/useUnlimited.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe("Use without use invalidator", () => {
paymentAmount: RENTAL_PAYMENT_AMONT,
paymentMint: paymentMint.publicKey,
},
timeInvalidation: { expiration: Date.now() / 1000 + 1 },
timeInvalidation: { maxExpiration: Date.now() / 1000 + 1 },
mint: rentalMint.publicKey,
issuerTokenAccountId: issuerTokenAccountId,
amount: new BN(1),
Expand Down

0 comments on commit d85784d

Please sign in to comment.