Skip to content

Commit f890543

Browse files
committed
chore: add support for MsgTransferDelegation
1 parent ccdd167 commit f890543

File tree

6 files changed

+201
-1
lines changed

6 files changed

+201
-1
lines changed

packages/sdk-ts/src/core/modules/msgs.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import MsgUnderwrite from './insurance/msgs/MsgUnderwrite.js'
3636
import MsgSendToEth from './peggy/msgs/MsgSendToEth.js'
3737
import MsgDelegate from './staking/msgs/MsgDelegate.js'
3838
import MsgUndelegate from './staking/msgs/MsgUndelegate.js'
39+
import MsgTransferDelegation from './staking/msgs/MsgTransferDelegation.js'
3940
import MsgEditValidator from './staking/msgs/MsgEditValidator.js'
4041
import MsgCreateValidator from './staking/msgs/MsgCreateValidator.js'
4142
import MsgBeginRedelegate from './staking/msgs/MsgBeginRedelegate.js'
@@ -95,6 +96,7 @@ export type Msgs =
9596
| MsgDelegate
9697
| MsgUndelegate
9798
| MsgBeginRedelegate
99+
| MsgTransferDelegation
98100
| MsgCancelUnbondingDelegation
99101
| MsgExecuteContract
100102
| MsgExecuteContractCompat

packages/sdk-ts/src/core/modules/staking/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import MsgUndelegate from './msgs/MsgUndelegate.js'
33
import MsgEditValidator from './msgs/MsgEditValidator.js'
44
import MsgCreateValidator from './msgs/MsgCreateValidator.js'
55
import MsgBeginRedelegate from './msgs/MsgBeginRedelegate.js'
6+
import MsgTransferDelegation from './msgs/MsgTransferDelegation.js'
67
import MsgCancelUnbondingDelegation from './msgs/MsgCancelUnbondingDelegation.js'
78

89
export {
@@ -11,5 +12,6 @@ export {
1112
MsgEditValidator,
1213
MsgCreateValidator,
1314
MsgBeginRedelegate,
15+
MsgTransferDelegation,
1416
MsgCancelUnbondingDelegation,
1517
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { BigNumberInBase } from '@injectivelabs/utils'
2+
import MsgTransferDelegation from './MsgTransferDelegation.js'
3+
import snakecaseKeys from 'snakecase-keys'
4+
import { mockFactory, prepareEip712 } from '@injectivelabs/utils/test-utils'
5+
import {
6+
getEip712TypedData,
7+
getEip712TypedDataV2,
8+
} from '../../../tx/eip712/eip712.js'
9+
import { IndexerGrpcWeb3GwApi } from './../../../../client/indexer/grpc/IndexerGrpcWeb3GwApi.js'
10+
import { EIP712Version } from '@injectivelabs/ts-types'
11+
12+
const params: MsgTransferDelegation['params'] = {
13+
validatorAddress: mockFactory.validatorAddress,
14+
injectiveAddress: mockFactory.injectiveAddress,
15+
receiverAddress: mockFactory.injectiveAddress2,
16+
amount: {
17+
amount: new BigNumberInBase(1).toFixed(),
18+
denom: 'inj',
19+
},
20+
}
21+
22+
const protoType = '/cosmos.staking.v1beta1.MsgTransferDelegation'
23+
const protoTypeAmino = 'cosmos-sdk/MsgTransferDelegation'
24+
const protoParams = {
25+
validatorAddress: params.validatorAddress,
26+
delegatorAddress: params.injectiveAddress,
27+
receiverAddress: params.receiverAddress,
28+
amount: params.amount,
29+
}
30+
const protoParamsAmino = snakecaseKeys(protoParams)
31+
const message = MsgTransferDelegation.fromJSON(params)
32+
33+
describe('MsgTransferDelegation', () => {
34+
it('generates proper proto', () => {
35+
const proto = message.toProto()
36+
37+
expect(proto).toStrictEqual(protoParams)
38+
})
39+
40+
it('generates proper data', () => {
41+
const data = message.toData()
42+
43+
expect(data).toStrictEqual({
44+
'@type': protoType,
45+
...protoParams,
46+
})
47+
})
48+
49+
it('generates proper amino', () => {
50+
const amino = message.toAmino()
51+
52+
expect(amino).toStrictEqual({
53+
type: protoTypeAmino,
54+
value: protoParamsAmino,
55+
})
56+
})
57+
58+
it('generates proper web3Gw', () => {
59+
const web3 = message.toWeb3Gw()
60+
61+
expect(web3).toStrictEqual({
62+
'@type': protoType,
63+
...protoParamsAmino,
64+
})
65+
})
66+
67+
describe('generates proper EIP712 compared to the Web3Gw (chain)', () => {
68+
const { endpoints, eip712Args, prepareEip712Request } = prepareEip712({
69+
messages: message,
70+
})
71+
72+
it('EIP712 v1', async () => {
73+
const eip712TypedData = getEip712TypedData(eip712Args)
74+
75+
const txResponse = await new IndexerGrpcWeb3GwApi(
76+
endpoints.indexer,
77+
).prepareEip712Request({
78+
...prepareEip712Request,
79+
eip712Version: EIP712Version.V1,
80+
})
81+
82+
expect(eip712TypedData).toStrictEqual(JSON.parse(txResponse.data))
83+
})
84+
85+
it('EIP712 v2', async () => {
86+
const eip712TypedData = getEip712TypedDataV2(eip712Args)
87+
88+
const txResponse = await new IndexerGrpcWeb3GwApi(
89+
endpoints.indexer,
90+
).prepareEip712Request({
91+
...prepareEip712Request,
92+
eip712Version: EIP712Version.V2,
93+
})
94+
95+
expect(eip712TypedData).toStrictEqual(JSON.parse(txResponse.data))
96+
})
97+
})
98+
})
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { MsgBase } from '../../MsgBase.js'
2+
import snakecaseKeys from 'snakecase-keys'
3+
import {
4+
CosmosBaseV1Beta1Coin,
5+
CosmosStakingV1Beta1Tx,
6+
} from '@injectivelabs/core-proto-ts'
7+
8+
export declare namespace MsgTransferDelegation {
9+
export interface Params {
10+
amount: {
11+
denom: string
12+
amount: string
13+
}
14+
injectiveAddress: string
15+
validatorAddress: string
16+
receiverAddress: string
17+
}
18+
19+
export type Proto = CosmosStakingV1Beta1Tx.MsgTransferDelegation
20+
}
21+
22+
/**
23+
* @category Messages
24+
*/
25+
export default class MsgTransferDelegation extends MsgBase<
26+
MsgTransferDelegation.Params,
27+
MsgTransferDelegation.Proto
28+
> {
29+
static fromJSON(params: MsgTransferDelegation.Params): MsgTransferDelegation {
30+
return new MsgTransferDelegation(params)
31+
}
32+
33+
public toProto() {
34+
const { params } = this
35+
36+
const coinAmount = CosmosBaseV1Beta1Coin.Coin.create()
37+
38+
coinAmount.denom = params.amount.denom
39+
coinAmount.amount = params.amount.amount
40+
41+
const message = CosmosStakingV1Beta1Tx.MsgTransferDelegation.create()
42+
43+
message.delegatorAddress = params.injectiveAddress
44+
message.validatorAddress = params.validatorAddress
45+
message.receiverAddress = params.receiverAddress
46+
message.amount = coinAmount
47+
48+
return CosmosStakingV1Beta1Tx.MsgTransferDelegation.fromPartial(message)
49+
}
50+
51+
public toData() {
52+
const proto = this.toProto()
53+
54+
return {
55+
'@type': '/cosmos.staking.v1beta1.MsgTransferDelegation',
56+
...proto,
57+
}
58+
}
59+
60+
public toAmino() {
61+
const proto = this.toProto()
62+
const message = {
63+
...snakecaseKeys(proto),
64+
}
65+
66+
return {
67+
type: 'cosmos-sdk/MsgTransferDelegation',
68+
value: message,
69+
}
70+
}
71+
72+
public toWeb3Gw() {
73+
const amino = this.toAmino()
74+
const { value } = amino
75+
76+
return {
77+
'@type': '/cosmos.staking.v1beta1.MsgTransferDelegation',
78+
...value,
79+
}
80+
}
81+
82+
public toDirectSign() {
83+
const proto = this.toProto()
84+
85+
return {
86+
type: '/cosmos.staking.v1beta1.MsgTransferDelegation',
87+
message: proto,
88+
}
89+
}
90+
91+
public toBinary(): Uint8Array {
92+
return CosmosStakingV1Beta1Tx.MsgTransferDelegation.encode(
93+
this.toProto(),
94+
).finish()
95+
}
96+
}

packages/sdk-ts/src/core/tx/eip712/maps.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,8 @@ export const protoTypeToAminoType = (type: string): string => {
571571
return 'cosmos-sdk/MsgUndelegate'
572572
case 'cosmos.staking.v1beta1.MsgBeginRedelegate':
573573
return 'cosmos-sdk/MsgBeginRedelegate'
574+
case 'cosmos.staking.v1beta1.MsgTransferDelegation':
575+
return 'cosmos-sdk/MsgTransferDelegation'
574576
case 'cosmos.staking.v1beta1.MsgCancelUnbondingDelegation':
575577
return 'cosmos-sdk/MsgCancelUnbondingDelegation'
576578
case 'cosmos.staking.v1beta1.MsgUpdateParams':

packages/ts-types/src/enums.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ export enum MsgType {
5252
MsgEditValidator = 'cosmos.staking.v1beta1.MsgEditValidator',
5353
MsgBeginRedelegate = 'cosmos.staking.v1beta1.MsgBeginRedelegate',
5454
MsgCreateValidator = 'cosmos.staking.v1beta1.MsgCreateValidator',
55-
MsgCancelUnbondingDelegation = 'cosmos.staking.v1beta1.MsgCancelUnbondingDelegation',
5655
MsgTransferDelegation = 'cosmos.staking.v1beta1.MsgTransferDelegation',
56+
MsgCancelUnbondingDelegation = 'cosmos.staking.v1beta1.MsgCancelUnbondingDelegation',
5757

5858
// Wasm
5959
MsgStoreCode = 'cosmwasm.wasm.v1.MsgStoreCode',

0 commit comments

Comments
 (0)