Skip to content

Commit

Permalink
feat: Custom Tx support any type! (#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
rrr523 authored Oct 5, 2023
1 parent 512181d commit 76a18f1
Show file tree
Hide file tree
Showing 14 changed files with 665 additions and 59 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-pots-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bnb-chain/greenfield-js-sdk': patch
---

feat: Custom Tx support Any Type!
4 changes: 4 additions & 0 deletions examples/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
"lint": "next lint"
},
"dependencies": {
"@bnb-chain/greenfield-cosmos-types": "0.4.0-alpha.23",
"@bnb-chain/greenfield-js-sdk": "workspace:*",
"@bnb-chain/greenfiled-file-handle": "workspace:*",
"@cosmjs/encoding": "^0.29.5",
"@cosmjs/proto-signing": "^0.29.5",
"@cosmjs/stargate": "^0.29.5",
"@ethersproject/bytes": "5.7.0",
"@ethersproject/signing-key": "^5.7.0",
"@ethersproject/wallet": "^5.7.0",
"@metamask/eth-sig-util": "^5.0.2",
Expand All @@ -29,6 +31,7 @@
"eslint": "^8.21.0",
"eslint-config-next": "13.1.6",
"evmosjs": "^0.2.17",
"lodash": "^4.17.21",
"long": "^5.2.1",
"next": "13.1.6",
"react": "18.2.0",
Expand All @@ -38,6 +41,7 @@
"wagmi": "^1.2.0"
},
"devDependencies": {
"@types/lodash": "^4.14.199",
"cross-env": "^7.0.3",
"dotenv": "^16.0.3"
}
Expand Down
183 changes: 183 additions & 0 deletions examples/nextjs/src/components/customtx/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,189 @@ export const CustomTx = () => {
>
broadcast with simulate
</button>

<br />

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

const tx = await client.txClient.txRaw({
address,
eip712MsgType: {
Msg1: [
{
name: 'grant',
type: 'TypeMsg1Grant',
},
{
name: 'grantee',
type: 'string',
},
{
name: 'granter',
type: 'string',
},
{
name: 'type',
type: 'string',
},
],
Msg2: [
{
name: 'initial_deposit',
type: 'TypeMsg2InitialDeposit[]',
},
{
name: 'messages',
type: 'TypeAny[]',
},
{
name: 'metadata',
type: 'string',
},
{
name: 'proposer',
type: 'string',
},
{
name: 'summary',
type: 'string',
},
{
name: 'title',
type: 'string',
},
{
name: 'type',
type: 'string',
},
],
TypeAny: [
{
name: 'type',
type: 'string',
},
{
name: 'value',
type: 'bytes',
},
],
TypeMsg1Grant: [
{
name: 'authorization',
type: 'TypeAny',
},
{
name: 'expiration',
type: 'string',
},
],
TypeMsg2InitialDeposit: [
{
name: 'amount',
type: 'string',
},
{
name: 'denom',
type: 'string',
},
],
Tx: [
{
name: 'account_number',
type: 'uint256',
},
{
name: 'chain_id',
type: 'uint256',
},
{
name: 'fee',
type: 'Fee',
},
{
name: 'memo',
type: 'string',
},
{
name: 'msg1',
type: 'Msg1',
},
{
name: 'msg2',
type: 'Msg2',
},
{
name: 'sequence',
type: 'uint256',
},
{
name: 'timeout_height',
type: 'uint256',
},
],
},
msgData: {
msg1: {
grant: {
authorization: {
type: '/cosmos.staking.v1beta1.StakeAuthorization',
value:
'ZXlKQWRIbHdaU0k2SWk5amIzTnRiM011YzNSaGEybHVaeTUyTVdKbGRHRXhMbE4wWVd0bFFYVjBhRzl5YVhwaGRHbHZiaUlzSW1Gc2JHOTNYMnhwYzNRaU9uc2lZV1JrY21WemN5STZXeUl3ZURCaFFUVXhOekJET0RVMFFVRXdPVE5sTVdNek1rWXhOVEk0TldSRk5FSm1OMlkyT0RBeVEyVWlYWDBzSW1GMWRHaHZjbWw2WVhScGIyNWZkSGx3WlNJNklrRlZWRWhQVWtsYVFWUkpUMDVmVkZsUVJWOUVSVXhGUjBGVVJTSXNJbTFoZUY5MGIydGxibk1pT25zaVlXMXZkVzUwSWpvaU1UQXdNREF3TURBd01EQXdNREF3TURBd01EQXdNQ0lzSW1SbGJtOXRJam9pUWs1Q0luMTk=',
},
expiration: '',
},
grantee: '0x7b5Fe22B5446f7C62Ea27B8BD71CeF94e03f3dF2',
granter: '0x1C893441AB6c1A75E01887087ea508bE8e07AAae',
type: '/cosmos.authz.v1beta1.MsgGrant',
},
msg2: {
initial_deposit: [
{
amount: '1000000000000000000',
denom: 'BNB',
},
],
messages: [
{
type: '/cosmos.staking.v1beta1.MsgCreateValidator',
value:
'eyJAdHlwZSI6Ii9jb3Ntb3Muc3Rha2luZy52MWJldGExLk1zZ0NyZWF0ZVZhbGlkYXRvciIsImJsc19rZXkiOiI4NmJmMWZlZTZjYjkwOGNmMjgzNjM2ZjVhMDk2NjBlNDlhMzk0ZjNiYzU4NTJjMWI4MjM4MTVhNjYzMzI3MGQzNTdhMzcwYjlkZmEwMGFlNGQ5MmFjYzNlNTAxM2ZlZDciLCJibHNfcHJvb2YiOiI4Y2YwYzQzZjEyNWJlNzc3MDI2NGFkMDMwYjhmMDg1NGY0MWYzNDVhMDg3ZjNjMWI4NjYwOWNiYmZmNzFkOTFjOTg4MzhiZWJhYzRkMGZhODA3YTZhZjYyNzU2NzQzNzIxNDRmZDZiYzcxYmRkMGY0MTQ4YWMwZTRmOWM3Y2Q3NDdhMDc4NzUzNTEzMWUxYTIwZGY3YmVlOGUyMmQ0NzE5ZmI4ZThlODVlYjBiMjRkNzkzODhjODA3NzZlNjQ2MWEiLCJjaGFsbGVuZ2VyX2FkZHJlc3MiOiIweDIxMjNCNjA3ZTFiOUU4QWU2NUZiRTEyNTg1QzFiRTY4MzhCYjMyQzciLCJjb21taXNzaW9uIjp7Im1heF9jaGFuZ2VfcmF0ZSI6IjAuMDEwMDAwMDAwMDAwMDAwMDAwIiwibWF4X3JhdGUiOiIxLjAwMDAwMDAwMDAwMDAwMDAwMCIsInJhdGUiOiIwLjA3MDAwMDAwMDAwMDAwMDAwMCJ9LCJkZWxlZ2F0b3JfYWRkcmVzcyI6IjB4MUM4OTM0NDFBQjZjMUE3NUUwMTg4NzA4N2VhNTA4YkU4ZTA3QUFhZSIsImRlc2NyaXB0aW9uIjp7ImRldGFpbHMiOiIiLCJpZGVudGl0eSI6IiIsIm1vbmlrZXIiOiJuYXNoIiwic2VjdXJpdHlfY29udGFjdCI6IiIsIndlYnNpdGUiOiIifSwiZnJvbSI6IjB4N2I1RmUyMkI1NDQ2ZjdDNjJFYTI3QjhCRDcxQ2VGOTRlMDNmM2RGMiIsIm1pbl9zZWxmX2RlbGVnYXRpb24iOiIxMDAwMDAwMDAwMDAwMDAwMDAwMDAwIiwicHVia2V5Ijp7IkB0eXBlIjoiL2Nvc21vcy5jcnlwdG8uZWQyNTUxOS5QdWJLZXkiLCJrZXkiOiIwY3NmL01zc2ZHT1FCcW1qazY5a0lURzBTT052OVgwSXdWWHQ1R3pzUEF3PSJ9LCJyZWxheWVyX2FkZHJlc3MiOiIweDIxMjNCNjA3ZTFiOUU4QWU2NUZiRTEyNTg1QzFiRTY4MzhCYjMyQzciLCJ2YWxpZGF0b3JfYWRkcmVzcyI6IjB4MGFBNTE3MEM4NTRBQTA5M2UxYzMyRjE1Mjg1ZEU0QmY3ZjY4MDJDZSIsInZhbHVlIjp7ImFtb3VudCI6IjEwMDAwMDAwMDAwMDAwMDAwMDAwMDAiLCJkZW5vbSI6IkJOQiJ9fQ==',
},
],
metadata: '',
proposer: '0x1C893441AB6c1A75E01887087ea508bE8e07AAae',
summary: 'create nash validator',
title: 'Create nash Validator',
type: '/cosmos.gov.v1.MsgSubmitProposal',
},
},
txRawHex:
'0x0a91090afc010a1e2f636f736d6f732e617574687a2e763162657461312e4d73674772616e7412d9010a2a307831433839333434314142366331413735453031383837303837656135303862453865303741416165122a3078376235466532324235343436663743363245613237423842443731436546393465303366336446321a7f0a7d0a2a2f636f736d6f732e7374616b696e672e763162657461312e5374616b65417574686f72697a6174696f6e124f0a1d0a03424e42121631303030303030303030303030303030303030303030122c0a2a30783061413531373043383534414130393365316333324631353238356445344266376636383032436520010a8f070a202f636f736d6f732e676f762e76312e4d73675375626d697450726f706f73616c12ea060af1050a2a2f636f736d6f732e7374616b696e672e763162657461312e4d736743726561746556616c696461746f7212c2050a060a046e617368123b0a1137303030303030303030303030303030301213313030303030303030303030303030303030301a1131303030303030303030303030303030301a1631303030303030303030303030303030303030303030222a3078314338393334343141423663314137354530313838373038376561353038624538653037414161652a2a30783061413531373043383534414130393365316333324631353238356445344266376636383032436532430a1d2f636f736d6f732e63727970746f2e656432353531392e5075624b657912220a20d1cb1ffccb2c7c639006a9a393af642131b448e36ff57d08c155ede46cec3c0c3a1d0a03424e42121631303030303030303030303030303030303030303030422a3078376235466532324235343436663743363245613237423842443731436546393465303366336446324a2a307832313233423630376531623945384165363546624531323538354331624536383338426233324337522a3078323132334236303765316239453841653635466245313235383543316245363833384262333243375a6038366266316665653663623930386366323833363336663561303936363065343961333934663362633538353263316238323338313561363633333237306433353761333730623964666130306165346439326163633365353031336665643762c001386366306334336631323562653737373032363461643033306238663038353466343166333435613038376633633162383636303963626266663731643931633938383338626562616334643066613830376136616636323735363734333732313434666436626337316264643066343134386163306534663963376364373437613037383735333531333165316132306466376265653865323264343731396662386538653835656230623234643739333838633830373736653634363161121a0a03424e421213313030303030303030303030303030303030301a2a3078314338393334343141423663314137354530313838373038376561353038624538653037414161652a15437265617465206e6173682056616c696461746f723215637265617465206e6173682076616c696461746f7212021200',
});

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

console.log(simulateInfo);

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

if (res.code === 0) {
alert('success');
}
}}
>
broadcast with simulate
</button>
</>
);
};
10 changes: 8 additions & 2 deletions examples/nextjs/src/components/feegrant/delObj.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
newObjectGRN,
PermissionTypes,
toTimestamp,
fromJsonTimestamp,
} from '@bnb-chain/greenfield-js-sdk';
import { Wallet } from '@ethersproject/wallet';
import { useState } from 'react';
Expand Down Expand Up @@ -49,16 +50,21 @@ export const DelObj = () => {
console.log('wallet', wallet.address, wallet.privateKey);
setWallet(wallet);

const tempAddr = wallet.address;

// 2. allow temporary account to submit specified tx and amount
const date = new Date();
date.setDate(date.getDate() + 1);
const grantAllowanceTx = await client.feegrant.grantAllowance({
granter: address,
grantee: wallet.address,
grantee: tempAddr,
allowedMessages: [MsgDeleteObjectTypeUrl],
amount: parseEther('0.09').toString(),
denom: 'BNB',
expirationTime: toTimestamp(date),
// expirationTime: fromJsonTimestamp({
// seconds: 1796428611,
// }),
});

// 3. Put bucket policy so that the temporary account can create objects within this bucket
Expand All @@ -72,7 +78,7 @@ export const DelObj = () => {
statements: [statement],
principal: {
type: PermissionTypes.PrincipalType.PRINCIPAL_TYPE_GNFD_ACCOUNT,
value: wallet.address,
value: tempAddr,
},
});

Expand Down
102 changes: 78 additions & 24 deletions examples/nextjs/src/components/validator/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,98 @@ import { client } from '@/client';
import { VALIDATOR_PRIVATEKEY } from '@/config/env';
import { Long } from '@bnb-chain/greenfield-js-sdk';
import { useAccount } from 'wagmi';

import { MsgSubmitProposal, MsgVote } from '@bnb-chain/greenfield-cosmos-types/cosmos/gov/v1/tx';
import {
MsgCreateValidator,
MsgEditValidator,
} from '@bnb-chain/greenfield-cosmos-types/cosmos/staking/v1beta1/tx';
import { Any } from '@bnb-chain/greenfield-cosmos-types/google/protobuf/any';
import { PubKey } from '@bnb-chain/greenfield-cosmos-types/cosmos/crypto/ed25519/keys';

/**
* decrpred
*/
export const Validator = () => {
const { address, connector } = useAccount();

return (
<>
{/* <h4>create validator</h4>
<h4>create validator</h4>
<button
onClick={async () => {
if (!address) return;
const createValidatorTx = await client.validator.createValidator(address, {

const createValidatorMsg = {
blsKey:
'85a874ce490ea46e3ed8883544496c8852de074094185ad5d881df95df71287c7a837da79b9e4e2aa5cee0d23ccff22f',
blsProof:
'9585baae6c196c98575626b86f15ea4b0c7e2d07947a58afe0a5deeb740e6d41e213e76eddc7d459809adaf4c2c8ed8401a6216a875cc0dfaaaf938ff7a57c109518671a592b0f9ed2105c8ec8e2d4031c4ffe473d7c4d505c4305ab008e1319',
challengerAddress: '0xfFFb326C7246A06a98fE14E9BDCe74f8e62B7A14',
commission: {
maxChangeRate: '1',
maxRate: '1',
rate: '1',
},
delegatorAddress: '0x5E52a4fc7575806f8E6c1D48dCdC98F9fED8D11e',
description: {
moniker: 'test',
details: 'test',
identity: 'test',
securityContact: 'test',
website: 'test',
details: 'BNBChain Validator',
identity: '',
moniker: 'BNBChain',
securityContact: '',
website: '',
},
blsKey: 'test',
blsProof: '',
challengerAddress: '0x4038993E087832D84e2Ac855d27f6b0b2EEc1907',
relayerAddress: '0xA4A2957E858529FFABBBb483D1D704378a9fca6b',
commission: {
maxChangeRate: '10000000000000000',
maxRate: '10000000000000000',
rate: '10000000000000000',
//
from: '0x7b5Fe22B5446f7C62Ea27B8BD71CeF94e03f3dF2',
minSelfDelegation: '1000000000000000000000',
pubkey: Any.fromPartial({
typeUrl: '/cosmos.crypto.ed25519.PubKey',
value: PubKey.encode({
key: Uint8Array.from([1, 2, 3, 4]),
}).finish(),
}),
relayerAddress: '0x96931F9918e930D3e48c77aaA479349805DbcdCd',
validatorAddress: '0x45D415799659720f4c90F7fd1Ef5D7390Ffc9166',
value: {
amount: '1000000000000000000000',
denom: 'BNB',
},
delegatorAddress: address,
from: address,
value: ,
};

// const createValidatorTx = await client.validator.createValidator(
// address,
// createValidatorMsg,
// );

// console.log('createValidatorTx.metaTxInfo', createValidatorTx.metaTxInfo);

const submitProposalTx = await client.proposal.submitProposal(createValidatorMsg, {
initialDeposit: [
{
amount: '10000000000000000',
denom: 'BNB',
},
],
messages: [
Any.fromPartial({
typeUrl: '/cosmos.staking.v1beta1.MsgCreateValidator',
value: MsgCreateValidator.encode(createValidatorMsg).finish(),
}),
],
metadata: 'metadata',
proposer: address,
summary: 'summary',
title: 'title',
});

const simulateInfo = await createValidatorTx.simulate({
console.log('submitProposalTx.meta', submitProposalTx.metaTxInfo);

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

console.log('simulateInfo', simulateInfo);

const res = await createValidatorTx.broadcast({
const res = await submitProposalTx.broadcast({
denom: 'BNB',
gasLimit: Number(simulateInfo?.gasLimit),
gasPrice: simulateInfo?.gasPrice || '5000000000',
Expand All @@ -54,8 +106,10 @@ export const Validator = () => {
}
}}
>
create
</button> */}
create validator
</button>

<br />

<button
onClick={async () => {
Expand Down
Loading

0 comments on commit 76a18f1

Please sign in to comment.