Skip to content

Commit f1a0bca

Browse files
authored
feat: add changeStudentId function (#38)
1 parent 8b9f971 commit f1a0bca

3 files changed

Lines changed: 376 additions & 0 deletions

File tree

src/IStudentManager.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ interface IStudentManager {
4040
event MileageMinted(bytes32 indexed studentId, address indexed account, address indexed admin, uint256 amount);
4141
event MileageBurned(bytes32 indexed studentId, address indexed account, address indexed admin, uint256 amount);
4242

43+
event StudentIdChanged(bytes32 indexed prevStudentId, bytes32 indexed studentId, address indexed account);
4344
event StudentRecordUpdated(bytes32 indexed studentId, address indexed account, address indexed targetAccount);
4445

4546
event transferMileageToken(bytes32 indexed fromStudentId, bytes32 indexed toStudentId, uint256 amount);

src/StudentManager.impl.sol

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,26 @@ contract StudentManagerImpl is IStudentManager, Initializable, SwMileageTokenFac
106106
emit AccountChanged(studentId, currentAccount, targetAccount);
107107
}
108108

109+
function changeStudentId(
110+
bytes32 nextId
111+
) external whenNotPaused {
112+
require(nextId != bytes32(0), "empty new student ID");
113+
114+
bytes32 currentId = _validateAccount();
115+
require(currentId != nextId, "student IDs must be different");
116+
require(students[nextId] == address(0), "new student ID already exists");
117+
118+
if (pendingAccountChanges[currentId].targetAccount != address(0)) {
119+
delete pendingAccountChanges[currentId];
120+
}
121+
122+
delete students[currentId];
123+
students[nextId] = msg.sender;
124+
studentByAddr[msg.sender] = nextId;
125+
126+
emit StudentIdChanged(currentId, nextId, msg.sender);
127+
}
128+
109129
function getPendingAccountChange(
110130
bytes32 studentId
111131
) external view returns (AccountChangeProposal memory) {
@@ -232,6 +252,25 @@ contract StudentManagerImpl is IStudentManager, Initializable, SwMileageTokenFac
232252
emit AccountChanged(studentId, currentAccount, targetAccount);
233253
}
234254

255+
function migrateStudentId(bytes32 currentId, bytes32 nextId) external onlyAdmin {
256+
require(currentId != bytes32(0) && nextId != bytes32(0), "empty student ID");
257+
require(currentId != nextId, "student IDs must be different");
258+
259+
address account = students[currentId];
260+
require(account != address(0), "student ID not registered");
261+
require(students[nextId] == address(0), "next student ID already exists");
262+
263+
if (pendingAccountChanges[currentId].targetAccount != address(0)) {
264+
delete pendingAccountChanges[currentId];
265+
}
266+
267+
delete students[currentId];
268+
students[nextId] = account;
269+
studentByAddr[account] = nextId;
270+
271+
emit StudentIdChanged(currentId, nextId, account);
272+
}
273+
235274
// Imediately update student record
236275
function updateStudentRecord(bytes32 studentId, address targetAccount, bool _clear) external onlyAdmin {
237276
require(studentId != bytes32(0), "empty student ID");

0 commit comments

Comments
 (0)