Skip to content

Commit 44ea94e

Browse files
authored
Merge pull request #100 from tinymanorg/feat/liquid-stake-sdk
Improve increaseStake method and fix fee calcution
2 parents e443dfa + 8b200b3 commit 44ea94e

File tree

8 files changed

+48
-32
lines changed

8 files changed

+48
-32
lines changed

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/liquid-stake/stAlgoClient.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ declare class TinymanSTAlgoClient extends TinymanBaseClient {
1111
calculateDecreaseStakeFee(accountAddress: string): Promise<number>;
1212
private getUserStateBoxName;
1313
private getApplyRateChangeTxnIfNeeded;
14+
private getUserBoxPaymentTxnIfNeeded;
15+
private getUserBoxPaymentTxn;
1416
private shouldApplyRateChange;
1517
private getApplyRateChangeTxn;
1618
}

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tinymanorg/tinyman-js-sdk",
3-
"version": "4.1.0",
3+
"version": "4.1.1",
44
"description": "Tinyman JS SDK",
55
"author": "Tinyman Core Team",
66
"license": "MIT",

src/folks-lending-pools/constants.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {SupportedNetwork} from "../util/commonTypes";
22

3-
// eslint-disable-next-line no-magic-numbers
43
const SECONDS_IN_YEAR = BigInt(365 * 24 * 60 * 60);
54
const ONE_14_DP = BigInt(1e14);
65
const ONE_16_DP = BigInt(1e16);

src/liquid-stake/stAlgoClient.ts

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ class TinymanSTAlgoClient extends TinymanBaseClient {
3333
const suggestedParams = await this.getSuggestedParams();
3434
const userStateBoxName = this.getUserStateBoxName(userAddress);
3535

36-
let newBox: Record<string, Struct> = {};
37-
3836
const txns = [
3937
...(await this.getApplyRateChangeTxnIfNeeded()),
38+
...(await this.getUserBoxPaymentTxnIfNeeded(userAddress, suggestedParams)),
4039
...(await this.getOptinTxnIfNeeded(userAddress, STALGO_ASSET_ID[this.network])),
4140
algosdk.makeAssetTransferTxnWithSuggestedParamsFromObject({
4241
from: userAddress,
@@ -59,25 +58,12 @@ class TinymanSTAlgoClient extends TinymanBaseClient {
5958
})
6059
];
6160

62-
if (!(await this.boxExists(userStateBoxName))) {
63-
newBox = {
64-
[fromByteArray(userStateBoxName)]: USER_STATE
65-
};
66-
67-
const boxPaymentTxn = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
68-
from: userAddress,
69-
to: this.applicationAddress,
70-
suggestedParams,
71-
amount: this.calculateMinBalance({boxes: newBox})
72-
});
73-
74-
txns.splice(1, 0, boxPaymentTxn);
75-
}
76-
77-
return this.setupTxnFeeAndAssignGroupId({
78-
txns,
79-
additionalFeeCount: 2
80-
});
61+
return Promise.all(
62+
this.setupTxnFeeAndAssignGroupId({
63+
txns,
64+
additionalFeeCount: 2
65+
})
66+
);
8167
}
8268

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

100-
return this.setupTxnFeeAndAssignGroupId({txns, additionalFeeCount: 2});
86+
return Promise.all(this.setupTxnFeeAndAssignGroupId({txns, additionalFeeCount: 2}));
10187
}
10288

10389
async claimRewards(userAddress: string) {
@@ -121,7 +107,7 @@ class TinymanSTAlgoClient extends TinymanBaseClient {
121107
})
122108
];
123109

124-
return this.setupTxnFeeAndAssignGroupId({txns, additionalFeeCount: 3});
110+
return Promise.all(this.setupTxnFeeAndAssignGroupId({txns, additionalFeeCount: 3}));
125111
}
126112

127113
async calculateIncreaseStakeFee(accountAddress: string) {
@@ -133,7 +119,7 @@ class TinymanSTAlgoClient extends TinymanBaseClient {
133119
const doesUserBoxExist = await this.boxExists(
134120
this.getUserStateBoxName(accountAddress)
135121
);
136-
const initialTxnCount = 3;
122+
const initialTxnCount = 4;
137123
const totalTxnCount =
138124
initialTxnCount +
139125
Number(shouldApplyRateChange) +
@@ -168,6 +154,38 @@ class TinymanSTAlgoClient extends TinymanBaseClient {
168154
return [];
169155
}
170156

157+
private async getUserBoxPaymentTxnIfNeeded(
158+
userAddress: string,
159+
suggestedParams: algosdk.SuggestedParams
160+
) {
161+
const userStateBoxName = this.getUserStateBoxName(userAddress);
162+
163+
if (!(await this.boxExists(userStateBoxName))) {
164+
return this.getUserBoxPaymentTxn(userStateBoxName, userAddress, suggestedParams);
165+
}
166+
167+
return [];
168+
}
169+
170+
private getUserBoxPaymentTxn(
171+
userStateBoxName: Uint8Array,
172+
userAddress: string,
173+
suggestedParams: algosdk.SuggestedParams
174+
) {
175+
const newBox: Record<string, Struct> = {
176+
[fromByteArray(userStateBoxName)]: USER_STATE
177+
};
178+
179+
const boxPaymentTxn = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
180+
from: userAddress,
181+
to: this.applicationAddress,
182+
suggestedParams,
183+
amount: this.calculateMinBalance({boxes: newBox})
184+
});
185+
186+
return [boxPaymentTxn];
187+
}
188+
171189
private async shouldApplyRateChange() {
172190
const now = Math.floor(Date.now() / SECOND_IN_MS);
173191

src/util/client/base/baseClient.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ abstract class TinymanBaseClient {
6363
)?.value;
6464

6565
if (searchValue) {
66-
// eslint-disable-next-line no-magic-numbers
6766
if (searchValue.type === 2) {
6867
return searchValue.uint;
6968
}

src/util/util.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ export function convertFromBaseUnits(
178178

179179
return roundNumber(
180180
{decimalPlaces: decimals},
181-
// eslint-disable-next-line no-magic-numbers
182181
Math.pow(10, -decimals) * Number(quantity)
183182
);
184183
}
@@ -190,7 +189,6 @@ export function convertToBaseUnits(
190189
assetDecimals: number | bigint,
191190
quantity: number | bigint
192191
) {
193-
// eslint-disable-next-line no-magic-numbers
194192
const baseAmount = Math.pow(10, Number(assetDecimals)) * Number(quantity);
195193

196194
// 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

0 commit comments

Comments
 (0)