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: Add SP Api types #424

Merged
merged 1 commit into from
Dec 19, 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/early-kids-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bnb-chain/greenfield-js-sdk': patch
---

feat: Add SP create API types
2 changes: 1 addition & 1 deletion examples/nextjs/src/components/group/info/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const GroupInfo = () => {
console.log('headGroupMember', headGroupMember);
}}
>
get group info
get group member info
</button>
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion examples/nextjs/src/components/group/update/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export const GroupUpdate = () => {
onClick={async () => {
if (!address) return;

const resource = GRNToString(newGroupGRN(address, 'zzxc1'));
const resource = GRNToString(newGroupGRN(address, groupName));

const updateGroupTx = await client.storage.setTag({
operator: address,
Expand Down
6 changes: 5 additions & 1 deletion packages/js-sdk/src/api/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
TxResponse,
} from '..';
import { RpcQueryClient } from '../clients/queryclient';
import { MsgSetTagSDKTypeEIP712 } from '@/messages/greenfield/storage/MsgSetTag';
import { getMsgSetTagSDKTypeEIP712 } from '@/messages/greenfield/storage/MsgSetTag';

export interface IStorage {
params(): Promise<QueryParamsResponse>;
Expand Down Expand Up @@ -108,6 +108,10 @@ export class Storage implements IStorage {
}

public async setTag(msg: MsgSetTag) {
const isTagsEmpty = msg?.tags?.tags?.length === 0;

const MsgSetTagSDKTypeEIP712 = getMsgSetTagSDKTypeEIP712(isTagsEmpty);

return await this.txClient.tx(
MsgSetTagTypeUrl,
msg.operator,
Expand Down
67 changes: 46 additions & 21 deletions packages/js-sdk/src/messages/greenfield/storage/MsgSetTag.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,49 @@
export const MsgSetTagSDKTypeEIP712 = {
import type { EIP712Msg } from '@/messages/utils';
import cloneDeep from 'lodash.clonedeep';

export const getMsgSetTagSDKTypeEIP712 = (isTagsEmpty: boolean) => {
const res: EIP712Msg = cloneDeep(MsgSetTagSDKTypeEIP712);

if (!isTagsEmpty) {
res.Msg1.push({
name: 'tags',
type: 'TypeMsg1Tags',
});

res.TypeMsg1Tags = [
{
name: 'tags',
type: 'TypeMsg1TagsTags[]',
},
];
res.TypeMsg1TagsTags = [
{
name: 'key',
type: 'string',
},
{
name: 'value',
type: 'string',
},
];
} else {
res.Msg1.push({
name: 'tags',
type: 'TypeMsg1Tags',
});

res.TypeMsg1Tags = [
{
name: 'tags',
type: 'string[]',
},
];
}

return res;
};

const MsgSetTagSDKTypeEIP712 = {
Msg1: [
{
name: 'operator',
Expand All @@ -8,29 +53,9 @@ export const MsgSetTagSDKTypeEIP712 = {
name: 'resource',
type: 'string',
},
{
name: 'tags',
type: 'TypeMsg1Tags',
},
{
name: 'type',
type: 'string',
},
],
TypeMsg1Tags: [
{
name: 'tags',
type: 'TypeMsg1TagsTags[]',
},
],
TypeMsg1TagsTags: [
{
name: 'key',
type: 'string',
},
{
name: 'value',
type: 'string',
},
],
};
48 changes: 45 additions & 3 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) {
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) {
GlobalVirtualGroupFamilyId: Number(o.GlobalVirtualGroupFamilyId),
SourceType: Number(o.SourceType),
Visibility: Number(o.Visibility),
Tags: {
Tags: tags,
},
};
}

export function formatObjectInfo(o: ObjectInfo) {
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) {
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) {
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