-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example Solana contract using the sealable trie
Introduce solana/trie-example smart contract which uses the sealable trie on Solana chain. The contract as well as its client are quite rudimentary but they allow demonstrating working code.
- Loading branch information
Showing
15 changed files
with
922 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
target/ | ||
/target/ | ||
/dist/ | ||
/node_modules/ | ||
package-lock.json | ||
Cargo.lock |
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,43 @@ | ||
{ | ||
"name": "solana-trie-example", | ||
"version": "0.0.1", | ||
"author": "Michał Nazarewicz <[email protected]>", | ||
"scripts": { | ||
"start": "ts-node solana/trie-example/client/main.ts", | ||
"start-with-test-validator": "start-server-and-test 'solana-test-validator --reset --quiet' http://localhost:8899/health start", | ||
"lint": "eslint --ext .ts solana/trie-example/* && prettier --check \"solana/trie-example/**/*.ts\"", | ||
"lint:fix": "eslint --ext .ts solana/trie-example/* --fix && prettier --write \"solana/trie-example/**/*.ts\"", | ||
"clean": "npm run clean:trie-example", | ||
"build:trie-example": "cargo build-sbf --manifest-path=solana/trie-example/Cargo.toml --sbf-out-dir=dist/trie-example", | ||
"deploy:trie-example": "solana program deploy dist/trie-example/trie.so", | ||
"clean:trie-example": "cargo clean --manifest-path=solana/trie-example/Cargo.toml && rm -rf ./dist", | ||
"test:trie-example": "cargo test-bpf --manifest-path=solana/trie-example/Cargo.toml", | ||
"pretty": "prettier --write 'solana/trie-example/client/*.ts'" | ||
}, | ||
"dependencies": { | ||
"@solana/web3.js": "^1.33.0", | ||
"mz": "^2.7.0", | ||
"tsconfig": "^7.0.0", | ||
"yaml": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"@tsconfig/recommended": "^1.0.1", | ||
"@types/eslint": "^8.2.2", | ||
"@types/eslint-plugin-prettier": "^3.1.0", | ||
"@types/mz": "^2.7.2", | ||
"@types/prettier": "^2.1.5", | ||
"@types/yaml": "^1.9.7", | ||
"@typescript-eslint/eslint-plugin": "^4.6.0", | ||
"@typescript-eslint/parser": "^4.6.0", | ||
"eslint": "^7.12.1", | ||
"eslint-config-prettier": "^6.15.0", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"prettier": "^2.1.2", | ||
"start-server-and-test": "^1.11.6", | ||
"ts-node": "^10.0.0", | ||
"typescript": "^4.0.5" | ||
}, | ||
"engines": { | ||
"node": ">=14.0.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 |
---|---|---|
|
@@ -12,4 +12,4 @@ pub mod trie; | |
#[cfg(test)] | ||
mod test_utils; | ||
|
||
pub use trie::Trie; | ||
pub use trie::{Error, Trie}; |
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,17 @@ | ||
[package] | ||
name = "solana-trie-example" | ||
authors = ["Michal Nazarewicz <[email protected]>"] | ||
version = "0.0.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
name = "trie" | ||
crate-type = ["cdylib", "lib"] | ||
|
||
[dependencies] | ||
derive_more.workspace = true | ||
solana-program.workspace = true | ||
|
||
memory.workspace = true | ||
sealable-trie.workspace = true | ||
stdx.workspace = true |
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,9 @@ | ||
#!/bin/sh | ||
|
||
set -eu | ||
|
||
root=$(git rev-parse --show-toplevel) | ||
cd "$root" | ||
cargo build-sbf \ | ||
--manifest-path=solana/trie-example/Cargo.toml \ | ||
--sbf-out-dir=dist/trie-example |
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,75 @@ | ||
import { | ||
establishConnection, | ||
establishPayer, | ||
checkProgram, | ||
getKey, | ||
setKey, | ||
sealKey, | ||
} from './trie'; | ||
|
||
async function main() { | ||
const operation = parseArgv(process.argv); | ||
if (!operation) { | ||
return; | ||
} | ||
|
||
// Establish connection to the cluster | ||
await establishConnection(); | ||
|
||
// Determine who pays for the fees | ||
await establishPayer(); | ||
|
||
// Check if the program has been deployed | ||
await checkProgram(); | ||
|
||
switch (operation[0]) { | ||
case "get": | ||
await getKey(operation[1]); | ||
break; | ||
case "set": | ||
await setKey(operation[1], operation[2]); | ||
break; | ||
case "seal": | ||
await sealKey(operation[1]); | ||
break; | ||
} | ||
|
||
console.log('Success'); | ||
} | ||
|
||
function parseArgv(argv: string[]): string[] | null { | ||
const cmd = argv[0] + ' ' + argv[1]; | ||
switch (argv[2] || "--help") { | ||
case "get": | ||
case "seal": | ||
if (argv.length != 4) { | ||
break; | ||
} | ||
return [argv[2], argv[3]]; | ||
case "set": | ||
if (argv.length != 5) { | ||
break; | ||
} | ||
return [argv[2], argv[3], argv[4]]; | ||
case "help": | ||
case "--help": | ||
case "-h": { | ||
console.log( | ||
`usage: ${cmd} get <hex-key>\n` + | ||
` ${cmd} set <hex-key> <hex-hash>\n` + | ||
` ${cmd} seal <hex-key>` | ||
) | ||
process.exit(0); | ||
} | ||
} | ||
console.error(`Invalid usage; see ${cmd} --help`); | ||
process.exit(-1); | ||
} | ||
|
||
main().then( | ||
() => process.exit(), | ||
err => { | ||
console.error(err); | ||
process.exit(-1); | ||
}, | ||
); |
Oops, something went wrong.