|
| 1 | +import { Aquafarm, fetchGlobalFarms, fetchUserFarms, getUserFarmAddress } from "@orca-so/aquafarm"; |
| 2 | +import { TOKEN_PROGRAM_ID, u64 } from "@solana/spl-token"; |
| 3 | +import { Connection, Keypair, PublicKey } from "@solana/web3.js"; |
| 4 | +import Decimal from "decimal.js"; |
| 5 | +import { |
| 6 | + deriveAssociatedTokenAddress, |
| 7 | + deserializeAccount, |
| 8 | + OrcaU64, |
| 9 | + ORCA_FARM_ID, |
| 10 | + resolveOrCreateAssociatedTokenAddress, |
| 11 | + TransactionBuilder, |
| 12 | + TransactionPayload, |
| 13 | + U64Utils, |
| 14 | +} from "../../.."; |
| 15 | +import { OrcaFarm } from "../../../public/"; |
| 16 | +import { |
| 17 | + createFarmConvertTokensInstruction, |
| 18 | + createFarmRevertTokensInstruction, |
| 19 | + createInitUserFarmInstruction, |
| 20 | +} from "../../../public/utils/web3/instructions/farm-instructions"; |
| 21 | +import { createApprovalInstruction } from "../../../public/utils/web3/instructions/pool-instructions"; |
| 22 | +import { Owner } from "../../../public/utils/web3/key-utils"; |
| 23 | +import { OrcaFarmParams } from "./farm-types"; |
| 24 | + |
| 25 | +export class OrcaFarmImpl implements OrcaFarm { |
| 26 | + private connection: Connection; |
| 27 | + private farmParams: OrcaFarmParams; |
| 28 | + |
| 29 | + constructor(connection: Connection, config: OrcaFarmParams) { |
| 30 | + this.connection = connection; |
| 31 | + this.farmParams = config; |
| 32 | + } |
| 33 | + |
| 34 | + public async getFarmBalance(owner: PublicKey): Promise<OrcaU64> { |
| 35 | + const address = await deriveAssociatedTokenAddress(owner, this.farmParams.farmTokenMint); |
| 36 | + |
| 37 | + const accountInfo = await this.connection.getAccountInfo(address); |
| 38 | + |
| 39 | + // User does not have a balance for this account |
| 40 | + if (accountInfo == undefined) { |
| 41 | + return OrcaU64.fromNumber(0, this.farmParams.baseTokenDecimals); |
| 42 | + } |
| 43 | + const result = deserializeAccount(accountInfo?.data); |
| 44 | + if (result == undefined) { |
| 45 | + throw new Error("Failed to parse user account for LP token."); |
| 46 | + } |
| 47 | + |
| 48 | + return OrcaU64.fromU64(result.amount, this.farmParams.baseTokenDecimals); |
| 49 | + } |
| 50 | + |
| 51 | + public async getFarmSupply(): Promise<OrcaU64> { |
| 52 | + const context = await this.connection.getTokenSupply(this.farmParams.farmTokenMint); |
| 53 | + |
| 54 | + const amt = new u64(context.value.amount); |
| 55 | + |
| 56 | + return OrcaU64.fromU64(amt, this.farmParams.baseTokenDecimals); |
| 57 | + } |
| 58 | + |
| 59 | + public async deposit( |
| 60 | + owner: Keypair | PublicKey, |
| 61 | + baseTokenAmount: Decimal | OrcaU64 |
| 62 | + ): Promise<TransactionPayload> { |
| 63 | + const _owner = new Owner(owner); |
| 64 | + const ownerAddress = _owner.publicKey; |
| 65 | + |
| 66 | + const baseTokenAmount_U64 = U64Utils.toFarmU64( |
| 67 | + baseTokenAmount, |
| 68 | + this.farmParams, |
| 69 | + "baseTokenAmount" |
| 70 | + ); |
| 71 | + |
| 72 | + const { address: farmAddress, rewardTokenMint } = this.farmParams; |
| 73 | + const userFarmPublicKey = ( |
| 74 | + await getUserFarmAddress(farmAddress, ownerAddress, TOKEN_PROGRAM_ID, ORCA_FARM_ID) |
| 75 | + )[0]; |
| 76 | + |
| 77 | + const globalFarms = await fetchGlobalFarms(this.connection, [farmAddress], ORCA_FARM_ID); |
| 78 | + const userFarms = await fetchUserFarms( |
| 79 | + this.connection, |
| 80 | + ownerAddress, |
| 81 | + [farmAddress], |
| 82 | + ORCA_FARM_ID |
| 83 | + ); |
| 84 | + |
| 85 | + if (!globalFarms) { |
| 86 | + throw new Error("Failed to get globalFarms information"); |
| 87 | + } |
| 88 | + const farm = new Aquafarm(globalFarms[0], ORCA_FARM_ID, userFarms && userFarms[0]); |
| 89 | + |
| 90 | + // If the user lacks the user farm, create it |
| 91 | + const initUserFarmInstruction = await createInitUserFarmInstruction( |
| 92 | + farm, |
| 93 | + userFarmPublicKey, |
| 94 | + _owner |
| 95 | + ); |
| 96 | + |
| 97 | + // If the user lacks the farm token account, create it |
| 98 | + const { address: userFarmTokenPublicKey, ...resolveFarmTokenInstructions } = |
| 99 | + await resolveOrCreateAssociatedTokenAddress( |
| 100 | + this.connection, |
| 101 | + _owner, |
| 102 | + farm.globalFarm.farmTokenMint |
| 103 | + ); |
| 104 | + |
| 105 | + // If the user lacks the reward token account, create it |
| 106 | + const { address: userRewardTokenPublicKey, ...resolveRewardTokenInstructions } = |
| 107 | + await resolveOrCreateAssociatedTokenAddress(this.connection, _owner, rewardTokenMint); |
| 108 | + |
| 109 | + // If the user lacks the base token account, create it |
| 110 | + const { address: userBaseTokenPublicKey, ...resolveBaseTokenInstructions } = |
| 111 | + await resolveOrCreateAssociatedTokenAddress( |
| 112 | + this.connection, |
| 113 | + _owner, |
| 114 | + this.farmParams.baseTokenMint |
| 115 | + ); |
| 116 | + |
| 117 | + // Approve transfer of base token to be converted to farm tokens |
| 118 | + const { userTransferAuthority, ...transferBaseTokenInstruction } = createApprovalInstruction( |
| 119 | + ownerAddress, |
| 120 | + baseTokenAmount_U64, |
| 121 | + userBaseTokenPublicKey |
| 122 | + ); |
| 123 | + |
| 124 | + // Convert base tokens to farm tokens |
| 125 | + const convertToFarmTokens = await createFarmConvertTokensInstruction( |
| 126 | + farm, |
| 127 | + userTransferAuthority.publicKey, |
| 128 | + userBaseTokenPublicKey, |
| 129 | + userFarmTokenPublicKey, |
| 130 | + userRewardTokenPublicKey, |
| 131 | + baseTokenAmount_U64, |
| 132 | + userFarmPublicKey, |
| 133 | + _owner |
| 134 | + ); |
| 135 | + |
| 136 | + return await new TransactionBuilder(this.connection, ownerAddress, _owner) |
| 137 | + .addInstruction(initUserFarmInstruction) |
| 138 | + .addInstruction(resolveFarmTokenInstructions) |
| 139 | + .addInstruction(resolveBaseTokenInstructions) |
| 140 | + .addInstruction(resolveRewardTokenInstructions) |
| 141 | + .addInstruction(transferBaseTokenInstruction) |
| 142 | + .addInstruction(convertToFarmTokens) |
| 143 | + .build(); |
| 144 | + } |
| 145 | + |
| 146 | + public async withdraw( |
| 147 | + owner: Keypair | PublicKey, |
| 148 | + baseTokenAmount: Decimal | OrcaU64 |
| 149 | + ): Promise<TransactionPayload> { |
| 150 | + const _owner = new Owner(owner); |
| 151 | + const ownerAddress = _owner.publicKey; |
| 152 | + |
| 153 | + const baseTokenAmount_U64 = U64Utils.toFarmU64( |
| 154 | + baseTokenAmount, |
| 155 | + this.farmParams, |
| 156 | + "baseTokenAmount" |
| 157 | + ); |
| 158 | + |
| 159 | + const { address: farmAddress, rewardTokenMint } = this.farmParams; |
| 160 | + const userFarmPublicKey = ( |
| 161 | + await getUserFarmAddress(farmAddress, ownerAddress, TOKEN_PROGRAM_ID, ORCA_FARM_ID) |
| 162 | + )[0]; |
| 163 | + |
| 164 | + const globalFarms = await fetchGlobalFarms(this.connection, [farmAddress], ORCA_FARM_ID); |
| 165 | + const userFarms = await fetchUserFarms( |
| 166 | + this.connection, |
| 167 | + ownerAddress, |
| 168 | + [farmAddress], |
| 169 | + ORCA_FARM_ID |
| 170 | + ); |
| 171 | + |
| 172 | + if (!globalFarms) { |
| 173 | + throw new Error("Failed to get globalFarms information"); |
| 174 | + } |
| 175 | + const farm = new Aquafarm(globalFarms[0], ORCA_FARM_ID, userFarms && userFarms[0]); |
| 176 | + |
| 177 | + if (!farm.isUserFarmInitialized()) { |
| 178 | + throw new Error("Failed to get userFarm information. Warning: withdraw from deposit address"); |
| 179 | + } |
| 180 | + |
| 181 | + // If the user lacks the farm token account, create it |
| 182 | + const { address: userFarmTokenPublicKey, ...resolveFarmTokenInstructions } = |
| 183 | + await resolveOrCreateAssociatedTokenAddress( |
| 184 | + this.connection, |
| 185 | + _owner, |
| 186 | + farm.globalFarm.farmTokenMint |
| 187 | + ); |
| 188 | + |
| 189 | + // If the user lacks the reward token account, create it |
| 190 | + const { address: userRewardTokenPublicKey, ...resolveRewardTokenInstructions } = |
| 191 | + await resolveOrCreateAssociatedTokenAddress(this.connection, _owner, rewardTokenMint); |
| 192 | + |
| 193 | + // Get user's baseToken token account |
| 194 | + const { address: userBaseTokenPublicKey, ...resolveBaseTokenInstructions } = |
| 195 | + await resolveOrCreateAssociatedTokenAddress( |
| 196 | + this.connection, |
| 197 | + _owner, |
| 198 | + this.farmParams.baseTokenMint |
| 199 | + ); |
| 200 | + |
| 201 | + // Approve transfer of farm tokens to be reverted to base tokens |
| 202 | + const { userTransferAuthority, ...transferFarmTokenInstruction } = createApprovalInstruction( |
| 203 | + ownerAddress, |
| 204 | + baseTokenAmount_U64, |
| 205 | + userFarmTokenPublicKey |
| 206 | + ); |
| 207 | + |
| 208 | + // Revert farm tokens to base tokens |
| 209 | + const revertFromFarmTokens = await createFarmRevertTokensInstruction( |
| 210 | + farm, |
| 211 | + userTransferAuthority.publicKey, |
| 212 | + userBaseTokenPublicKey, |
| 213 | + userFarmTokenPublicKey, |
| 214 | + userRewardTokenPublicKey, |
| 215 | + baseTokenAmount_U64, |
| 216 | + _owner |
| 217 | + ); |
| 218 | + |
| 219 | + return await new TransactionBuilder(this.connection, ownerAddress, _owner) |
| 220 | + .addInstruction(resolveFarmTokenInstructions) |
| 221 | + .addInstruction(resolveRewardTokenInstructions) |
| 222 | + .addInstruction(resolveBaseTokenInstructions) |
| 223 | + .addInstruction(transferFarmTokenInstruction) |
| 224 | + .addInstruction(revertFromFarmTokens) |
| 225 | + .build(); |
| 226 | + } |
| 227 | +} |
0 commit comments