-
Notifications
You must be signed in to change notification settings - Fork 22
/
MsgInstantiateContract.ts
193 lines (177 loc) 路 5.28 KB
/
MsgInstantiateContract.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { JSONSerializable, removeNull } from '../../../util/json';
import { AccAddress } from '../../bech32';
import { Coins } from '../../Coins';
import { Any } from '@terra-money/terra.proto/google/protobuf/any';
import { MsgInstantiateContract as MsgInstantiateContract_legacy_pb } from '@terra-money/legacy.proto/terra/wasm/v1beta1/tx';
import { MsgInstantiateContract as MsgInstantiateContract_pb } from '@terra-money/terra.proto/cosmwasm/wasm/v1/tx';
import Long from 'long';
export class MsgInstantiateContract extends JSONSerializable<
MsgInstantiateContract.Amino,
MsgInstantiateContract.Data,
MsgInstantiateContract.Proto
> {
public init_coins: Coins;
/**
* @param sender is a sender address
* @param admin is an optional contract admin address who can migrate the contract, put empty string to disable migration
* @param code_id is the reference to the stored WASM code
* @param init_msg json encoded message to be passed to the contract on instantiation
* @param init_coins are transferred to the contract on execution
* @param label label for the contract. v2 supported only
*/
constructor(
public sender: AccAddress,
public admin: AccAddress | undefined,
public code_id: number,
public init_msg: object | string,
init_coins: Coins.Input = {},
public label?: string
) {
super();
this.init_coins = new Coins(init_coins);
}
public static fromAmino(
data: MsgInstantiateContract.Amino,
_?: boolean
): MsgInstantiateContract {
const {
value: { sender, admin, code_id, msg, funds, label },
} = data as MsgInstantiateContract.AminoV2;
return new MsgInstantiateContract(
sender,
admin,
Number.parseInt(code_id),
msg,
Coins.fromAmino(funds),
label
);
}
public toAmino(_?: boolean): MsgInstantiateContract.Amino {
const { sender, admin, code_id, init_msg, init_coins, label } = this;
return {
type: 'wasm/MsgInstantiateContract',
value: {
sender,
admin,
code_id: code_id.toFixed(),
label,
msg: removeNull(init_msg),
funds: init_coins.toAmino(),
},
};
}
public static fromProto(
proto: MsgInstantiateContract.Proto,
_?: boolean
): MsgInstantiateContract {
const p = proto as MsgInstantiateContract_pb;
return new MsgInstantiateContract(
p.sender,
p.admin !== '' ? p.admin : undefined,
p.codeId.toNumber(),
JSON.parse(Buffer.from(p.msg).toString('utf-8')),
Coins.fromProto(p.funds),
p.label !== '' ? p.label : undefined
);
}
public toProto(_?: boolean): MsgInstantiateContract.Proto {
const { sender, admin, code_id, init_msg, init_coins, label } = this;
return MsgInstantiateContract_pb.fromPartial({
admin,
codeId: Long.fromNumber(code_id),
funds: init_coins.toProto(),
msg: Buffer.from(JSON.stringify(init_msg), 'utf-8'),
sender,
label,
});
}
public packAny(isClassic?: boolean): Any {
return Any.fromPartial({
typeUrl: '/cosmwasm.wasm.v1.MsgInstantiateContract',
value: MsgInstantiateContract_pb.encode(
this.toProto(isClassic) as MsgInstantiateContract_pb
).finish(),
});
}
public static unpackAny(
msgAny: Any,
isClassic?: boolean
): MsgInstantiateContract {
return MsgInstantiateContract.fromProto(
MsgInstantiateContract_pb.decode(msgAny.value),
isClassic
);
}
public static fromData(
data: MsgInstantiateContract.Data,
_?: boolean
): MsgInstantiateContract {
const { sender, admin, code_id, label, msg, funds } =
data as MsgInstantiateContract.DataV2;
return new MsgInstantiateContract(
sender,
admin !== '' ? admin : undefined,
Number.parseInt(code_id),
msg,
Coins.fromData(funds),
label
);
}
public toData(_?: boolean): MsgInstantiateContract.Data {
const { sender, admin, code_id, label, init_msg, init_coins } = this;
return {
'@type': '/cosmwasm.wasm.v1.MsgInstantiateContract',
sender,
admin: admin || '',
code_id: code_id.toFixed(),
label,
msg: removeNull(init_msg),
funds: init_coins.toData(),
};
}
}
export namespace MsgInstantiateContract {
export interface AminoV1 {
type: 'wasm/MsgInstantiateContract';
value: {
sender: AccAddress;
admin?: AccAddress;
code_id: string;
init_msg: object | string;
init_coins: Coins.Amino;
};
}
export interface AminoV2 {
type: 'wasm/MsgInstantiateContract';
value: {
sender: AccAddress;
admin?: AccAddress;
code_id: string;
label?: string;
msg: object | string;
funds: Coins.Amino;
};
}
export interface DataV1 {
'@type': '/terra.wasm.v1beta1.MsgInstantiateContract';
sender: AccAddress;
admin: AccAddress;
code_id: string;
init_msg: object | string;
init_coins: Coins.Data;
}
export interface DataV2 {
'@type': '/cosmwasm.wasm.v1.MsgInstantiateContract';
sender: AccAddress;
admin: AccAddress;
code_id: string;
label?: string;
msg: object | string;
funds: Coins.Data;
}
export type Amino = AminoV1 | AminoV2;
export type Data = DataV1 | DataV2;
export type Proto =
| MsgInstantiateContract_legacy_pb
| MsgInstantiateContract_pb;
}