Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Sp Api types #464

Merged
merged 1 commit into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chatty-steaks-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bnb-chain/greenfield-js-sdk': patch
---

feat: Sp Api Types
25 changes: 23 additions & 2 deletions examples/nextjs/src/components/bucket/create/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { client, selectSp } from '@/client';
import { getOffchainAuthKeys } from '@/utils/offchainAuth';
import { GRNToString, newBucketGRN, newGroupGRN } from '@bnb-chain/greenfield-js-sdk';
import { add } from 'lodash';
import { useState } from 'react';
import { useAccount } from 'wagmi';

Expand Down Expand Up @@ -58,13 +60,32 @@ export const CreateBucket = () => {
},
);

const simulateInfo = await createBucketTx.simulate({
const setTagTx = await client.storage.setTag({
operator: address,
resource: GRNToString(newBucketGRN(createBucketInfo.bucketName)),
tags: {
tags: [
{
key: 'x',
value: 'xx',
},
{
key: 'y',
value: 'yy',
},
],
},
});

const tx = await client.txClient.multiTx([createBucketTx, setTagTx]);

const simulateInfo = await tx.simulate({
denom: 'BNB',
});

console.log('simulateInfo', simulateInfo);

const res = await createBucketTx.broadcast({
const res = await tx.broadcast({
denom: 'BNB',
gasLimit: Number(simulateInfo?.gasLimit),
gasPrice: simulateInfo?.gasPrice || '5000000000',
Expand Down
42 changes: 42 additions & 0 deletions packages/js-sdk/src/types/sp/Common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ export interface BucketInfo {
PaymentAddress: string;
SourceType: number;
Visibility: number;
Tags: {
Tags: {
Key: string;
Value: string;
}[];
};
}

export interface StreamRecord {
Expand Down Expand Up @@ -85,9 +91,20 @@ export interface ObjectInfo {
RedundancyType: number;
SourceType: number;
Visibility: number;
Tags: {
Tags: {
Key: string;
Value: string;
}[];
};
}

export function formatBucketInfo(o: BucketInfo): BucketInfo {
let tags = o.Tags.Tags || [];
if (!Array.isArray(tags)) {
tags = [tags];
}

return {
...o,
// PrimarySpId: Number(item.BucketInfo.PrimarySpId),
Expand All @@ -97,10 +114,18 @@ export function formatBucketInfo(o: BucketInfo): BucketInfo {
GlobalVirtualGroupFamilyId: Number(o.GlobalVirtualGroupFamilyId),
SourceType: Number(o.SourceType),
Visibility: Number(o.Visibility),
Tags: {
Tags: tags,
},
};
}

export function formatObjectInfo(o: ObjectInfo): ObjectInfo {
let tags = o.Tags.Tags || [];
if (!Array.isArray(tags)) {
tags = [tags];
}

return {
...o,
CreateAt: Number(o.CreateAt),
Expand All @@ -111,6 +136,9 @@ export function formatObjectInfo(o: ObjectInfo): ObjectInfo {
RedundancyType: Number(o.RedundancyType),
SourceType: Number(o.SourceType),
Visibility: Number(o.Visibility),
Tags: {
Tags: tags,
},
};
}

Expand Down Expand Up @@ -140,13 +168,27 @@ export interface GroupInfo {
SourceType: number;
Id: number;
Extra: string;
Tags: {
Tags: {
Key: string;
Value: string;
}[];
};
}

export function formatGroupInfo(o: GroupInfo): GroupInfo {
let tags = o.Tags.Tags || [];
if (!Array.isArray(tags)) {
tags = [tags];
}

return {
...o,
SourceType: Number(o.SourceType),
Id: Number(o.Id),
Tags: {
Tags: tags,
},
};
}

Expand Down
Loading