Skip to content

Commit

Permalink
feat: Create Group with tags
Browse files Browse the repository at this point in the history
  • Loading branch information
rrr523 committed Dec 7, 2023
1 parent 04dff41 commit 991211b
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 15 deletions.
59 changes: 52 additions & 7 deletions examples/nextjs/src/components/group/create/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export const CreateGroup = () => {
creator: address,
groupName: createGroupInfo.groupName,
extra: 'extra info',
tags: {
tags: [],
},
});

const simulateInfo = await createGroupTx.simulate({
Expand All @@ -42,23 +45,65 @@ export const CreateGroup = () => {
gasPrice: simulateInfo.gasPrice,
payer: address,
granter: '',
signTypedDataCallback: async (addr: string, message: string) => {
const provider = await connector?.getProvider();
return await provider?.request({
method: 'eth_signTypedData_v4',
params: [addr, message],
});
// signTypedDataCallback: async (addr: string, message: string) => {
// const provider = await connector?.getProvider();
// return await provider?.request({
// method: 'eth_signTypedData_v4',
// params: [addr, message],
// });
// },
});

if (res.code === 0) {
alert('create group success');
}

console.log(res);
}}
>
create group with empty tags
</button>

<button
onClick={async () => {
if (!address) return;

const createGroupTx = await client.group.createGroup({
creator: address,
groupName: createGroupInfo.groupName,
extra: 'extra info',
tags: {
tags: [
{
key: 'key',
value: 'value',
},
],
},
});

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

console.log(simulateInfo);

const res = await createGroupTx.broadcast({
denom: 'BNB',
gasLimit: Number(simulateInfo.gasLimit),
gasPrice: simulateInfo.gasPrice,
payer: address,
granter: '',
});

if (res.code === 0) {
alert('create group success');
}

console.log(res);
}}
>
create group
create group with tags
</button>
</div>
);
Expand Down
18 changes: 11 additions & 7 deletions packages/js-sdk/src/api/group.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
import { TxClient } from '../clients/txClient';
import { MsgCreateGroupSDKTypeEIP712 } from '../messages/greenfield/storage/MsgCreateGroup';
import { MsgDeleteGroupSDKTypeEIP712 } from '../messages/greenfield/storage/MsgDeleteGroup';
import { MsgLeaveGroupSDKTypeEIP712 } from '../messages/greenfield/storage/MsgLeaveGroup';
import { MsgUpdateGroupExtraSDKTypeEIP712 } from '../messages/greenfield/storage/MsgUpdateGroupExtra';
import { getMsgUpdateGroupMemberSDKTypeEIP712 } from '../messages/greenfield/storage/MsgUpdateGroupMember';
import { GRNToString, newBucketGRN, newGroupGRN, newObjectGRN } from '../utils/grn';
import {
QueryGroupNFTResponse,
QueryHeadGroupMemberResponse,
Expand Down Expand Up @@ -34,6 +27,13 @@ import {
TxResponse,
} from '..';
import { RpcQueryClient } from '../clients/queryclient';
import { TxClient } from '../clients/txClient';
import { getMsgCreateGroupSDKTypeWithTagEIP712 } from '../messages/greenfield/storage/MsgCreateGroup';
import { MsgDeleteGroupSDKTypeEIP712 } from '../messages/greenfield/storage/MsgDeleteGroup';
import { MsgLeaveGroupSDKTypeEIP712 } from '../messages/greenfield/storage/MsgLeaveGroup';
import { MsgUpdateGroupExtraSDKTypeEIP712 } from '../messages/greenfield/storage/MsgUpdateGroupExtra';
import { getMsgUpdateGroupMemberSDKTypeEIP712 } from '../messages/greenfield/storage/MsgUpdateGroupMember';
import { GRNToString, newBucketGRN, newGroupGRN, newObjectGRN } from '../utils/grn';
import { Storage } from './storage';

export interface IGroup {
Expand Down Expand Up @@ -111,6 +111,10 @@ export class Group implements IGroup {
private queryClient: RpcQueryClient = container.resolve(RpcQueryClient);

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

const MsgCreateGroupSDKTypeEIP712 = getMsgCreateGroupSDKTypeWithTagEIP712(isTagsEmpty);

return await this.txClient.tx(
MsgCreateGroupTypeUrl,
msg.creator,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export const MsgCreateGroupSDKTypeEIP712 = {
import type { EIP712Msg } from '@/messages/utils';
import cloneDeep from 'lodash.clonedeep';

const MsgCreateGroupSDKTypeEIP712 = {
Msg1: [
{
name: 'creator',
Expand All @@ -18,3 +21,45 @@ export const MsgCreateGroupSDKTypeEIP712 = {
},
],
};

export const getMsgCreateGroupSDKTypeWithTagEIP712 = (isTagsEmpty: boolean) => {
const res: EIP712Msg = cloneDeep(MsgCreateGroupSDKTypeEIP712);

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;
};

0 comments on commit 991211b

Please sign in to comment.