Skip to content

Commit

Permalink
Claim links
Browse files Browse the repository at this point in the history
  • Loading branch information
jpbogle committed Feb 3, 2022
1 parent 4fbf409 commit fadbca1
Show file tree
Hide file tree
Showing 13 changed files with 373 additions and 21 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cardinal/token-manager",
"version": "0.0.2",
"version": "0.0.3",
"description": "Cardinal token manager SDK",
"keywords": [
"solana",
Expand Down
6 changes: 3 additions & 3 deletions programs/cardinal-paid-claim-approver/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cardinal-paid-claim-approver"
version = "0.0.2"
version = "0.0.3"
description = "Cardinal paid claim approver"
edition = "2021"
homepage = "https://cardinal.so"
Expand All @@ -25,8 +25,8 @@ anchor-spl = "0.20.1"
spl-associated-token-account = "1.0.2"
spl-token = { version = "3.1.1", features = ["no-entrypoint"] }
solana-program = "1.8.1"
cardinal-token-manager = { version = "^0.0.2", path = "../cardinal-token-manager", features = ["cpi"] }
cardinal-payment-manager = { version = "^0.0.2", path = "../cardinal-payment-manager", features = ["cpi"] }
cardinal-token-manager = { version = "^0.0.3", path = "../cardinal-token-manager", features = ["cpi"] }
cardinal-payment-manager = { version = "^0.0.3", path = "../cardinal-payment-manager", features = ["cpi"] }

[dev-dependencies]
proptest = { version = "1.0" }
4 changes: 2 additions & 2 deletions programs/cardinal-payment-manager/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cardinal-payment-manager"
version = "0.0.2"
version = "0.0.3"
description = "Cardinal paid claim approver"
edition = "2021"
homepage = "https://cardinal.so"
Expand All @@ -25,7 +25,7 @@ anchor-spl = "0.20.1"
spl-associated-token-account = "1.0.2"
spl-token = { version = "3.1.1", features = ["no-entrypoint"] }
solana-program = "1.8.1"
cardinal-token-manager = { version = "^0.0.2", path = "../cardinal-token-manager", features = ["cpi"] }
cardinal-token-manager = { version = "^0.0.3", path = "../cardinal-token-manager", features = ["cpi"] }

[dev-dependencies]
proptest = { version = "1.0" }
4 changes: 2 additions & 2 deletions programs/cardinal-rent-receipt/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cardinal-rent-receipt"
version = "0.0.2"
version = "0.0.3"
description = "Cardinal paid claim approver"
edition = "2021"
homepage = "https://cardinal.so"
Expand All @@ -25,7 +25,7 @@ anchor-spl = "0.20.1"
spl-associated-token-account = "1.0.2"
spl-token = { version = "3.1.1", features = ["no-entrypoint"] }
solana-program = "1.8.1"
cardinal-token-manager = { version = "^0.0.2", path = "../cardinal-token-manager", features = ["cpi"] }
cardinal-token-manager = { version = "^0.0.3", path = "../cardinal-token-manager", features = ["cpi"] }

[dev-dependencies]
proptest = { version = "1.0" }
4 changes: 2 additions & 2 deletions programs/cardinal-rental-counter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cardinal-rental-counter"
version = "0.0.2"
version = "0.0.3"
description = "Cardinal paid claim approver"
edition = "2021"
homepage = "https://cardinal.so"
Expand All @@ -25,7 +25,7 @@ anchor-spl = "0.20.1"
spl-associated-token-account = "1.0.2"
spl-token = { version = "3.1.1", features = ["no-entrypoint"] }
solana-program = "1.8.1"
cardinal-token-manager = { version = "^0.0.2", path = "../cardinal-token-manager", features = ["cpi"] }
cardinal-token-manager = { version = "^0.0.3", path = "../cardinal-token-manager", features = ["cpi"] }

[dev-dependencies]
proptest = { version = "1.0" }
4 changes: 2 additions & 2 deletions programs/cardinal-time-invalidator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cardinal-time-invalidator"
version = "0.0.2"
version = "0.0.3"
description = "Cardinal paid claim approver"
edition = "2021"
homepage = "https://cardinal.so"
Expand All @@ -25,7 +25,7 @@ anchor-spl = "0.20.1"
spl-associated-token-account = "1.0.2"
spl-token = { version = "3.1.1", features = ["no-entrypoint"] }
solana-program = "1.8.1"
cardinal-token-manager = { version = "^0.0.2", path = "../cardinal-token-manager", features = ["cpi"] }
cardinal-token-manager = { version = "^0.0.3", path = "../cardinal-token-manager", features = ["cpi"] }

[dev-dependencies]
proptest = { version = "1.0" }
2 changes: 1 addition & 1 deletion programs/cardinal-token-manager/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cardinal-token-manager"
version = "0.0.2"
version = "0.0.3"
description = "Cardinal token manager"
edition = "2021"
homepage = "https://cardinal.so"
Expand Down
168 changes: 168 additions & 0 deletions src/claimLinks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import { BN, utils, web3 } from "@project-serum/anchor";
import type { Wallet } from "@saberhq/solana-contrib";
import { SPLToken } from "@saberhq/token-utils";
import {
ASSOCIATED_TOKEN_PROGRAM_ID,
Token,
TOKEN_PROGRAM_ID,
} from "@solana/spl-token";
import type { Connection, PublicKey } from "@solana/web3.js";
import { Keypair, Transaction } from "@solana/web3.js";

import { tokenManager } from "./programs";
import { TokenManagerKind } from "./programs/tokenManager";
import { findTokenManagerAddress } from "./programs/tokenManager/pda";
import { withFindOrInitAssociatedTokenAccount } from "./utils";

export const getLink = (mintId: PublicKey, otp: Keypair): string => {
return `https://claim.cardinal.so/${mintId.toString()}?otp=${utils.bytes.bs58.encode(
otp.secretKey
)}`;
};

export const fromLink = (link: string): [PublicKey, Keypair] => {
try {
const [_, mintId, otp] =
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
/https:\/\/claim\.cardinal\.so\/(.*)\?otp=(.*)/.exec(link)!;
return [
new web3.PublicKey(mintId as string),
Keypair.fromSecretKey(utils.bytes.bs58.decode(otp as string)),
];
} catch (e) {
console.log("Error decoding link: ", e, link);
throw e;
}
};

export const issueToken = async (
connection: Connection,
wallet: Wallet,
{
rentalMint,
issuerTokenAccountId,
amount = new BN(1),
kind = TokenManagerKind.Managed,
}: {
rentalMint: PublicKey;
issuerTokenAccountId: PublicKey;
amount?: BN;
kind?: TokenManagerKind;
}
): Promise<[Transaction, PublicKey, Keypair]> => {
const otp = Keypair.generate();
const transaction = new Transaction();

// init token manager
const [tokenManagerIx, tokenManagerId] = await tokenManager.instruction.init(
connection,
wallet,
rentalMint
);
transaction.add(tokenManagerIx);

transaction.add(
tokenManager.instruction.setClaimApprover(
connection,
wallet,
tokenManagerId,
otp.publicKey
)
);

if (kind === TokenManagerKind.Managed) {
transaction.add(
SPLToken.createSetAuthorityInstruction(
TOKEN_PROGRAM_ID,
rentalMint,
tokenManagerId,
"FreezeAccount",
wallet.publicKey,
[]
)
);
}

// issuer
const tokenManagerTokenAccountId = await withFindOrInitAssociatedTokenAccount(
transaction,
connection,
rentalMint,
tokenManagerId,
wallet.publicKey,
true
);

transaction.add(
tokenManager.instruction.issue(
connection,
wallet,
tokenManagerId,
amount,
rentalMint,
tokenManagerTokenAccountId,
issuerTokenAccountId,
kind
)
);

return [transaction, tokenManagerId, otp];
};

export const claimFromLink = async (
connection: Connection,
wallet: Wallet,
mintId: PublicKey,
otpKeypair: Keypair
): Promise<Transaction> => {
const transaction = new Transaction();
// const otp = utils.bytes.bs58.decode(otpString);
// const keypair = Keypair.fromSecretKey(otp);

const [tokenManagerId] = await findTokenManagerAddress(mintId);
const tokenManagerData = await tokenManager.accounts.getTokenManager(
connection,
tokenManagerId
);

// approve claim request
const [createClaimReceiptIx, claimReceiptId] =
await tokenManager.instruction.createClaimReceipt(
connection,
wallet,
tokenManagerId,
otpKeypair.publicKey
);
transaction.add(createClaimReceiptIx);

const tokenManagerTokenAccountId = await Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
tokenManagerData.parsed.mint,
tokenManagerId,
true
);

const recipientTokenAccountId = await withFindOrInitAssociatedTokenAccount(
transaction,
connection,
tokenManagerData.parsed.mint,
wallet.publicKey,
wallet.publicKey
);

// claim
transaction.add(
tokenManager.instruction.claim(
connection,
wallet,
tokenManagerId,
tokenManagerData.parsed.mint,
tokenManagerTokenAccountId,
recipientTokenAccountId,
claimReceiptId
)
);

return transaction;
};
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./api";
export * as claimLinks from "./claimLinks";
export * from "./utils";
38 changes: 37 additions & 1 deletion src/programs/tokenManager/instruction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { SystemProgram } from "@solana/web3.js";

import type { TOKEN_MANAGER_PROGRAM, TokenManagerKind } from "./constants";
import { TOKEN_MANAGER_ADDRESS, TOKEN_MANAGER_IDL } from "./constants";
import { findTokenManagerAddress } from "./pda";
import { findClaimReceiptId, findTokenManagerAddress } from "./pda";

export const init = async (
connection: Connection,
Expand Down Expand Up @@ -201,3 +201,39 @@ export const claim = (
: [],
});
};

export const createClaimReceipt = async (
connection: Connection,
wallet: Wallet,
tokenManagerId: PublicKey,
claimApproverId: PublicKey
): Promise<[TransactionInstruction, PublicKey]> => {
const provider = new Provider(connection, wallet, {});
const tokenManagerProgram = new Program<TOKEN_MANAGER_PROGRAM>(
TOKEN_MANAGER_IDL,
TOKEN_MANAGER_ADDRESS,
provider
);

const [claimReceiptId, claimReceiptBump] = await findClaimReceiptId(
tokenManagerId,
wallet.publicKey
);

return [
tokenManagerProgram.instruction.createClaimReceipt(
claimReceiptBump,
wallet.publicKey,
{
accounts: {
tokenManager: tokenManagerId,
claimApprover: claimApproverId,
claimReceipt: claimReceiptId,
payer: wallet.publicKey,
systemProgram: SystemProgram.programId,
},
}
),
claimReceiptId,
];
};
2 changes: 1 addition & 1 deletion src/programs/tokenManager/pda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const findClaimReceiptId = async (
tokenManagerKey: PublicKey,
recipientKey: PublicKey
): Promise<[PublicKey, number]> => {
return await PublicKey.findProgramAddress(
return PublicKey.findProgramAddress(
[
utils.bytes.utf8.encode(CLAIM_RECEIPT_SEED),
tokenManagerKey.toBuffer(),
Expand Down
Loading

0 comments on commit fadbca1

Please sign in to comment.