-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
220 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@bnb-chain/greenfield-js-sdk': patch | ||
--- | ||
|
||
feat: Distribution api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
packages/chain-sdk/src/messages/cosmos/distribution/MsgFundCommunityPoolTypeUrl.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
], | ||
}; |
16 changes: 16 additions & 0 deletions
16
packages/chain-sdk/src/messages/cosmos/distribution/MsgSetWithdrawAddress.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
], | ||
}; |
16 changes: 16 additions & 0 deletions
16
packages/chain-sdk/src/messages/cosmos/distribution/MsgWithdrawDelegatorReward.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
], | ||
}; |
12 changes: 12 additions & 0 deletions
12
packages/chain-sdk/src/messages/cosmos/distribution/MsgWithdrawValidatorCommission.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
], | ||
}; |