Skip to content

Commit

Permalink
feat: Distribution API
Browse files Browse the repository at this point in the history
  • Loading branch information
rrr523 committed Sep 11, 2023
1 parent 0bcca80 commit 30065c2
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 46 deletions.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-cobras-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bnb-chain/greenfield-js-sdk': patch
---

feat: Distribution api
80 changes: 80 additions & 0 deletions examples/nextjs/src/components/distribution/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { client } from '@/client';
import { useState } from 'react';
import { useAccount } from 'wagmi';

export const Distribution = () => {
const { address, connector } = useAccount();
const [createGroupInfo, setCreateGroupInfo] = useState({
groupName: '',
});

return (
<div>
<h3>distribution</h3>

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

const tx = await client.distribution.setWithdrawAddress({
delegatorAddress: address,
withdrawAddress: '0xCdB16f541e1445150f9211Dd564668eb01b26E75',
});

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

// console.log('simuluateInfo', simuluateInfo);
const res = await tx.broadcast({
denom: 'BNB',
gasLimit: Number(210000),
gasPrice: '5000000000',
payer: address,
granter: '',
});

console.log('res', res);

if (res.code === 0) {
alert('success');
}
}}
>
setWithdrawAddress
</button>
<br />
<button
onClick={async () => {
if (!address) return;

const tx = await client.distribution.withdrawDelegatorReward({
delegatorAddress: address,
validatorAddress: '0xCdB16f541e1445150f9211Dd564668eb01b26E75',
});

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

// console.log('simuluateInfo', simuluateInfo);
const res = await tx.broadcast({
denom: 'BNB',
gasLimit: Number(210000),
gasPrice: '5000000000',
payer: address,
granter: '',
});

console.log('res', res);

if (res.code === 0) {
alert('success');
}
}}
>
withdrawDelegatorReward
</button>
</div>
);
};
1 change: 0 additions & 1 deletion examples/nextjs/src/components/query/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
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';
Expand Down
3 changes: 3 additions & 0 deletions examples/nextjs/src/pages/tx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { useAccount } from 'wagmi';
import { PaymentComponent } from '@/components/payment';
import { Validator } from '@/components/validator';
import { Proposal } from '@/components/proposal';
import { Distribution } from '@/components/distribution';

export default function Tx() {
const isMounted = useIsMounted();
Expand Down Expand Up @@ -53,6 +54,8 @@ export default function Tx() {
<hr style={{ margin: '10px 0' }} />
<Validator />
<hr style={{ margin: '10px 0' }} />
<Distribution />
<hr style={{ margin: '10px 0' }} />
<MultiMsg />
</>
)}
Expand Down
101 changes: 56 additions & 45 deletions packages/chain-sdk/src/api/distribution.ts
Original file line number Diff line number Diff line change
@@ -1,79 +1,90 @@
import { TxClient } from '@/clients/txClient';
import { MsgFundCommunityPoolTypeUrlSDKTypeEIP712 } from '@/messages/cosmos/distribution/MsgFundCommunityPoolTypeUrl';
import { MsgSetWithdrawAddressSDKTypeEIP712 } from '@/messages/cosmos/distribution/MsgSetWithdrawAddress';
import { MsgWithdrawDelegatorRewardSDKTypeEIP712 } from '@/messages/cosmos/distribution/MsgWithdrawDelegatorReward';
import { MsgWithdrawValidatorCommissionSDKTypeEIP712 } from '@/messages/cosmos/distribution/MsgWithdrawValidatorCommission';
import {
MsgFundCommunityPoolResponse,
MsgSetWithdrawAddressResponse,
MsgWithdrawDelegatorRewardResponse,
MsgWithdrawValidatorCommissionResponse,
MsgFundCommunityPool,
MsgSetWithdrawAddress,
MsgWithdrawDelegatorReward,
MsgWithdrawValidatorCommission,
} from '@bnb-chain/greenfield-cosmos-types/cosmos/distribution/v1beta1/tx';
import { Coin } from '@cosmjs/proto-signing';
import { container, injectable } from 'tsyringe';
import { Basic } from './basic';
import { container, delay, inject, injectable } from 'tsyringe';
import {
MsgFundCommunityPoolTypeUrl,
MsgSetWithdrawAddressTypeUrl,
MsgWithdrawDelegatorRewardTypeUrl,
MsgWithdrawValidatorCommissionTypeUrl,
TxResponse,
} from '..';
import { RpcQueryClient } from '../clients/queryclient';
export interface IDistribution {
/**
* sets the withdrawal address for a delegator address
*/
setWithdrawAddress(
withdrawAddress: string,
delegatorAddress: string,
): Promise<MsgSetWithdrawAddressResponse>;
setWithdrawAddress(msg: MsgSetWithdrawAddress): Promise<TxResponse>;

/**
* withdraw accumulated commission by validator
*/
withdrawValidatorCommission(
validatorAddress: string,
): Promise<MsgWithdrawValidatorCommissionResponse>;
address: string,
msg: MsgWithdrawValidatorCommission,
): Promise<TxResponse>;

/**
* withdraw rewards by a delegator
*/
withdrawDelegatorReward(
validatorAddress: string,
delegatorAddress: string,
): Promise<MsgWithdrawDelegatorRewardResponse>;
withdrawDelegatorReward(msg: MsgWithdrawDelegatorReward): Promise<TxResponse>;

/**
* sends coins directly from the sender to the community pool.
*/
fundCommunityPoolundComm(
amount: Coin[],
depositor: string,
): Promise<MsgFundCommunityPoolResponse>;
fundCommunityPoolundComm(address: string, msg: MsgFundCommunityPool): Promise<TxResponse>;
}

@injectable()
export class Distribution implements IDistribution {
private basic: Basic = container.resolve(Basic);
constructor(@inject(delay(() => TxClient)) private txClient: TxClient) {}
private queryClient: RpcQueryClient = container.resolve(RpcQueryClient);

public async setWithdrawAddress(withdrawAddress: string, delegatorAddress: string) {
const rpc = await this.queryClient.getMsgClient();
return await rpc.SetWithdrawAddress({
withdrawAddress,
delegatorAddress,
});
public async setWithdrawAddress(msg: MsgSetWithdrawAddress) {
return await this.txClient.tx(
MsgSetWithdrawAddressTypeUrl,
msg.delegatorAddress,
MsgSetWithdrawAddressSDKTypeEIP712,
MsgSetWithdrawAddress.toSDK(msg),
MsgSetWithdrawAddress.encode(msg).finish(),
);
}

public async withdrawValidatorCommission(validatorAddress: string) {
const rpc = await this.queryClient.getMsgClient();
return rpc.WithdrawValidatorCommission({
validatorAddress,
});
public async withdrawValidatorCommission(address: string, msg: MsgWithdrawValidatorCommission) {
return await this.txClient.tx(
MsgWithdrawValidatorCommissionTypeUrl,
address,
MsgWithdrawValidatorCommissionSDKTypeEIP712,
MsgWithdrawValidatorCommission.toSDK(msg),
MsgWithdrawValidatorCommission.encode(msg).finish(),
);
}

public async withdrawDelegatorReward(validatorAddress: string, delegatorAddress: string) {
const rpc = await this.queryClient.getMsgClient();
return rpc.WithdrawDelegatorReward({
delegatorAddress,
validatorAddress,
});
public async withdrawDelegatorReward(msg: MsgWithdrawDelegatorReward) {
return await this.txClient.tx(
MsgWithdrawDelegatorRewardTypeUrl,
msg.delegatorAddress,
MsgWithdrawDelegatorRewardSDKTypeEIP712,
MsgWithdrawDelegatorReward.toSDK(msg),
MsgWithdrawDelegatorReward.encode(msg).finish(),
);
}

public async fundCommunityPoolundComm(amount: Coin[], depositor: string) {
const rpc = await this.queryClient.getMsgClient();
return rpc.FundCommunityPool({
amount,
depositor,
});
public async fundCommunityPoolundComm(address: string, msg: MsgFundCommunityPool) {
return await this.txClient.tx(
MsgFundCommunityPoolTypeUrl,
address,
MsgFundCommunityPoolTypeUrlSDKTypeEIP712,
MsgFundCommunityPool.toSDK(msg),
MsgFundCommunityPool.encode(msg).finish(),
);
}
}
6 changes: 6 additions & 0 deletions packages/chain-sdk/src/constants/typeUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ export const AllowedMsgAllowanceTypeUrl = '/cosmos.feegrant.v1beta1.AllowedMsgAl
export const MsgEditValidatorTypeUrl = '/cosmos.staking.v1beta1.MsgEditValidator';
export const MsgCreateValidatorTypeUrl = '/cosmos.staking.v1beta1.MsgCreateValidator';
export const MsgVoteTypeUrl = '/cosmos.gov.v1.MsgVote';
export const MsgWithdrawDelegatorRewardTypeUrl =
'/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward';
export const MsgSetWithdrawAddressTypeUrl = '/cosmos.distribution.v1beta1.MsgSetWithdrawAddress';
export const MsgWithdrawValidatorCommissionTypeUrl =
'/cosmos.distribution.v1beta1.MsgWithdrawValidatorCommission';
export const MsgFundCommunityPoolTypeUrl = '/cosmos.distribution.v1beta1.MsgFundCommunityPool';
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export const MsgFundCommunityPoolTypeUrlSDKTypeEIP712 = {
Msg1: [
{
name: 'amount',
type: 'TypeMsg1Amount[]',
},
{
name: 'depositor',
type: 'string',
},
{
name: 'type',
type: 'string',
},
],
TypeMsg1Amount: [
{
name: 'amount',
type: 'string',
},
{
name: 'denom',
type: 'string',
},
],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const MsgSetWithdrawAddressSDKTypeEIP712 = {
Msg1: [
{
name: 'delegator_address',
type: 'string',
},
{
name: 'type',
type: 'string',
},
{
name: 'withdraw_address',
type: 'string',
},
],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const MsgWithdrawDelegatorRewardSDKTypeEIP712 = {
Msg1: [
{
name: 'type',
type: 'string',
},
{
name: 'delegator_address',
type: 'string',
},
{
name: 'validator_address',
type: 'string',
},
],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const MsgWithdrawValidatorCommissionSDKTypeEIP712 = {
Msg1: [
{
name: 'type',
type: 'string',
},
{
name: 'validator_address',
type: 'string',
},
],
};

0 comments on commit 30065c2

Please sign in to comment.