-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add example contracts showcase #294
Closed
Closed
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
6d9e2af
Add example contracts showcase
AurelienFT 1180f9d
try ci
AurelienFT de64386
Add new job
AurelienFT 316ad21
try trigger
AurelienFT 7b6bf7b
try trigger 2
AurelienFT 22ea37d
try new version
AurelienFT 79f00b8
fix ident
AurelienFT 97ea038
fix ident 2
AurelienFT 89c93dc
try new ci
AurelienFT 60ca992
contracts change
AurelienFT c0d737b
try to fix ci
AurelienFT 609ab2b
try final ci with debug
AurelienFT 248fcf9
fix ident
AurelienFT 12c5e41
remove caching
AurelienFT fea3242
fix env variable name
AurelienFT 56bc57c
try a failed test
AurelienFT e6622b3
Remove all debug
AurelienFT 4b03534
Update massa-as-sdk version
AurelienFT 731e8ec
update fee minimal
AurelienFT c34a3f8
update fees deployment
AurelienFT c3cd256
Use new version web3 for the test interactions
AurelienFT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,47 @@ | ||
name: Contract Showcase CI | ||
|
||
on: | ||
pull_request: | ||
paths: | ||
- contracts_showcase/** | ||
|
||
defaults: | ||
run: | ||
working-directory: ./contracts_showcase/ | ||
|
||
jobs: | ||
collect_dirs: | ||
runs-on: ubuntu-latest | ||
outputs: | ||
dirs: ${{ steps.dirs.outputs.dirs }} | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- id: dirs | ||
run: echo "dirs=$(ls -d ./*/ | jq --raw-input --slurp --compact-output 'split("\n")[:-1]')" >> ${GITHUB_OUTPUT} | ||
|
||
run_tests: | ||
needs: collect_dirs | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
dir: ${{ fromJson(needs.collect_dirs.outputs.dirs) }} | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v3 | ||
|
||
- name: Install node | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: "18" | ||
cache: "npm" | ||
|
||
- name: Install dependencies | ||
working-directory: ./contracts_showcase/${{ matrix.dir }} | ||
run: npm install | ||
|
||
- name: Test | ||
working-directory: ./contracts_showcase/${{ matrix.dir }} | ||
run: npm run start | ||
env: | ||
JSON_RPC_URL_PUBLIC: ${{ secrets.JSON_RPC_URL_PUBLIC }} | ||
WALLET_SECRET_KEY: ${{ secrets.WALLET_PRIVATE_KEY }} |
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,7 @@ | ||
## How to add a new contract showcase | ||
|
||
Copy/Paste one of the sub-folder here. | ||
|
||
In the `assembly/contracts` copy the source code of your contract and in `interaction.ts`, deploy your contract and make some possible interactions with it using massa-web3. You can use the examples from the other sub-folder to create yours. | ||
|
||
Try trigger |
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,7 @@ | ||
## Contracts showcase | ||
|
||
In this section you will find examples of smart contracts showcasing usage of objects available in the `massa-as-sdk` package. | ||
|
||
Each of the examples has a `assembly/contracts` folder containing the smart contract codes and a `interaction.ts` file that contains all the deployment and interaction with the smart contract. | ||
|
||
You can go to any folder copy the `.env.example` to a `.env` add your secret key, update the RPC if you don't want to use buildnet and run `npm run start`. It will run all the actions in the `interaction.ts` |
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,2 @@ | ||
WALLET_SECRET_KEY= | ||
JSON_RPC_URL_PUBLIC=https://buildnet.massa.net/api/v2 |
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,2 @@ | ||
.env | ||
node_modules/ |
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,21 @@ | ||
{ | ||
"targets": { | ||
"debug": { | ||
"outFile": "build/main.wasm", | ||
"sourceMap": true, | ||
"debug": true | ||
}, | ||
"release": { | ||
"outFile": "build/main.wasm", | ||
"sourceMap": true, | ||
"optimizeLevel": 3, | ||
"shrinkLevel": 0, | ||
"converge": false, | ||
"noAssert": false | ||
} | ||
}, | ||
"options": { | ||
"exportRuntime": true, | ||
"bindings": "esm" | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
contracts_showcase/accesscontrol/assembly/contracts/main.ts
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 { Args, stringToBytes } from "@massalabs/as-types"; | ||
import { | ||
Address, | ||
Storage, | ||
Context, | ||
caller, | ||
} from "@massalabs/massa-as-sdk"; | ||
import { AccessControl } from "@massalabs/massa-as-sdk/assembly/security"; | ||
|
||
const controller = new AccessControl<u8>(1); | ||
|
||
const ADMIN = controller.newPermission("admin"); | ||
const USER = controller.newPermission("user"); | ||
|
||
export function constructor(argsSer: StaticArray<u8>): void { | ||
if (!Context.isDeployingContract()) { | ||
return; | ||
} | ||
|
||
const args = new Args(argsSer); | ||
const admin = new Address(args.nextString().unwrap()); | ||
const user = new Address(args.nextString().unwrap()); | ||
controller.grantPermission(ADMIN, admin); | ||
controller.grantPermission(USER, admin); | ||
controller.grantPermission(USER, user); | ||
Storage.set("admin", admin.toString()); | ||
} | ||
|
||
export function changeAdmin(argsSer: StaticArray<u8>): void { | ||
const args = new Args(argsSer); | ||
const newAdmin = new Address(args.nextString().unwrap()); | ||
controller.mustHavePermission(ADMIN, caller()); | ||
controller.grantPermission(ADMIN, newAdmin); | ||
controller.revokePermission(ADMIN, caller()); | ||
Storage.set("admin", newAdmin.toString()); | ||
} | ||
|
||
export function getAdmin(): StaticArray<u8> { | ||
controller.mustHaveAnyPermission(USER | ADMIN, caller()); | ||
return stringToBytes(Storage.get("admin")); | ||
} |
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,126 @@ | ||
import { | ||
Account, | ||
AccountOperation, | ||
Args, | ||
JsonRPCClient, | ||
OperationStatus, | ||
SmartContract, | ||
bytesToStr, | ||
} from "@massalabs/massa-web3"; | ||
import path from "path"; | ||
import { fileURLToPath } from "url"; | ||
import { readFileSync } from "fs"; | ||
import * as dotenv from "dotenv"; | ||
|
||
// Load environment variables from .env file | ||
dotenv.config(); | ||
|
||
// // Helper function to read bytecode from file | ||
function getByteCode(folderName: string, fileName: string): Buffer { | ||
// Obtain the current file name and directory paths | ||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = path.dirname(__filename); | ||
return readFileSync(path.join(__dirname, folderName, fileName)); | ||
} | ||
|
||
// // Helper function to get the address from the deployment result | ||
// async function getEventsFromOp(opId: string): Promise<IEvent[]> { | ||
// // Wait for the operation to be speculative success or Speculative failure | ||
// // First promise that resolves stops the execution | ||
// await web3Client | ||
// .smartContracts() | ||
// .awaitMultipleRequiredOperationStatus(opId, [ | ||
// EOperationStatus.SPECULATIVE_SUCCESS, | ||
// EOperationStatus.SPECULATIVE_ERROR, | ||
// ]); | ||
|
||
// // Filter to get the deployment event | ||
// const eventsFilter = { | ||
// start: null, | ||
// end: null, | ||
// original_caller_address: null, | ||
// original_operation_id: opId, | ||
// emitter_address: null, | ||
// is_final: false, | ||
// } as IEventFilter; | ||
|
||
// // Get the deployment event | ||
// const deploymentEvents = await EventPoller.getEventsOnce( | ||
// eventsFilter, | ||
// web3Client | ||
// ); | ||
// return deploymentEvents; | ||
// } | ||
|
||
// Get environment variables | ||
const decimals = 10 ** 9; | ||
const apiUrl = "https://buildnet.massa.net/api/v2"; | ||
|
||
const adminAccount = await Account.fromEnv(); | ||
const adminAddress = adminAccount.address.toString(); | ||
const userAccount = await Account.generate(); | ||
const client = new JsonRPCClient(apiUrl); | ||
const adminAccountOperation = await new AccountOperation(adminAccount, client); | ||
|
||
adminAccountOperation.transfer(userAccount.address, BigInt(10 * decimals)); | ||
|
||
const contract = await SmartContract.deploy( | ||
client, | ||
adminAccount, | ||
{ | ||
byteCode: getByteCode("build", "main.wasm"), | ||
coins: BigInt(10 * decimals), | ||
parameter: new Args() | ||
.addString(adminAddress) | ||
.addString(userAccount.address.toString()) | ||
.serialize(), | ||
}, | ||
{ | ||
waitFinalExecution: false, | ||
} | ||
); | ||
|
||
const adminAddressSC = bytesToStr( | ||
(await contract.read("getAdmin", new Args().serialize())).value | ||
); | ||
if (adminAddressSC !== adminAddress) { | ||
throw new Error("Admin address is not the expected one"); | ||
} | ||
console.log("Admin address is the expected one"); | ||
|
||
console.log( | ||
"Trying to change the admin address with the user account (not allowed)..." | ||
); | ||
try { | ||
await contract.call( | ||
"changeAdmin", | ||
new Args().addString(userAccount.address.toString()).serialize(), | ||
{ | ||
account: userAccount, | ||
} | ||
); | ||
} catch { | ||
console.log("Admin address change failed as expected"); | ||
} | ||
|
||
console.log( | ||
"Trying to change the admin address with the deployer account (allowed)..." | ||
); | ||
const op = await contract.call( | ||
"changeAdmin", | ||
new Args().addString(userAccount.address.toString()).serialize(), | ||
{ | ||
account: adminAccount, | ||
} | ||
); | ||
await op.waitSpeculativeExecution(); | ||
console.log("Admin address changed successfully"); | ||
|
||
const adminAddressSC2 = bytesToStr( | ||
(await contract.read("getAdmin", new Args().serialize())).value | ||
); | ||
if (adminAddressSC2 !== userAccount.address.toString()) { | ||
throw new Error("Admin address is not the expected one"); | ||
} | ||
|
||
process.exit(0); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is exported by massa web3