Skip to content

Commit

Permalink
Use kwargs in rpcs
Browse files Browse the repository at this point in the history
  • Loading branch information
nhynes committed Apr 24, 2020
1 parent 45418c7 commit 43b1e3f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 20 deletions.
16 changes: 7 additions & 9 deletions app/service-clients/greeter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,21 @@ export class Greeter {
{ greeting }: { greeting: string },
options?: oasis.DeployOptions,
): Promise<Greeter> {
const payload = Greeter.makeDeployPayload(greeting);
try {
const deployedAddr = await gateway.deploy(
await Greeter.makeDeployPayload(greeting),
options,
);
const deployedAddr = await gateway.deploy(payload, options);
return new Greeter(deployedAddr, gateway);
} catch (e) {
throw e;
}
}
public static async makeDeployPayload(greeting: string): Promise<Buffer> {
private static makeDeployPayload(greeting: string): Buffer {
const encoder = new oasis.Encoder();
encoder.writeU8Array(Buffer.from(Greeter.BYTECODE, 'base64'));
return oasis.abiEncode(['string' as oasis.Schema], [greeting], encoder);
}
public async greet(
name: string,
{ name }: { name: string },
options?: oasis.RpcOptions,
): Promise<string> {
const payload = Greeter.makeGreetPayload(name);
Expand All @@ -85,7 +83,7 @@ export class Greeter {
throw e;
}
}
public static makeGreetPayload(name: string): Buffer {
private static makeGreetPayload(name: string): Buffer {
const encoder = new oasis.Encoder();
encoder.writeU8(0);
return oasis.abiEncode(['string' as oasis.Schema], [name], encoder);
Expand All @@ -101,9 +99,9 @@ export class Greeter {
throw e;
}
}
public static makeGetGreetedPayload(): Buffer {
private static makeGetGreetedPayload(): Buffer {
const encoder = new oasis.Encoder();
encoder.writeU8(1);
return oasis.abiEncode([], [], encoder);
return encoder.finish();
}
}
5 changes: 3 additions & 2 deletions app/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ async function main() {
});
console.log(`Deployed Greeter at ${service.address.hex}`);

const sub = await Greeted.subscribe(gw, null /* listen to any address */);
const sub = await Greeted.subscribe(gw, service.address);

console.log(`Greeter says: ${await service.greet('sample-app')}`);
const greeting = await service.greet({ name: 'sample-app' }); // emits a `Greeted` event
console.log(`Greeter says: ${greeting}`);

const event = await sub.first();
console.log(`event: ${event.from.hex} greeted ${event.to}!`);
Expand Down
12 changes: 5 additions & 7 deletions app/test/greeter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Greeter Test', () => {
let service: Greeter;

// create a gateway to the oasis node
let gw: Gateway = new Gateway(
const gw: Gateway = new Gateway(
'http://localhost:1234',
'AAAAGYHZxhwjJXjnGEIiyDCyZJq+Prknbneb9gYe9teCKrGa',
);
Expand All @@ -25,13 +25,11 @@ describe('Greeter Test', () => {
});

it('known greeting', async () => {
let sub = await Greeted.subscribe(gw, service.address);
let greeting = await service.greet('friend');
const sub = await Greeted.subscribe(gw, service.address);
const greeting = await service.greet({ name: 'friend' });
expect(greeting).toBe('Hello, friend');
for await (const event of sub) {
expect(event.to).toBe('friend');
break;
}
const event = await sub.first();
expect(event.to).toBe('friend');
});

afterAll(async () => {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"scripts": {
"build": "tsc -b",
"test": "jest",
"lint": "eslint 'app/src/**.ts'; prettier --check 'app/src/**.ts' package.json tsconfig.json",
"start": "node dist/src/main.js"
"lint": "eslint 'app/{src,test}/**.ts'; prettier --check 'app/{src,test}/**.ts' package.json tsconfig.json",
"deploy": "node dist/src/main.js"
},
"keywords": [],
"author": "",
Expand Down

0 comments on commit 43b1e3f

Please sign in to comment.