Skip to content

Commit

Permalink
Merge pull request #200 from gnoswap-labs/GSW-341-implement-the-adena…
Browse files Browse the repository at this point in the history
…-client

[GSW-341] feat: Implement the Adena Client
  • Loading branch information
jinoosss authored Sep 25, 2023
2 parents 05b4fc0 + a0fcbc3 commit c1ff291
Show file tree
Hide file tree
Showing 27 changed files with 364 additions and 200 deletions.
82 changes: 0 additions & 82 deletions packages/web/src/common/clients/wallet-client/adena-client.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { createTimeout } from "@common/utils/client-util";
import {
WalletResponse,
SendTransactionRequestParam,
AccountInfo,
SendTransactionResponse,
} from "../protocols";
import { WalletClient } from "../wallet-client";
import { Adena } from "./adena";

export class AdenaClient implements WalletClient {
private adena: Adena | null;

constructor() {
this.adena = null;
this.initAdena();
}

private initAdena = () => {
if (typeof window !== "undefined" && typeof window.adena !== "undefined") {
this.adena = window.adena;
}
};

private getAdena() {
if (this.adena === null) {
throw new Error("Not found");
}
return this.adena;
}

public existsWallet = (): boolean => {
return this.adena !== null;
};

public getAccount(): Promise<WalletResponse<AccountInfo>> {
return createTimeout(this.getAdena().GetAccount());
}

public addEstablishedSite = (sitename: string): Promise<WalletResponse> => {
return createTimeout(this.getAdena().AddEstablish(sitename));
};

public sendTransaction = (
transaction: SendTransactionRequestParam,
): Promise<WalletResponse<SendTransactionResponse>> => {
return createTimeout(this.getAdena().DoContract(transaction));
};

public addEventChangedAccount = (callback: (accountId: string) => void) => {
this.getAdena().On("changedAccount", callback);
};

public addEventChangedNetwork = (callback: (networkId: string) => void) => {
this.getAdena().On("changedNetwork", callback);
};

public static createAdenaClient() {
return new AdenaClient();
}
}
94 changes: 94 additions & 0 deletions packages/web/src/common/clients/wallet-client/adena/adena.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
export interface Adena {
AddEstablish: (name: string) => Promise<Response>;
GetAccount: () => Promise<Response<AccountInfo>>;
DoContract: (
mesasage: SendTransactionRequestParam,
) => Promise<Response<SendTransactionResponse>>;
Sign: (mesasage: SendTransactionRequestParam) => Promise<Response>;
AddNetwork: (
chain: AddNetworkRequestParam,
) => Promise<Response<AddNetworkResponse>>;
SwitchNetwork: (chainId: string) => Promise<Response<SwitchNetworkResponse>>;
On: (eventName: string, callback: (message: string) => void) => void;
}

interface Response<T = {}> {
code: number;
status: string;
type: string;
message: string;
data: T;
}

interface AccountInfo {
status: "ACTIVE" | "IN_ACTIVE";
address: string;
coins: string;
publicKey: {
"@type": string;
value: string;
};
accountNumber: number;
sequence: number;
chainId: string;
}

interface SendTransactionRequestParam {
messages: Array<TransactionMessage>;
gasFee: number;
gasWanted: number;
memo?: string;
}

/**
* Transaction Request Message
*/
type TransactionMessage =
| TransactionMessageOfBankMsgSend
| TransactionMessageOfContract;

interface TransactionMessageOfBankMsgSend {
from_address: string;
to_address: string;
amount: string;
}

interface TransactionMessageOfContract {
caller: string;
send: string;
pkg_path: string;
func: string;
args: string[];
}

/**
* Send Transaction Response
*/
type SendTransactionResponse =
| SendTransactionSuccessResponse
| SendTransactionErrorResponse;

interface SendTransactionSuccessResponse {
hash: string;
}

interface SendTransactionErrorResponse {
type: string;
message: string;
}

interface AddNetworkRequestParam {
chainId: string;
rpcUrl: string;
chainName: string;
}

interface AddNetworkResponse {
chainId: string;
rpcUrl: string;
chainName: string;
}

interface SwitchNetworkResponse {
chainId: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./adena-client";
10 changes: 5 additions & 5 deletions packages/web/src/common/clients/wallet-client/protocols/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from "./inject-establish-request";
export * from "./inject-exists-request";
export * from "./inject-get-account-request";
export * from "./inject-response";
export * from "./inject-send-transaction-request";
export * from "./wallet-status";
export * from "./wallet-account";
export * from "./wallet-response";
export * from "./wallet-transaction";
export * from "./wallet-event";

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { WalletResponse } from "./wallet-response";

export interface WalletAccountMethod {
getAccount: () => Promise<WalletResponse<AccountInfo>>;
}

export interface AccountInfo {
status: "ACTIVE" | "IN_ACTIVE";
address: string;
coins: string;
publicKey: {
"@type": string;
value: string;
};
accountNumber: number;
sequence: number;
chainId: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface WalletEventMethod {
addEventChangedAccount: (callback: (accountId: string) => void) => void;

addEventChangedNetwork: (callback: (networkId: string) => void) => void;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { WalletResponse } from "./wallet-response";

export interface WalletNetworkMethod {
addNetwork: (
network: AddNetworkRequestParam,
) => Promise<WalletResponse<AddNetworkResponse>>;

switchNetwork: (
chainId: string,
) => Promise<WalletResponse<SwitchNetworkResponse>>;
}

export interface AddNetworkRequestParam {
chainId: string;
rpcUrl: string;
chainName: string;
}

export interface AddNetworkResponse {
chainId: string;
rpcUrl: string;
chainName: string;
}

export interface SwitchNetworkResponse {
chainId: string;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export interface InjectResponse<T> {
export interface WalletResponse<T = {}> {
code: number;
status: string;
type: string;
message: string;
data: T;
data: T | null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { WalletResponse } from "./wallet-response";

export interface WalletStatusMethod {
existsWallet: () => boolean;

addEstablishedSite: (sitename: string) => Promise<WalletResponse>;
}
Loading

0 comments on commit c1ff291

Please sign in to comment.