Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

Commit

Permalink
Merge pull request #199 from credix-finance/anchor-v0.21
Browse files Browse the repository at this point in the history
refactor: bump anchor version to 0.21.0
  • Loading branch information
Ruven Salamon authored Feb 16, 2022
2 parents 571c951 + d8384ad commit d0fb4ae
Show file tree
Hide file tree
Showing 17 changed files with 13,466 additions and 18,102 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"@material-ui/core": "^4.12.3",
"@material-ui/icons": "^4.11.2",
"@mui/material": "^5.2.5",
"@project-serum/anchor": "^0.20.1",
"@project-serum/anchor": "^0.21.0",
"@saberhq/anchor-contrib": "^1.11.3",
"@solana/spl-token": "^0.1.8",
"@solana/wallet-adapter-base": "^0.7.1",
Expand Down
90 changes: 41 additions & 49 deletions src/client/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ export const getDealAccounts = multiAsync(
);

export const getGlobalMarketStateAccountData = multiAsync(
async (connection: Connection, wallet: typeof Wallet, globalMarketSeed: string) => {
async (connection: Connection, wallet: Wallet, globalMarketSeed: string) => {
const program = newCredixProgram(connection, wallet);
const globalMarketStatePDA = await findGlobalMarketStatePDA(globalMarketSeed);
return program.account.globalMarketState.fetchNullable(globalMarketStatePDA[0]);
}
);

const getBaseMintPK = multiAsync(
async (connection: Connection, wallet: typeof Wallet, globalMarketSeed: string) => {
async (connection: Connection, wallet: Wallet, globalMarketSeed: string) => {
const globalMarketState = await getGlobalMarketStateAccountData(
connection,
wallet,
Expand All @@ -65,7 +65,7 @@ const getBaseMintPK = multiAsync(
);

export const getUserBaseTokenAccount = multiAsync(
async (connection: Connection, wallet: typeof Wallet, globalMarketSeed: string) => {
async (connection: Connection, wallet: Wallet, globalMarketSeed: string) => {
const baseMint = await getBaseMintPK(connection, wallet, globalMarketSeed);
const accounts = await connection.getParsedTokenAccountsByOwner(wallet.publicKey, {
mint: baseMint,
Expand All @@ -76,9 +76,8 @@ export const getUserBaseTokenAccount = multiAsync(
);

export const getUserBaseBalance = multiAsync(
async (connection: Connection, wallet: typeof Wallet, globalMarketSeed: string) => {
async (connection: Connection, wallet: Wallet, globalMarketSeed: string) => {
const tokenAccount = await getUserBaseTokenAccount(connection, wallet, globalMarketSeed);

if (!tokenAccount) {
return ZERO;
}
Expand All @@ -88,7 +87,7 @@ export const getUserBaseBalance = multiAsync(
);

export const getLiquidityPoolBalance = multiAsync(
async (connection: Connection, wallet: typeof Wallet, globalMarketSeed: string) => {
async (connection: Connection, wallet: Wallet, globalMarketSeed: string) => {
const liquidityPoolTokenAddress = await getLiquidityPoolAssociatedBaseTokenAddressPK(
connection,
wallet,
Expand All @@ -105,7 +104,7 @@ export const getLiquidityPoolBalance = multiAsync(
);

const getOutstandingCredit = multiAsync(
async (connection: Connection, wallet: typeof Wallet, globalMarketSeed: string) => {
async (connection: Connection, wallet: Wallet, globalMarketSeed: string) => {
const globalMarketStateData = await getGlobalMarketStateAccountData(
connection,
wallet,
Expand All @@ -121,7 +120,7 @@ const getOutstandingCredit = multiAsync(
);

export const getGatekeeperNetwork = multiAsync(
async (connection: Connection, wallet: typeof Wallet, globalMarketSeed: string) => {
async (connection: Connection, wallet: Wallet, globalMarketSeed: string) => {
const globalMarketStateData = await getGlobalMarketStateAccountData(
connection,
wallet,
Expand Down Expand Up @@ -154,7 +153,7 @@ export const getClusterTime = multiAsync(async (connection: Connection) => {
});

const getWeightedAverageFinancingFee = multiAsync(
async (connection: Connection, wallet: typeof Wallet, globalMarketSeed: string) => {
async (connection: Connection, wallet: Wallet, globalMarketSeed: string) => {
const _deals = await getDealAccounts(connection, wallet, globalMarketSeed);
const _clusterTime = getClusterTime(connection);
let principalSum = new Big(0);
Expand Down Expand Up @@ -184,7 +183,7 @@ const getWeightedAverageFinancingFee = multiAsync(
);

const getTVL = multiAsync(
async (connection: Connection, wallet: typeof Wallet, globalMarketSeed: string) => {
async (connection: Connection, wallet: Wallet, globalMarketSeed: string) => {
const _liquidityPoolBalance = getLiquidityPoolBalance(connection, wallet, globalMarketSeed);
const _outstandingCredit = getOutstandingCredit(connection, wallet, globalMarketSeed);
const [liquidityPoolBalance, outstandingCredit] = await Promise.all([
Expand All @@ -197,7 +196,7 @@ const getTVL = multiAsync(
);

const getLiquidityPoolAssociatedBaseTokenAddressPK = multiAsync(
async (connection: Connection, wallet: typeof Wallet, globalMarketSeed: string) => {
async (connection: Connection, wallet: Wallet, globalMarketSeed: string) => {
const signingAuthorityPDA = await findSigningAuthorityPDA(globalMarketSeed);
return getAssociatedBaseTokenAddressPK(
connection,
Expand All @@ -210,7 +209,7 @@ const getLiquidityPoolAssociatedBaseTokenAddressPK = multiAsync(
);

const getTreasuryPoolTokenAccountPK = multiAsync(
async (connection: Connection, wallet: typeof Wallet, globalMarketSeed: string) => {
async (connection: Connection, wallet: Wallet, globalMarketSeed: string) => {
const globalMarketStateData = await getGlobalMarketStateAccountData(
connection,
wallet,
Expand All @@ -228,7 +227,7 @@ const getTreasuryPoolTokenAccountPK = multiAsync(
const getAssociatedBaseTokenAddressPK = multiAsync(
async (
connection: Connection,
wallet: typeof Wallet,
wallet: Wallet,
publicKey: PublicKey,
offCurve: boolean,
globalMarketSeed: string
Expand All @@ -245,7 +244,7 @@ const getAssociatedBaseTokenAddressPK = multiAsync(
);

const getInvestorLPAssociatedTokenAddress = multiAsync(
async (connection: Connection, wallet: typeof Wallet, globalMarketSeed: string) => {
async (connection: Connection, wallet: Wallet, globalMarketSeed: string) => {
const lpTokenMintPK = await getLPTokenMintPK(connection, wallet, globalMarketSeed);
return Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID,
Expand All @@ -257,15 +256,10 @@ const getInvestorLPAssociatedTokenAddress = multiAsync(
);

const getGatewayToken = multiAsync(
async (
connection: Connection,
wallet: typeof Wallet,
userPK: PublicKey,
globalMarketSeed: string
) => {
async (connection: Connection, wallet: Wallet, userPK: PublicKey, globalMarketSeed: string) => {
const gatekeeperNetwork = await getGatekeeperNetwork(
connection,
wallet as typeof Wallet,
wallet as Wallet,
globalMarketSeed
);
const gatewayToken = await findGatewayToken(connection, userPK, gatekeeperNetwork);
Expand All @@ -279,7 +273,7 @@ const getGatewayToken = multiAsync(
);

const getLPTokenMintPK = multiAsync(
async (connection: Connection, wallet: typeof Wallet, globalMarketSeed: string) => {
async (connection: Connection, wallet: Wallet, globalMarketSeed: string) => {
const globalMarketStateData = await getGlobalMarketStateAccountData(
connection,
wallet,
Expand All @@ -295,7 +289,7 @@ const getLPTokenMintPK = multiAsync(
);

export const getPoolStats = multiAsync(
async (connection: Connection, wallet: typeof Wallet, globalMarketSeed: string) => {
async (connection: Connection, wallet: Wallet, globalMarketSeed: string) => {
const _tvl = getTVL(connection, wallet, globalMarketSeed);
const _ff = getWeightedAverageFinancingFee(connection, wallet, globalMarketSeed);
const _outstandingCredit = getOutstandingCredit(connection, wallet, globalMarketSeed);
Expand All @@ -313,7 +307,7 @@ export const getPoolStats = multiAsync(
);

export const depositInvestment = multiAsync(
async (amount: Big, connection: Connection, wallet: typeof Wallet, globalMarketSeed: string) => {
async (amount: Big, connection: Connection, wallet: Wallet, globalMarketSeed: string) => {
const depositAmount = new BN(amount.toNumber());
const program = newCredixProgram(connection, wallet);
const _globalMarketStatePDA = findGlobalMarketStatePDA(globalMarketSeed);
Expand Down Expand Up @@ -391,7 +385,7 @@ export const depositInvestment = multiAsync(
);

export const withdrawInvestment = multiAsync(
async (amount: Big, connection: Connection, wallet: typeof Wallet, globalMarketSeed: string) => {
async (amount: Big, connection: Connection, wallet: Wallet, globalMarketSeed: string) => {
const program = newCredixProgram(connection, wallet);
const _globalMarketStatePDA = findGlobalMarketStatePDA(globalMarketSeed);
const _userAssociatedBaseTokenAddressPK = getAssociatedBaseTokenAddressPK(
Expand Down Expand Up @@ -478,7 +472,7 @@ export const withdrawInvestment = multiAsync(
export const getDealAccountData = multiAsync(
async (
connection: Connection,
wallet: typeof Wallet,
wallet: Wallet,
borrower: PublicKey,
dealNumber: number,
globalMarketSeed: string
Expand All @@ -490,12 +484,7 @@ export const getDealAccountData = multiAsync(
);

export const getBorrowerInfoAccountData = multiAsync(
async (
connection: Connection,
wallet: typeof Wallet,
borrower: PublicKey,
globalMarketSeed: string
) => {
async (connection: Connection, wallet: Wallet, borrower: PublicKey, globalMarketSeed: string) => {
const borrowerInfoPDA = await findBorrowerInfoPDA(borrower, globalMarketSeed);
const program = newCredixProgram(connection, wallet);

Expand All @@ -504,7 +493,7 @@ export const getBorrowerInfoAccountData = multiAsync(
);

export const getWithdrawFeePercentage = multiAsync(
async (connection: Connection, wallet: typeof Wallet, globalMarketSeed: string) => {
async (connection: Connection, wallet: Wallet, globalMarketSeed: string) => {
const globalMarketStateData = await getGlobalMarketStateAccountData(
connection,
wallet,
Expand All @@ -520,7 +509,7 @@ export const getWithdrawFeePercentage = multiAsync(
);

export const getInterestFeePercentage = multiAsync(
async (connection: Connection, wallet: typeof Wallet, globalMarketSeed: string) => {
async (connection: Connection, wallet: Wallet, globalMarketSeed: string) => {
const globalMarketStateData = await getGlobalMarketStateAccountData(
connection,
wallet,
Expand All @@ -544,7 +533,7 @@ export const createDeal = multiAsync(
dealNumber: number,
dealName: string,
connection: Connection,
wallet: typeof Wallet,
wallet: Wallet,
globalMarketSeed: string
) => {
const _dealPDA = findDealPDA(borrower, dealNumber, globalMarketSeed);
Expand Down Expand Up @@ -573,8 +562,6 @@ export const createDeal = multiAsync(
};

const tx = program.rpc.createDeal(
dealPDA[1],
borrowerInfoPDA[1],
principalAmount,
financingFeeAmount,
0,
Expand Down Expand Up @@ -604,7 +591,7 @@ export const activateDeal = multiAsync(
borrower: PublicKey,
dealNumber: number,
connection: Connection,
wallet: typeof Wallet,
wallet: Wallet,
globalMarketSeed: string
) => {
const program = newCredixProgram(connection, wallet);
Expand Down Expand Up @@ -671,7 +658,7 @@ export const activateDeal = multiAsync(
);

const getLPTokenSupply = multiAsync(
async (connection: Connection, wallet: typeof Wallet, globalMarketSeed: string) => {
async (connection: Connection, wallet: Wallet, globalMarketSeed: string) => {
const lpTokenMintPK = await getLPTokenMintPK(connection, wallet, globalMarketSeed);
return connection
.getTokenSupply(lpTokenMintPK)
Expand All @@ -680,7 +667,7 @@ const getLPTokenSupply = multiAsync(
);

const getLPTokenPrice = multiAsync(
async (connection: Connection, wallet: typeof Wallet, globalMarketSeed: string) => {
async (connection: Connection, wallet: Wallet, globalMarketSeed: string) => {
const _tvl = getTVL(connection, wallet, globalMarketSeed);
const _lpTokenSupply = getLPTokenSupply(connection, wallet, globalMarketSeed);

Expand All @@ -695,20 +682,25 @@ const getLPTokenPrice = multiAsync(
);

const getUserLPTokenAmount = multiAsync(
async (connection: Connection, wallet: typeof Wallet, globalMarketSeed: string) => {
async (connection: Connection, wallet: Wallet, globalMarketSeed: string) => {
const lpAssociatedAddress = await getInvestorLPAssociatedTokenAddress(
connection,
wallet,
globalMarketSeed
);

const lpBalance = await connection.getTokenAccountBalance(lpAssociatedAddress);
return new Big(lpBalance.value.amount);
try {
const lpBalance = await connection.getTokenAccountBalance(lpAssociatedAddress);
return new Big(lpBalance.value.amount);
} catch (e) {
console.log(e);
return ZERO;
}
}
);

export const getLPTokenBaseBalance = multiAsync(
async (connection: Connection, wallet: typeof Wallet, globalMarketSeed: string) => {
async (connection: Connection, wallet: Wallet, globalMarketSeed: string) => {
const _lpTokenPrice = getLPTokenPrice(connection, wallet, globalMarketSeed);
const _userLPTokenAmount = getUserLPTokenAmount(connection, wallet, globalMarketSeed);

Expand All @@ -727,7 +719,7 @@ export const repayDeal = multiAsync(
repaymentType: RepaymentType,
dealNumber: number,
connection: Connection,
wallet: typeof Wallet,
wallet: Wallet,
globalMarketSeed: string
) => {
const program = newCredixProgram(connection, wallet);
Expand Down Expand Up @@ -814,7 +806,7 @@ export const issueCredixPass = async (
isUnderwriter: boolean,
isBorrower: boolean,
connection: Connection,
wallet: typeof Wallet,
wallet: Wallet,
globalMarketSeed: string
) => {
const program = newCredixProgram(connection, wallet);
Expand All @@ -826,7 +818,7 @@ export const issueCredixPass = async (
_getCredixPassPDA,
]);

const tx = program.rpc.createCredixPass(credixPassPDA[1], isUnderwriter, isBorrower, {
const tx = program.rpc.createCredixPass(isUnderwriter, isBorrower, {
accounts: {
owner: wallet.publicKey,
passHolder: publicKey,
Expand All @@ -847,7 +839,7 @@ export const updateCredixPass = async (
isUnderwriter: boolean,
isBorrower: boolean,
connection: Connection,
wallet: typeof Wallet,
wallet: Wallet,
globalMarketSeed: string
) => {
const program = newCredixProgram(connection, wallet);
Expand Down Expand Up @@ -877,7 +869,7 @@ export const getCredixPassInfo = multiAsync(
async (
publicKey: PublicKey,
connection: Connection,
wallet: typeof Wallet,
wallet: Wallet,
globalMarketSeed: string
) => {
const program = newCredixProgram(connection, wallet);
Expand Down
2 changes: 1 addition & 1 deletion src/client/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Connection } from "@solana/web3.js";
import { config } from "config";
import { CredixProgram } from "types/program.types";

export const newCredixProgram = (connection: Connection, wallet: typeof Wallet) => {
export const newCredixProgram = (connection: Connection, wallet: Wallet) => {
const provider = new Provider(connection, wallet, config.confirmOptions);
const saberProvider = makeSaberProvider(provider);
return newProgram<CredixProgram>(config.idl, config.clusterConfig.programId, saberProvider);
Expand Down
Loading

0 comments on commit d0fb4ae

Please sign in to comment.