Skip to content

Commit

Permalink
Feat/batch upload (#171)
Browse files Browse the repository at this point in the history
* feat: Feegrant

* feat: Feegrant

* Create angry-horses-enjoy.md
  • Loading branch information
rrr523 authored Jul 20, 2023
1 parent d894bad commit e2ebac6
Show file tree
Hide file tree
Showing 9 changed files with 252 additions and 154 deletions.
6 changes: 6 additions & 0 deletions .changeset/angry-horses-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@demo/wallet": patch
"@bnb-chain/greenfield-chain-sdk": patch
---

feat: Feegrant `grantAllowance` API
1 change: 1 addition & 0 deletions examples/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@cosmjs/proto-signing": "^0.29.5",
"@cosmjs/stargate": "^0.29.5",
"@ethersproject/signing-key": "^5.7.0",
"@ethersproject/wallet": "^5.7.0",
"@metamask/eth-sig-util": "^5.0.2",
"@next/font": "13.1.6",
"@rainbow-me/rainbowkit": "^1.0.5",
Expand Down
87 changes: 87 additions & 0 deletions examples/nextjs/src/components/batch/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { client, selectSp } from '@/client';
import { GRNToString, newBucketGRN, PermissionTypes } from '@bnb-chain/greenfield-chain-sdk';
import { Wallet } from '@ethersproject/wallet';
import { useState } from 'react';
import { useAccount, useNetwork } from 'wagmi';

export const FeeGrant = () => {
const { address } = useAccount();
const [bucketName, setBucketName] = useState<string>('');

return (
<>
<h4>Feegrant</h4>
bucket name :
<input
value={bucketName}
placeholder="bucket name"
onChange={(e) => {
setBucketName(e.target.value);
}}
/>
<br />
<button
onClick={async () => {
if (!address) return;

// select sp to be primary sp
const spInfo = await selectSp();
const { primarySpAddress } = spInfo;

/* await client.bucket.createBucket({
bucketName,
creator: address,
visibility: 'VISIBILITY_TYPE_PUBLIC_READ',
chargedReadQuota: '0',
spInfo,
signType: 'authTypeV2',
}); */

const { sequence } = await client.account.getAccount(address);
console.log('sequence', sequence);

const wallet = Wallet.createRandom();
console.log('wallet', wallet, wallet.privateKey);

const grantAllowanceTx = await client.feegrant.grantAllowance({
granter: address,
grantee: wallet.address,
// allowance: ,
});

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

console.log('simulateInfo:', simulateInfo);

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

console.log(res);

// const statement: PermissionTypes.Statement = {
// effect: PermissionTypes.Effect.EFFECT_ALLOW,
// actions: [PermissionTypes.ActionType.ACTION_UPDATE_BUCKET_INFO],
// resources: [GRNToString(newBucketGRN(bucketName))],
// };
// const putPolicyTx = await client.bucket.putBucketPolicy(bucketName, {
// operator: address,
// statements: [statement],
// principal: {
// type: PermissionTypes.PrincipalType.PRINCIPAL_TYPE_GNFD_ACCOUNT,
// value: '0x0000000000000000000000000000000000000001',
// },
// });
}}
>
go batch
</button>
</>
);
};
3 changes: 3 additions & 0 deletions examples/nextjs/src/pages/tx.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { FeeGrant } from '@/components/batch';
import { Bucket } from '@/components/bucket';
import { Deposit } from '@/components/deposit';
import { Group } from '@/components/group';
Expand Down Expand Up @@ -44,6 +45,8 @@ export default function Tx() {
<hr style={{ margin: '10px 0' }} />
<Policy />
<hr style={{ margin: '10px 0' }} />
<FeeGrant />
<hr style={{ margin: '10px 0' }} />
<MultiMsg />
</>
)}
Expand Down
3 changes: 3 additions & 0 deletions packages/chain-sdk/src/api/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ import {
QueryGetPaymentAccountsByOwnerResponse,
} from '@bnb-chain/greenfield-cosmos-types/greenfield/payment/query';
import { MsgCreatePaymentAccount } from '@bnb-chain/greenfield-cosmos-types/greenfield/payment/tx';
import { toBuffer } from '@ethereumjs/util';
import { hexlify } from '@ethersproject/bytes';
import { container, delay, inject, singleton } from 'tsyringe';
import {
Long,
MsgCreatePaymentAccountTypeUrl,
MsgMultiSendTypeUrl,
MsgSendTypeUrl,
Expand Down
11 changes: 10 additions & 1 deletion packages/chain-sdk/src/api/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
import { makeAuthInfoBytes } from '@cosmjs/proto-signing';
import { DeliverTxResponse, StargateClient } from '@cosmjs/stargate';
import { Tendermint37Client } from '@cosmjs/tendermint-rpc';
import { toBuffer } from '@ethereumjs/util';
import { bufferToHex, toBuffer } from '@ethereumjs/util';
import Long from 'long';
import { container, inject, singleton } from 'tsyringe';
import { BroadcastOptions, ISimulateGasFee, MetaTxInfo, SimulateOptions, TxResponse } from '..';
Expand Down Expand Up @@ -366,6 +366,9 @@ export class Basic implements IBasic {
txOption,
);

// console.log('txOption', txOption);
// console.log('msgEIP712', msgEIP712);

let signature,
pubKey = undefined;

Expand All @@ -388,8 +391,14 @@ export class Basic implements IBasic {
messageHash,
});
pubKey = makeCosmsPubKey(pk);

// console.log('messageHash', bufferToHex(messageHash));
// console.log('signature', signature);
// console.log('pubKey', pubKey, bufferToHex(Buffer.from(pubKey.value)));
}

// console.log('eip712', eip712, JSON.stringify(eip712));

const authInfoBytes = this.getAuthInfoBytes({
denom,
sequence: accountInfo.sequence + '',
Expand Down
65 changes: 62 additions & 3 deletions packages/chain-sdk/src/api/feegrant.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { MsgGrantAllowanceSDKTypeEIP712 } from '@/messages/feegrant/MsgGrantAllowance';
import { MsgRevokeAllowanceSDKTypeEIP712 } from '@/messages/feegrant/MsgRevokeAllowance';
import {
AllowedMsgAllowance,
BasicAllowance,
} from '@bnb-chain/greenfield-cosmos-types/cosmos/feegrant/v1beta1/feegrant';
import {
QueryAllowanceRequest,
QueryAllowanceResponse,
Expand All @@ -10,8 +14,16 @@ import {
MsgGrantAllowance,
MsgRevokeAllowance,
} from '@bnb-chain/greenfield-cosmos-types/cosmos/feegrant/v1beta1/tx';
import { Any } from '@bnb-chain/greenfield-cosmos-types/google/protobuf/any';
import { Timestamp } from '@bnb-chain/greenfield-cosmos-types/google/protobuf/timestamp';
import {
base64FromBytes,
bytesFromBase64,
toTimestamp,
} from '@bnb-chain/greenfield-cosmos-types/helpers';
import { toBuffer } from '@ethereumjs/util';
import { container, singleton } from 'tsyringe';
import { MsgGrantAllowanceTypeUrl, MsgRevokeAllowanceTypeUrl, TxResponse } from '..';
import { encodeToHex, MsgGrantAllowanceTypeUrl, MsgRevokeAllowanceTypeUrl, TxResponse } from '..';
import { Basic } from './basic';
import { RpcQueryClient } from './queryclient';

Expand All @@ -31,12 +43,59 @@ export class FeeGrant implements IFeeGrant {
private queryClient: RpcQueryClient = container.resolve(RpcQueryClient);

public async grantAllowance(msg: MsgGrantAllowance) {
const basicAllowance: BasicAllowance = {
spendLimit: [
{
amount: '111',
denom: 'BNB',
},
],
};

const allowedMsgAllowance: AllowedMsgAllowance = {
allowedMessages: ['/greenfield.storage.MsgCreateObject'],
allowance: Any.fromPartial({
typeUrl: '/cosmos.feegrant.v1beta1.BasicAllowance',
value: BasicAllowance.encode(basicAllowance).finish(),
}),
};

const grantAllowance: MsgGrantAllowance = {
...msg,
allowance: Any.fromPartial({
typeUrl: '/cosmos.feegrant.v1beta1.AllowedMsgAllowance',
value: AllowedMsgAllowance.encode(allowedMsgAllowance).finish(),
}),
};

const marshal = {
'@type': '/cosmos.feegrant.v1beta1.AllowedMsgAllowance',
allowed_messages: ['/greenfield.storage.MsgCreateObject'],
allowance: Any.fromPartial({
typeUrl: '/cosmos.feegrant.v1beta1.BasicAllowance',
value: BasicAllowance.encode(basicAllowance).finish(),
}),
// expiration: null,
// spend_limit: [
// {
// amount: '111',
// denom: 'BNB',
// },
// ],
};

return await this.basic.tx(
MsgGrantAllowanceTypeUrl,
msg.granter,
MsgGrantAllowanceSDKTypeEIP712,
MsgGrantAllowance.toSDK(msg),
MsgGrantAllowance.encode(msg).finish(),
{
...MsgGrantAllowance.toSDK(grantAllowance),
allowance: {
type: grantAllowance.allowance?.typeUrl,
value: toBuffer('0x' + encodeToHex(JSON.stringify(marshal))),
},
},
MsgGrantAllowance.encode(grantAllowance).finish(),
);
}

Expand Down
17 changes: 17 additions & 0 deletions packages/chain-sdk/src/utils/allowance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// import { AllowedMsgAllowance } from '@bnb-chain/greenfield-cosmos-types/cosmos/feegrant/v1beta1/feegrant';
// import { Any } from '@bnb-chain/greenfield-cosmos-types/google/protobuf/any';

// const myAny: Any = {
// typeUrl: '/cosmos.feegrant.v1beta1.MsgAllowance',
// // value:
// };

// const x: AllowedMsgAllowance = {
// allowedMessages: [],
// allowance: myAny,
// };

// export const NewAllowedMsgAllowance = (
// allowance: any,
// allowedMsgs: string[],
// ): AllowedMsgAllowance => {};
Loading

0 comments on commit e2ebac6

Please sign in to comment.