Skip to content

Commit 39549a0

Browse files
authored
more sway examples (#118)
* Create liquiditypool.ts * Update index.ts (#119) * Update Forc.toml (#120)
1 parent 3561008 commit 39549a0

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { ExampleMenuItem } from "../../components/ExampleDropdown";
22
import { EXAMPLE_SWAY_CONTRACT_COUNTER } from "./counter";
3+
import { EXAMPLE_SWAY_CONTRACT_LIQUIDITY_POOL } from "./liquiditypool";
34
import { EXAMPLE_SWAY_CONTRACT_SINGLEASSET } from "./singleasset";
45
import { EXAMPLE_SWAY_CONTRACT_MULTIASSET } from "./multiasset";
56

67
export const EXAMPLE_SWAY_CONTRACTS: ExampleMenuItem[] = [
78
{ label: "Counter.sw", code: EXAMPLE_SWAY_CONTRACT_COUNTER },
9+
{ label: "LiquidityPool.sw", code: EXAMPLE_SWAY_CONTRACT_LIQUIDITY_POOL },
810
{ label: "SingleAsset.sw", code: EXAMPLE_SWAY_CONTRACT_SINGLEASSET },
911
{ label: "MultiAsset.sw", code: EXAMPLE_SWAY_CONTRACT_MULTIASSET },
1012
];
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
export const EXAMPLE_SWAY_CONTRACT_LIQUIDITY_POOL = `contract;
2+
3+
use std::{
4+
asset::{
5+
mint_to,
6+
transfer,
7+
},
8+
call_frames::msg_asset_id,
9+
constants::DEFAULT_SUB_ID,
10+
context::msg_amount
11+
};
12+
13+
abi LiquidityPool {
14+
fn deposit(recipient: Address);
15+
fn withdraw(recipient: Address);
16+
}
17+
18+
const BASE_ASSET: AssetId = AssetId::from(0x9ae5b658754e096e4d681c548daf46354495a437cc61492599e33fc64dcdc30c);
19+
20+
impl LiquidityPool for Contract {
21+
fn deposit(recipient: Address) {
22+
assert(msg_asset_id() == BASE_ASSET);
23+
assert(msg_amount() > 0);
24+
25+
// Mint two times the amount.
26+
let amount_to_mint = msg_amount() * 2;
27+
28+
// Mint some LP assets based upon the amount of the base asset.
29+
mint_to(Identity::Address(recipient), DEFAULT_SUB_ID, amount_to_mint);
30+
}
31+
32+
fn withdraw(recipient: Address) {
33+
let asset_id = AssetId::default();
34+
assert(msg_asset_id() == asset_id);
35+
assert(msg_amount() > 0);
36+
37+
// Amount to withdraw.
38+
let amount_to_transfer = msg_amount() / 2;
39+
40+
// Transfer base asset to recipient.
41+
transfer(Identity::Address(recipient), BASE_ASSET, amount_to_transfer);
42+
}
43+
}`;

projects/swaypad/Forc.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ name = "swaypad"
66

77
[dependencies]
88
standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.7.0" }
9-
sway_libs = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.25.1" }
9+
sway_libs = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.25.2" }

0 commit comments

Comments
 (0)