Skip to content
This repository has been archived by the owner on Oct 13, 2023. It is now read-only.

Commit

Permalink
fix: update stake authorization to take validator array (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
0a1c authored Apr 20, 2023
1 parent da8eca3 commit 65c38c4
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 35 deletions.
2 changes: 1 addition & 1 deletion docs/transactions/authz/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const createTxMsgGenericRevoke: (
```ts
export interface MsgStakeAuthorizationParams {
granteeAddress: string
validatorAddress: string
validatorAddress: string | string[]
denom: string
maxTokens: string | undefined
expiration: number
Expand Down
36 changes: 36 additions & 0 deletions packages/proto/src/messages/authz/stake.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { createStakeAuthorization } from './stake'
import { StakeAuthorization } from '../../proto/cosmos/staking/authz'
import { denom, val, val2 } from '../../proto/tests/utils'
import { JSONOptions } from '../../proto/tests/common'

const validateStakeAuthorization = (validators: string | string[]) => {
const authorization = 1
const amount = '100000'

const msg = createStakeAuthorization(validators, denom, amount, authorization)

const expValidators = Array.isArray(validators) ? validators : [validators]

expect(msg.message.toJson(JSONOptions)).toStrictEqual({
allow_list: {
address: expValidators,
},
authorization_type: authorization,
max_tokens: {
denom,
amount,
},
})
expect(msg.path).toStrictEqual(StakeAuthorization.typeName)
}

describe('test authz staking messages', () => {
it('encodes single validator addresses', () => {
validateStakeAuthorization(val)
})

it('encodes multiple validator addresses', () => {
const validators = [val, val2]
validateStakeAuthorization(validators)
})
})
7 changes: 5 additions & 2 deletions packages/proto/src/messages/authz/stake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ import {
} from '../../proto/cosmos/staking/authz.js'

export function createStakeAuthorization(
allowAddress: string,
allowAddresses: string | string[],
denom: string,
maxTokens: string | undefined,
authorizationType: AuthorizationType,
) {
const addresses = Array.isArray(allowAddresses)
? allowAddresses
: [allowAddresses]
const msg = new StakeAuthorization({
validators: {
value: new StakeAuthorization_Validators({
address: [allowAddress],
address: addresses,
}),
case: 'allowList',
},
Expand Down
73 changes: 42 additions & 31 deletions packages/transactions/src/messages/authz/stakingGrant.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,58 @@ import TestUtils from '../../tests/utils'

const { context, denom } = TestUtils
const granteeAddress = TestUtils.addr1
const validatorAddress = TestUtils.addrVal1
const maxTokens = TestUtils.amount1
const expiration = 10000

const params: MsgStakeAuthorizationParams = {
const createParams = (validatorAddress: string | string[]) => ({
granteeAddress,
validatorAddress,
denom,
maxTokens,
expiration,
})

const validatePayload = (params: MsgStakeAuthorizationParams) => {
const typedData = {
types: {},
message: {},
}

const stakeAuthType = Proto.Cosmos.Staking.Authz.AuthorizationType.DELEGATE
const auth = createStakeAuthorization(
params.validatorAddress,
params.denom,
params.maxTokens,
stakeAuthType,
)

const messageCosmos = createMsgGrant(
context.sender.accountAddress,
params.granteeAddress,
auth,
params.expiration,
)

const payload = createTxMsgStakeAuthorization(context, params)
const expectedPayload = createTransactionPayload(
context,
typedData,
messageCosmos,
)

expect(payload).toStrictEqual(expectedPayload)
}

describe('test tx payload', () => {
it('produces tx payloads as expected', () => {
const typedData = {
types: {},
message: {},
}

const stakeAuthType = Proto.Cosmos.Staking.Authz.AuthorizationType.DELEGATE
const auth = createStakeAuthorization(
params.validatorAddress,
params.denom,
params.maxTokens,
stakeAuthType,
)

const messageCosmos = createMsgGrant(
context.sender.accountAddress,
params.granteeAddress,
auth,
params.expiration,
)

const payload = createTxMsgStakeAuthorization(context, params)
const expectedPayload = createTransactionPayload(
context,
typedData,
messageCosmos,
)

expect(payload).toStrictEqual(expectedPayload)
it('correctly produces tx payloads with single validator', () => {
const validator = TestUtils.addrVal1
const params = createParams(validator)
validatePayload(params)
})

it('correctly produces tx payloads with multiple validators', () => {
const validators = [TestUtils.addrVal1, TestUtils.addrVal2]
const params = createParams(validators)
validatePayload(params)
})
})
2 changes: 1 addition & 1 deletion packages/transactions/src/messages/authz/stakingGrant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createTransactionPayload, TxContext } from '../base.js'

export interface MsgStakeAuthorizationParams {
granteeAddress: string
validatorAddress: string
validatorAddress: string | string[]
denom: string
maxTokens: string | undefined
expiration: number
Expand Down

0 comments on commit 65c38c4

Please sign in to comment.