-
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.
Example of programmatic update of JS Smart Contract (#287)
* Example of programmatic update of JS Smart Contract Co-authored-by: Serhii Volovyk <SergeyVolovyk@gmail.com>
- Loading branch information
Showing
6 changed files
with
114 additions
and
0 deletions.
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
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
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,49 @@ | ||
import { Worker } from "near-workspaces"; | ||
import test from "ava"; | ||
import * as fs from "fs"; | ||
import * as path from "path"; | ||
|
||
test.beforeEach(async (t) => { | ||
const worker = await Worker.init(); | ||
const root = worker.rootAccount; | ||
|
||
const ali = await root.createSubAccount("ali"); | ||
|
||
const contract = await root.devDeploy( | ||
"build/programmatic-update-before.wasm" | ||
); | ||
|
||
await contract.call(contract, "init", { manager: ali.accountId }); | ||
|
||
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("the contract can be programmatically updated", async (t) => { | ||
const { ali, contract } = t.context.accounts; | ||
|
||
// ASSERT BEFORE CODE UPDATE | ||
const codeBefore = await contract.viewCodeRaw(); | ||
const beforeDefaultGreeting = await contract.view("get_greeting", {}); | ||
t.is(beforeDefaultGreeting, "Hello"); | ||
|
||
// ACT (UPDATE CODE) | ||
const code = fs.readFileSync( | ||
path.resolve("./build/programmatic-update-after.wasm") | ||
); | ||
await ali.call(contract, "updateContract", code, { | ||
gas: "300" + "0".repeat(12), // 300 Tgas | ||
}); | ||
|
||
// ASSERT AFTER CODE UPDATE | ||
const codeAfter = await contract.viewCodeRaw(); | ||
const afterDefaultGreeting = await contract.view("view_greeting", {}); | ||
t.not(codeBefore, codeAfter, "code should be different after update"); | ||
t.is(afterDefaultGreeting, "Hi"); | ||
}); |
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
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,30 @@ | ||
import { NearBindgen, near, initialize, assert, view } from "near-sdk-js"; | ||
|
||
@NearBindgen({ requireInit: true }) | ||
export class ProgrammaticUpdateAfter { | ||
greeting = "Hello"; | ||
|
||
@initialize({ privateFunction: true }) | ||
init({ manager }: { manager: string }) { | ||
near.log(`Setting manager to be ${manager}`); | ||
near.storageWrite("MANAGER", manager); | ||
} | ||
|
||
@view({}) // Method renamed and return "Hi" when greeting is "Hello" | ||
view_greeting(): string { | ||
return this.greeting.replace("Hello", "Hi"); | ||
} | ||
} | ||
|
||
export function updateContract() { | ||
const manager = near.storageRead("MANAGER"); | ||
assert( | ||
near.predecessorAccountId() === manager, | ||
"Only the manager can update the code" | ||
); | ||
|
||
const promiseId = near.promiseBatchCreate(near.currentAccountId()); | ||
near.promiseBatchActionDeployContract(promiseId, near.input()); | ||
|
||
return near.promiseReturn(promiseId); | ||
} |
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,30 @@ | ||
import { NearBindgen, near, initialize, assert, view } from "near-sdk-js"; | ||
|
||
@NearBindgen({ requireInit: true }) | ||
export class ProgrammaticUpdateBefore { | ||
greeting = "Hello"; | ||
|
||
@initialize({ privateFunction: true }) | ||
init({ manager }: { manager: string }) { | ||
near.log(`Setting manager to be ${manager}`); | ||
near.storageWrite("MANAGER", manager); | ||
} | ||
|
||
@view({}) // This method will be renamed after update and will return "Hi" if greeting is "Hello" | ||
get_greeting(): string { | ||
return this.greeting; | ||
} | ||
} | ||
|
||
export function updateContract() { | ||
const manager = near.storageRead("MANAGER"); | ||
assert( | ||
near.predecessorAccountId() === manager, | ||
"Only the manager can update the code" | ||
); | ||
|
||
const promiseId = near.promiseBatchCreate(near.currentAccountId()); | ||
near.promiseBatchActionDeployContract(promiseId, near.input()); | ||
|
||
return near.promiseReturn(promiseId); | ||
} |