From 801494875af97cdd66d0394246aab27525cbebb0 Mon Sep 17 00:00:00 2001 From: emerson Date: Fri, 4 Nov 2022 00:44:55 +0000 Subject: [PATCH] Refactor --- src/protocols/genopets.ts | 118 ++++++++++++++++++----------------- tests/testAdapterGenopets.ts | 68 +++++++++----------- 2 files changed, 91 insertions(+), 95 deletions(-) diff --git a/src/protocols/genopets.ts b/src/protocols/genopets.ts index ed4093d..d055077 100644 --- a/src/protocols/genopets.ts +++ b/src/protocols/genopets.ts @@ -56,52 +56,54 @@ export class ProtocolGenopets implements IProtocolFarm { const farmWrapper = new genopets.FarmInfoWrapper(farm); const farmerId = await genopets.infos.getFarmerId(farm, userKey); const farmerAccount = await this._connection.getAccountInfo(farmerId); - let userDeposit = genopets.getFarmerDepositKey(userKey, 0); - let userReDeposit = genopets.getFarmerDepositKey(userKey, 1); + let userDeposit = genopets.getFarmerInstanceKey(userKey, 0); + let userReDeposit = genopets.getFarmerInstanceKey(userKey, 1); if (farmerAccount) { const farmer = (await genopets.infos.getFarmer( this._connection, farmerId )) as genopets.FarmerInfo; const farmerWrapper = new genopets.FarmerInfoWrapper(farmer); - userDeposit = farmerWrapper.getUserDeposit(); - userReDeposit = farmerWrapper.getUserReDeposit(); + userDeposit = farmerWrapper.getFarmerInstance(); + userReDeposit = farmerWrapper.getLatestFarmerInstance(); } let preInstructions: anchor.web3.TransactionInstruction[] = []; const userPoolTokenAccount = await getAssociatedTokenAddress( - params.mint, + farm.poolToken, userKey ); - preInstructions.push(await createATAWithoutCheckIx(userKey, params.mint)); + preInstructions.push( + await createATAWithoutCheckIx(userKey, farm.poolToken) + ); const userSgeneTokenAccount = await getAssociatedTokenAddress( - farm.mintSgene, + farm.master.mintSgene, userKey ); preInstructions.push( - await createATAWithoutCheckIx(userKey, farm.mintSgene) + await createATAWithoutCheckIx(userKey, farm.master.mintSgene) ); const vaultATA = await getAssociatedTokenAddress( - params.mint, + farm.poolToken, farmerId, true ); preInstructions.push( - await createATAWithoutCheckIx(farmerId, params.mint, userKey) + await createATAWithoutCheckIx(farmerId, farm.poolToken, userKey) ); const remainingAccounts = [ { pubkey: userKey, isSigner: true, isWritable: true }, // 0 - { pubkey: farm.farmId, isSigner: false, isWritable: true }, // 1 + { pubkey: farm.master.id, isSigner: false, isWritable: true }, // 1 { - pubkey: farmWrapper.getStakingPool(params.mint), + pubkey: farm.farmId, isSigner: false, isWritable: true, }, // 2 - { pubkey: params.mint, isSigner: false, isWritable: true }, // 3 + { pubkey: farm.poolToken, isSigner: false, isWritable: true }, // 3 { pubkey: farmerId, isSigner: false, isWritable: true }, // 4 { pubkey: userPoolTokenAccount, @@ -119,12 +121,12 @@ export class ProtocolGenopets implements IProtocolFarm { isWritable: true, }, // 7 { - pubkey: farm.sgeneMinter, + pubkey: farm.master.sgeneMinter, isSigner: false, isWritable: false, }, // 8 { - pubkey: farm.mintSgene, + pubkey: farm.master.mintSgene, isSigner: false, isWritable: true, }, // 9 @@ -192,49 +194,51 @@ export class ProtocolGenopets implements IProtocolFarm { const farmWrapper = new genopets.FarmInfoWrapper(farm); const farmerId = await genopets.infos.getFarmerId(farm, userKey); const farmerAccount = await this._connection.getAccountInfo(farmerId); - let userDeposit = genopets.getFarmerDepositKey(userKey, 0); - let userReDeposit = genopets.getFarmerDepositKey(userKey, 1); + let userDeposit = genopets.getFarmerInstanceKey(userKey, 0); + let userReDeposit = genopets.getFarmerInstanceKey(userKey, 1); if (farmerAccount) { const farmer = (await genopets.infos.getFarmer( this._connection, farmerId )) as genopets.FarmerInfo; const farmerWrapper = new genopets.FarmerInfoWrapper(farmer); - userDeposit = params.farmerKey || farmerWrapper.getUserDeposit(); - userReDeposit = farmerWrapper.getUserDeposit(); + userDeposit = params.farmerKey || farmerWrapper.getFarmerInstance(); + userReDeposit = farmerWrapper.getFarmerInstance(); } let preInstructions: anchor.web3.TransactionInstruction[] = []; const userPoolTokenAccount = await getAssociatedTokenAddress( - params.mint, + farm.poolToken, userKey ); - preInstructions.push(await createATAWithoutCheckIx(userKey, params.mint)); + preInstructions.push( + await createATAWithoutCheckIx(userKey, farm.poolToken) + ); const userSgeneTokenAccount = await getAssociatedTokenAddress( - farm.mintSgene, + farm.master.mintSgene, userKey ); preInstructions.push( - await createATAWithoutCheckIx(userKey, farm.mintSgene) + await createATAWithoutCheckIx(userKey, farm.master.mintSgene) ); const vaultATA = await getAssociatedTokenAddress( - params.mint, + farm.poolToken, farmerId, true ); const remainingAccounts = [ { pubkey: userKey, isSigner: true, isWritable: true }, // 0 - { pubkey: farm.farmId, isSigner: false, isWritable: true }, // 1 + { pubkey: farm.master.id, isSigner: false, isWritable: true }, // 1 { - pubkey: farmWrapper.getStakingPool(params.mint), + pubkey: farm.farmId, isSigner: false, isWritable: true, }, // 2 - { pubkey: params.mint, isSigner: false, isWritable: false }, // 3 + { pubkey: farm.poolToken, isSigner: false, isWritable: false }, // 3 { pubkey: farmerId, isSigner: false, isWritable: true }, // 4 { pubkey: userPoolTokenAccount, @@ -247,7 +251,7 @@ export class ProtocolGenopets implements IProtocolFarm { isWritable: true, }, // 6 { - pubkey: farm.sgeneMinter, + pubkey: farm.master.sgeneMinter, isSigner: false, isWritable: false, }, // 7 @@ -257,17 +261,17 @@ export class ProtocolGenopets implements IProtocolFarm { isWritable: true, }, // 8 { - pubkey: farm.mintSgene, + pubkey: farm.master.mintSgene, isSigner: false, isWritable: true, }, // 9 { - pubkey: farm.geneRewarder, + pubkey: farm.master.geneRewarder, isSigner: false, isWritable: false, }, // 10 { - pubkey: farm.ataGeneRewarder, + pubkey: farm.master.ataGeneRewarder, isSigner: false, isWritable: true, }, // 11 @@ -341,17 +345,17 @@ export class ProtocolGenopets implements IProtocolFarm { const farmWrapper = new genopets.FarmInfoWrapper(farm); const farmerId = await genopets.infos.getFarmerId(farm, userKey); const farmerAccount = await this._connection.getAccountInfo(farmerId); - let userDeposit = genopets.getFarmerDepositKey(userKey, 0); - let userReDeposit = genopets.getFarmerDepositKey(userKey, 1); + let userDeposit = genopets.getFarmerInstanceKey(userKey, 0); + let userReDeposit = genopets.getFarmerInstanceKey(userKey, 1); let preInstructions: anchor.web3.TransactionInstruction[] = []; const userSgeneTokenAccount = await getAssociatedTokenAddress( - farm.mintSgene, + farm.master.mintSgene, userKey ); preInstructions.push( - await createATAWithoutCheckIx(userKey, farm.mintSgene) + await createATAWithoutCheckIx(userKey, farm.master.mintSgene) ); let remainingAccounts: anchor.web3.AccountMeta[] = []; @@ -363,21 +367,21 @@ export class ProtocolGenopets implements IProtocolFarm { farmerId )) as genopets.FarmerInfo; const farmerWrapper = new genopets.FarmerInfoWrapper(farmer); - userDeposit = farmerWrapper.getUserDeposit(); - userReDeposit = farmerWrapper.getUserReDeposit(); + userDeposit = farmerWrapper.getFarmerInstance(); + userReDeposit = farmerWrapper.getLatestFarmerInstance(); } remainingAccounts = [ { pubkey: userKey, isSigner: true, isWritable: true }, // 0 - { pubkey: farm.farmId, isSigner: false, isWritable: true }, // 1 + { pubkey: farm.master.id, isSigner: false, isWritable: true }, // 1 { pubkey: farmerId, isSigner: false, isWritable: true }, // 2 { - pubkey: farm.sgeneMinter, + pubkey: farm.master.sgeneMinter, isSigner: false, isWritable: false, }, // 3 { - pubkey: farm.mintSgene, + pubkey: farm.master.mintSgene, isSigner: false, isWritable: true, }, // 4 @@ -420,32 +424,32 @@ export class ProtocolGenopets implements IProtocolFarm { farmerId )) as genopets.FarmerInfo; const farmerWrapper = new genopets.FarmerInfoWrapper(farmer); - userDeposit = params.farmerKey || farmerWrapper.getUserDeposit(); - userReDeposit = farmerWrapper.getUserDeposit(); + userDeposit = params.farmerKey || farmerWrapper.getFarmerInstance(); + userReDeposit = farmerWrapper.getFarmerInstance(); } const userPoolTokenAccount = await getAssociatedTokenAddress( - params.mint, + farm.poolToken, userKey ); preInstructions.push( - await createATAWithoutCheckIx(userKey, params.mint) + await createATAWithoutCheckIx(userKey, farm.poolToken) ); const vaultATA = await getAssociatedTokenAddress( - params.mint, + farm.poolToken, farmerId, true ); remainingAccounts = [ { pubkey: userKey, isSigner: true, isWritable: true }, // 0 - { pubkey: farm.farmId, isSigner: false, isWritable: true }, // 1 + { pubkey: farm.master.id, isSigner: false, isWritable: true }, // 1 { - pubkey: farmWrapper.getStakingPool(params.mint), + pubkey: farm.farmId, isSigner: false, isWritable: true, }, // 2 - { pubkey: params.mint, isSigner: false, isWritable: false }, // 3 + { pubkey: farm.poolToken, isSigner: false, isWritable: false }, // 3 { pubkey: farmerId, isSigner: false, isWritable: true }, // 4 { pubkey: userPoolTokenAccount, @@ -458,7 +462,7 @@ export class ProtocolGenopets implements IProtocolFarm { isWritable: true, }, // 6 { - pubkey: farm.sgeneMinter, + pubkey: farm.master.sgeneMinter, isSigner: false, isWritable: false, }, // 7 @@ -468,17 +472,17 @@ export class ProtocolGenopets implements IProtocolFarm { isWritable: true, }, // 8 { - pubkey: farm.mintSgene, + pubkey: farm.master.mintSgene, isSigner: false, isWritable: true, }, // 9 { - pubkey: farm.geneRewarder, + pubkey: farm.master.geneRewarder, isSigner: false, isWritable: false, }, // 10 { - pubkey: farm.ataGeneRewarder, + pubkey: farm.master.ataGeneRewarder, isSigner: false, isWritable: true, }, // 11 @@ -521,16 +525,16 @@ export class ProtocolGenopets implements IProtocolFarm { farmerId )) as genopets.FarmerInfo; const farmerWrapper = new genopets.FarmerInfoWrapper(farmer); - userDeposit = params.farmerKey || farmerWrapper.getUserDeposit(); - userReDeposit = farmerWrapper.getUserReDeposit(); + userDeposit = params.farmerKey || farmerWrapper.getFarmerInstance(); + userReDeposit = farmerWrapper.getLatestFarmerInstance(); } remainingAccounts = [ { pubkey: userKey, isSigner: true, isWritable: true }, // 0 - { pubkey: farm.farmId, isSigner: false, isWritable: true }, // 1 + { pubkey: farm.master.id, isSigner: false, isWritable: true }, // 1 { pubkey: farmerId, isSigner: false, isWritable: true }, // 2 { - pubkey: farm.sgeneMinter, + pubkey: farm.master.sgeneMinter, isSigner: false, isWritable: false, }, // 3 @@ -540,7 +544,7 @@ export class ProtocolGenopets implements IProtocolFarm { isWritable: true, }, // 4 { - pubkey: farm.mintSgene, + pubkey: farm.master.mintSgene, isSigner: false, isWritable: true, }, // 5 diff --git a/tests/testAdapterGenopets.ts b/tests/testAdapterGenopets.ts index 53cfcc2..d0ea6d7 100644 --- a/tests/testAdapterGenopets.ts +++ b/tests/testAdapterGenopets.ts @@ -57,9 +57,7 @@ describe("Gateway", () => { const poolId = new anchor.web3.PublicKey( "Enq8vJucRbkzKA1i1PahJNhMyUTzoVL5Cs8n5rC3NLGn" // GENE-USDC ); - const farmId = new anchor.web3.PublicKey( - "tEAbLeDznDdQ5jvdk1cdm2qUzoYmyc6nX5FChAyAB2U" - ); + const farmId = genopets.getFarmId(mint); const addLiquidityParams: AddLiquidityParams = { protocol: SupportedProtocols.Raydium, poolId, @@ -105,9 +103,7 @@ describe("Gateway", () => { }); it("Unstake + Harvest in Genopets", async () => { - const farmId = new anchor.web3.PublicKey( - "tEAbLeDznDdQ5jvdk1cdm2qUzoYmyc6nX5FChAyAB2U" - ); + const farmId = genopets.getFarmId(mint); const poolId = new anchor.web3.PublicKey( "Enq8vJucRbkzKA1i1PahJNhMyUTzoVL5Cs8n5rC3NLGn" // GENE-USDC ); @@ -133,43 +129,43 @@ describe("Gateway", () => { mint, }; - let depositId: anchor.web3.PublicKey; - for (let deposit of farmer.userDeposit) { - if (deposit?.isYield == true) { - depositId = deposit.depositId; + let farmerInstanceId: anchor.web3.PublicKey; + for (let farmerInstance of farmer.instance) { + if (farmerInstance?.isYield == true) { + farmerInstanceId = farmerInstance.id; } } const harvestParams2: HarvestParams = { protocol: SupportedProtocols.Genopets, farmId, type: HarvestType.completeAsSGene, - farmerKey: depositId!, + farmerKey: farmerInstanceId!, mint, }; - for (let deposit of farmer.userDeposit) { - const timestamp = Number(deposit?.lockUntil); + for (let farmerInstance of farmer.instance) { + const timestamp = Number(farmerInstance?.lockUntil); const currentTimestamp = Number(new Date()) / 1000; if ( - deposit?.isYield == false && - deposit.poolToken.equals(mint) && + farmerInstance?.isYield == false && + farmerInstance.poolToken.equals(mint) && timestamp < currentTimestamp ) { - depositId = deposit.depositId; + farmerInstanceId = farmerInstance.id; } } const harvestParams3: HarvestParams = { protocol: SupportedProtocols.Genopets, farmId, type: HarvestType.completeAsGene, - farmerKey: depositId!, + farmerKey: farmerInstanceId!, mint, }; const unstakeParams: UnstakeParams = { protocol: SupportedProtocols.Genopets, farmId, shareAmount: 1000, // dummy - farmerKey: depositId!, + farmerKey: farmerInstanceId!, mint, }; const removeLiquidityParams: RemoveLiquidityParams = { @@ -208,9 +204,7 @@ describe("Gateway", () => { }); // it("Unstake all in Genopets", async () => { - // const farmId = new anchor.web3.PublicKey( - // "tEAbLeDznDdQ5jvdk1cdm2qUzoYmyc6nX5FChAyAB2U" - // ); + // const farmId = genopets.getFarmId(mint); // const farm = (await genopets.infos.getFarm( // connection, @@ -231,28 +225,28 @@ describe("Gateway", () => { // const harvestParams1: HarvestParams = { // protocol: SupportedProtocols.Genopets, // farmId, - // type: HarvestType.ClaimRewards, + // type: HarvestType.initialize, // mint, // }; // await gateway.harvest(harvestParams1); - // let depositId: anchor.web3.PublicKey; + // let farmerInstanceId: anchor.web3.PublicKey; // let counter = 0; - // for (let deposit of farmer.userDeposit) { - // const timestamp = Number(deposit?.lockUntil); + // for (let farmerInstance of farmer.instance) { + // const timestamp = Number(farmerInstance?.lockUntil); // const currentTimestamp = Number(new Date()) / 1000; // if ( - // deposit?.isYield == false && - // deposit.poolToken.equals(mint) && + // farmerInstance?.isYield == false && + // farmerInstance.poolToken.equals(mint) && // timestamp < currentTimestamp // ) { - // depositId = deposit.depositId; + // farmerInstanceId = farmerInstance.id; // const unstakeParams: UnstakeParams = { // protocol: SupportedProtocols.Genopets, // farmId, // shareAmount: 1000, // dummy - // farmerKey: depositId!, + // farmerKey: farmerInstanceId!, // mint, // }; @@ -282,9 +276,7 @@ describe("Gateway", () => { // }); // it("Harvest all in Genopets", async () => { - // const farmId = new anchor.web3.PublicKey( - // "tEAbLeDznDdQ5jvdk1cdm2qUzoYmyc6nX5FChAyAB2U" - // ); + // const farmId = genopets.getFarmId(mint); // const farm = (await genopets.infos.getFarm( // connection, @@ -302,17 +294,17 @@ describe("Gateway", () => { // const gateway = new GatewayBuilder(provider); - // let depositId: anchor.web3.PublicKey; + // let farmerInstanceId: anchor.web3.PublicKey; // let counter = 0; - // for (let deposit of farmer.userDeposit) { - // if (deposit?.isYield == true) { - // depositId = deposit.depositId; + // for (let farmerInstance of farmer.instance) { + // if (farmerInstance?.isYield == true) { + // farmerInstanceId = farmerInstance.id; // const harvestParams2: HarvestParams = { // protocol: SupportedProtocols.Genopets, // farmId, - // type: HarvestType.WithdrawAsSgene, - // farmerKey: depositId!, + // type: HarvestType.completeAsSGene, + // farmerKey: farmerInstanceId!, // mint, // };