Skip to content

Commit

Permalink
Merge pull request #41 from aldrin-labs/feature/upgrade-refund-manage…
Browse files Browse the repository at this point in the history
…r-add-more-methods

Feature/upgrade refund manager add more methods
  • Loading branch information
avernikoz authored Mar 21, 2024
2 parents cce316f + 6faba85 commit 16d4f5d
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 1 deletion.
14 changes: 14 additions & 0 deletions examples/refund/get-claim-amount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { RefundManagerSingleton } from "../../src";
import { suiProviderUrl, user } from "../common";

// yarn ts-node examples/refund/get-claim-amount.ts
(async () => {
const refundManager = RefundManagerSingleton.getInstance(suiProviderUrl);

const result = await refundManager.getClaimAmount({
poolObjectId: RefundManagerSingleton.REFUND_POOL_OBJECT_ID,
affectedAddress: user,
});

console.debug("result: ", result);
})();
13 changes: 13 additions & 0 deletions examples/refund/get-unclaimed-addresses-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { RefundManagerSingleton } from "../../src";
import { suiProviderUrl } from "../common";

// yarn ts-node examples/refund/get-unclaimed-addresses-list.ts
(async () => {
const refundManager = RefundManagerSingleton.getInstance(suiProviderUrl);

const result = await refundManager.getUnclaimedAddressesList({
poolObjectId: RefundManagerSingleton.REFUND_POOL_OBJECT_ID,
});

console.debug("result: ", result);
})();
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@avernikoz/rinbot-sui-sdk",
"version": "2.4.8",
"version": "2.4.9",
"description": "Sui Trading Bot SDK (typescript)",
"files": [
"dist"
Expand Down
122 changes: 122 additions & 0 deletions src/managers/refund/RefundManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { verifyPersonalMessage } from "@mysten/sui.js/verify";
import { ObjectArg } from "../../transactions/types";
import { obj } from "../../transactions/utils";
import { hexStringToByteArray } from "./utils";
import BigNumber from "bignumber.js";
import { SUI_DENOMINATOR } from "../..";

/**
* @class RefundManagerSingleton
Expand Down Expand Up @@ -140,6 +142,126 @@ export class RefundManagerSingleton {
return phase;
}

public async getUnclaimedAddressesList({
poolObjectId,
transaction,
}: {
poolObjectId: string;
transaction?: TransactionBlock;
}) {
const tx = transaction ?? new TransactionBlock();

const txRes = tx.moveCall({
target: `${RefundManagerSingleton.REFUND_PACKAGE_ADDRESS}::refund::unclaimed`,
typeArguments: [],
arguments: [obj(tx, poolObjectId)],
});

tx.setGasBudget(RefundManagerSingleton.REFUND_GAS_BUGET);

const res = await this.provider.devInspectTransactionBlock({
sender: RefundManagerSingleton.SIMLATION_ACCOUNT_ADDRESS,
transactionBlock: tx,
});

if (!res.results) {
throw new Error("No results found for the request phase request");
}

const returnValues = res.results[0].returnValues;

if (!returnValues) {
throw new Error("Return values are undefined");
}

const table = returnValues[0][0];

return table;
}

public async getClaimAmountNormal({
poolObjectId,
affectedAddress,
}: {
poolObjectId: string;
affectedAddress: string;
}) {
const tx = new TransactionBlock();

const txRes = tx.moveCall({
target: `${RefundManagerSingleton.REFUND_PACKAGE_ADDRESS}::refund::amount_to_claim`,
typeArguments: [],
arguments: [obj(tx, poolObjectId), tx.pure(affectedAddress)],
});

tx.setGasBudget(RefundManagerSingleton.REFUND_GAS_BUGET);

const res = await this.provider.devInspectTransactionBlock({
sender: RefundManagerSingleton.SIMLATION_ACCOUNT_ADDRESS,
transactionBlock: tx,
});

if (!res.results) {
throw new Error("No results found for the request phase request");
}

const returnValues = res.results[0].returnValues;

if (!returnValues) {
throw new Error("Return values are undefined");
}

const amount = returnValues[0][0][0];

return { mist: amount, sui: new BigNumber(amount).div(SUI_DENOMINATOR).toString() };
}

public async getClaimAmountBoosted({
poolObjectId,
affectedAddress,
}: {
poolObjectId: string;
affectedAddress: string;
}) {
const tx = new TransactionBlock();

const txRes = tx.moveCall({
target: `${RefundManagerSingleton.REFUND_PACKAGE_ADDRESS}::refund::amount_to_claim_boosted`,
typeArguments: [],
arguments: [obj(tx, poolObjectId), tx.pure(affectedAddress)],
});

tx.setGasBudget(RefundManagerSingleton.REFUND_GAS_BUGET);

const res = await this.provider.devInspectTransactionBlock({
sender: RefundManagerSingleton.SIMLATION_ACCOUNT_ADDRESS,
transactionBlock: tx,
});

if (!res.results) {
throw new Error("No results found for the request phase request");
}

const returnValues = res.results[0].returnValues;

if (!returnValues) {
throw new Error("Return values are undefined");
}

const amount = returnValues[0][0][0];

return { mist: amount, sui: new BigNumber(amount).div(SUI_DENOMINATOR).toString() };
}

public async getClaimAmount({ poolObjectId, affectedAddress }: { poolObjectId: string; affectedAddress: string }) {
const [normalRefund, boosedRefund] = await Promise.all([
this.getClaimAmountNormal({ poolObjectId, affectedAddress }),
this.getClaimAmountBoosted({ poolObjectId, affectedAddress }),
]);

return { normalRefund, boosedRefund };
}

public static getAllowBoostedClaim({
publisherObjectId,
poolObjectId,
Expand Down

0 comments on commit 16d4f5d

Please sign in to comment.