Skip to content

Commit

Permalink
[shuffle] refactor context.ts into ConsoleContext,UserContext
Browse files Browse the repository at this point in the history
  • Loading branch information
dimroc authored and bors-libra committed Nov 29, 2021
1 parent ffbcfb3 commit 6fd961e
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 38 deletions.
5 changes: 2 additions & 3 deletions shuffle/move/examples/integration/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import {
assert,
assertEquals,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import * as context from "../main/context.ts";
import { defaultUserContext } from "../main/context.ts";
import * as devapi from "../main/devapi.ts";
import * as helpers from "../main/helpers.ts";

Deno.test("invokeScriptFunction", async () => {
const scriptFunction =
context.senderAddress + "::Message::set_message";
const scriptFunction = defaultUserContext.address + "::Message::set_message";
let txn = await helpers.invokeScriptFunction(
scriptFunction,
[],
Expand Down
65 changes: 47 additions & 18 deletions shuffle/move/examples/main/context.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,61 @@
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

import * as _path from "https://deno.land/[email protected]/path/mod.ts";
import urlcat from "https://deno.land/x/[email protected]/src/index.ts";
import { BcsDeserializer } from "./generated/bcs/mod.ts";
import { isURL } from "https://deno.land/x/[email protected]/mod.ts";

export const shuffleBaseNetworksPath = String(Deno.env.get("SHUFFLE_BASE_NETWORKS_PATH"));
export const projectPath = String(Deno.env.get("PROJECT_PATH"));
export const networkName = String(Deno.env.get("SHUFFLE_NETWORK_NAME"))
export const nodeUrl = getNetworkEndpoint(
String(Deno.env.get("SHUFFLE_NETWORK_DEV_API_URL")),
);
export const senderAddress = String(Deno.env.get("SENDER_ADDRESS"));
export const privateKeyPath = String(Deno.env.get("PRIVATE_KEY_PATH"));
const privateKeyBytes = bcsToBytes(
await Deno.readFile(privateKeyPath)
);

export function privateKey(): Uint8Array {
return privateKeyBytes.slice(0);
class ConsoleContext {
constructor(
readonly projectPath: string,
readonly networkName: string,
readonly networksPath: string,
readonly nodeUrl: string,
) {}

static fromEnv(): ConsoleContext {
return new ConsoleContext(
String(Deno.env.get("PROJECT_PATH")),
String(Deno.env.get("SHUFFLE_NETWORK_NAME")),
String(Deno.env.get("SHUFFLE_BASE_NETWORKS_PATH")),
getNetworkEndpoint(
String(Deno.env.get("SHUFFLE_NETWORK_DEV_API_URL")),
),
);
}
}

export const consoleContext = ConsoleContext.fromEnv();

class UserContext {
constructor(
readonly username: string,
readonly address: string,
readonly privateKeyPath: string,
) {}

static fromEnv(username: string): UserContext {
return new UserContext(
username,
String(Deno.env.get("SENDER_ADDRESS")),
String(Deno.env.get("PRIVATE_KEY_PATH")),
);
}

async readPrivateKey(): Promise<Uint8Array> {
return bcsToBytes(
await Deno.readFile(this.privateKeyPath),
);
}
}

export const defaultUserContext = UserContext.fromEnv("default");

export function addressOrDefault(addr: string | undefined): string {
if (addr) {
return addr;
}
return senderAddress;
return defaultUserContext.address;
}

function getNetworkEndpoint(inputNetwork: string) {
Expand All @@ -44,9 +73,9 @@ function getNetworkEndpoint(inputNetwork: string) {

function bcsToBytes(bcsBytes: Uint8Array): Uint8Array {
const bcsDeserializer = new BcsDeserializer(bcsBytes);
return bcsDeserializer.deserializeBytes()
return bcsDeserializer.deserializeBytes();
}

export function relativeUrl(tail: string) {
return new URL(tail, nodeUrl).href;
return new URL(tail, consoleContext.nodeUrl).href;
}
4 changes: 3 additions & 1 deletion shuffle/move/examples/main/devapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ export async function transaction(versionOrHash: string) {
}

// Polls for a specific transaction to complete, returning the txn object.
export async function waitForTransactionCompletion(versionOrHash: string): Promise<any> {
export async function waitForTransactionCompletion(
versionOrHash: string,
): Promise<any> {
let txn = await transaction(versionOrHash);
for (let i = 0; i < 20; i++) {
if (txn.type !== "pending_transaction") {
Expand Down
6 changes: 3 additions & 3 deletions shuffle/move/examples/main/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

// deno-lint-ignore-file no-explicit-any
import * as DiemTypes from "./generated/diemTypes/mod.ts";
import * as context from "./context.ts";
import { defaultUserContext } from "./context.ts";
import * as devapi from "./devapi.ts";
import * as ed from "https://deno.land/x/[email protected]/mod.ts";
import * as util from "https://deno.land/[email protected]/node/util.ts";
Expand Down Expand Up @@ -65,9 +65,9 @@ export async function invokeScriptFunction(
args: any[],
): Promise<any> {
return await invokeScriptFunctionWithoutContext(
context.senderAddress,
defaultUserContext.address,
await devapi.sequenceNumber(),
context.privateKey(),
await defaultUserContext.readPrivateKey(),
scriptFunction,
typeArguments,
args,
Expand Down
34 changes: 21 additions & 13 deletions shuffle/move/examples/main/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import * as DiemHelpers from "./helpers.ts";
import * as DiemTypes from "./generated/diemTypes/mod.ts";
import * as codegen from "./generated/diemStdlib/mod.ts";
import * as context from "./context.ts";
import { consoleContext, defaultUserContext } from "./context.ts";
import * as devapi from "./devapi.ts";
import * as util from "https://deno.land/[email protected]/node/util.ts";
import { green } from "https://deno.land/x/[email protected]/mod.ts";
Expand All @@ -18,13 +18,19 @@ function highlight(content: string) {
}

export async function printWelcome() {
console.log(`Loading Project ${highlight(context.projectPath)}`);
console.log(`Sender Account Address ${highlight(context.senderAddress)}`);
console.log(`Loading Project ${highlight(consoleContext.projectPath)}`);
console.log(
`Default Account Address ${highlight(defaultUserContext.address)}`,
);
console.log(
`"helpers", "devapi", "context", "main", "codegen", "help" top level objects available`,
);
console.log(`Run "help" for more information on top level objects`);
console.log(`Connecting to Node ${highlight(context.nodeUrl)}`);
console.log(
`Connecting to ${consoleContext.networkName} at ${
highlight(consoleContext.nodeUrl)
}`,
);
console.log(await devapi.ledgerInfo());
console.log();
}
Expand All @@ -38,9 +44,9 @@ export async function setMessageScriptFunction(
textEncoder.encode(message),
);
return await DiemHelpers.buildAndSubmitTransaction(
context.senderAddress,
defaultUserContext.address,
await devapi.sequenceNumber(),
context.privateKey(),
await defaultUserContext.readPrivateKey(),
payload,
);
}
Expand All @@ -55,9 +61,9 @@ export async function setMessageScript(
);
const payload = new DiemTypes.TransactionPayloadVariantScript(script);
return await DiemHelpers.buildAndSubmitTransaction(
context.senderAddress,
defaultUserContext.address,
await devapi.sequenceNumber(),
context.privateKey(),
await defaultUserContext.readPrivateKey(),
payload,
);
}
Expand All @@ -67,7 +73,9 @@ export async function setMessageScript(
// but with a different address. See main/sources/NFT.move
// This is optional, as createTestNFTScriptFunction handles init.
export async function initializeTestNFT() {
const nftAddress = DiemHelpers.hexToAccountAddress(context.senderAddress);
const nftAddress = DiemHelpers.hexToAccountAddress(
defaultUserContext.address,
);

// Create the type tag representing TestNFT to pass to the generic
// script `initialize_nft`
Expand All @@ -83,9 +91,9 @@ export async function initializeTestNFT() {
const script = codegen.Stdlib.encodeInitializeNftScript(testNftType);
const payload = new DiemTypes.TransactionPayloadVariantScript(script);
return await DiemHelpers.buildAndSubmitTransaction(
context.senderAddress,
defaultUserContext.address,
await devapi.sequenceNumber(),
context.privateKey(),
await defaultUserContext.readPrivateKey(),
payload,
);
}
Expand All @@ -99,9 +107,9 @@ export async function createTestNFTScriptFunction(
textEncoder.encode(contentUri),
);
return await DiemHelpers.buildAndSubmitTransaction(
context.senderAddress,
defaultUserContext.address,
await devapi.sequenceNumber(),
context.privateKey(),
await defaultUserContext.readPrivateKey(),
payload,
);
}
Expand Down

0 comments on commit 6fd961e

Please sign in to comment.