Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: change env name #36

Merged
merged 4 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ async function main() {
if(!privateKey){
throw new Error('please define a private key');
}
const counterparties: string[] = ["0xE16955e95D088bd30746c7fb7d76cDA436b86F63"];
const counterparties: string[] = ["0x69281BEC892a036F3F500cE77109B3C3Fcb42c02"];
const envName = "testing-0x156207E1ca9746e5a387930c8695d84bc8dAD69F";

const wallet = new Wallet(privateKey);
const xmtpClient = await BosonXmtpClient.initialise(wallet, envName);
const xmtpClient = await BosonXmtpClient.initialise(wallet, envName, 'dev');

// const threads: any[] = await xmtpClient.getThreads(counterparties);
// console.log(threads);

const threadId = {exchangeId: '71', buyerId: '9', sellerId: '3'};
// const thread: ThreadObject = await xmtpClient.getThread(threadId, counterparties[0], {startTime: new Date(1659092409961)});
// console.log('thread', JSON.stringify(thread, null, 2))
for await (const messages of await xmtpClient.monitorThread(threadId, counterparties[0])) {
console.log(messages.data.content.value)
}
const threadId = {exchangeId: '27', buyerId: '8', sellerId: '4'};
const thread: ThreadObject = await xmtpClient.getThread(threadId, counterparties[0], {startTime: new Date(0),endTime: new Date()});
console.log('thread', JSON.stringify(thread, null, 2))
// for await (const messages of await xmtpClient.monitorThread(threadId, counterparties[0])) {
// console.log(messages.data.content.value)
// }

// await exampleEncodeAndSendStringMessage(xmtpClient, counterparties[1]);
// await exampleDecodeStringMessage(xmtpClient, counterparties[1]);
Expand Down
2 changes: 1 addition & 1 deletion src/example/index2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async function main() {
const envName = "testing-0x156207E1ca9746e5a387930c8695d84bc8dAD69F";

const wallet = new Wallet(privateKey);
const xmtpClient = await BosonXmtpClient.initialise(wallet, envName);
const xmtpClient = await BosonXmtpClient.initialise(wallet, envName, 'dev');

// const threads: any[] = await xmtpClient.getThreads(counterparties);
// console.log(threads);
Expand Down
16 changes: 11 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
TextCodec
} from "@xmtp/xmtp-js";
import { Signer } from "ethers";
import { XmtpClient } from "./xmtp/client";
import { XmtpClient, XmtpEnv } from "./xmtp/client";
import { BosonCodec, ContentTypeBoson } from "./xmtp/codec/boson-codec";
import {
MessageData,
Expand All @@ -29,8 +29,13 @@ export class BosonXmtpClient extends XmtpClient {
* @param client - XMTP client
* @param envName - environment name (e.g. "production", "test", etc)
*/
constructor(signer: Signer, client: Client, envName: string) {
super(signer, client, envName);
constructor(
signer: Signer,
client: Client,
envName: string,
xmtpEnvName: XmtpEnv
) {
super(signer, client, envName, xmtpEnvName);
}

/**
Expand All @@ -41,13 +46,14 @@ export class BosonXmtpClient extends XmtpClient {
*/
public static async initialise(
signer: Signer,
envName: string
envName: string,
xmtpEnvName: XmtpEnv
): Promise<BosonXmtpClient> {
const client: Client = await Client.create(signer, {
codecs: [new TextCodec(), new BosonCodec(envName)]
});

return new BosonXmtpClient(signer, client, envName);
return new BosonXmtpClient(signer, client, envName, xmtpEnvName);
}

/**
Expand Down
27 changes: 22 additions & 5 deletions src/xmtp/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,30 @@ import {
isValidMessageType
} from "../util/v0.0.1/functions";

export type XmtpEnv = "production" | "dev";

export class XmtpClient {
signer: Signer;
client: Client;
envName: string;
xmtpEnvName: XmtpEnv;

/**
* Class constructor
* @param signer - wallet to initialise
* @param client - XMTP client
* @param envName - environment name (e.g. "production", "test", etc)
*/
constructor(signer: Signer, client: Client, envName: string) {
constructor(
signer: Signer,
client: Client,
envName: string,
xmtpEnvName: XmtpEnv
) {
this.signer = signer;
this.client = client;
this.envName = envName;
this.xmtpEnvName = xmtpEnvName;
}

/**
Expand All @@ -39,14 +48,15 @@ export class XmtpClient {
*/
public static async initialise(
signer: Signer,
xmtpEnvName: XmtpEnv,
envName: string
): Promise<XmtpClient> {
const client: Client = await Client.create(signer, {
env: envName === "production" ? "production" : "dev",
env: xmtpEnvName,
codecs: [new TextCodec(), new BosonCodec(envName)]
});

return new XmtpClient(signer, client, envName);
return new XmtpClient(signer, client, envName, xmtpEnvName);
}

/**
Expand All @@ -58,10 +68,11 @@ export class XmtpClient {
*/
public static async isXmtpEnabled(
address: string,
xmtpEnvName: XmtpEnv,
envName: string
): Promise<boolean> {
const wallet: Wallet = Wallet.createRandom();
const bosonXmtp = await XmtpClient.initialise(wallet, envName);
const bosonXmtp = await XmtpClient.initialise(wallet, xmtpEnvName, envName);
return await bosonXmtp.client.canMessage(address);
}

Expand Down Expand Up @@ -99,7 +110,13 @@ export class XmtpClient {
* @returns Conversation - {@link Conversation}
*/
public async startConversation(counterparty: string): Promise<Conversation> {
if (!(await XmtpClient.isXmtpEnabled(counterparty, this.envName))) {
if (
!(await XmtpClient.isXmtpEnabled(
counterparty,
this.xmtpEnvName,
this.envName
))
) {
throw new Error(`${counterparty} has not initialised their XMTP client`);
}
return await this.client.conversations.newConversation(counterparty);
Expand Down