-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…-client [GSW-341] feat: Implement the Adena Client
- Loading branch information
Showing
27 changed files
with
364 additions
and
200 deletions.
There are no files selected for viewing
82 changes: 0 additions & 82 deletions
82
packages/web/src/common/clients/wallet-client/adena-client.ts
This file was deleted.
Oops, something went wrong.
61 changes: 61 additions & 0 deletions
61
packages/web/src/common/clients/wallet-client/adena/adena-client.ts
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 |
---|---|---|
@@ -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
94
packages/web/src/common/clients/wallet-client/adena/adena.ts
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 |
---|---|---|
@@ -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; | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./adena-client"; |
10 changes: 5 additions & 5 deletions
10
packages/web/src/common/clients/wallet-client/protocols/index.ts
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,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"; |
5 changes: 0 additions & 5 deletions
5
packages/web/src/common/clients/wallet-client/protocols/inject-establish-request.ts
This file was deleted.
Oops, something went wrong.
3 changes: 0 additions & 3 deletions
3
packages/web/src/common/clients/wallet-client/protocols/inject-exists-request.ts
This file was deleted.
Oops, something went wrong.
18 changes: 0 additions & 18 deletions
18
packages/web/src/common/clients/wallet-client/protocols/inject-get-account-request.ts
This file was deleted.
Oops, something went wrong.
19 changes: 0 additions & 19 deletions
19
packages/web/src/common/clients/wallet-client/protocols/inject-send-transaction-request.ts
This file was deleted.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
packages/web/src/common/clients/wallet-client/protocols/wallet-account.ts
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 |
---|---|---|
@@ -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; | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/web/src/common/clients/wallet-client/protocols/wallet-event.ts
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface WalletEventMethod { | ||
addEventChangedAccount: (callback: (accountId: string) => void) => void; | ||
|
||
addEventChangedNetwork: (callback: (networkId: string) => void) => void; | ||
} |
27 changes: 27 additions & 0 deletions
27
packages/web/src/common/clients/wallet-client/protocols/wallet-network.ts
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 |
---|---|---|
@@ -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; | ||
} |
4 changes: 2 additions & 2 deletions
4
...allet-client/protocols/inject-response.ts → ...allet-client/protocols/wallet-response.ts
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,7 +1,7 @@ | ||
export interface InjectResponse<T> { | ||
export interface WalletResponse<T = {}> { | ||
code: number; | ||
status: string; | ||
type: string; | ||
message: string; | ||
data: T; | ||
data: T | null; | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/web/src/common/clients/wallet-client/protocols/wallet-status.ts
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { WalletResponse } from "./wallet-response"; | ||
|
||
export interface WalletStatusMethod { | ||
existsWallet: () => boolean; | ||
|
||
addEstablishedSite: (sitename: string) => Promise<WalletResponse>; | ||
} |
Oops, something went wrong.