Skip to content

Commit

Permalink
Feat/validator (#310)
Browse files Browse the repository at this point in the history
* feat: EditValidator

* feat: Add proposal api

* refactor: TxClient

* feat: Bucket listBucketsByPaymentAccount api

* fix: Proposal and Validator API

* feat: ListUserPaymentAccount api
  • Loading branch information
rrr523 authored Sep 7, 2023
1 parent 53b8001 commit 695379e
Show file tree
Hide file tree
Showing 41 changed files with 1,221 additions and 380 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-seas-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bnb-chain/greenfield-js-sdk': patch
---

feat: Validator API - editValidator
5 changes: 5 additions & 0 deletions .changeset/soft-doors-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bnb-chain/greenfield-js-sdk': patch
---

feat: Proposal vote api
5 changes: 5 additions & 0 deletions .changeset/spicy-jokes-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bnb-chain/greenfield-js-sdk': patch
---

feat: ListUserPaymentAccount api
5 changes: 5 additions & 0 deletions .changeset/stale-beans-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bnb-chain/greenfield-js-sdk': patch
---

feat: Bucket listBucketsByPaymentAccount api
5 changes: 5 additions & 0 deletions .changeset/stupid-dodos-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bnb-chain/greenfield-js-sdk': patch
---

refactor: basic.multi -> txClient.multi
3 changes: 3 additions & 0 deletions examples/nextjs/.env.simple
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ NEXT_PUBLIC_CROSS_CHAIN_CONTRACT_ADDRESS=0xeEBe00Ac0756308ac4AaBfD76c05c4F3088B8
# ACCOUNT
NEXT_PUBLIC_ACCOUNT_ADDRESS=
NEXT_PUBLIC_ACCOUNT_PRIVATEKEY=

# VALIDATOR
NEXT_PUBLIC_VALIDATOR_PRIVATEKEY=
2 changes: 1 addition & 1 deletion examples/nextjs/src/components/feegrant/createObj.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const CreateObj = () => {
});

// 4. broadcast txs include 2 msg
const txs = await client.basic.multiTx([grantAllowanceTx, putPolicyTx]);
const txs = await client.txClient.multiTx([grantAllowanceTx, putPolicyTx]);
const simuluateInfo = await txs.simulate({
denom: 'BNB',
});
Expand Down
2 changes: 1 addition & 1 deletion examples/nextjs/src/components/feegrant/delObj.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const DelObj = () => {
});

// 4. broadcast txs include 2 msg
const txs = await client.basic.multiTx([grantAllowanceTx, putPolicyTx]);
const txs = await client.txClient.multiTx([grantAllowanceTx, putPolicyTx]);
const simuluateInfo = await txs.simulate({
denom: 'BNB',
});
Expand Down
2 changes: 1 addition & 1 deletion examples/nextjs/src/components/multimsg/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const MultiMsg = () => {

console.log(transferOutTx.metaTxInfo);

const txs = await client.basic.multiTx([transferTx, transferOutTx]);
const txs = await client.txClient.multiTx([transferTx, transferOutTx]);

const simuluateInfo = await txs.simulate({
denom: 'BNB',
Expand Down
48 changes: 48 additions & 0 deletions examples/nextjs/src/components/proposal/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { client } from '@/client';
import { VALIDATOR_PRIVATEKEY } from '@/config/env';
import { Long } from '@bnb-chain/greenfield-js-sdk';
import { useAccount } from 'wagmi';

export const Proposal = () => {
const { address, connector } = useAccount();

return (
<>
<h3>proposal</h3>
<h4>vote</h4>
<button
onClick={async () => {
if (!address) return;

const voteProposalTx = await client.proposal.voteProposal({
metadata: 'meta',
option: 1,
proposalId: Long.fromString('15'),
voter: address,
});

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

console.log('simulateInfo', simulateInfo);

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

if (res.code === 0) {
alert('success');
}
}}
>
vote proposal
</button>
</>
);
};
55 changes: 54 additions & 1 deletion examples/nextjs/src/components/query/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { client } from '@/client';
import { ACCOUNT_PRIVATEKEY } from '@/config/env';
import { getOffchainAuthKeys } from '@/utils/offchainAuth';
import { Long } from '@bnb-chain/greenfield-js-sdk';
import { useAccount } from 'wagmi';

export const QueryComponent = () => {
const { address } = useAccount();
const { address, connector } = useAccount();
return (
<>
<h2>open console panel</h2>
Expand Down Expand Up @@ -117,6 +119,24 @@ export const QueryComponent = () => {
onClick={async () => {
if (!address) return;

const { paymentAccounts } = await client.payment.getPaymentAccountsByOwner({
owner: address,
});

const res = await client.bucket.listBucketsByPaymentAccount({
paymentAccount: paymentAccounts[0],
});
console.log('res', res);
}}
>
list bucket by payment
</button>
</li>
<li>
<button
onClick={async () => {
if (!address) return;

const res = await client.sp.listGroupsMembers({
groupId: 269,
});
Expand Down Expand Up @@ -161,6 +181,39 @@ export const QueryComponent = () => {
list user owned groups
</button>
</li>

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

const provider = await connector?.getProvider();
const offChainData = await getOffchainAuthKeys(address, provider);
if (!offChainData) {
alert('No offchain, please create offchain pairs first');
return;
}

const res = await client.payment.listUserPaymentAccounts(
{
account: address,
},
{
// type: 'ECDSA',
// privateKey: ACCOUNT_PRIVATEKEY,
type: 'EDDSA',
domain: window.location.origin,
seed: offChainData.seedString,
address,
},
);

console.log('res', res);
}}
>
listUserPaymentAccounts
</button>
</li>
</ul>
</>
);
Expand Down
128 changes: 128 additions & 0 deletions examples/nextjs/src/components/validator/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { client } from '@/client';
import { VALIDATOR_PRIVATEKEY } from '@/config/env';
import { Long } from '@bnb-chain/greenfield-js-sdk';
import { useAccount } from 'wagmi';

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

return (
<>
{/* <h4>create validator</h4>
<button
onClick={async () => {
if (!address) return;
const createValidatorTx = await client.validator.createValidator(address, {
description: {
moniker: 'test',
details: 'test',
identity: 'test',
securityContact: 'test',
website: 'test',
},
blsKey: 'test',
blsProof: '',
challengerAddress: '0x4038993E087832D84e2Ac855d27f6b0b2EEc1907',
relayerAddress: '0xA4A2957E858529FFABBBb483D1D704378a9fca6b',
commission: {
maxChangeRate: '10000000000000000',
maxRate: '10000000000000000',
rate: '10000000000000000',
},
delegatorAddress: address,
from: address,
value: ,
});
const simulateInfo = await createValidatorTx.simulate({
denom: 'BNB',
});
console.log('simulateInfo', simulateInfo);
const res = await createValidatorTx.broadcast({
denom: 'BNB',
gasLimit: Number(simulateInfo?.gasLimit),
gasPrice: simulateInfo?.gasPrice || '5000000000',
payer: address,
granter: '',
});
if (res.code === 0) {
alert('success');
}
}}
>
create
</button> */}

<button
onClick={async () => {
const res = await client.validator.listValidators({
status: 'BOND_STATUS_BONDED',
pagination: {
limit: Long.fromInt(10),
offset: Long.fromInt(0),
countTotal: true,
key: Uint8Array.from([]),
reverse: false,
},
});

console.log('res', res);

// console.log('blsKey', toHex(res.validators[0].blsKey));
}}
>
list
</button>

<h4>edit validator</h4>
<button
onClick={async () => {
if (!address) return;

const editValidatorTx = await client.validator.editValidator(address, {
blsKey:
'0xb3c5eaba9ae74bc6308054f586db909ea8482d158b24789d66c47578bb9b293b7d00d5acfb0645e528f39526ff04994b',
blsProof: '',
challengerAddress: '0x52A57571D08692d2D60596DD9b588Ff59ACdf02E',
description: {
moniker: 'test',
details: 'test',
identity: 'test',
securityContact: 'test',
website: 'test',
},
relayerAddress: '0x68EA59d9aFB06a838E05d511eDdA2049D1b6fF61',
validatorAddress: '0x84800aaFDD595f1b7C77292c98988ad424068470',
commissionRate: '',
minSelfDelegation: '',
});

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

console.log('simulateInfo', simulateInfo);

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

if (res.code === 0) {
alert('success');
}
}}
>
edit validator
</button>
</>
);
};
2 changes: 2 additions & 0 deletions examples/nextjs/src/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
NEXT_PUBLIC_CROSS_CHAIN_CONTRACT_ADDRESS,
NEXT_PUBLIC_ACCOUNT_ADDRESS,
NEXT_PUBLIC_ACCOUNT_PRIVATEKEY,
NEXT_PUBLIC_VALIDATOR_PRIVATEKEY,
} = publicRuntimeConfig || {};

export const GRPC_URL = NEXT_PUBLIC_GRPC_URL;
Expand All @@ -22,3 +23,4 @@ export const TOKEN_HUB_CONTRACT_ADDRESS = NEXT_PUBLIC_TOKEN_HUB_CONTRACT_ADDRESS
export const CROSS_CHAIN_CONTRACT_ADDRESS = NEXT_PUBLIC_CROSS_CHAIN_CONTRACT_ADDRESS;
export const ACCOUNT_ADDRESS = NEXT_PUBLIC_ACCOUNT_ADDRESS;
export const ACCOUNT_PRIVATEKEY = NEXT_PUBLIC_ACCOUNT_PRIVATEKEY;
export const VALIDATOR_PRIVATEKEY = NEXT_PUBLIC_VALIDATOR_PRIVATEKEY;
6 changes: 6 additions & 0 deletions examples/nextjs/src/pages/tx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { Withdraw } from '@/components/withdraw';
import { useIsMounted } from '@/hooks/useIsMounted';
import { useAccount } from 'wagmi';
import { PaymentComponent } from '@/components/payment';
import { Validator } from '@/components/validator';
import { Proposal } from '@/components/proposal';

export default function Tx() {
const isMounted = useIsMounted();
Expand Down Expand Up @@ -47,6 +49,10 @@ export default function Tx() {
<hr style={{ margin: '10px 0' }} />
<FeeGrant />
<hr style={{ margin: '10px 0' }} />
<Proposal />
<hr style={{ margin: '10px 0' }} />
<Validator />
<hr style={{ margin: '10px 0' }} />
<MultiMsg />
</>
)}
Expand Down
10 changes: 5 additions & 5 deletions packages/chain-sdk/src/api/account.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { TxClient } from '@/clients/txClient';
import { MsgMultiSendSDKTypeEIP712 } from '@/messages/bank/MsgMultiSend';
import { MsgSendSDKTypeEIP712 } from '@/messages/bank/MsgSend';
import { MsgCreatePaymentAccountSDKTypeEIP712 } from '@/messages/greenfield/payment/MsgCreatePaymentAccount';
Expand All @@ -24,7 +25,6 @@ import {
MsgSendTypeUrl,
TxResponse,
} from '..';
import { Basic } from './basic';
import { RpcQueryClient } from '../clients/queryclient';

export interface IAccount {
Expand Down Expand Up @@ -67,12 +67,12 @@ export interface IAccount {

@singleton()
export class Account implements IAccount {
constructor(@inject(delay(() => Basic)) private basic: Basic) {}
constructor(@inject(delay(() => TxClient)) private txClient: TxClient) {}

private queryClient = container.resolve(RpcQueryClient);

public async multiTransfer(address: string, msg: MsgMultiSend) {
return await this.basic.tx(
return await this.txClient.tx(
MsgMultiSendTypeUrl,
address,
MsgMultiSendSDKTypeEIP712,
Expand All @@ -82,7 +82,7 @@ export class Account implements IAccount {
}

public async createPaymentAccount(msg: MsgCreatePaymentAccount) {
return await this.basic.tx(
return await this.txClient.tx(
MsgCreatePaymentAccountTypeUrl,
msg.creator,
MsgCreatePaymentAccountSDKTypeEIP712,
Expand Down Expand Up @@ -131,7 +131,7 @@ export class Account implements IAccount {
}

public async transfer(msg: MsgSend) {
return await this.basic.tx(
return await this.txClient.tx(
MsgSendTypeUrl,
msg.fromAddress,
MsgSendSDKTypeEIP712,
Expand Down
Loading

0 comments on commit 695379e

Please sign in to comment.