Skip to content

Commit

Permalink
Add min amount to collect on fundraiser creation (#9)
Browse files Browse the repository at this point in the history
* add min amount to collect on fundraiser creation

* update program address
  • Loading branch information
makarychev authored Apr 7, 2024
1 parent d1a380d commit 3df4e5e
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ seeds = false
skip-lint = false

[programs.localnet]
donaproto = "6ELhLt25aFjrqiPYfMKerP74iXaytnAcUKtNAWmPi1WW"
donaproto = "HbNNG85aBuR9W5F8YobTeDRRmXWFbDhLDS6WbLzWbLhH"

[registry]
url = "https://api.apr.dev"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct DonationProtocolData {
pub treasury: Pubkey,
pub donation_mint: Pubkey,
pub min_amount_to_earn: u64,
pub min_amount_to_collect: u64,
pub treasury_owner_bump: u8
}

Expand All @@ -20,12 +21,14 @@ impl DonationProtocolData {
const TREASURY_LEN: usize = mem::size_of::<Pubkey>();
const DONATION_MINT_LEN: usize = mem::size_of::<Pubkey>();
const MIN_AMOUNT_TO_EARN_LEN: usize = mem::size_of::<u64>();
const MIN_AMOUNT_TO_COLLECT_LEN: usize = mem::size_of::<u64>();
const TREASURY_OWNER_BUMP_LEN: usize = mem::size_of::<u8>();
pub const LEN: usize = DISCRIMINATOR_LEN
+ DonationProtocolData::TREASURY_MINT_LEN
+ DonationProtocolData::TREASURY_LEN
+ DonationProtocolData::DONATION_MINT_LEN
+ DonationProtocolData::MIN_AMOUNT_TO_EARN_LEN
+ DonationProtocolData::MIN_AMOUNT_TO_COLLECT_LEN
+ DonationProtocolData::TREASURY_OWNER_BUMP_LEN;
}

Expand Down
2 changes: 2 additions & 0 deletions programs/donaproto/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ pub enum DonationError {
DonationEndingReqiuirementsNotMet,
#[msg("Ipfs hash too long")]
IpfsHashTooLong,
#[msg("Donation amount too low")]
DonationAmountTooLow,
}
9 changes: 7 additions & 2 deletions programs/donaproto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub mod errors;

use crate::errors::DonationError;

declare_id!("6ELhLt25aFjrqiPYfMKerP74iXaytnAcUKtNAWmPi1WW");
declare_id!("HbNNG85aBuR9W5F8YobTeDRRmXWFbDhLDS6WbLzWbLhH");

#[program]
pub mod donaproto {
Expand All @@ -16,6 +16,7 @@ pub mod donaproto {
pub fn initialize_donation_protocol(
ctx: Context<InitializeDonationProtocol>,
min_amount_to_earn: u64,
min_amount_to_collect: u64,
treasury_owner_bump: u8,
) -> Result<()> {
let donation_data = &mut ctx.accounts.donation_protocol_data;
Expand All @@ -24,6 +25,7 @@ pub mod donaproto {
donation_data.donation_mint = ctx.accounts.donation_mint.key();
donation_data.min_amount_to_earn = min_amount_to_earn;
donation_data.treasury_owner_bump = treasury_owner_bump;
donation_data.min_amount_to_collect = min_amount_to_collect;

Ok(())
}
Expand Down Expand Up @@ -71,6 +73,9 @@ pub mod donaproto {
if ipfs_hash.len() > MAX_IPFS_HASH_LEN {
return Err(DonationError::IpfsHashTooLong.into());
}
if amount < ctx.accounts.donation_protocol.min_amount_to_collect {
return Err(DonationError::DonationAmountTooLow.into());
}

let donation_data = &mut ctx.accounts.donation_data;
donation_data.amount_collecting = amount;
Expand Down Expand Up @@ -130,7 +135,7 @@ pub mod donaproto {
if amount >= donation_protocol.min_amount_to_earn {
// TODO: add calculation for reward amount
let reward_treasury_balance = ctx.accounts.reward_treasury.amount;
let mut reward_amount = amount.checked_div(100).unwrap();
let mut reward_amount = amount;
if reward_amount > reward_treasury_balance {
reward_amount = reward_treasury_balance.checked_div(100).unwrap();
}
Expand Down
2 changes: 2 additions & 0 deletions tests/createDonation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe("donaproto", () => {
const creatorWallet = anchor.web3.Keypair.generate();
let creatorDataPubkey, creatorDataBump;
let creatorDonationTokenAccount;
const minAmountToCollect = new anchor.BN(1_000_000);

before(async () => {
donationMintPubKey = await createMint(
Expand Down Expand Up @@ -66,6 +67,7 @@ describe("donaproto", () => {

await program.rpc.initializeDonationProtocol(
minAmountToEarn,
minAmountToCollect,
treasuryOwnerBump,
{
accounts: {
Expand Down
22 changes: 12 additions & 10 deletions tests/donate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe("donaproto", () => {
const contributorWallet = anchor.web3.Keypair.generate();
let treasuryTokenAccount, treasuryOwnerPubkey, treasuryOwnerBump;
let donationHoldingWallet;
const minAmountToCollect = new anchor.BN(1_000_000);

before(async () => {
donationMintPubKey = await createMint(
Expand Down Expand Up @@ -74,6 +75,7 @@ describe("donaproto", () => {

await program.rpc.initializeDonationProtocol(
minAmountToEarn,
minAmountToCollect,
treasuryOwnerBump,
{
accounts: {
Expand Down Expand Up @@ -103,7 +105,7 @@ describe("donaproto", () => {
donationMintPubKey,
creatorDonationTokenAccount.address,
donationMintAuthority,
10000000000
10_000_000_000
)

const [creatorDataPubkeyFound, creatorDataBumpFound] = anchor.web3.PublicKey.findProgramAddressSync(
Expand Down Expand Up @@ -140,12 +142,12 @@ describe("donaproto", () => {
assert.equal(onchainCreatorData.totalAmountCollecting, 0);

// top up creator wallet to be able to pay for createDonation tx
await rechargeWallet(connection, creatorWallet.publicKey, 1000000000);
await rechargeWallet(connection, creatorWallet.publicKey, 10_00_000_000);
const creatorWalletBalance = await connection.getBalance(creatorWallet.publicKey);

const amount = new anchor.BN(1000000000); // 1000$
const amount = new anchor.BN(1_000_000_000); // 1000$
const ipfsHash = "some_ipfs_hash";
const endingTimestamp = await getNowTs(provider) + 100000;
const endingTimestamp = await getNowTs(provider) + 100_000;
const [holdingWalletOwnerPubkey, holdingWalletOwnerBump] = anchor.web3.PublicKey.findProgramAddressSync(
[
Buffer.from(HOLDING_PREFIX),
Expand Down Expand Up @@ -199,7 +201,7 @@ describe("donaproto", () => {
});

it("donates and earns reward", async () => {
await rechargeWallet(connection, contributorWallet.publicKey, 1000000000);
await rechargeWallet(connection, contributorWallet.publicKey, 1_000_000_000);
const [contributorDataPubkey, contributorDataBump] = anchor.web3.PublicKey.findProgramAddressSync(
[
Buffer.from(CONTRIBUTOR_PREFIX),
Expand Down Expand Up @@ -234,7 +236,7 @@ describe("donaproto", () => {
donationMintPubKey,
contributorDonationTokenAccount.address,
donationMintAuthority,
10000000000, // 10000$
10_000_000_000, // 10000$
)

const contributorRewardTokenAccount = await getOrCreateAssociatedTokenAccount(
Expand All @@ -246,7 +248,7 @@ describe("donaproto", () => {

// when treasury has not enough reward, contributor can't earn reward
// but can donate
const amount = new anchor.BN(1000000); // 1000$
const amount = new anchor.BN(1_000_000); // 1000$
await program.rpc.donate(
amount,
{
Expand Down Expand Up @@ -289,7 +291,7 @@ describe("donaproto", () => {
rewardsMintPubKey,
treasuryTokenAccount.address,
rewardMintAuthority,
10000000000, // 10000$
10_000_000_000, // 10000$
)

await program.rpc.donate(
Expand All @@ -316,15 +318,15 @@ describe("donaproto", () => {
balanceDonationHoldingWallet = await connection.getTokenAccountBalance(donationHoldingWallet.address);
assert.equal(balanceDonationHoldingWallet.value.amount, amount.muln(2).toString());
balanceContributorRewardTokenAccount = await connection.getTokenAccountBalance(contributorRewardTokenAccount.address);
assert.equal(balanceContributorRewardTokenAccount.value.amount, "10000");
assert.equal(balanceContributorRewardTokenAccount.value.amount, "1000000");

onchainDonationData = await program.account.donationData.fetch(donationData.publicKey);
assert.equal(onchainDonationData.totalAmountReceived.toString(), amount.muln(2).toString());
assert.equal(onchainDonationData.isClosed, false);

onchainContributorData = await program.account.contributorData.fetch(contributorDataPubkey);
assert.equal(onchainContributorData.totalAmountDonated.toString(), amount.muln(2).toString());
assert.equal(onchainContributorData.totalAmountEarned.toString(), "10000");
assert.equal(onchainContributorData.totalAmountEarned.toString(), "1000000");
assert.equal(onchainContributorData.donationsCount.toString(), 2);
});
});
2 changes: 2 additions & 0 deletions tests/initializeContributor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe("donaproto", () => {
const donationProtocolData = anchor.web3.Keypair.generate();
const minAmountToEarn = new anchor.BN(1000);
const contributorWallet = anchor.web3.Keypair.generate();
const minAmountToCollect = new anchor.BN(1_000_000);

before(async () => {
donationMintPubKey = await createMint(
Expand Down Expand Up @@ -62,6 +63,7 @@ describe("donaproto", () => {

await program.rpc.initializeDonationProtocol(
minAmountToEarn,
minAmountToCollect,
treasuryOwnerBump,
{
accounts: {
Expand Down
2 changes: 2 additions & 0 deletions tests/initializeCreator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe("donaproto", () => {
const donationProtocolData = anchor.web3.Keypair.generate();
const minAmountToEarn = new anchor.BN(1000);
const creatorWallet = anchor.web3.Keypair.generate();
const minAmountToCollect = new anchor.BN(1_000_000);

before(async () => {
donationMintPubKey = await createMint(
Expand Down Expand Up @@ -62,6 +63,7 @@ describe("donaproto", () => {

await program.rpc.initializeDonationProtocol(
minAmountToEarn,
minAmountToCollect,
treasuryOwnerBump,
{
accounts: {
Expand Down
2 changes: 2 additions & 0 deletions tests/initializeDonationProtocol.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe("donaproto", () => {
let donationMintPubKey, rewardsMintPubKey
const rewardMintAuthority = payer;
const rewardMintDecimals = 9;
const minAmountToCollect = new anchor.BN(1_000_000);

before(async () => {
donationMintPubKey = await createMint(
Expand Down Expand Up @@ -62,6 +63,7 @@ describe("donaproto", () => {

const tx = await program.rpc.initializeDonationProtocol(
minAmountToEarn,
minAmountToCollect,
treasuryOwnerBump,
{
accounts: {
Expand Down
2 changes: 2 additions & 0 deletions tests/withdrawFunds.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ describe("donaproto", () => {
program.programId,
);
let contributorDonationTokenAccount, contributorRewardTokenAccount;
const minAmountToCollect = new anchor.BN(1_000_000);

before(async () => {
donationMintPubKey = await createMint(
Expand Down Expand Up @@ -85,6 +86,7 @@ describe("donaproto", () => {

await program.rpc.initializeDonationProtocol(
minAmountToEarn,
minAmountToCollect,
treasuryOwnerBump,
{
accounts: {
Expand Down

0 comments on commit 3df4e5e

Please sign in to comment.