Skip to content

Commit

Permalink
Merge pull request #359 from classic-terra/fix-wasm-minimal
Browse files Browse the repository at this point in the history
Feat: Support WASM Proposal Types and API (Minimal)
  • Loading branch information
alecande11 authored Jul 6, 2023
2 parents 2092cb2 + ec6a9a3 commit c6e7ca4
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 388 deletions.
74 changes: 9 additions & 65 deletions src/client/lcd/api/WasmAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,6 @@ export class WasmAPI extends BaseAPI {
codeID: number,
params: APIParams = {}
): Promise<CodeInfo> {
if (this.lcd.config.isClassic) {
const endpoint = `/terra/wasm/v1beta1/codes/${codeID}`;
return this.c
.get<{ code_info: CodeInfo.DataV1 }>(endpoint, params)
.then(({ code_info: d }) => ({
code_id: Number.parseInt(d.code_id),
code_hash: d.code_hash,
creator: d.creator,
}));
}
const endpoint = `/cosmwasm/wasm/v1/code/${codeID}`;

return this.c
Expand All @@ -139,19 +129,6 @@ export class WasmAPI extends BaseAPI {
contractAddress: AccAddress,
params: APIParams = {}
): Promise<ContractInfo> {
if (this.lcd.config.isClassic) {
const endpoint = `/terra/wasm/v1beta1/contracts/${contractAddress}`;
return this.c
.get<{ contract_info: ContractInfo.DataV1 }>(endpoint, params)
.then(({ contract_info: d }) => ({
code_id: Number.parseInt(d.code_id),
address: d.address,
creator: d.creator,
admin: d.admin !== '' ? d.admin : undefined,
init_msg: d.init_msg,
}));
}

// new endpoint doesn't return init_msg so have to retrieve it from history
const [historyEntry] = await this.contractHistory(contractAddress);

Expand All @@ -175,33 +152,18 @@ export class WasmAPI extends BaseAPI {
query: object | string,
params: APIParams = {}
): Promise<T> {
if (this.lcd.config.isClassic) {
const endpoint = `/terra/wasm/v1beta1/contracts/${contractAddress}/store`;
return this.c
.get<{ query_result: T }>(endpoint, {
...params,
query_msg: Buffer.from(JSON.stringify(query), 'utf-8').toString(
'base64'
),
})
.then(d => d.query_result);
} else {
const query_msg = Buffer.from(JSON.stringify(query), 'utf-8').toString(
'base64'
);
const endpoint = `/cosmwasm/wasm/v1/contract/${contractAddress}/smart/${query_msg}`;
return this.c
.get<{ data: T }>(endpoint, {
...params,
})
.then(d => d.data);
}
const query_msg = Buffer.from(JSON.stringify(query), 'utf-8').toString(
'base64'
);
const endpoint = `/cosmwasm/wasm/v1/contract/${contractAddress}/smart/${query_msg}`;
return this.c
.get<{ data: T }>(endpoint, {
...params,
})
.then(d => d.data);
}

public async parameters(params: APIParams = {}): Promise<WasmParams> {
if (!this.lcd.config.isClassic) {
throw new Error('Not supported for the network');
}
return this.c
.get<{ params: WasmParams.Data }>(`/terra/wasm/v1beta1/params`, params)
.then(({ params: d }) => ({
Expand All @@ -212,9 +174,6 @@ export class WasmAPI extends BaseAPI {
}

public async pinnedCodes(params: APIParams = {}): Promise<PinnedCodes> {
if (this.lcd.config.isClassic) {
throw new Error('Not supported for the network');
}
return this.c
.get<{ pinned_code: PinnedCodes.Data }>(
`/cosmwasm/wasm/v1/codes/pinned`,
Expand All @@ -230,9 +189,6 @@ export class WasmAPI extends BaseAPI {
query_data: string,
params: APIParams = {}
): Promise<QueryResult> {
if (this.lcd.config.isClassic) {
throw new Error('Not supported for the network');
}
return this.c
.get<{ result: QueryResult.Data }>(
`/cosmwasm/wasm/v1/contract/${contractAddress}/raw/${Buffer.from(
Expand All @@ -251,9 +207,6 @@ export class WasmAPI extends BaseAPI {
query_data: object | string,
params: APIParams = {}
): Promise<QueryResult> {
if (this.lcd.config.isClassic) {
throw new Error('Not supported for the network');
}
return this.c
.get<{ result: QueryResult.Data }>(
`/cosmwasm/wasm/v1/contract/${contractAddress}/smart/${Buffer.from(
Expand All @@ -271,9 +224,6 @@ export class WasmAPI extends BaseAPI {
contractAddress: AccAddress,
params: Partial<PaginationOptions & APIParams> = {}
): Promise<[HistoryEntry[], Pagination]> {
if (this.lcd.config.isClassic) {
throw new Error('Not supported for the network');
}
return this.c
.get<{
entries: HistoryEntry.Data[];
Expand All @@ -289,9 +239,6 @@ export class WasmAPI extends BaseAPI {
contractAddress: AccAddress,
params: Partial<PaginationOptions & APIParams> = {}
): Promise<[Model[], Pagination]> {
if (this.lcd.config.isClassic) {
throw new Error('Not supported for the network');
}
return this.c
.get<{
models: Model.Data[];
Expand All @@ -311,9 +258,6 @@ export class WasmAPI extends BaseAPI {
public async allCodes(
params: Partial<PaginationOptions & APIParams> = {}
): Promise<[CodeInfo[], Pagination]> {
if (this.lcd.config.isClassic) {
throw new Error('Not supported for the network');
}
return this.c
.get<{
codeInfos: CodeInfo.DataV2[];
Expand Down
36 changes: 6 additions & 30 deletions src/core/wasm/proposals/ClearAdminProposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,15 @@ export class ClearAdminProposal extends JSONSerializable<

public static fromAmino(
data: ClearAdminProposal.Amino,
isClassic?: boolean
_isClassic?: boolean
): ClearAdminProposal {
if (isClassic) {
throw new Error('Not supported for the network');
}
const {
value: { title, description, contract },
} = data as ClearAdminProposal.Amino;
return new ClearAdminProposal(title, description, contract);
}

public toAmino(isClassic?: boolean): ClearAdminProposal.Amino {
if (isClassic) {
throw new Error('Not supported for the network');
}
public toAmino(_isClassic?: boolean): ClearAdminProposal.Amino {
const { title, description, contract } = this;
return {
type: 'wasm/ClearAdminProposal',
Expand All @@ -55,22 +49,16 @@ export class ClearAdminProposal extends JSONSerializable<

public static fromProto(
proto: ClearAdminProposal.Proto,
isClassic?: boolean
_isClassic?: boolean
): ClearAdminProposal {
if (isClassic) {
throw new Error('Not supported for the network');
}
return new ClearAdminProposal(
proto.title,
proto.description,
proto.contract
);
}

public toProto(isClassic?: boolean): ClearAdminProposal.Proto {
if (isClassic) {
throw new Error('Not supported for the network');
}
public toProto(_isClassic?: boolean): ClearAdminProposal.Proto {
const { title, description, contract } = this;
return ClearAdminProposal_pb.fromPartial({
title,
Expand All @@ -79,9 +67,6 @@ export class ClearAdminProposal extends JSONSerializable<
});
}
public packAny(isClassic?: boolean): Any {
if (isClassic) {
throw new Error('Not supported for the network');
}
return Any.fromPartial({
typeUrl: '/cosmwasm.wasm.v1.ClearAdminProposal',
value: ClearAdminProposal_pb.encode(this.toProto(isClassic)).finish(),
Expand All @@ -92,9 +77,6 @@ export class ClearAdminProposal extends JSONSerializable<
msgAny: Any,
isClassic?: boolean
): ClearAdminProposal {
if (isClassic) {
throw new Error('Not supported for the network');
}
return ClearAdminProposal.fromProto(
ClearAdminProposal_pb.decode(msgAny.value),
isClassic
Expand All @@ -103,19 +85,13 @@ export class ClearAdminProposal extends JSONSerializable<

public static fromData(
data: ClearAdminProposal.Data,
isClassic?: boolean
_isClassic?: boolean
): ClearAdminProposal {
if (isClassic) {
throw new Error('Not supported for the network');
}
const { title, description, contract } = data as ClearAdminProposal.Data;
return new ClearAdminProposal(title, description, contract);
}

public toData(isClassic?: boolean): ClearAdminProposal.Data {
if (isClassic) {
throw new Error('Not supported for the network');
}
public toData(_isClassic?: boolean): ClearAdminProposal.Data {
const { title, description, contract } = this;
return {
'@type': '/cosmwasm.wasm.v1.ClearAdminProposal',
Expand Down
82 changes: 29 additions & 53 deletions src/core/wasm/proposals/ExecuteContractProposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,8 @@ export class ExecuteContractProposal extends JSONSerializable<

public static fromAmino(
data: ExecuteContractProposal.Amino,
isClassic?: boolean
_isClassic?: boolean
): ExecuteContractProposal {
if (isClassic) {
throw new Error('Not supported for the network');
}
const {
value: { title, description, run_as, contract, msg, funds },
} = data as ExecuteContractProposal.Amino;
Expand All @@ -55,11 +52,8 @@ export class ExecuteContractProposal extends JSONSerializable<
);
}

public toAmino(isClassic?: boolean): ExecuteContractProposal.Amino {
public toAmino(_isClassic?: boolean): ExecuteContractProposal.Amino {
const { title, description, run_as, contract, execute_msg, coins } = this;
if (isClassic) {
throw new Error('Not supported for the network');
}
return {
type: 'wasm/ExecuteContractProposal',
value: {
Expand All @@ -75,11 +69,8 @@ export class ExecuteContractProposal extends JSONSerializable<

public static fromProto(
proto: ExecuteContractProposal.Proto,
isClassic?: boolean
_isClassic?: boolean
): ExecuteContractProposal {
if (isClassic) {
throw new Error('Not supported for the network');
}
return new ExecuteContractProposal(
proto.title,
proto.description,
Expand All @@ -90,33 +81,25 @@ export class ExecuteContractProposal extends JSONSerializable<
);
}

public toProto(isClassic?: boolean): ExecuteContractProposal.Proto {
public toProto(_isClassic?: boolean): ExecuteContractProposal.Proto {
const { title, description, run_as, contract, execute_msg, coins } = this;
if (isClassic) {
throw new Error('Not supported for the network');
} else {
return ExecuteContractProposal_pb.fromPartial({
title,
description,
funds: coins.toProto(),
contract,
runAs: run_as,
msg: Buffer.from(JSON.stringify(removeNull(execute_msg)), 'utf-8'),
});
}
return ExecuteContractProposal_pb.fromPartial({
title,
description,
funds: coins.toProto(),
contract,
runAs: run_as,
msg: Buffer.from(JSON.stringify(removeNull(execute_msg)), 'utf-8'),
});
}

public packAny(isClassic?: boolean): Any {
if (isClassic) {
throw new Error('Not supported for the network');
} else {
return Any.fromPartial({
typeUrl: '/cosmwasm.wasm.v1.ExecuteContractProposal',
value: ExecuteContractProposal_pb.encode(
this.toProto(isClassic)
).finish(),
});
}
return Any.fromPartial({
typeUrl: '/cosmwasm.wasm.v1.ExecuteContractProposal',
value: ExecuteContractProposal_pb.encode(
this.toProto(isClassic)
).finish(),
});
}

public static unpackAny(
Expand All @@ -131,11 +114,8 @@ export class ExecuteContractProposal extends JSONSerializable<

public static fromData(
data: ExecuteContractProposal.Data,
isClassic?: boolean
_isClassic?: boolean
): ExecuteContractProposal {
if (isClassic) {
throw new Error('Not supported for the network');
}
const { title, description, run_as, contract, msg, funds } =
data as ExecuteContractProposal.Data;
return new ExecuteContractProposal(
Expand All @@ -148,21 +128,17 @@ export class ExecuteContractProposal extends JSONSerializable<
);
}

public toData(isClassic?: boolean): ExecuteContractProposal.Data {
public toData(_isClassic?: boolean): ExecuteContractProposal.Data {
const { title, description, run_as, contract, execute_msg, coins } = this;
if (isClassic) {
throw new Error('Not supported for the network');
} else {
return {
'@type': '/cosmwasm.wasm.v1.ExecuteContractProposal',
title,
description,
run_as,
contract,
msg: execute_msg,
funds: coins.toData(),
};
}
return {
'@type': '/cosmwasm.wasm.v1.ExecuteContractProposal',
title,
description,
run_as,
contract,
msg: execute_msg,
funds: coins.toData(),
};
}
}

Expand Down
Loading

0 comments on commit c6e7ca4

Please sign in to comment.