-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(js): support vouchers in transaction builder (#258)
- Loading branch information
1 parent
04e8c30
commit fbe22b4
Showing
18 changed files
with
211 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
js/lib/ | ||
js/parser.wasm | ||
js/test/rmrk-*/ | ||
js/test/ping/ | ||
|
||
node_modules/ | ||
target/ | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,3 @@ | ||
# simple ping 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,7 @@ | ||
[package] | ||
name = "ping-app" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
sails-rtl.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,20 @@ | ||
#![no_std] | ||
|
||
use sails_rtl::gstd::gprogram; | ||
use service::PingService; | ||
|
||
pub mod service; | ||
|
||
#[derive(Default)] | ||
pub struct Program; | ||
|
||
#[gprogram] | ||
impl Program { | ||
pub fn new() -> Self { | ||
Self | ||
} | ||
|
||
pub fn ping(&self) -> service::PingService { | ||
PingService::new() | ||
} | ||
} |
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,19 @@ | ||
use sails_rtl::{gstd::gservice, prelude::*}; | ||
|
||
#[derive(Default)] | ||
pub struct PingService {} | ||
|
||
#[gservice] | ||
impl PingService { | ||
pub fn new() -> Self { | ||
Self {} | ||
} | ||
|
||
pub fn ping(&mut self, input: String) -> Result<String, String> { | ||
if input != "ping" { | ||
Err("Invalid input".into()) | ||
} else { | ||
Ok("pong".into()) | ||
} | ||
} | ||
} |
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,13 @@ | ||
[package] | ||
name = "ping" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
ping-app = { path = "../app" } | ||
sails-rtl.workspace = true | ||
|
||
[build-dependencies] | ||
gwasm-builder.workspace = true | ||
ping-app = { path = "../app" } | ||
sails-idl-gen.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,15 @@ | ||
use ping_app::Program; | ||
use sails_idl_gen::program; | ||
use std::{env, fs::File, path::PathBuf}; | ||
|
||
fn main() { | ||
gwasm_builder::build(); | ||
|
||
let manifest_dir_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); | ||
|
||
let idl_file_path = manifest_dir_path.join("ping.idl"); | ||
|
||
let idl_file = File::create(idl_file_path).unwrap(); | ||
|
||
program::generate_idl::<Program>(idl_file).unwrap(); | ||
} |
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,8 @@ | ||
constructor { | ||
New : (); | ||
}; | ||
|
||
service Ping { | ||
Ping : (input: str) -> result (str, str); | ||
}; | ||
|
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,4 @@ | ||
#![no_std] | ||
|
||
#[cfg(target_arch = "wasm32")] | ||
pub use ping_app::wasm::*; |
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
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,67 @@ | ||
import { GearApi, HexString, MessageQueued, VoucherIssued, decodeAddress } from '@gear-js/api'; | ||
import { KeyringPair } from '@polkadot/keyring/types'; | ||
import { waitReady } from '@polkadot/wasm-crypto'; | ||
import { Keyring } from '@polkadot/api'; | ||
import { readFileSync } from 'fs'; | ||
|
||
import { Sails } from '../lib'; | ||
import { Program } from './ping/lib'; | ||
|
||
let sails: Sails; | ||
let api: GearApi; | ||
let alice: KeyringPair; | ||
let aliceRaw: HexString; | ||
let charlie: KeyringPair; | ||
let charlieRaw: HexString; | ||
let code: Buffer; | ||
|
||
const CATALOG_WASM_PATH = '../target/wasm32-unknown-unknown/release/ping.opt.wasm'; | ||
|
||
beforeAll(async () => { | ||
sails = await Sails.new(); | ||
api = await GearApi.create({ providerAddress: 'ws://127.0.0.1:9944' }); | ||
await waitReady(); | ||
const keyring = new Keyring({ type: 'sr25519' }); | ||
alice = keyring.addFromUri('//Alice'); | ||
aliceRaw = decodeAddress(alice.address); | ||
charlie = keyring.addFromUri('//Charlie'); | ||
charlieRaw = decodeAddress(charlie.address); | ||
code = readFileSync(CATALOG_WASM_PATH); | ||
}); | ||
|
||
afterAll(async () => { | ||
await api.disconnect(); | ||
await new Promise((resolve) => { | ||
setTimeout(resolve, 2000); | ||
}); | ||
}); | ||
|
||
describe('Ping', () => { | ||
let program: Program; | ||
|
||
test('create program', async () => { | ||
program = new Program(api); | ||
|
||
const transaction = await program.newCtorFromCode(code).withAccount(alice).calculateGas(); | ||
|
||
const { msgId, blockHash, response } = await transaction.signAndSend(); | ||
|
||
expect(msgId).toBeDefined(); | ||
expect(blockHash).toBeDefined(); | ||
|
||
await response(); | ||
}); | ||
|
||
test('ping', async () => { | ||
const transaction = await program.ping.ping('ping').withAccount(alice).calculateGas(); | ||
|
||
const { msgId, blockHash, response } = await transaction.signAndSend(); | ||
|
||
expect(msgId).toBeDefined(); | ||
expect(blockHash).toBeDefined(); | ||
|
||
const result = await response(); | ||
|
||
expect(result).toHaveProperty('ok', 'pong'); | ||
}); | ||
}); |
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