Skip to content

Commit

Permalink
Merge pull request #14 from Bucket-Protocol/dev
Browse files Browse the repository at this point in the history
[WIP] New query methods : getAllBuckets, getUserBottle
  • Loading branch information
andreidev1 authored Dec 11, 2023
2 parents 1031a9d + e25399b commit fc3f393
Show file tree
Hide file tree
Showing 10 changed files with 866 additions and 32 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# bucket-protocol-sdk

## 0.6.0

### Minor Changes

- 898178c: Closes [#13](https://github.com/Bucket-Protocol/bucket-protocol-sdk/issues/13)

## 0.5.0

### Minor Changes
Expand Down
23 changes: 8 additions & 15 deletions package-lock.json

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

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "bucket-protocol-sdk",
"author": "Andrei <andreid.dev@gmail.com>",
"version": "0.5.0",
"version": "0.6.0",
"license": "Apache-2.0",
"description": "Bucket Protocol TypeScript SDK",
"type": "module",
Expand All @@ -22,8 +22,9 @@
"lint:fix": "pnpm run eslint:fix && pnpm run prettier:fix"
},
"dependencies": {
"@mysten/bcs": "^0.7.4",
"@mysten/sui.js": "^0.48.0"
"@mysten/bcs": "^0.9.0",
"@mysten/sui.js": "^0.48.0",
"superstruct": "^1.0.3"
},
"devDependencies": {
"@changesets/cli": "^2.26.2",
Expand Down
13 changes: 5 additions & 8 deletions pnpm-lock.yaml

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

180 changes: 175 additions & 5 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// Copyright Andrei <andreid.dev@gmail.com>

import { DevInspectResults, SuiClient, getFullnodeUrl } from "@mysten/sui.js/client";
import { DevInspectResults, SuiClient, getFullnodeUrl, SuiObjectResponse } from "@mysten/sui.js/client";
import { TransactionBlock } from "@mysten/sui.js/transactions";
import { normalizeSuiAddress, SUI_CLOCK_OBJECT_ID } from "@mysten/sui.js/utils";
import { BCS, getSuiMoveConfig } from "@mysten/bcs"
import { getObjectFields } from "./objects/objectTypes";

import { MAINNET_PACKAGE_ID, TESTNET_PACKAGE_ID } from "./utils/constants";
import { BucketConstants, PaginatedBottleSummary, PackageType} from "./types";

import { MAINNET_PACKAGE_ID, TESTNET_PACKAGE_ID, MARKET_COINS_TYPE_LIST, PROTOCOL_ID } from "./utils/constants";
import { BucketConstants, PaginatedBottleSummary, PackageType, BucketList, BucketResponseResult, BucketTypeInfo, BottleAmountsList, BottleInfoResult} from "./types";

const DUMMY_ADDRESS = normalizeSuiAddress("0x0");

Expand All @@ -31,7 +33,6 @@ export class BucketClient {

this.client = client;
this.packageType = options?.packageType ?? "mainnet";

}

async depositToTank(
Expand Down Expand Up @@ -568,5 +569,174 @@ export class BucketClient {
return bucketObject

}
async getAllBuckets(){
/**
* @description Get all buckets
*/
try {
const protocolFields = await this.client.getDynamicFields({
parentId: PROTOCOL_ID,
});

const bucketList = protocolFields.data.filter((item) =>
item.objectType.includes("Bucket")
);

const objectTypeList = bucketList.map((item) => item.objectType);

const accept_coin_type = Object.values(MARKET_COINS_TYPE_LIST);
const accept_coin_name = Object.keys(MARKET_COINS_TYPE_LIST);
const coinTypeList = objectTypeList.map(
(type) => type.split("<").pop()?.replace(">", "") ?? ""
);

const objectNameList: string[] = [];

coinTypeList.forEach((type) => {
const typeIndex = accept_coin_type.indexOf(type);
const coinName = accept_coin_name[typeIndex];
objectNameList.push(coinName ?? "");
});
const objectIdList = bucketList.map((item) => item.objectId);

const respones: SuiObjectResponse[] = await this.client.multiGetObjects({
ids: objectIdList,
options: {
showContent: true,
showType: true, //Check could we get type from response later
},
});

const bucketInfoList: BucketList = {};

respones.map((res, index) => {
const fields = getObjectFields(res) as BucketTypeInfo;

const bucketInfo: BucketResponseResult = {
baseFeeRate: Number(fields.base_fee_rate ?? 5_000),
bottleTableSize: fields.bottle_table.fields.table.fields.size ?? "",
collateralDecimal: fields.collateral_decimal ?? 0,
collateralVault: fields.collateral_vault ?? "",
latestRedemptionTime: Number(fields.latest_redemption_time ?? 0),
minCollateralRatio: fields.min_collateral_ratio ?? "",
mintedBuckAmount: fields.minted_buck_amount ?? "",
minBottleSize: fields.min_bottle_size ?? "",
recoveryModeThreshold: fields.recovery_mode_threshold ?? "",
};

bucketInfoList[objectNameList[index] ?? ""] = bucketInfo;
});

return bucketInfoList;
} catch (error) {
return {};
}
};

}
async getUserBottle(address: string) {
/**
* @description Get bucket constants (decoded BCS values)
* @address User address that belong to bottle
*/
if (!address) return null;

try {
const protocolFields = await this.client.getDynamicFields({
parentId: PROTOCOL_ID,
});

const bucketList = protocolFields.data.filter((item) =>
item.objectType.includes("Bucket")
);

const objectTypeList = bucketList.map((item) => item.objectType);

const accept_coin_type = Object.values(MARKET_COINS_TYPE_LIST);
const accept_coin_name = Object.keys(MARKET_COINS_TYPE_LIST);

const coinTypeList = objectTypeList.map(
(type) => type.split("<").pop()?.replace(">", "") ?? ""
);

const objectNameList: string[] = [];

coinTypeList.forEach((type) => {
const typeIndex = accept_coin_type.indexOf(type);
const coinName = accept_coin_name[typeIndex];
objectNameList.push(coinName ?? "");
});

const objectIdList = bucketList.map((item) => item.objectId);

const respones: SuiObjectResponse[] = await this.client.multiGetObjects({
ids: objectIdList,
options: {
showContent: true,
showType: true, //Check could we get type from response later
},
});

const bottleIdList: {
name: string;
id: string;
dec: number;
}[] = [];

respones.map((res, index) => {
//Filter out WBTC and WETH
//When we launch WBTC and WETH, we need to remove this exception
if (objectNameList[index] === "WBTC" || objectNameList[index] === "WETH")
return;

const bucketFields = getObjectFields(res) as BucketTypeInfo;

bottleIdList.push({
name: objectNameList[index] ?? "",
id: bucketFields.bottle_table.fields.table.fields.id.id,
dec: bucketFields.collateral_decimal,
});
});

const bottleAmountsList: BottleAmountsList = {};

for (const bottle of bottleIdList) {
await this.client
.getDynamicFieldObject({
parentId: bottle.id ?? "",
name: {
type: "address",
value: address,
},
})
.then((bottleInfo) => {
const bottleInfoFields = getObjectFields(
bottleInfo
) as BottleInfoResult;

if (!bottleInfoFields) {
bottleAmountsList[bottle.name ?? ""] = {
collateralAmount: 0,
buckAmount: 0,
decimals: bottle.dec,
};
} else {
bottleAmountsList[bottle.name ?? ""] = {
collateralAmount:
bottleInfoFields.value.fields.value.fields.collateral_amount,
buckAmount:
bottleInfoFields.value.fields.value.fields.buck_amount,
decimals: bottle.dec,
};
}
})
.catch((error) => {
console.log("error", error);
});
}

return bottleAmountsList;
} catch (error) {
return {};
}
};
}
55 changes: 55 additions & 0 deletions src/objects/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import { type CallArg } from "@mysten/sui.js/bcs";
import type { Infer } from "superstruct";
import {
boolean,
define,
literal,
nullable,
object,
record,
string,
union,
} from "superstruct";

export const ObjectOwner = union([
object({
AddressOwner: string(),
}),
object({
ObjectOwner: string(),
}),
object({
Shared: object({
initial_shared_version: nullable(string()),
}),
}),
literal("Immutable"),
]);
export type ObjectOwner = Infer<typeof ObjectOwner>;

export type SuiJsonValue =
| boolean
| number
| string
| CallArg
| Array<SuiJsonValue>;
export const SuiJsonValue = define<SuiJsonValue>("SuiJsonValue", () => true);

const ProtocolConfigValue = union([
object({ u32: string() }),
object({ u64: string() }),
object({ f64: string() }),
]);
type ProtocolConfigValue = Infer<typeof ProtocolConfigValue>;

export const ProtocolConfig = object({
attributes: record(string(), nullable(ProtocolConfigValue)),
featureFlags: record(string(), boolean()),
maxSupportedProtocolVersion: string(),
minSupportedProtocolVersion: string(),
protocolVersion: string(),
});
export type ProtocolConfig = Infer<typeof ProtocolConfig>;
Loading

0 comments on commit fc3f393

Please sign in to comment.