forked from near/near-sdk-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic-updates-update.js
85 lines (72 loc) · 2.31 KB
/
basic-updates-update.js
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
import {NearBindgen, call, view, near, migrate, Vector, assert, UnorderedMap, LookupSet, ONE_NEAR} from "near-sdk-js";
import {MigrationDemo} from "../build/state-migration-new.js";
import {Contract} from "../build/nested-collections.js";
const POINT_ONE = ONE_NEAR / 10000n;
class OldPostedMessage {
constructor() {
this.premium = false;
this.sender = "";
this.text = "";
}
}
@NearBindgen({})
export class OldState {
constructor() {
this.messages = new Vector("a");
this.payments = new Vector("b");
}
}
class PostedMessage {
constructor() {
this.payment = 0n;
this.premium = false;
this.sender = "";
this.text = "";
}
static new(payment, premium, sender, text) {
let posted_message = new PostedMessage();
posted_message.payment = payment;
posted_message.premium = premium;
posted_message.sender = sender;
posted_message.text = text;
return posted_message;
}
}
@NearBindgen({})
export class GuestBook {
constructor() {
this.messages = new Vector("a");
}
@migrate({})
migrateState() {
assert(this.messages !== undefined, "Contract state should not be deserialized in @migrate");
// retrieve the current state from the contract
const _state = OldState._getState();
const _contract = OldState._create();
if (_state) {
OldState._reconstruct(_contract, _state);
}
let new_messages = new Vector("p");
_contract.messages.toArray().forEach((posted, idx) => {
let payment = _contract
.payments
.get(idx) || 0n;
new_messages.push(PostedMessage.new(payment, posted.premium, posted.sender, posted.text));
});
_contract.messages.clear();
_contract.payments.clear();
this.messages = new_messages;
}
@call({payableFunction: true})
add_message({text}) {
const payment = near.attachedDeposit();
let premium = payment > POINT_ONE;
const sender = near.predecessorAccountId();
let message = PostedMessage.new(payment, premium, sender, text);
this.messages.push(message);
}
@view({})
get_message({ index }) {
return this.messages.get(index) || null;
}
}