Skip to content

Commit

Permalink
Merge pull request #100 from tinymanorg/feat/liquid-stake-sdk
Browse files Browse the repository at this point in the history
Improve increaseStake method and fix fee calcution
  • Loading branch information
gulcinuras authored Dec 6, 2024
2 parents e443dfa + 8b200b3 commit 44ea94e
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 32 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions dist/liquid-stake/stAlgoClient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ declare class TinymanSTAlgoClient extends TinymanBaseClient {
calculateDecreaseStakeFee(accountAddress: string): Promise<number>;
private getUserStateBoxName;
private getApplyRateChangeTxnIfNeeded;
private getUserBoxPaymentTxnIfNeeded;
private getUserBoxPaymentTxn;
private shouldApplyRateChange;
private getApplyRateChangeTxn;
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tinymanorg/tinyman-js-sdk",
"version": "4.1.0",
"version": "4.1.1",
"description": "Tinyman JS SDK",
"author": "Tinyman Core Team",
"license": "MIT",
Expand Down
1 change: 0 additions & 1 deletion src/folks-lending-pools/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {SupportedNetwork} from "../util/commonTypes";

// eslint-disable-next-line no-magic-numbers
const SECONDS_IN_YEAR = BigInt(365 * 24 * 60 * 60);
const ONE_14_DP = BigInt(1e14);
const ONE_16_DP = BigInt(1e16);
Expand Down
66 changes: 42 additions & 24 deletions src/liquid-stake/stAlgoClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ class TinymanSTAlgoClient extends TinymanBaseClient {
const suggestedParams = await this.getSuggestedParams();
const userStateBoxName = this.getUserStateBoxName(userAddress);

let newBox: Record<string, Struct> = {};

const txns = [
...(await this.getApplyRateChangeTxnIfNeeded()),
...(await this.getUserBoxPaymentTxnIfNeeded(userAddress, suggestedParams)),
...(await this.getOptinTxnIfNeeded(userAddress, STALGO_ASSET_ID[this.network])),
algosdk.makeAssetTransferTxnWithSuggestedParamsFromObject({
from: userAddress,
Expand All @@ -59,25 +58,12 @@ class TinymanSTAlgoClient extends TinymanBaseClient {
})
];

if (!(await this.boxExists(userStateBoxName))) {
newBox = {
[fromByteArray(userStateBoxName)]: USER_STATE
};

const boxPaymentTxn = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
from: userAddress,
to: this.applicationAddress,
suggestedParams,
amount: this.calculateMinBalance({boxes: newBox})
});

txns.splice(1, 0, boxPaymentTxn);
}

return this.setupTxnFeeAndAssignGroupId({
txns,
additionalFeeCount: 2
});
return Promise.all(
this.setupTxnFeeAndAssignGroupId({
txns,
additionalFeeCount: 2
})
);
}

async decreaseStake(amount: number, userAddress: string) {
Expand All @@ -97,7 +83,7 @@ class TinymanSTAlgoClient extends TinymanBaseClient {
})
];

return this.setupTxnFeeAndAssignGroupId({txns, additionalFeeCount: 2});
return Promise.all(this.setupTxnFeeAndAssignGroupId({txns, additionalFeeCount: 2}));
}

async claimRewards(userAddress: string) {
Expand All @@ -121,7 +107,7 @@ class TinymanSTAlgoClient extends TinymanBaseClient {
})
];

return this.setupTxnFeeAndAssignGroupId({txns, additionalFeeCount: 3});
return Promise.all(this.setupTxnFeeAndAssignGroupId({txns, additionalFeeCount: 3}));
}

async calculateIncreaseStakeFee(accountAddress: string) {
Expand All @@ -133,7 +119,7 @@ class TinymanSTAlgoClient extends TinymanBaseClient {
const doesUserBoxExist = await this.boxExists(
this.getUserStateBoxName(accountAddress)
);
const initialTxnCount = 3;
const initialTxnCount = 4;
const totalTxnCount =
initialTxnCount +
Number(shouldApplyRateChange) +
Expand Down Expand Up @@ -168,6 +154,38 @@ class TinymanSTAlgoClient extends TinymanBaseClient {
return [];
}

private async getUserBoxPaymentTxnIfNeeded(
userAddress: string,
suggestedParams: algosdk.SuggestedParams
) {
const userStateBoxName = this.getUserStateBoxName(userAddress);

if (!(await this.boxExists(userStateBoxName))) {
return this.getUserBoxPaymentTxn(userStateBoxName, userAddress, suggestedParams);
}

return [];
}

private getUserBoxPaymentTxn(
userStateBoxName: Uint8Array,
userAddress: string,
suggestedParams: algosdk.SuggestedParams
) {
const newBox: Record<string, Struct> = {
[fromByteArray(userStateBoxName)]: USER_STATE
};

const boxPaymentTxn = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
from: userAddress,
to: this.applicationAddress,
suggestedParams,
amount: this.calculateMinBalance({boxes: newBox})
});

return [boxPaymentTxn];
}

private async shouldApplyRateChange() {
const now = Math.floor(Date.now() / SECOND_IN_MS);

Expand Down
1 change: 0 additions & 1 deletion src/util/client/base/baseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ abstract class TinymanBaseClient {
)?.value;

if (searchValue) {
// eslint-disable-next-line no-magic-numbers
if (searchValue.type === 2) {
return searchValue.uint;
}
Expand Down
2 changes: 0 additions & 2 deletions src/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ export function convertFromBaseUnits(

return roundNumber(
{decimalPlaces: decimals},
// eslint-disable-next-line no-magic-numbers
Math.pow(10, -decimals) * Number(quantity)
);
}
Expand All @@ -190,7 +189,6 @@ export function convertToBaseUnits(
assetDecimals: number | bigint,
quantity: number | bigint
) {
// eslint-disable-next-line no-magic-numbers
const baseAmount = Math.pow(10, Number(assetDecimals)) * Number(quantity);

// make sure the final value is an integer. This prevents this kind of computation errors: 0.0012 * 100000 = 119.99999999999999 and rounds this result into 120
Expand Down

0 comments on commit 44ea94e

Please sign in to comment.