|
| 1 | +import { Any } from '@terra-money/terra.proto/google/protobuf/any'; |
| 2 | +import { Setting as Setting_pb } from '@terra-money/terra.proto/terra/smartaccount/v1/setting'; |
| 3 | +import { JSONSerializable } from '../../../../util/json'; |
| 4 | +import { AuthorizationMsg } from './AuthorizationMsg'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Setting holds the contract address and initial message |
| 8 | + * to be passed to the contract for custom authorization |
| 9 | + */ |
| 10 | +export class Setting extends JSONSerializable< |
| 11 | + Setting.Amino, |
| 12 | + Setting.Data, |
| 13 | + Setting.Proto |
| 14 | +> { |
| 15 | + /** |
| 16 | + * |
| 17 | + * @param contractAddress contract address of authorization logic |
| 18 | + * @param initMsg initial message to be passed to the contract |
| 19 | + */ |
| 20 | + constructor( |
| 21 | + public owner: string, |
| 22 | + public authorization: AuthorizationMsg[], |
| 23 | + public preTransaction: string[], |
| 24 | + public postTransaction: string[], |
| 25 | + public fallback: boolean |
| 26 | + ) { |
| 27 | + super(); |
| 28 | + } |
| 29 | + |
| 30 | + public static fromAmino(data: Setting.Amino): Setting { |
| 31 | + const { |
| 32 | + value: { |
| 33 | + owner, |
| 34 | + authorization, |
| 35 | + preTransaction, |
| 36 | + postTransaction, |
| 37 | + fallback, |
| 38 | + }, |
| 39 | + } = data; |
| 40 | + return new Setting( |
| 41 | + owner, |
| 42 | + authorization, |
| 43 | + preTransaction, |
| 44 | + postTransaction, |
| 45 | + fallback |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + public toAmino(): Setting.Amino { |
| 50 | + const { owner, authorization, preTransaction, postTransaction, fallback } = |
| 51 | + this; |
| 52 | + return { |
| 53 | + value: { |
| 54 | + owner, |
| 55 | + authorization, |
| 56 | + preTransaction, |
| 57 | + postTransaction, |
| 58 | + fallback, |
| 59 | + }, |
| 60 | + }; |
| 61 | + } |
| 62 | + |
| 63 | + public static fromData(data: Setting.Data): Setting { |
| 64 | + const { |
| 65 | + owner, |
| 66 | + authorization, |
| 67 | + pre_transaction, |
| 68 | + post_transaction, |
| 69 | + fallback, |
| 70 | + } = data; |
| 71 | + return new Setting( |
| 72 | + owner, |
| 73 | + authorization, |
| 74 | + pre_transaction, |
| 75 | + post_transaction, |
| 76 | + fallback |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + public toData(): Setting.Data { |
| 81 | + const { owner, authorization, preTransaction, postTransaction, fallback } = |
| 82 | + this; |
| 83 | + return { |
| 84 | + owner, |
| 85 | + authorization, |
| 86 | + pre_transaction: preTransaction, |
| 87 | + post_transaction: postTransaction, |
| 88 | + fallback, |
| 89 | + }; |
| 90 | + } |
| 91 | + |
| 92 | + public static fromProto(proto: Setting.Proto): Setting { |
| 93 | + return new Setting( |
| 94 | + proto.owner, |
| 95 | + proto.authorization.map(AuthorizationMsg.fromProto), |
| 96 | + proto.preTransaction, |
| 97 | + proto.postTransaction, |
| 98 | + proto.fallback |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + public toProto(): Setting.Proto { |
| 103 | + const { owner, authorization, preTransaction, postTransaction, fallback } = |
| 104 | + this; |
| 105 | + return Setting_pb.fromPartial({ |
| 106 | + owner, |
| 107 | + authorization, |
| 108 | + preTransaction, |
| 109 | + postTransaction, |
| 110 | + fallback, |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + public packAny(): Any { |
| 115 | + return Any.fromPartial({ |
| 116 | + value: Setting_pb.encode(this.toProto()).finish(), |
| 117 | + }); |
| 118 | + } |
| 119 | + |
| 120 | + public static unpackAny(msgAny: Any): Setting { |
| 121 | + return Setting.fromProto(Setting_pb.decode(msgAny.value)); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +export namespace Setting { |
| 126 | + export interface Amino { |
| 127 | + value: { |
| 128 | + owner: string; |
| 129 | + authorization: AuthorizationMsg[]; |
| 130 | + preTransaction: string[]; |
| 131 | + postTransaction: string[]; |
| 132 | + fallback: boolean; |
| 133 | + }; |
| 134 | + } |
| 135 | + |
| 136 | + export interface Data { |
| 137 | + owner: string; |
| 138 | + authorization: AuthorizationMsg[]; |
| 139 | + pre_transaction: string[]; |
| 140 | + post_transaction: string[]; |
| 141 | + fallback: boolean; |
| 142 | + } |
| 143 | + |
| 144 | + export type Proto = Setting_pb; |
| 145 | +} |
0 commit comments