tests: example rust contracts moved from warp-wasm-templates

This commit is contained in:
robal
2023-03-17 17:55:17 +01:00
parent c1d8fb7a52
commit dbe52e82cd
75 changed files with 17993 additions and 1 deletions

225
crates/toy-contract/Cargo.lock generated Normal file
View File

@@ -0,0 +1,225 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "bumpalo"
version = "3.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "js-sys"
version = "0.3.61"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "445dde2150c55e483f3d8416706b97ec8e8237c307e5b7b4b8dd15e6af2a0730"
dependencies = [
"wasm-bindgen",
]
[[package]]
name = "log"
version = "0.4.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
dependencies = [
"cfg-if",
]
[[package]]
name = "once_cell"
version = "1.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3"
[[package]]
name = "proc-macro2"
version = "1.0.51"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d727cae5b39d21da60fa540906919ad737832fe0b1c165da3a34d6548c849d6"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b"
dependencies = [
"proc-macro2",
]
[[package]]
name = "serde"
version = "1.0.152"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde-wasm-bindgen"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3b143e2833c57ab9ad3ea280d21fd34e285a42837aeb0ee301f4f41890fa00e"
dependencies = [
"js-sys",
"serde",
"wasm-bindgen",
]
[[package]]
name = "serde_derive"
version = "1.0.152"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af487d118eecd09402d70a5d72551860e788df87b464af30e5ea6a38c75c541e"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "toy-contract"
version = "0.1.0"
dependencies = [
"js-sys",
"serde",
"serde-wasm-bindgen",
"warp-contracts",
"wasm-bindgen",
"wasm-bindgen-futures",
]
[[package]]
name = "unicode-ident"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc"
[[package]]
name = "warp-contracts"
version = "0.1.2"
dependencies = [
"js-sys",
"serde",
"serde-wasm-bindgen",
"warp-contracts-core",
"warp-contracts-macro",
"wasm-bindgen",
"wasm-bindgen-futures",
]
[[package]]
name = "warp-contracts-core"
version = "0.1.2"
dependencies = [
"serde",
"serde-wasm-bindgen",
"wasm-bindgen",
"wasm-bindgen-futures",
]
[[package]]
name = "warp-contracts-macro"
version = "0.1.2"
dependencies = [
"quote",
"syn",
"warp-contracts-core",
]
[[package]]
name = "wasm-bindgen"
version = "0.2.84"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "31f8dcbc21f30d9b8f2ea926ecb58f6b91192c17e9d33594b3df58b2007ca53b"
dependencies = [
"cfg-if",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.84"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95ce90fd5bcc06af55a641a86428ee4229e44e07033963a2290a8e241607ccb9"
dependencies = [
"bumpalo",
"log",
"once_cell",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-futures"
version = "0.4.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f219e0d211ba40266969f6dbdd90636da12f75bee4fc9d6c23d1260dadb51454"
dependencies = [
"cfg-if",
"js-sys",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.84"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c21f77c0bedc37fd5dc21f897894a5ca01e7bb159884559461862ae90c0b4c5"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.84"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2aff81306fcac3c7515ad4e177f521b5c9a15f2b08f4e32d823066102f35a5f6"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.84"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0046fef7e28c3804e5e38bfa31ea2a0f73905319b677e57ebe37e49358989b5d"
[[package]]
name = "web-sys"
version = "0.3.61"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e33b99f4b23ba3eec1a53ac264e35a755f00e966e0065077d6027c0f575b0b97"
dependencies = [
"js-sys",
"wasm-bindgen",
]

View File

@@ -0,0 +1,18 @@
[package]
name = "toy-contract"
version = "0.1.0"
edition = "2021"
description = "test"
repository = "not applicable"
license = "MIT"
[lib]
crate-type = ["cdylib"]
[dependencies]
warp-contracts = { version = "0.1.2", path = "../warp-contracts" }
wasm-bindgen = "=0.2.84"
wasm-bindgen-futures = { version = "0.4.34" }
serde = { version = "1.0", features = ["derive"] }
serde-wasm-bindgen = "0.5"
js-sys = "0.3.61"

View File

View File

View File

@@ -0,0 +1,16 @@
module.exports = {
// Automatically clear mock calls and instances between every test
clearMocks: true,
moduleFileExtensions: ['ts', 'js'],
testPathIgnorePatterns: ['/tests\/data/'],
testEnvironment: 'node',
transformIgnorePatterns: ['<rootDir>/node_modules/(?!@assemblyscript/.*)'],
transform: {
'^.+\\.(ts|js)$': 'ts-jest',
},
};

View File

@@ -0,0 +1,64 @@
{
"name": "warp-rust-template",
"version": "0.0.1",
"description": "Warp WASM Rust contract template",
"main": "index.js",
"author": "Redstone Team <dev@redstone.finance>",
"license": "MIT",
"scripts": {
"build": "wasm-pack build --target nodejs --release --out-name rust-contract .",
"build:dev": "wasm-pack build --target nodejs --dev --out-name rust-contract .",
"build:profiling": "wasm-pack build --target nodejs --profiling --out-name rust-contract .",
"gen-bindings": "yarn gen-json && yarn gen-ts",
"gen-json": "GENERATE_JSON=1 cargo test generate_json -- --nocapture && yarn format:json-bindings",
"gen-ts": "ts-node --transpileOnly ./scripts/generate-ts.ts && yarn format:ts-bindings",
"test": "jest ./tests",
"lint": "eslint . --ext .ts",
"lint:fix": "eslint . --ext .ts --fix",
"format:json-bindings": "eslint 'contract/definition/bindings/json/*.json' --fix",
"format:ts-bindings": "eslint 'contract/definition/bindings/ts/*.ts' --fix",
"prettier:format": "prettier --config .prettierrc '**/**/*.ts' --write",
"deploy:local": "yarn build && node deploy/local/deploy-local.js",
"deploy:testnet": "yarn build && node deploy/testnet/deploy-test.js",
"deploy:mainnet": "yarn build && node deploy/mainnet/deploy-mainnet.js",
"read:local": "node deploy/local/read-contract-state-local.js",
"read:testnet": "node deploy/testnet/read-contract-state-testnet.js",
"read:mainnet": "node deploy/mainnet/read-contract-state-mainnet.js",
"transfer:local": "node deploy/local/interact-transfer-local.js",
"transfer:testnet": "node deploy/testnet/interact-transfer-testnet.js",
"transfer:mainnet": "node deploy/mainnet/interact-transfer-mainnet.js",
"balance:local": "node deploy/local/interact-balance-local.js",
"balance:testnet": "node deploy/testnet/interact-balance-testnet.js",
"balance:mainnet": "node deploy/mainnet/interact-balance-mainnet.js"
},
"engines": {
"node": ">=16.5"
},
"dependencies": {
"arlocal": "^1.1.59",
"arweave": "1.12.4",
"json-schema-to-typescript": "^11.0.1",
"typescript": "^4.6.2",
"warp-contracts": "1.2.56",
"warp-contracts-plugin-deploy": "^1.0.0"
},
"resolutions": {
"arweave": "1.12.4"
},
"overrides": {
"arweave": "1.12.4"
},
"devDependencies": {
"@types/jest": "^27.4.1",
"@types/node": "^18.6.3",
"@typescript-eslint/eslint-plugin": "^5.40.0",
"@typescript-eslint/parser": "^5.40.0",
"eslint": "^8.25.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.1",
"jest": "^28.1.3",
"prettier": "^2.3.2",
"ts-jest": "^28.0.7",
"ts-node": "^10.9.1"
}
}

View File

@@ -0,0 +1,28 @@
use serde::{Serialize, Deserialize};
use warp_contracts::{warp_contract, handler_result::{WriteResult, ViewResult}};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct State {
x: u8,
}
#[derive(Debug, Deserialize)]
pub struct Action {
x: u8,
}
#[derive(Debug, Serialize)]
pub struct View {
x: u8,
}
#[warp_contract(write)]
pub fn handle(mut state: State, action: Action) -> WriteResult<State, ()> {
state.x = action.x;
WriteResult::Success(state)
}
#[warp_contract(view)]
pub fn view(state: &State, _action: Action) -> ViewResult<View, ()> {
ViewResult::Success(View { x: state.x })
}

View File

@@ -0,0 +1,116 @@
import fs from 'fs';
import ArLocal from 'arlocal';
import Arweave from 'arweave';
import { JWKInterface } from 'arweave/node/lib/wallet';
import {
LoggerFactory,
Warp,
SmartWeaveTags,
WarpFactory,
TagsParser,
ArweaveWrapper,
WasmSrc,
Contract
} from 'warp-contracts';
import { DeployPlugin } from 'warp-contracts-plugin-deploy';
import path from 'path';
jest.setTimeout(30000);
class State {
constructor(public x: number) {}
}
class View {
constructor(public x: number) {}
}
class Action {
constructor(public x: number) {}
}
describe('Testing the Rust WASM Profit Sharing Token', () => {
let contractSrc: Buffer;
let contractGlueCodeFile: string;
let wallet: JWKInterface;
const initialState = new State(0);
let arweave: Arweave;
let arlocal: ArLocal;
let warp: Warp;
let toyContract: Contract<State>;
let contractTxId: string;
let arweaveWrapper: ArweaveWrapper;
let tagsParser: TagsParser;
beforeAll(async () => {
arlocal = new ArLocal(1820, false);
await arlocal.start();
tagsParser = new TagsParser();
LoggerFactory.INST.logLevel('debug');
LoggerFactory.INST.logLevel('debug', 'WASM:Rust');
//LoggerFactory.INST.logLevel('debug', 'WasmContractHandlerApi');
warp = WarpFactory.forLocal(1820).use(new DeployPlugin());
({ arweave } = warp);
arweaveWrapper = new ArweaveWrapper(arweave);
({ jwk: wallet } = await warp.generateWallet());
contractSrc = fs.readFileSync(path.join(__dirname, '../pkg/rust-contract_bg.wasm'));
const contractSrcCodeDir: string = path.join(__dirname, '../src');
contractGlueCodeFile = path.join(__dirname, '../pkg/rust-contract.js');
// deploying contract using the new SDK.
({ contractTxId } = await warp.deploy({
wallet,
initState: JSON.stringify(initialState),
src: contractSrc,
wasmSrcCodeDir: contractSrcCodeDir,
wasmGlueCode: contractGlueCodeFile
}));
toyContract = warp.contract<State>(contractTxId);
// connecting wallet to the PST contract
toyContract.connect(wallet);
});
afterAll(async () => {
await arlocal.stop();
});
it('should properly deploy contract', async () => {
const contractTx = await arweave.transactions.get(contractTxId);
expect(contractTx).not.toBeNull();
const contractSrcTxId = tagsParser.getTag(contractTx, SmartWeaveTags.CONTRACT_SRC_TX_ID);
const contractSrcTx = await arweave.transactions.get(contractSrcTxId);
expect(tagsParser.getTag(contractSrcTx, SmartWeaveTags.CONTENT_TYPE)).toEqual('application/wasm');
expect(tagsParser.getTag(contractSrcTx, SmartWeaveTags.WASM_LANG)).toEqual('rust');
expect(tagsParser.getTag(contractSrcTx, SmartWeaveTags.WASM_META)).toBeTruthy();
const srcTxData = await arweaveWrapper.txData(contractSrcTxId);
const wasmSrc = new WasmSrc(srcTxData);
expect(wasmSrc.wasmBinary()).not.toBeNull();
expect(wasmSrc.additionalCode()).toEqual(fs.readFileSync(contractGlueCodeFile, 'utf-8'));
expect((await wasmSrc.sourceCode()).size).toEqual(1);
});
it('should read state', async () => {
const { cachedValue } = await toyContract.readState();
expect(cachedValue.state).toEqual(initialState);
});
it('should write/view state', async () => {
await toyContract.writeInteraction<Action>(new Action(4));
const interactionResult = await toyContract.viewState<Action, View>(new Action(17));
expect(interactionResult.result.x).toEqual(4);
});
});

View File

@@ -0,0 +1,10 @@
{
"compilerOptions": {
"moduleResolution": "Node",
"target": "ES2018",
"lib": ["dom"],
"esModuleInterop": true,
"downlevelIteration": true
},
"exclude": ["node_modules"]
}

File diff suppressed because it is too large Load Diff