-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathposition_manager.move
More file actions
81 lines (70 loc) · 2.22 KB
/
Copy pathposition_manager.move
File metadata and controls
81 lines (70 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
/// Position manager is responsible for managing the positions of the users.
/// It is used to track the supply and loan shares of the users.
module margin_trading::position_manager;
use sui::table::{Self, Table};
public struct PositionManager has store {
positions: Table<address, Position>,
}
public struct Position has store {
shares: u64,
referral: Option<address>,
}
public(package) fun create_position_manager(ctx: &mut TxContext): PositionManager {
PositionManager {
positions: table::new(ctx),
}
}
/// Increase the supply shares of the user and return outstanding supply shares.
public(package) fun increase_user_supply(
self: &mut PositionManager,
referral: Option<address>,
supply_shares: u64,
ctx: &TxContext,
): (u64, Option<address>) {
let user = ctx.sender();
self.add_supply_entry(referral, ctx);
let user_position = self.positions.borrow_mut(user);
let current_referral = user_position.referral;
user_position.shares = user_position.shares + supply_shares;
user_position.referral = referral;
(user_position.shares, current_referral)
}
/// Decrease the supply shares of the user and return outstanding supply shares.
public(package) fun decrease_user_supply(
self: &mut PositionManager,
supply_shares: u64,
ctx: &TxContext,
): (u64, Option<address>) {
let user = ctx.sender();
let user_position = self.positions.borrow_mut(user);
user_position.shares = user_position.shares - supply_shares;
(user_position.shares, user_position.referral)
}
public(package) fun add_supply_entry(
self: &mut PositionManager,
referral: Option<address>,
ctx: &TxContext,
) {
let user = ctx.sender();
if (!self.positions.contains(user)) {
self
.positions
.add(
user,
Position {
shares: 0,
referral,
},
);
}
}
public(package) fun user_supply_shares(self: &PositionManager, ctx: &TxContext): u64 {
let user = ctx.sender();
if (self.positions.contains(user)) {
self.positions.borrow(user).shares
} else {
0
}
}