Skip to content

Commit

Permalink
add typescript for contract interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
TropicalDog17 committed Nov 25, 2023
1 parent a84c917 commit 18c9e0e
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ pub mod execute {
coin.amount.into()
};
let asked_asset = pair_info.1.clone();

Check failure on line 136 in src/contract.rs

View workflow job for this annotation

GitHub Actions / Lints

redundant clone
// Pair of hINJ-INJ on testnet

let swap_astro_msg = pair::ExecuteMsg::Swap {
offer_asset: Asset::native(&offer_asset, amount),
Expand All @@ -149,7 +148,6 @@ pub mod execute {
msg: to_json_binary(&swap_astro_msg)?,
funds: coins(amount, &offer_asset),
};
assert!(offer_asset == "inj");
let submessage = SubMsg::reply_on_success(exec_cw20_mint_msg, SWAP_REPLY_ID);
let res = Response::new()
.add_submessage(submessage)
Expand Down Expand Up @@ -200,12 +198,12 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
}

pub mod query {
use crate::contract::*;
use crate::msg::{GetPairResponse, GetPoolAddrResponse};
use crate::state::POOL_INFO;
use crate::{contract::*, error};
use cosmwasm_std::StdResult;

pub fn query_pair(deps: Deps, env: Env, pool_address: String) -> StdResult<GetPairResponse> {
pub fn query_pair(deps: Deps, _env: Env, pool_address: String) -> StdResult<GetPairResponse> {
let pair: Vec<_> = POOL_INFO
.idx
.address
Expand All @@ -228,7 +226,7 @@ pub mod query {
}
pub fn query_pool_addr(
deps: Deps,
env: Env,
_env: Env,
token_1: String,
token_2: String,
) -> StdResult<GetPoolAddrResponse> {
Expand All @@ -252,7 +250,7 @@ pub mod query {
.iter()
.map(|pool_info| pool_info.1.address.to_string().clone())

Check failure on line 251 in src/contract.rs

View workflow job for this annotation

GitHub Actions / Lints

redundant clone
.collect::<Vec<_>>();
return Ok(GetPoolAddrResponse { pool_addresses });
Ok(GetPoolAddrResponse { pool_addresses })
}
}

Expand All @@ -261,7 +259,6 @@ mod tests {
use crate::msg::GetPairResponse;

use super::*;
use cosmwasm_std::coins;
use cosmwasm_std::Addr;
use cw_multi_test::{App, ContractWrapper, Executor};

Expand Down
122 changes: 122 additions & 0 deletions ts/LqExpress.client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/**
* This file was automatically generated by @cosmwasm/[email protected].
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
* and run the @cosmwasm/ts-codegen generate command to regenerate this file.
*/

import { CosmWasmClient, SigningCosmWasmClient, ExecuteResult } from "@cosmjs/cosmwasm-stargate";
import { Coin, StdFee } from "@cosmjs/amino";
import { InstantiateMsg, ExecuteMsg, QueryMsg, GetPairResponse, GetPoolAddrResponse } from "./LqExpress.types";
export interface LqExpressReadOnlyInterface {
contractAddress: string;
getPair: ({
poolAddress
}: {
poolAddress: string;
}) => Promise<GetPairResponse>;
getPoolAddr: ({
token1,
token2
}: {
token1: string;
token2: string;
}) => Promise<GetPoolAddrResponse>;
}
export class LqExpressQueryClient implements LqExpressReadOnlyInterface {
client: CosmWasmClient;
contractAddress: string;

constructor(client: CosmWasmClient, contractAddress: string) {
this.client = client;
this.contractAddress = contractAddress;
this.getPair = this.getPair.bind(this);
this.getPoolAddr = this.getPoolAddr.bind(this);
}

getPair = async ({
poolAddress
}: {
poolAddress: string;
}): Promise<GetPairResponse> => {
return this.client.queryContractSmart(this.contractAddress, {
get_pair: {
pool_address: poolAddress
}
});
};
getPoolAddr = async ({
token1,
token2
}: {
token1: string;
token2: string;
}): Promise<GetPoolAddrResponse> => {
return this.client.queryContractSmart(this.contractAddress, {
get_pool_addr: {
token_1: token1,
token_2: token2
}
});
};
}
export interface LqExpressInterface extends LqExpressReadOnlyInterface {
contractAddress: string;
sender: string;
astro: ({
pairAddress
}: {
pairAddress: string;
}, fee?: number | StdFee | "auto", memo?: string, _funds?: Coin[]) => Promise<ExecuteResult>;
addSupportedPool: ({
poolAddress,
token1,
token2
}: {
poolAddress: string;
token1: string;
token2: string;
}, fee?: number | StdFee | "auto", memo?: string, _funds?: Coin[]) => Promise<ExecuteResult>;
}
export class LqExpressClient extends LqExpressQueryClient implements LqExpressInterface {
client: SigningCosmWasmClient;
sender: string;
contractAddress: string;

constructor(client: SigningCosmWasmClient, sender: string, contractAddress: string) {
super(client, contractAddress);
this.client = client;
this.sender = sender;
this.contractAddress = contractAddress;
this.astro = this.astro.bind(this);
this.addSupportedPool = this.addSupportedPool.bind(this);
}

astro = async ({
pairAddress
}: {
pairAddress: string;
}, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise<ExecuteResult> => {
return await this.client.execute(this.sender, this.contractAddress, {
astro: {
pair_address: pairAddress
}
}, fee, memo, _funds);
};
addSupportedPool = async ({
poolAddress,
token1,
token2
}: {
poolAddress: string;
token1: string;
token2: string;
}, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise<ExecuteResult> => {
return await this.client.execute(this.sender, this.contractAddress, {
add_supported_pool: {
pool_address: poolAddress,
token_1: token1,
token_2: token2
}
}, fee, memo, _funds);
};
}
35 changes: 35 additions & 0 deletions ts/LqExpress.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* This file was automatically generated by @cosmwasm/[email protected].
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
* and run the @cosmwasm/ts-codegen generate command to regenerate this file.
*/

export interface InstantiateMsg {}
export type ExecuteMsg = {
astro: {
pair_address: string;
};
} | {
add_supported_pool: {
pool_address: string;
token_1: string;
token_2: string;
};
};
export type QueryMsg = {
get_pair: {
pool_address: string;
};
} | {
get_pool_addr: {
token_1: string;
token_2: string;
};
};
export interface GetPairResponse {
token_1: string;
token_2: string;
}
export interface GetPoolAddrResponse {
pool_addresses: string[];
}

0 comments on commit 18c9e0e

Please sign in to comment.