Skip to content

Commit

Permalink
feat: expose get binded obligation and get binded vesca for borrow in…
Browse files Browse the repository at this point in the history
…centive
  • Loading branch information
fum-is-chum committed May 10, 2024
1 parent c03c516 commit e0a4365
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 55 deletions.
61 changes: 6 additions & 55 deletions src/builders/borrowIncentiveBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { TransactionBlock } from '@mysten/sui.js/transactions';
import { SUI_CLOCK_OBJECT_ID } from '@mysten/sui.js/utils';
import { SuiTxBlock as SuiKitTxBlock } from '@scallop-io/sui-kit';
import { borrowIncentiveRewardCoins } from '../constants/enum';
import { getObligations, getObligationLocked } from '../queries';
import {
getObligations,
getObligationLocked,
getBindedObligationId,
} from '../queries';
import { requireSender } from '../utils';
import type { SuiObjectArg } from '@scallop-io/sui-kit';
import type { ScallopBuilder } from '../models';
Expand Down Expand Up @@ -75,59 +79,6 @@ const requireObligationInfo = async (
};
};

/**
* Check veSca bind status
* @param query
* @param veScaKey
* @returns
*/
export const getBindedObligationId = async (
builder: ScallopBuilder,
veScaKey: string
) => {
const borrowIncentiveObjectId = builder.address.get('borrowIncentive.object');
const incentivePoolsId = builder.address.get(
'borrowIncentive.incentivePools'
);
const veScaPkgId = IS_VE_SCA_TEST
? '0xb220d034bdf335d77ae5bfbf6daf059c2cc7a1f719b12bfed75d1736fac038c8'
: builder.address.get('vesca.id');

const client = builder.suiKit.client();

// get incentive pools
const incentivePoolsResponse = await client.getObject({
id: incentivePoolsId,
options: {
showContent: true,
},
});

if (incentivePoolsResponse.data?.content?.dataType !== 'moveObject')
return false;
const incentivePoolFields = incentivePoolsResponse.data.content.fields as any;
const veScaBindTableId = incentivePoolFields.ve_sca_bind.fields.id
.id as string;

// check if veSca is inside the bind table
const keyType = `${borrowIncentiveObjectId}::typed_id::TypedID<${veScaPkgId}::ve_sca::VeScaKey>`;
const veScaBindTableResponse = await client.getDynamicFieldObject({
parentId: veScaBindTableId,
name: {
type: keyType,
value: veScaKey,
},
});

if (veScaBindTableResponse.data?.content?.dataType !== 'moveObject')
return false;
const veScaBindTableFields = veScaBindTableResponse.data.content
.fields as any;
// get obligationId pair
const obligationId = veScaBindTableFields.value.fields.id as string;

return obligationId;
};
/**
* Generate borrow incentive normal methods.
*
Expand Down Expand Up @@ -322,7 +273,7 @@ const generateBorrowIncentiveQuickMethod: GenerateBorrowIncentiveQuickMethod =
const veSca = await requireVeSca(builder, txBlock, veScaKey);
if (veSca) {
const bindedObligationId = await getBindedObligationId(
builder,
builder.query,
veSca.keyId
);
// if bindedObligationId is equal to obligationId, then use it again
Expand Down
20 changes: 20 additions & 0 deletions src/models/scallopQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import {
getObligationAccounts,
getObligationAccount,
getTotalValueLocked,
getBindedObligationId,
getBindedVeScaKey,
} from '../queries';
import {
ScallopQueryParams,
Expand Down Expand Up @@ -512,4 +514,22 @@ export class ScallopQuery {
public async getTvl(indexer: boolean = false) {
return await getTotalValueLocked(this, indexer);
}

/**
* Get binded obligationId from a veScaKey if it exists.
* @param veScaKey
* @returns obligationId
*/
public async getBindedObligationId(veScaKey: string) {
return await getBindedObligationId(this, veScaKey);
}

/**
* Get binded veSCA key from a obligationId if it exists.
* @param obligationId
* @returns veScaKey
*/
public async getBindedVeScaKey(obligationId: string) {
return await getBindedVeScaKey(this, obligationId);
}
}
95 changes: 95 additions & 0 deletions src/queries/borrowIncentiveQuery.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { normalizeStructTag } from '@mysten/sui.js/utils';
import { SuiTxBlock as SuiKitTxBlock } from '@scallop-io/sui-kit';
import {
IS_VE_SCA_TEST,
SUPPORT_BORROW_INCENTIVE_POOLS,
SUPPORT_BORROW_INCENTIVE_REWARDS,
} from '../constants';
Expand Down Expand Up @@ -197,3 +198,97 @@ export const queryBorrowIncentiveAccounts = async (

return borrowIncentiveAccounts;
};

/**
* Check veSca bind status
* @param query
* @param veScaKey
* @returns
*/
export const getBindedObligationId = async (
query: ScallopQuery,
veScaKey: string
): Promise<string | null> => {
const borrowIncentiveObjectId = query.address.get('borrowIncentive.object');
const incentivePoolsId = query.address.get('borrowIncentive.incentivePools');
const veScaPkgId = IS_VE_SCA_TEST
? '0xb220d034bdf335d77ae5bfbf6daf059c2cc7a1f719b12bfed75d1736fac038c8'
: query.address.get('vesca.id');

const client = query.suiKit.client();

// get incentive pools
const incentivePoolsResponse = await client.getObject({
id: incentivePoolsId,
options: {
showContent: true,
},
});

if (incentivePoolsResponse.data?.content?.dataType !== 'moveObject')
return null;
const incentivePoolFields = incentivePoolsResponse.data.content.fields as any;
const veScaBindTableId = incentivePoolFields.ve_sca_bind.fields.id
.id as string;

// check if veSca is inside the bind table
const keyType = `${borrowIncentiveObjectId}::typed_id::TypedID<${veScaPkgId}::ve_sca::VeScaKey>`;
const veScaBindTableResponse = await client.getDynamicFieldObject({
parentId: veScaBindTableId,
name: {
type: keyType,
value: veScaKey,
},
});

if (veScaBindTableResponse.data?.content?.dataType !== 'moveObject')
return null;
const veScaBindTableFields = veScaBindTableResponse.data.content
.fields as any;
// get obligationId pair
const obligationId = veScaBindTableFields.value.fields.id as string;

return obligationId;
};

export const getBindedVeScaKey = async (
query: ScallopQuery,
obliationId: string
): Promise<string | null> => {
const borrowIncentiveObjectId = query.address.get('borrowIncentive.object');
const incentiveAccountsId = query.address.get(
'borrowIncentive.incentiveAccounts'
);
const corePkg = query.address.get('core.object');
const client = query.suiKit.client();

// get IncentiveAccounts object
const incentiveAccountsObject = await client.getObject({
id: incentiveAccountsId,
options: {
showContent: true,
},
});
if (incentiveAccountsObject.data?.content?.dataType !== 'moveObject')
return null;
const incentiveAccountsTableId = (
incentiveAccountsObject.data.content.fields as any
).accounts.fields.id.id;

// Search in the table
const bindedIncentiveAcc = await client.getDynamicFieldObject({
parentId: incentiveAccountsTableId,
name: {
type: `${borrowIncentiveObjectId}::typed_id::TypedID<${corePkg}::obligation::Obligation>`,
value: obliationId,
},
});

if (bindedIncentiveAcc.data?.content?.dataType !== 'moveObject') return null;
const bindedIncentiveAccFields = bindedIncentiveAcc.data.content
.fields as any;

return (
bindedIncentiveAccFields.value.fields.binded_ve_sca_key?.fields.id ?? null
);
};

0 comments on commit e0a4365

Please sign in to comment.