diff --git a/README.md b/README.md index d4ffc43..86c50a7 100644 --- a/README.md +++ b/README.md @@ -1,64 +1,93 @@ -# CosmWasm Template +# Atomic Order Token Swap Contract -Template for multi-contract [CosmWasm](https://github.com/CosmWasm/cosmwasm) projects +The Swap contract allows instant swap between two different tokens. Under the hood, it is using atomic orders to place market orders in one or more spot markets. -## How to Use +## Getting started -Install [cargo-make](https://sagiegurari.github.io/cargo-make/): +Anyone can instantiate an instance of the Swap contract. There is version of this contract uploaded to the Injective Mainnet already and can be found on https://explorer.injective.network/code/67/. -```sh -cargo install --force cargo-make -``` +Before instantiating your contract, as the contract owner, you have three questions to answer. -Run formatter: +### 1. Which address should be the fee recipient? -```sh -cargo make fmt -``` +Since orders placed by the Swap contract are order in the Injective Exchange Module, that means each order can have a fee recipient which can receive 40% of the trading fee. Typically, Exchange dApps would set the fee recipient as their own addresses. -Run tests: +### 2. What tokens should this contract support? -```sh -cargo make test -``` +Each token available in the contract must have a route defined. Route means which markets should token A go through in order to get token B. For example, if you would like to support swapping between ATOM and INJ, then you would have to set route by providing the contract the market IDs of ATOM/USDT and INJ/USDT, so that the it knows the route of swapping ATOM and INJ would be ATOM <> USDT <> INJ. -Run linter (clippy): +At this moment, the contract can only support markets quoted in USDT. -```sh -cargo make lint -``` +### 3. How much buffer should be provided to this contract? -Check for unused dependencies: +As the contract owner, you also have to provide funds to the contract which will be used when the swap happens. The buffer is used by the contract when it place orders. If the user wants to swap a big amount or swap in an illiquid market, then more buffer is required. An error will occur when the contract buffer cannot satisfy the user's input amount. -```sh -cargo make udeps -``` +At this moment, the buffer should only be in USDT. + +## Messages + +### Instantiate -Compile all contracts using [rust-optimizer](https://github.com/CosmWasm/rust-optimizer): +Initializes the contract state with the contract version and configuration details. The config includes an administrator address and a fee recipient address. -```sh -cargo make optimize +```rust +pub fn instantiate( + deps: DepsMut, + env: Env, + info: MessageInfo, + msg: InstantiateMsg, +) -> Result, ContractError> ``` -Once optimized, verify the wasm binaries are ready to be uploaded to the blockchain: +### Execute -```sh -cargo make check +Handles different types of transactions and admin functions: + +- SwapMinOutput: Swap with the minimum output quantity. +- SwapExactOutput: Swap with an exact output quantity. +- SetRoute: Set a swap route. +- DeleteRoute: Delete a swap route. +- UpdateConfig: Update the contract configuration. +- WithdrawSupportFunds: Withdraw the support funds from the contract. + +```rust +pub fn execute( + deps: DepsMut, + env: Env, + info: MessageInfo, + msg: ExecuteMsg, +) -> Result, ContractError> ``` -Generate JSON schema for all contracts: +### Reply -```sh -cargo make schema +Handles the replies from other contracts or transactions. + +```rust +pub fn reply( + deps: DepsMut, + env: Env, + msg: Reply, +) -> Result, ContractError> ``` -Publish contracts and packages to [crates.io](https://crates.io/): +### Query + +Handles various queries to the contract: -```sh -cargo make publish +- GetRoute: Get a specific swap route. +- GetOutputQuantity: Get the output quantity for a given input quantity. +- GetInputQuantity: Get the input quantity for a given output quantity. +- GetAllRoutes: Get all available swap routes. + +```rust +pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult ``` -**NOTE:** For the last two tasks (schema and publish), you need to update the shell script in [`Makefile.toml`](./Makefile.toml) for them to work. +## Disclaimer + +This contract is designed for educational purposes only. The contributors to this codebase are not liable to any future usage. + ## License diff --git a/examples.sh b/examples.sh new file mode 100644 index 0000000..6829463 --- /dev/null +++ b/examples.sh @@ -0,0 +1,41 @@ +# Variables +KBT="--keyring-backend test" +CHAIN_ID="injective-1" +HOME="--home ." +GAS_AND_FEE="--gas=6000000 --gas-prices=500000000inj" +NODE="https://k8s.global.mainnet.tm.injective.network:443" +SYNC_MODE="--broadcast-mode sync" +USER="swap-exec" +code_id="67" +ADMIN="inj1exmuhajlxg08l4a59rchsjycxk42dgydg7u62l" + +# MAINNET MARKETS +INJUSDT=0xa508cb32923323679f29a032c70342c147c17d0145625922b0ef22e955c844c0 +ATOMUSDT=0x0511ddc4e6586f3bfe1acb2dd905f8b8a82c97e1edaef654b12ca7e6031ca0fa +WETHUSDT=0xd1956e20d74eeb1febe31cd37060781ff1cb266f49e0512b446a5fafa9a16034 +WMATICUSDT=0xb9a07515a5c239fcbfa3e25eaa829a03d46c4b52b9ab8ee6be471e9eb0e9ea31 +USDCUSDCET=0xda0bb7a7d8361d17a9d2327ed161748f33ecbf02738b45a7dd1d812735d1531c +SOMMUSDT=0x0686357b934c761784d58a2b8b12618dfe557de108a220e06f8f6580abb83aab +GFUSDT=0x7f71c4fba375c964be8db7fc7a5275d974f8c6cdc4d758f2ac4997f106bb052b + +# MAINNET DENOMS +USDT=peggy0xdAC17F958D2ee523a2206206994597C13D831ec7 +WETH=peggy0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 +ATOM=ibc/C4CFF46FD6DE35CA4CF4CE031E643C8FDC9BA4B99AE598E9B0ED98FE3A2319F9 +WMATIC=factory/inj14ejqjyq8um4p3xfqj74yld5waqljf88f9eneuk/inj1dxv423h8ygzgxmxnvrf33ws3k94aedfdevxd8h +SOL=factory/inj14ejqjyq8um4p3xfqj74yld5waqljf88f9eneuk/inj1sthrn5ep8ls5vzz8f9gp89khhmedahhdkqa8z3 +SOMM=ibc/34346A60A95EB030D62D6F5BDD4B745BE18E8A693372A8A347D5D53DBBB1328B +USDCET=factory/inj14ejqjyq8um4p3xfqj74yld5waqljf88f9eneuk/inj1q6zlut7gtkzknkk773jecujwsdkgq882akqksk +GF=peggy0xAaEf88cEa01475125522e117BFe45cF32044E238 + + + +# INSTANTIATE +INIT='{"admin":"'$ADMIN_ADDRESS'", "fee_recipient":{"address": "'$FEE_RECIPIENT_ADDRESS'"}}' +INSTANTIATE_TX_HASH=$(yes 12345678 | injectived tx wasm instantiate $CODE_ID "$INIT" --label="Your Swap Contract" \ +--from=$USER --chain-id="$CHAIN_ID" --yes --admin=$USER $HOME --node=$NODE \ +$GAS_AND_FEE $SYNC_MODE $KBT ) + +# SET ROUTE +INJ_USDT_ROUTE='{"set_route":{"source_denom":"inj","target_denom":"'$USDT'","route":["'$INJUSDT'"]}}' +TX_HASH=$(injectived tx wasm execute $SWAP_CONTRACT_ADDRESS "$INJ_USDT_ROUTE" $HOME --from=$USER $KBT --chain-id=$CHAIN_ID --yes $GAS_AND_FEE --node=$NODE | grep txhash | awk '{print $2}') \ No newline at end of file