-
Notifications
You must be signed in to change notification settings - Fork 3
Support Genopets #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ghost
wants to merge
27
commits into
master
Choose a base branch
from
support-genopets
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Support Genopets #112
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
02e9223
v0.2.14-test.2
0fb6808
v0.2.14-test.3
384fd2d
v0.2.14-test.4
ae19d1a
v0.2.14-test.5
ea8ea64
v0.2.14-test.6
d4a34fc
wip
6651fb3
v0.2.14-test.7
519edca
Add deposit in farmer
26325cd
Update farmer test
a8fe84e
Add Id to Deposit interface
13d1e18
v0.2.14-test.8
aad5ada
Update getFarmer
a103632
Update test
287ac3c
v0.2.14-test.9
618bc68
Fix layout issue
7d913bf
v0.2.14-test.10
04c60f5
Rename
81d2582
Remove check and add error handling
90fafd4
Rename function
4db1ad4
Remove redundant
a354cc8
Update type
0b2401b
v0.2.14-test.11
9e87238
Rename function
33f5fca
Refactor
b9b1967
v0.2.14-test.12
27d36f6
Rename
897e958
v0.2.14-test.13
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import { PublicKey } from "@solana/web3.js"; | ||
|
|
||
| export const GENOPETS_FARM_PROGRAM_ID = new PublicKey("StaKe9nb7aUjXpjpZ45o6uJBsZxj2BWCDBtjk8LCg2v"); | ||
|
|
||
| // calculate by findProgramAddressSync([Buffer.from("stake-master-seed")], GENOPETS_FARM_PROGRAM_ID) | ||
| export const FARM_MASTER_ID = new PublicKey("tEAbLeDznDdQ5jvdk1cdm2qUzoYmyc6nX5FChAyAB2U"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import { PublicKey } from "@solana/web3.js"; | ||
| import BN from "bn.js"; | ||
| import { IFarmInfo, IFarmerInfo } from "../types"; | ||
|
|
||
| export * from "./ids"; | ||
| export * from "./infos"; | ||
| export * from "./layouts"; | ||
| export * from "./utils"; | ||
|
|
||
| export interface FarmInfo extends IFarmInfo { | ||
| master: FarmMaster; | ||
| poolToken: PublicKey; | ||
| tokenDecimals: BN; | ||
| weight: BN; | ||
| earliestUnlockDate: BN; | ||
| usersLockingWeight: BN; | ||
| poolTokenReserve: BN; | ||
| weightPerToken: BN; | ||
| governanceEligible: boolean; | ||
| } | ||
|
|
||
| export interface FarmMaster { | ||
| id: PublicKey; | ||
| authority: PublicKey; | ||
| sgeneMinter: PublicKey; | ||
| mintSgene: PublicKey; | ||
| geneMint: PublicKey; | ||
| geneRewarder: PublicKey; | ||
| totalGeneRewarded: BN; | ||
| ataGeneRewarder: PublicKey; | ||
| totalGeneAllocated: BN; | ||
| totalWeight: BN; | ||
| startTime: BN; | ||
| endTime: BN; | ||
| epochTime: BN; | ||
| decayFactorPerEpoch: BN; | ||
| initialGenesPerEpoch: BN; | ||
| stakeParams: { | ||
| minStakeDuration: BN; | ||
| maxStakeDuration: BN; | ||
| }; | ||
| pausedState: BN; | ||
| totalRewardWeight: BN; | ||
| accumulatedYieldRewardsPerWeight: BN; | ||
| lastYieldDistribution: BN; | ||
| totalGeneStaked: BN; | ||
| timeFactor: BN; | ||
| } | ||
|
|
||
| export interface FarmerInfo extends IFarmerInfo { | ||
| totalRewardWeight: BN; | ||
| subYieldRewards: BN; | ||
| activeDeposits: BN; | ||
| totalRewards: BN; | ||
| currentDepositIndex: BN; | ||
| instance: FarmerInstance[]; | ||
| } | ||
|
|
||
| export interface FarmerInstance { | ||
| id: PublicKey; | ||
| user: PublicKey; | ||
| amount: BN; | ||
| poolToken: PublicKey; | ||
| rewardWeight: BN; | ||
| depositTimestamp: BN; | ||
| depositMultiplier: BN; | ||
| lockFrom: BN; | ||
| lockUntil: BN; | ||
| isYield: boolean; | ||
| tokenDecimals: BN; | ||
| active: boolean; | ||
| governanceEligible: boolean; | ||
| } | ||
|
|
||
| export const defaultFarmerMaster = { | ||
| id: PublicKey.default, | ||
| authority: PublicKey.default, | ||
| sgeneMinter: PublicKey.default, | ||
| mintSgene: PublicKey.default, | ||
| geneMint: PublicKey.default, | ||
| geneRewarder: PublicKey.default, | ||
| totalGeneRewarded: new BN(0), | ||
| ataGeneRewarder: PublicKey.default, | ||
| totalGeneAllocated: new BN(0), | ||
| totalWeight: new BN(0), | ||
| startTime: new BN(0), | ||
| endTime: new BN(0), | ||
| epochTime: new BN(0), | ||
| decayFactorPerEpoch: new BN(0), | ||
| initialGenesPerEpoch: new BN(0), | ||
| stakeParams: { | ||
| minStakeDuration: new BN(0), | ||
| maxStakeDuration: new BN(0), | ||
| }, | ||
| pausedState: new BN(0), | ||
| totalRewardWeight: new BN(0), | ||
| accumulatedYieldRewardsPerWeight: new BN(0), | ||
| lastYieldDistribution: new BN(0), | ||
| totalGeneStaked: new BN(0), | ||
| timeFactor: new BN(0), | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,246 @@ | ||
| import { Connection, PublicKey, AccountInfo, DataSizeFilter, GetProgramAccountsConfig } from "@solana/web3.js"; | ||
| import { IFarmInfoWrapper, IInstanceFarm } from "../types"; | ||
| import { FARM_MASTER_ID, GENOPETS_FARM_PROGRAM_ID } from "./ids"; | ||
| import { FARMER_INSTANCE_LAYOUT, FARMER_LAYOUT, FARM_MASTER_LAYOUT, FARM_LAYOUT } from "./layouts"; | ||
| import * as types from "."; | ||
| import { getMultipleAccounts } from "../utils"; | ||
| import { getFarmerInstanceKey } from "./utils"; | ||
|
|
||
| let infos: IInstanceFarm; | ||
| infos = class InstanceGenopets { | ||
| static async getAllFarms(connection: Connection): Promise<types.FarmInfo[]> { | ||
| const farmMasterAccount = await connection.getAccountInfo(FARM_MASTER_ID); | ||
| const farmMaster = this._parseFarmMaster(farmMasterAccount?.data!, FARM_MASTER_ID); | ||
|
|
||
| const sizeFilter: DataSizeFilter = { | ||
| dataSize: 250, | ||
| }; | ||
| const filters = [sizeFilter]; | ||
| const config: GetProgramAccountsConfig = { filters: filters }; | ||
| const allFarms = await connection.getProgramAccounts(GENOPETS_FARM_PROGRAM_ID, config); | ||
| const farms = allFarms.map((account) => { | ||
| let farm = this.parseFarm(account.account.data, account.pubkey); | ||
| farm.master = farmMaster; | ||
| return farm; | ||
| }); | ||
|
|
||
| return farms; | ||
| } | ||
|
|
||
| static async getAllFarmWrappers(connection: Connection): Promise<types.FarmInfoWrapper[]> { | ||
| return (await this.getAllFarms(connection)).map((farmInfo) => new FarmInfoWrapper(farmInfo)); | ||
| } | ||
|
|
||
| static async getFarm(connection: Connection, farmId: PublicKey): Promise<types.FarmInfo> { | ||
| const [farmMasterAccount, farmAccount] = await getMultipleAccounts(connection, [FARM_MASTER_ID, farmId]); | ||
| if (!farmMasterAccount || !farmAccount) throw "Error: Failed to get farm"; | ||
| const farmMaster = this._parseFarmMaster(farmMasterAccount.account?.data!, FARM_MASTER_ID); | ||
| let farm = this.parseFarm(farmAccount.account?.data!, farmId); | ||
| farm.master = farmMaster; | ||
|
|
||
| return farm; | ||
| } | ||
|
|
||
| static async getFarmWrapper(connection: Connection, farmId: PublicKey): Promise<FarmInfoWrapper> { | ||
| const farm = await this.getFarm(connection, farmId); | ||
|
|
||
| return new FarmInfoWrapper(farm); | ||
| } | ||
|
|
||
| static parseFarm(data: Buffer, farmId: PublicKey): types.FarmInfo { | ||
| const decodedData = FARM_LAYOUT.decode(data); | ||
|
|
||
| let { | ||
| poolToken, | ||
| tokenDecimals, | ||
| weight, | ||
| earliestUnlockDate, | ||
| usersLockingWeight, | ||
| poolTokenReserve, | ||
| weightPerToken, | ||
| governanceEligible, | ||
| } = decodedData; | ||
|
|
||
| return { | ||
| farmId, | ||
| master: types.defaultFarmerMaster, | ||
| poolToken, | ||
| tokenDecimals, | ||
| weight, | ||
| earliestUnlockDate, | ||
| usersLockingWeight, | ||
| poolTokenReserve, | ||
| weightPerToken, | ||
| governanceEligible, | ||
| }; | ||
| } | ||
|
|
||
| private static _parseFarmMaster(data: Buffer, id: PublicKey): types.FarmMaster { | ||
| const decodedData = FARM_MASTER_LAYOUT.decode(data); | ||
|
|
||
| let { | ||
| authority, | ||
| sgeneMinter, | ||
| mintSgene, | ||
| geneMint, | ||
| geneRewarder, | ||
| totalGeneRewarded, | ||
| ataGeneRewarder, | ||
| totalGeneAllocated, | ||
| totalWeight, | ||
| startTime, | ||
| endTime, | ||
| epochTime, | ||
| decayFactorPerEpoch, | ||
| initialGenesPerEpoch, | ||
| stakeParams, | ||
| pausedState, | ||
| totalRewardWeight, | ||
| accumulatedYieldRewardsPerWeight, | ||
| lastYieldDistribution, | ||
| totalGeneStaked, | ||
| timeFactor, | ||
| } = decodedData; | ||
|
|
||
| return { | ||
| id, | ||
| authority, | ||
| sgeneMinter, | ||
| mintSgene, | ||
| geneMint, | ||
| geneRewarder, | ||
| totalGeneRewarded, | ||
| ataGeneRewarder, | ||
| totalGeneAllocated, | ||
| totalWeight, | ||
| startTime, | ||
| endTime, | ||
| epochTime, | ||
| decayFactorPerEpoch, | ||
| initialGenesPerEpoch, | ||
| stakeParams, | ||
| pausedState, | ||
| totalRewardWeight, | ||
| accumulatedYieldRewardsPerWeight, | ||
| lastYieldDistribution, | ||
| totalGeneStaked, | ||
| timeFactor, | ||
| }; | ||
| } | ||
|
|
||
| static async getAllFarmers(connection: Connection, userKey: PublicKey): Promise<types.FarmerInfo[]> { | ||
| const farmerId = PublicKey.findProgramAddressSync( | ||
| [Buffer.from("staker-seed"), userKey.toBuffer()], | ||
| GENOPETS_FARM_PROGRAM_ID | ||
| )[0]; | ||
| const farmer = await this.getFarmer(connection, farmerId); | ||
|
|
||
| return [farmer]; | ||
| } | ||
|
|
||
| static async getFarmerId(farmInfo: types.FarmInfo, userKey: PublicKey): Promise<PublicKey> { | ||
| const [farmerId, _] = PublicKey.findProgramAddressSync( | ||
| [Buffer.from("staker-seed"), userKey.toBuffer()], | ||
| GENOPETS_FARM_PROGRAM_ID | ||
| ); | ||
|
|
||
| return farmerId; | ||
| } | ||
|
|
||
| static async getFarmer(connection: Connection, farmerId: PublicKey, version?: number): Promise<types.FarmerInfo> { | ||
| let data = (await connection.getAccountInfo(farmerId)) as AccountInfo<Buffer>; | ||
| let farmer = this.parseFarmer(data.data, farmerId); | ||
| const farmerInstanceKeys: PublicKey[] = []; | ||
| for (let i = 0; i < Number(farmer.currentDepositIndex); i++) { | ||
| farmerInstanceKeys.push(getFarmerInstanceKey(farmer.userKey, i)); | ||
| } | ||
| const farmerInstanceAccounts = await getMultipleAccounts(connection, farmerInstanceKeys); | ||
| farmer.instance = farmerInstanceAccounts | ||
| .map((farmerInstance) => { | ||
| return farmerInstance.account?.data | ||
| ? this._parseFarmerInstance(farmerInstance.account?.data, farmerInstance.pubkey) | ||
| : null; | ||
| }) | ||
| .filter((farmerInstance) => Boolean(farmerInstance)) as types.FarmerInstance[]; | ||
|
|
||
| return farmer; | ||
| } | ||
|
|
||
| static parseFarmer(data: Buffer, farmerId: PublicKey): types.FarmerInfo { | ||
| let decodedData = FARMER_LAYOUT.decode(data); | ||
| let { owner, totalRewardWeight, subYieldRewards, activeDeposits, totalRewards, currentDepositIndex } = decodedData; | ||
|
|
||
| return { | ||
| farmerId, | ||
| userKey: new PublicKey(owner), | ||
| totalRewardWeight, | ||
| subYieldRewards, | ||
| activeDeposits, | ||
| totalRewards, | ||
| currentDepositIndex, | ||
| instance: [], | ||
| }; | ||
| } | ||
|
|
||
| private static _parseFarmerInstance(data: Buffer, depositId: PublicKey): types.FarmerInstance { | ||
| let decodedData = FARMER_INSTANCE_LAYOUT.decode(data); | ||
| let { | ||
| user, | ||
| amount, | ||
| poolToken, | ||
| rewardWeight, | ||
| depositTimestamp, | ||
| depositMultiplier, | ||
| lockFrom, | ||
| lockUntil, | ||
| isYield, | ||
| tokenDecimals, | ||
| active, | ||
| governanceEligible, | ||
| } = decodedData; | ||
|
|
||
| return { | ||
| id: depositId, | ||
| user, | ||
| amount, | ||
| poolToken, | ||
| rewardWeight, | ||
| depositTimestamp, | ||
| depositMultiplier, | ||
| lockFrom, | ||
| lockUntil, | ||
| isYield, | ||
| tokenDecimals, | ||
| active, | ||
| governanceEligible, | ||
| }; | ||
| } | ||
| }; | ||
|
|
||
| export { infos }; | ||
|
|
||
| export class FarmInfoWrapper implements IFarmInfoWrapper { | ||
| constructor(public farmInfo: types.FarmInfo) {} | ||
|
|
||
| getStakedAmount(): number { | ||
| // TODO | ||
| return 0; | ||
| } | ||
|
|
||
| getAprs(_x: number, _y: number, _z: number): number[] { | ||
| // TODO | ||
| return []; | ||
| } | ||
| } | ||
|
|
||
| export class FarmerInfoWrapper { | ||
| constructor(public farmerInfo: types.FarmerInfo) {} | ||
|
|
||
| getFarmerInstance() { | ||
| return getFarmerInstanceKey(this.farmerInfo.userKey, Number(this.farmerInfo.currentDepositIndex)); | ||
| } | ||
|
|
||
| getLatestFarmerInstance() { | ||
| return getFarmerInstanceKey(this.farmerInfo.userKey, Number(this.farmerInfo.currentDepositIndex) + 1); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.