-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #400 from fospring/feat-migrate-example
feat: add example for docs of migration
- Loading branch information
Showing
4 changed files
with
188 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import {NEAR, Worker} from "near-workspaces"; | ||
import test from "ava"; | ||
|
||
test.beforeEach(async (t) => { | ||
const worker = await Worker.init(); | ||
const root = worker.rootAccount; | ||
const contract = await root.devDeploy("./build/basic-updates-base.wasm"); | ||
|
||
const ali = await root.createSubAccount("ali"); | ||
|
||
t.context.worker = worker; | ||
t.context.accounts = { root, contract, ali }; | ||
}); | ||
|
||
test.afterEach.always(async (t) => { | ||
await t.context.worker.tearDown().catch((error) => { | ||
console.log("Failed to tear down the worker:", error); | ||
}); | ||
}); | ||
|
||
test("migration basic updates works", async (t) => { | ||
const { contract, ali } = t.context.accounts; | ||
|
||
await ali.call(contract, "add_message", { text: "hello" }, { attachedDeposit: NEAR.parse("1 N").toJSON() }); | ||
let message0 = await contract.view("get_message", { index: 0 }); | ||
let payment0 = await contract.view("get_payment", { index: 0 }); | ||
console.log("message0= ", message0," payment0=", payment0) | ||
t.assert( | ||
message0.text === "hello" && message0.premium && message0.sender === ali.accountId | ||
); | ||
t.assert(payment0 == NEAR.parse("1 N")); | ||
|
||
await contract.deploy("./build/basic-updates-update.wasm"); | ||
await ali.call(contract, "migrateState", {}); | ||
|
||
let messageUpdated0 = await contract.view("get_message", { index: 0 }); | ||
console.log("messageUpdated0= ", messageUpdated0); | ||
t.assert( | ||
message0.text === messageUpdated0.text && message0.premium === messageUpdated0.premium && message0.sender === messageUpdated0.sender && payment0 == messageUpdated0.payment | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,8 @@ | |
"build:state-migration:original": "near-sdk-js build src/state-migration-original.ts build/state-migration-original.wasm", | ||
"build:state-migration:new": "near-sdk-js build src/state-migration-new.ts build/state-migration-new.wasm", | ||
"build:status-deserialize-class": "near-sdk-js build src/status-deserialize-class.js build/status-deserialize-class.wasm", | ||
"build:basic-updates-base": "near-sdk-js build src/basic-updates-base.js build/basic-updates-base.wasm", | ||
"build:basic-updates-update": "near-sdk-js build src/basic-updates-update.js build/basic-updates-update.wasm", | ||
"test": "ava && pnpm test:counter-lowlevel && pnpm test:counter-ts", | ||
"test:nft": "ava __tests__/standard-nft/*", | ||
"test:ft": "ava __tests__/standard-ft/*", | ||
|
@@ -57,7 +59,8 @@ | |
"test:status-message-borsh": "ava __tests__/test-status-message-borsh.ava.js", | ||
"test:status-message-serialize-err": "ava __tests__/test-status-message-serialize-err.ava.js", | ||
"test:status-message-deserialize-err": "ava __tests__/test-status-message-deserialize-err.ava.js", | ||
"test:status-deserialize-class": "ava __tests__/test-status-deserialize-class.ava.js" | ||
"test:status-deserialize-class": "ava __tests__/test-status-deserialize-class.ava.js", | ||
"test:basic-updates": "ava __tests__/test-basic-updates.ava.js" | ||
}, | ||
"author": "Near Inc <[email protected]>", | ||
"license": "Apache-2.0", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { | ||
NearBindgen, | ||
call, | ||
view, | ||
near, | ||
migrate, | ||
Vector, | ||
assert, | ||
UnorderedMap, | ||
LookupSet, | ||
validateAccountId, ONE_NEAR | ||
} from "near-sdk-js"; | ||
|
||
const POINT_ONE = ONE_NEAR / 10000n; | ||
|
||
class PostedMessage { | ||
constructor() { | ||
this.premium = false; | ||
this.sender = ""; | ||
this.text = ""; | ||
} | ||
|
||
static new(premium, sender, text) { | ||
let posted_message = new PostedMessage(); | ||
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"); | ||
this.payments = new Vector("b"); | ||
} | ||
|
||
@call({payableFunction: true}) | ||
add_message({text}) { | ||
const payment = near.attachedDeposit(); | ||
let premium = payment > POINT_ONE; | ||
const sender = near.predecessorAccountId(); | ||
let message = PostedMessage.new(premium, sender, text); | ||
this.messages.push(message); | ||
this.payments.push(payment); | ||
} | ||
|
||
@view({}) | ||
get_message({ index }) { | ||
return this.messages.get(index) || null; | ||
} | ||
|
||
@view({}) | ||
get_payment({ index }) { | ||
return this.payments.get(index) || null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; | ||
} | ||
} |