forked from sui-foundation/sui-move-intro-course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transcript.move
113 lines (97 loc) · 3.78 KB
/
transcript.move
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (c) 2022, Sui Foundation
// SPDX-License-Identifier: Apache-2.0
/// A basic object example for Sui Move, part of the Sui Move intro course:
/// https://github.com/sui-foundation/sui-move-intro-course
///
module sui_intro_unit_two::transcript {
use sui::object::{Self, ID, UID};
use sui::tx_context::{Self, TxContext};
use sui::transfer;
use sui::event;
struct WrappableTranscript has key, store {
id: UID,
history: u8,
math: u8,
literature: u8,
}
struct Folder has key {
id: UID,
transcript: WrappableTranscript,
intended_address: address
}
struct TeacherCap has key {
id: UID
}
/// Event marking when a transcript has been requested
struct TranscriptRequestEvent has copy, drop {
// The Object ID of the transcript wrapper
wrapper_id: ID,
// The requester of the transcript
requester: address,
// The intended address of the transcript
intended_address: address,
}
// Error code for when a non-intended address tries to unpack the transcript wrapper
const ENotIntendedAddress: u64 = 1;
/// Module initializer is called only once on module publish.
fun init(ctx: &mut TxContext) {
transfer::transfer(TeacherCap {
id: object::new(ctx)
}, tx_context::sender(ctx))
}
public entry fun add_additional_teacher(_: &TeacherCap, new_teacher_address: address, ctx: &mut TxContext){
transfer::transfer(
TeacherCap {
id: object::new(ctx)
},
new_teacher_address
)
}
public entry fun create_wrappable_transcript_object(_: &TeacherCap, history: u8, math: u8, literature: u8, ctx: &mut TxContext){
let wrappableTranscript = WrappableTranscript {
id: object::new(ctx),
history,
math,
literature,
};
transfer::transfer(wrappableTranscript, tx_context::sender(ctx))
}
// You are allowed to retrieve the score but cannot modify it
public fun view_score(transcriptObject: &WrappableTranscript): u8{
transcriptObject.literature
}
// You are allowed to view and edit the score but not allowed to delete it
public entry fun update_score(_: &TeacherCap, transcriptObject: &mut WrappableTranscript, score: u8){
transcriptObject.literature = score
}
// You are allowed to do anything with the score, including view, edit, delete the entire transcript itself.
public entry fun delete_transcript(_: &TeacherCap, transcriptObject: WrappableTranscript){
let WrappableTranscript {id, history: _, math: _, literature: _ } = transcriptObject;
object::delete(id);
}
public entry fun request_transcript(transcript: WrappableTranscript, intended_address: address, ctx: &mut TxContext){
let folderObject = Folder {
id: object::new(ctx),
transcript,
intended_address
};
event::emit(TranscriptRequestEvent {
wrapper_id: object::uid_to_inner(&folderObject.id),
requester: tx_context::sender(ctx),
intended_address,
});
//We transfer the wrapped transcript object directly to the intended address
transfer::transfer(folderObject, intended_address);
}
public entry fun unpack_wrapped_transcript(folder: Folder, ctx: &mut TxContext){
// Check that the person unpacking the transcript is the intended viewer
assert!(folder.intended_address == tx_context::sender(ctx), ENotIntendedAddress);
let Folder {
id,
transcript,
intended_address:_,
} = folder;
transfer::transfer(transcript, tx_context::sender(ctx));
object::delete(id)
}
}