Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cvm): docs and examples and amount serde improve #4266

Merged
merged 11 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions code/xcvm/flake-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
nodePackages.npm
];
text = ''
echo "generating TypeScript types definitions from JSON schema of CosmWasm contracts"
echo "generating TypeScript types and client definitions from JSON schema of CosmWasm contracts"
cd code/xcvm
npm install
rm --recursive --force dist
Expand All @@ -68,10 +68,6 @@
cargo run --bin gateway --package xc-core
npm run build-xc-core

rm --recursive --force schema s
cargo run --bin interpreter --package cw-xc-interpreter
npm run build-cw-xc-interpreter

npm publish
'';
};
Expand Down
94 changes: 94 additions & 0 deletions code/xcvm/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/// example usage of CVM and MANTIS contracts
import { CwXcCoreClient } from "./dist/cw-xc-core/CwXcCore.client.js"
import { GasPrice } from "@cosmjs/stargate"
import { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate"

import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
import { Coin } from "cosmjs-types/cosmos/base/v1beta1/coin.js";

const print = console.info

/// NOTE: please note that for details of network prefix and RPC please contact Centauri mainnet support
/// NOTE: this is minimal example with no error handling, syntax sugar or elaborated clients,
/// NOTE: only raw clients and types are used, please contact FE team for React integration
print("creating wallet")
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(
// replace with your key
"apart ahead month tennis merge canvas possible cannon lady reward traffic city hamster monitor lesson nasty midnight sniff enough spatial rare multiply keep task",
{
// ensure this prefix is actual
prefix: "centauri",
}
);

/// at least one account must be created with address to use as sender
const sender = (await wallet.getAccounts())[0].address
print(sender)

print("creating RPC client")
// replace with RPC use really use, this RPC may not work at point you call it"
const rawClient = await SigningCosmWasmClient.connectWithSigner("https://rpc.composable.nodestake.top:443", wallet,
{
gasPrice: GasPrice.fromString("0.25ppica")
})

print("checking CVM contract deployed")

/// update sender according you wallet and contract address in official public CVM docs
const client = new CwXcCoreClient(rawClient, sender, "centauri1c676xpc64x9lxjfsvpn7ajw2agutthe75553ws45k3ld46vy8pts0w203g")
/// check official docs about PICA asset id
const PICA = await client.getAssetById({ assetId: "158456325028528675187087900673" });
print(PICA)

print("let transfer PICA Centaur to Osmosis")
const msg = {
executeProgram: {
assets: [
["158456325028528675187087900673", "123456789000"]
],
salt: "737061776e5f776974685f6173736573",
program: {
tag: "737061776e5f776974685f6173736574",
instructions: [
{
spawn: {
network_id: 3,
salt: "737061776e5f776974685f6173736574",
assets: [
[
"158456325028528675187087900673",
{
amount: {
intercept: "123456789000",
slope: "0"
},
is_unit: false
}
]
],
program: {
tag: "737061776e5f776974685f6173736574",
instructions: [
// so we just transferred PICA to Osmosis virtual wallet
// you can encode here transfer to account on Osmosis (from virtual wallet)
// or do exchange, see swap-pica-to-osmosis.json
// or do raw calls of contracts as per specification
// just fill in instructions according shape
]
}
}
}
]
}

},
tip: "centauri1u2sr0p2j75fuezu92nfxg5wm46gu22ywfgul6k",
}

const coin = Coin.fromPartial({ denom: "ppica", amount: "123456789000" })
const result = await client.executeProgram(msg, "auto", null, [coin])

print(result)

// will pair code with @bengalmozzi order contract - which is simpler
// i mostly used this one as example https://github.com/cosmos/cosmjs/blob/main/packages/cosmwasm-stargate/src/signingcosmwasmclient.spec.ts
6 changes: 5 additions & 1 deletion code/xcvm/lib/core/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,18 @@ impl From<u128> for Balance {
}
}

/// See https://en.wikipedia.org/wiki/Linear_equation#Slope%E2%80%93intercept_form_or_Gradient-intercept_form
#[cfg_attr(feature = "std", derive(schemars::JsonSchema))]
#[derive(
Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Encode, Decode, TypeInfo, Serialize, Deserialize,
)]
#[serde(rename_all = "snake_case")]
/// See https://en.wikipedia.org/wiki/Linear_equation#Slope%E2%80%93intercept_form_or_Gradient-intercept_form
pub struct Amount {
/// absolute amount
#[serde(skip_serializing_if = "Option::is_none", default)]
pub intercept: Displayed<u128>,
/// part of MAX_PARTS from remaining after intercept subtraction
#[serde(skip_serializing_if = "Option::is_none", default)]
pub slope: Displayed<u64>,
}

Expand Down
8 changes: 8 additions & 0 deletions code/xcvm/lib/core/src/gateway/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ pub enum QueryMsg {
GetExchangeById { exchange_id: ExchangeId },
}

/// gets all assets in CVM registry without underlying native information
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
#[cfg_attr(feature = "std", derive(schemars::JsonSchema))]
pub struct GetAllAssetsResponse {
pub assets: Vec<AssetId>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
#[cfg_attr(feature = "std", derive(schemars::JsonSchema))]
Expand Down
Loading
Loading