Skip to content

Commit

Permalink
Merge pull request #52 from invariant-labs/add-remaining-invaraint-fu…
Browse files Browse the repository at this point in the history
…nctions-to-sdk

add remaining functions to sdk
  • Loading branch information
none00y authored Jun 26, 2024
2 parents ab1fbd5 + af3bd5b commit 0d99cfd
Show file tree
Hide file tree
Showing 36 changed files with 1,692 additions and 146 deletions.
69 changes: 44 additions & 25 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,21 @@ jobs:
- name: Checkout source code
uses: actions/checkout@v3

- name: Cache Crates
uses: actions/cache@v3
with:
path: ~/.cargo
key: ${{ runner.os }}-substrate-contracts-node-${{ hashFiles('rust-toolchain.toml') }}

- name: Install prerequisites
run: sudo apt install -y build-essential clang cmake curl
run: sudo apt install -y build-essential clang cmake curl binaryen

- name: Install Rust toolchain
run: |
rustup install 1.74.1
rustup target add wasm32-unknown-unknown
- name: Install rust 1.78
uses: actions-rs/toolchain@v1
with:
toolchain: 1.78
default: true
target: wasm32-unknown-unknown

- name: Build
run: |
Expand All @@ -38,16 +46,21 @@ jobs:
- name: Checkout source code
uses: actions/checkout@v3

- name: Cache Crates
uses: actions/cache@v3
with:
path: ~/.cargo
key: ${{ runner.os }}-substrate-contracts-node-${{ hashFiles('rust-toolchain.toml') }}

- name: Install prerequisites
run: sudo apt install -y build-essential clang cmake curl
run: sudo apt install -y build-essential clang cmake curl binaryen

- name: Install Rust toolchain
run: |
rustup install 1.74.1
rustup target add wasm32-unknown-unknown
- name: Instal Gear node
run: curl https://get.gear.rs/gear-v1.1.0-x86_64-unknown-linux-gnu.tar.xz | tar Jx
- name: Install rust 1.78
uses: actions-rs/toolchain@v1
with:
toolchain: 1.78
default: true
target: wasm32-unknown-unknown

- name: Run tests script (with building)
run: |
Expand All @@ -62,23 +75,25 @@ jobs:
- name: Checkout source code
uses: actions/checkout@v3

- name: Cache Crates
uses: actions/cache@v3
with:
path: ~/.cargo
key: ${{ runner.os }}-substrate-contracts-node-${{ hashFiles('rust-toolchain.toml') }}

- name: Install prerequisites
run: sudo apt install -y build-essential
run: sudo apt install -y build-essential binaryen

- name: Install Gear node
run: |
mkdir -p target/tmp &&
cd target/tmp &&
curl https://get.gear.rs/gear-v1.3.0-x86_64-unknown-linux-gnu.tar.xz | tar Jx
- name: Install rust 1.78
uses: actions-rs/toolchain@v1
with:
toolchain: 1.78
default: true
target: wasm32-unknown-unknown

- name: Read .nvmrc
run: echo NVMRC=`cat ./sdk/.nvmrc` >> $GITHUB_ENV

- name: Install Rust toolchain
run: |
rustup install 1.74.1
rustup target add wasm32-unknown-unknown

- name: Setup node
uses: actions/setup-node@v4
with:
Expand All @@ -92,6 +107,10 @@ jobs:
chmod +x build.sh
./build.sh
shell: bash

- name: Install Gear node
run: |
cargo xtask node
- name: Test SDK
working-directory: ./sdk
Expand Down
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ default = ["binary-vendor", "wasm-opt"]
test = ["gstd/debug"]

[workspace]
members = ["xtask", "gear-erc20/app", "gear-erc20/wasm", "src/wasm"]
members = ["xtask", "gear-erc20/wasm", "src/wasm"]
package.version = "0.1.0"
package.edition = "2021"

Expand All @@ -61,8 +61,8 @@ gmeta = "1.4.1"
gear-core = { git = "https://github.com/gear-tech/gear.git", tag = "v1.4.1" }
gtest = { git = "https://github.com/gear-tech/gear.git", tag = "v1.4.1" }
gclient = { git = "https://github.com/gear-tech/gear.git", tag = "v1.4.1" }
sails-idl-gen = { git = "https://github.com/gear-tech/sails" }
sails-rtl = { git = "https://github.com/gear-tech/sails" }
sails-idl-gen = { git = "https://github.com/gear-tech/sails", rev = "31a3c1a06817b69040af0c069967dd0b31c9530b" }
sails-rtl = { git = "https://github.com/gear-tech/sails", rev = "31a3c1a06817b69040af0c069967dd0b31c9530b" }

# External
tokio = { version = "1.36", features = ["full"] }
Expand Down
6 changes: 6 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
set -e
rm -rf ./target/wasm32-unknown-unknown/
case $1 in
dev)
cargo build -p invariant-wasm --release --features "test" &&
cargo build -p "gear-erc20-wasm" --release --features "test"
;;
dev-sdk)
cargo build -p invariant-wasm --release &&
cargo build -p "gear-erc20-wasm" --release --features "test"
;;
*)
cargo build -p invariant-wasm --release &&
Expand Down
1 change: 0 additions & 1 deletion gear-erc20/app/tests/test_gtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ fn gtest_test_pausable() {
assert!(res.main_failed());
}

//#[cfg(feature = "test")]
#[test]
fn gtest_test_transfer_fail() {
let sys = System::new();
Expand Down
10 changes: 5 additions & 5 deletions io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Default for InvariantConfig {
#[scale_info(crate = gstd::scale_info)]
pub enum InvariantEvent {
PositionCreatedEvent {
block_timestamp: u64,
timestamp: u64,
address: ActorId,
pool_key: PoolKey,
liquidity_delta: Liquidity,
Expand All @@ -37,8 +37,8 @@ pub enum InvariantEvent {
current_sqrt_price: SqrtPrice,
},
PositionRemovedEvent {
block_timestamp: u64,
caller: ActorId,
timestamp: u64,
address: ActorId,
pool_key: PoolKey,
liquidity: Liquidity,
lower_tick_index: i32,
Expand All @@ -48,13 +48,13 @@ pub enum InvariantEvent {
CrossTickEvent {
timestamp: u64,
address: ActorId,
pool: PoolKey,
pool_key: PoolKey,
indexes: Vec<i32>,
},
SwapEvent {
timestamp: u64,
address: ActorId,
pool: PoolKey,
pool_key: PoolKey,
amount_in: TokenAmount,
amount_out: TokenAmount,
fee: TokenAmount,
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[toolchain]
channel = "stable"
channel = "1.78"
Binary file modified sdk/contracts/gear_erc20/gear_erc20.opt.wasm
Binary file not shown.
8 changes: 4 additions & 4 deletions sdk/contracts/invariant/invariant.idl
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ service Service {
query QuoteRoute : (amount_in: TokenAmount, swaps: vec SwapHop) -> result (TokenAmount, InvariantError);

events {
PositionCreatedEvent: struct { block_timestamp: u64, address: actor_id, pool_key: PoolKey, liquidity_delta: Liquidity, lower_tick: i32, upper_tick: i32, current_sqrt_price: SqrtPrice };
PositionRemovedEvent: struct { block_timestamp: u64, caller: actor_id, pool_key: PoolKey, liquidity: Liquidity, lower_tick_index: i32, upper_tick_index: i32, sqrt_price: SqrtPrice };
CrossTickEvent: struct { timestamp: u64, address: actor_id, pool: PoolKey, indexes: vec i32 };
SwapEvent: struct { timestamp: u64, address: actor_id, pool: PoolKey, amount_in: TokenAmount, amount_out: TokenAmount, fee: TokenAmount, start_sqrt_price: SqrtPrice, target_sqrt_price: SqrtPrice, x_to_y: bool };
PositionCreatedEvent: struct { timestamp: u64, address: actor_id, pool_key: PoolKey, liquidity_delta: Liquidity, lower_tick: i32, upper_tick: i32, current_sqrt_price: SqrtPrice };
PositionRemovedEvent: struct { timestamp: u64, address: actor_id, pool_key: PoolKey, liquidity: Liquidity, lower_tick_index: i32, upper_tick_index: i32, sqrt_price: SqrtPrice };
CrossTickEvent: struct { timestamp: u64, address: actor_id, pool_key: PoolKey, indexes: vec i32 };
SwapEvent: struct { timestamp: u64, address: actor_id, pool_key: PoolKey, amount_in: TokenAmount, amount_out: TokenAmount, fee: TokenAmount, start_sqrt_price: SqrtPrice, target_sqrt_price: SqrtPrice, x_to_y: bool };
}
};

Binary file modified sdk/contracts/invariant/invariant.opt.wasm
Binary file not shown.
9 changes: 6 additions & 3 deletions sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@
"node:local": "../target/tmp/gear --dev",
"node:local:stop": "pkill -9 gear",
"start": "npm run build && node --experimental-wasm-modules --trace-uncaught target/index.js",
"wasm:build": "cd ./src/wasm && wasm-pack build && rm ./pkg/.gitignore",
"test:local": "npm run test:fungible-token && npm run test:deposits && npm run test:invariant && npm run test:protocol-fee",
"wasm:build": "cd ./src/wasm && wasm-pack build --features tsify/js && rm ./pkg/.gitignore",
"test:local": "npm run test:fungible-token && npm run test:deposits && npm run test:invariant && npm run test:protocol-fee && npm run test:position && npm run test:math && npm run test:events",
"test:fungible-token": "npx mocha --config mocharc.json ./tests/fungible-token.test.ts",
"test:invariant": "npx mocha --config mocharc.json ./tests/invariant.test.ts",
"test:protocol-fee": "npx mocha --config mocharc.json ./tests/protocol-fee.test.ts",
"test:deposits": "npx mocha --config mocharc.json ./tests/deposits.test.ts",
"contract:build": "cd ../ && ./build.sh",
"test:position": "npx mocha --config mocharc.json ./tests/position.test.ts",
"test:events": "npx mocha --config mocharc.json ./tests/events.test.ts",
"test:math": "npx mocha --config mocharc.json ./tests/math.test.ts",
"contract:build": "cd ../ && ./build.sh dev-sdk",
"invariant:copy": "cp ../src/wasm/invariant.idl ./contracts/invariant/invariant.idl && cp ../target/wasm32-unknown-unknown/release/invariant_wasm.opt.wasm ./contracts/invariant/invariant.opt.wasm",
"erc-20:copy": "cp ../gear-erc20/wasm/erc20.idl ./contracts/gear_erc20/gear_erc20.idl && cp ../target/wasm32-unknown-unknown/release/gear_erc20_wasm.opt.wasm ./contracts/gear_erc20/gear_erc20.opt.wasm",
"invariant:generate": "npm run invariant:copy && npx sails-js generate ./contracts/invariant/invariant.idl -o ./src/invariant-contract.ts -n InvariantContract",
Expand Down
10 changes: 10 additions & 0 deletions sdk/src/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ export class FungibleToken {
return response()
}

async setTransferFail(flag: boolean) {
if (!this.admin) {
throw new Error('Admin account is required to set transfer failure')
}

const tx = await this.erc20.erc20.setFailTransfer(flag).withGas(this.gasLimit)
const { response } = await tx.withAccount(this.admin).signAndSend()
return response()
}

async transferTx(to: ActorId, amount: bigint) {
return this.erc20.erc20.transfer(to as any, amount as any).withGas(this.gasLimit)
}
Expand Down
3 changes: 2 additions & 1 deletion sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { InvariantEvent } from './schema.js'
export { FungibleToken } from './erc20.js'
export { Invariant } from './invariant.js'
export { Percentage } from 'invariant-vara-wasm'
export { Percentage } from './schema.js'
16 changes: 8 additions & 8 deletions sdk/src/invariant-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,54 +527,54 @@ export class Service {
return result[2].toJSON() as unknown as { ok: TokenAmount } | { err: InvariantError };
}

public subscribeToPositionCreatedEventEvent(callback: (data: { block_timestamp: number | string; address: string; pool_key: PoolKey; liquidity_delta: Liquidity; lower_tick: number; upper_tick: number; current_sqrt_price: SqrtPrice }) => void | Promise<void>): Promise<() => void> {
public subscribeToPositionCreatedEventEvent(callback: (data: { timestamp: number | string; address: string; pool_key: PoolKey; liquidity_delta: Liquidity; lower_tick: number; upper_tick: number; current_sqrt_price: SqrtPrice }) => void | Promise<void>): Promise<() => void> {
return this._program.api.gearEvents.subscribeToGearEvent('UserMessageSent', ({ data: { message } }) => {;
if (!message.source.eq(this._program.programId) || !message.destination.eq(ZERO_ADDRESS)) {
return;
}

const payload = message.payload.toHex();
if (getServiceNamePrefix(payload) === 'Service' && getFnNamePrefix(payload) === 'PositionCreatedEvent') {
callback(this._program.registry.createType('(String, String, {"blockTimestamp":"u64","address":"[u8;32]","poolKey":"PoolKey","liquidityDelta":"Liquidity","lowerTick":"i32","upperTick":"i32","currentSqrtPrice":"SqrtPrice"})', message.payload)[2].toJSON() as any as { block_timestamp: number | string; address: string; pool_key: PoolKey; liquidity_delta: Liquidity; lower_tick: number; upper_tick: number; current_sqrt_price: SqrtPrice });
callback(this._program.registry.createType('(String, String, {"timestamp":"u64","address":"[u8;32]","poolKey":"PoolKey","liquidityDelta":"Liquidity","lowerTick":"i32","upperTick":"i32","currentSqrtPrice":"SqrtPrice"})', message.payload)[2].toJSON() as any as { timestamp: number | string; address: string; pool_key: PoolKey; liquidity_delta: Liquidity; lower_tick: number; upper_tick: number; current_sqrt_price: SqrtPrice });
}
});
}

public subscribeToPositionRemovedEventEvent(callback: (data: { block_timestamp: number | string; caller: string; pool_key: PoolKey; liquidity: Liquidity; lower_tick_index: number; upper_tick_index: number; sqrt_price: SqrtPrice }) => void | Promise<void>): Promise<() => void> {
public subscribeToPositionRemovedEventEvent(callback: (data: { timestamp: number | string; address: string; pool_key: PoolKey; liquidity: Liquidity; lower_tick_index: number; upper_tick_index: number; sqrt_price: SqrtPrice }) => void | Promise<void>): Promise<() => void> {
return this._program.api.gearEvents.subscribeToGearEvent('UserMessageSent', ({ data: { message } }) => {;
if (!message.source.eq(this._program.programId) || !message.destination.eq(ZERO_ADDRESS)) {
return;
}

const payload = message.payload.toHex();
if (getServiceNamePrefix(payload) === 'Service' && getFnNamePrefix(payload) === 'PositionRemovedEvent') {
callback(this._program.registry.createType('(String, String, {"blockTimestamp":"u64","caller":"[u8;32]","poolKey":"PoolKey","liquidity":"Liquidity","lowerTickIndex":"i32","upperTickIndex":"i32","sqrtPrice":"SqrtPrice"})', message.payload)[2].toJSON() as any as { block_timestamp: number | string; caller: string; pool_key: PoolKey; liquidity: Liquidity; lower_tick_index: number; upper_tick_index: number; sqrt_price: SqrtPrice });
callback(this._program.registry.createType('(String, String, {"timestamp":"u64","address":"[u8;32]","poolKey":"PoolKey","liquidity":"Liquidity","lowerTickIndex":"i32","upperTickIndex":"i32","sqrtPrice":"SqrtPrice"})', message.payload)[2].toJSON() as any as { timestamp: number | string; address: string; pool_key: PoolKey; liquidity: Liquidity; lower_tick_index: number; upper_tick_index: number; sqrt_price: SqrtPrice });
}
});
}

public subscribeToCrossTickEventEvent(callback: (data: { timestamp: number | string; address: string; pool: PoolKey; indexes: Array<number> }) => void | Promise<void>): Promise<() => void> {
public subscribeToCrossTickEventEvent(callback: (data: { timestamp: number | string; address: string; pool_key: PoolKey; indexes: Array<number> }) => void | Promise<void>): Promise<() => void> {
return this._program.api.gearEvents.subscribeToGearEvent('UserMessageSent', ({ data: { message } }) => {;
if (!message.source.eq(this._program.programId) || !message.destination.eq(ZERO_ADDRESS)) {
return;
}

const payload = message.payload.toHex();
if (getServiceNamePrefix(payload) === 'Service' && getFnNamePrefix(payload) === 'CrossTickEvent') {
callback(this._program.registry.createType('(String, String, {"timestamp":"u64","address":"[u8;32]","pool":"PoolKey","indexes":"Vec<i32>"})', message.payload)[2].toJSON() as any as { timestamp: number | string; address: string; pool: PoolKey; indexes: Array<number> });
callback(this._program.registry.createType('(String, String, {"timestamp":"u64","address":"[u8;32]","poolKey":"PoolKey","indexes":"Vec<i32>"})', message.payload)[2].toJSON() as any as { timestamp: number | string; address: string; pool_key: PoolKey; indexes: Array<number> });
}
});
}

public subscribeToSwapEventEvent(callback: (data: { timestamp: number | string; address: string; pool: PoolKey; amount_in: TokenAmount; amount_out: TokenAmount; fee: TokenAmount; start_sqrt_price: SqrtPrice; target_sqrt_price: SqrtPrice; x_to_y: boolean }) => void | Promise<void>): Promise<() => void> {
public subscribeToSwapEventEvent(callback: (data: { timestamp: number | string; address: string; pool_key: PoolKey; amount_in: TokenAmount; amount_out: TokenAmount; fee: TokenAmount; start_sqrt_price: SqrtPrice; target_sqrt_price: SqrtPrice; x_to_y: boolean }) => void | Promise<void>): Promise<() => void> {
return this._program.api.gearEvents.subscribeToGearEvent('UserMessageSent', ({ data: { message } }) => {;
if (!message.source.eq(this._program.programId) || !message.destination.eq(ZERO_ADDRESS)) {
return;
}

const payload = message.payload.toHex();
if (getServiceNamePrefix(payload) === 'Service' && getFnNamePrefix(payload) === 'SwapEvent') {
callback(this._program.registry.createType('(String, String, {"timestamp":"u64","address":"[u8;32]","pool":"PoolKey","amountIn":"TokenAmount","amountOut":"TokenAmount","fee":"TokenAmount","startSqrtPrice":"SqrtPrice","targetSqrtPrice":"SqrtPrice","xToY":"bool"})', message.payload)[2].toJSON() as any as { timestamp: number | string; address: string; pool: PoolKey; amount_in: TokenAmount; amount_out: TokenAmount; fee: TokenAmount; start_sqrt_price: SqrtPrice; target_sqrt_price: SqrtPrice; x_to_y: boolean });
callback(this._program.registry.createType('(String, String, {"timestamp":"u64","address":"[u8;32]","poolKey":"PoolKey","amountIn":"TokenAmount","amountOut":"TokenAmount","fee":"TokenAmount","startSqrtPrice":"SqrtPrice","targetSqrtPrice":"SqrtPrice","xToY":"bool"})', message.payload)[2].toJSON() as any as { timestamp: number | string; address: string; pool_key: PoolKey; amount_in: TokenAmount; amount_out: TokenAmount; fee: TokenAmount; start_sqrt_price: SqrtPrice; target_sqrt_price: SqrtPrice; x_to_y: boolean });
}
});
}
Expand Down
Loading

0 comments on commit 0d99cfd

Please sign in to comment.