-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[shuffle] refactor context.ts into ConsoleContext,UserContext
- Loading branch information
1 parent
ffbcfb3
commit 6fd961e
Showing
5 changed files
with
76 additions
and
38 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 |
---|---|---|
|
@@ -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, | ||
[], | ||
|
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,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) { | ||
|
@@ -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; | ||
} |
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 |
---|---|---|
|
@@ -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"; | ||
|
@@ -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, | ||
|
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 |
---|---|---|
|
@@ -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"; | ||
|
@@ -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(); | ||
} | ||
|
@@ -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, | ||
); | ||
} | ||
|
@@ -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, | ||
); | ||
} | ||
|
@@ -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` | ||
|
@@ -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, | ||
); | ||
} | ||
|
@@ -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, | ||
); | ||
} | ||
|