Skip to content

Commit

Permalink
chore(example): Update custom tx
Browse files Browse the repository at this point in the history
  • Loading branch information
rrr523 committed Sep 11, 2023
1 parent f1e60a2 commit 54d30c3
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/soft-onions-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@demo/wallet': patch
---

chore: Update deps
1 change: 1 addition & 0 deletions examples/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"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",
Expand Down
90 changes: 90 additions & 0 deletions examples/nextjs/src/components/customtx/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { client } from '@/client';
import { useAccount } from 'wagmi';
import { MsgSend } from '@bnb-chain/greenfield-cosmos-types/cosmos/bank/v1beta1/tx';
import { parseEther } from 'viem';

export const CustomTx = () => {
const { address } = useAccount();

return (
<>
<h4>Custome Tx</h4>

<br />
<button
onClick={async () => {
if (!address) return;
const transferParams: MsgSend = {
amount: [
{
denom: 'BNB',
amount: '100000000',
},
],
fromAddress: address,
toAddress: '0x0000000000000000000000000000000000000001',
};

const transferTx = await client.txClient.tx(
'/cosmos.bank.v1beta1.MsgSend',
address,
{
Msg1: [
{
name: 'type',
type: 'string',
},
{
name: 'from_address',
type: 'string',
},
{
name: 'to_address',
type: 'string',
},
{
name: 'amount',
type: 'TypeMsg1Amount[]',
},
],
TypeMsg1Amount: [
{
name: 'denom',
type: 'string',
},
{
name: 'amount',
type: 'string',
},
],
},
MsgSend.toSDK(transferParams),
MsgSend.encode(transferParams).finish(),
);

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

console.log('simulateInfo', simulateInfo);

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

console.log('res', res);

if (res.code === 0) {
alert('success');
}
}}
>
broadcast with simulate
</button>
</>
);
};
4 changes: 4 additions & 0 deletions examples/nextjs/src/pages/tx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { Validator } from '@/components/validator';
import { Proposal } from '@/components/proposal';
import { Distribution } from '@/components/distribution';
import { VirtualGroup } from '@/components/vg';
import { CustomTx } from '@/components/customtx';

export default function Tx() {
const isMounted = useIsMounted();
Expand Down Expand Up @@ -60,6 +61,9 @@ export default function Tx() {
<VirtualGroup />
<hr style={{ margin: '10px 0' }} />
<MultiMsg />
<hr style={{ margin: '10px 0' }} />
<CustomTx />
<hr style={{ margin: '10px 0' }} />
</>
)}
</div>
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 54d30c3

Please sign in to comment.