Skip to content

Commit

Permalink
Merge pull request #256 from gnoswap-labs/GSW-684-handle-wrapped-gnot
Browse files Browse the repository at this point in the history
feat: [GSW-684] Handle Wrapped GNOT
  • Loading branch information
jinoosss authored Dec 15, 2023
2 parents 0d99c6d + 8bb57ae commit 3721bdb
Show file tree
Hide file tree
Showing 36 changed files with 578 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from "../protocols/wallet-network";
import { WalletClient } from "../wallet-client";
import { Adena } from "./adena";
import { parseTransactionResponse } from "./adena-client.util";

export class AdenaClient implements WalletClient {
private adena: Adena | null;
Expand Down Expand Up @@ -49,11 +50,11 @@ export class AdenaClient implements WalletClient {
};

public sendTransaction = (
transaction: SendTransactionRequestParam
): Promise<WalletResponse<SendTransactionResponse>> => {
transaction: SendTransactionRequestParam,
): Promise<WalletResponse<SendTransactionResponse<string | null>>> => {
const request = {
...transaction,
messages: transaction.messages.map((message) => {
messages: transaction.messages.map(message => {
if (isContractMessage(message)) {
return {
type: "/vm.m_call",
Expand All @@ -66,7 +67,16 @@ export class AdenaClient implements WalletClient {
};
}),
};
return createTimeout(this.getAdena().DoContract(request));
return createTimeout<
WalletResponse<SendTransactionResponse<string | null>>
>(
this.getAdena()
.DoContract(request)
.then(response => {
console.log("Injection Response", response);
return parseTransactionResponse(response);
}),
);
};

public addEventChangedAccount = (callback: (accountId: string) => void) => {
Expand All @@ -85,13 +95,13 @@ export class AdenaClient implements WalletClient {
}

public switchNetwork = (
chainId: string
chainId: string,
): Promise<WalletResponse<SwitchNetworkResponse>> => {
return createTimeout(this.getAdena().SwitchNetwork(chainId));
};

public addNetwork = (
network: AddNetworkRequestParam
network: AddNetworkRequestParam,
): Promise<WalletResponse<AddNetworkResponse>> => {
return createTimeout(this.getAdena().AddNetwork(network));
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { SendTransactionResponse, WalletResponse } from "../protocols";
import {
AdenaSendTransactionResponse,
AdenaSendTransactionSuccessResponse,
} from "./adena";
import {} from "@utils/rpc-utils";

function matchValues(str: string): string[] {
const regexp = /\((.*)\)/g;
const result = str.match(regexp);
if (result === null || result.length < 1) {
return [];
}
return result;
}

function parseABCIValue(str: string): string {
const regexp = /\s.*$/;
return str.replace(regexp, "").slice(1);
}

export function isTransactionSuccessResponse(
walletResponse: WalletResponse<AdenaSendTransactionResponse>,
): walletResponse is WalletResponse<AdenaSendTransactionSuccessResponse> {
return walletResponse.status === "success";
}

export function parseTransactionResponse(
walletResponse: WalletResponse<AdenaSendTransactionResponse>,
): WalletResponse<SendTransactionResponse<string | null>> {
if (!isTransactionSuccessResponse(walletResponse)) {
return {
...walletResponse,
data: {
hash: "",
height: "",
data: null,
},
} as WalletResponse<SendTransactionResponse<string | null>>;
}

let data: string | null = null;
const encodedData = walletResponse.data?.deliver_tx.ResponseBase.Data || null;
if (encodedData) {
const decodedData = window.atob(encodedData);
try {
const result = matchValues(decodedData);
data = parseABCIValue(result[0]);
} catch {
data = null;
}
}
return {
code: walletResponse.code,
status: walletResponse.status,
type: walletResponse.type,
message: walletResponse.message,
data: {
hash: walletResponse.data?.hash || "",
height: walletResponse.data?.height || "",
data,
},
} as WalletResponse<SendTransactionResponse<string | null>>;
}
43 changes: 30 additions & 13 deletions packages/web/src/common/clients/wallet-client/adena/adena.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ export interface Adena {
GetAccount: () => Promise<Response<AccountInfo>>;
DoContract: (
mesasage: SendTransactionRequestParam,
) => Promise<Response<SendTransactionResponse>>;
) => Promise<Response<AdenaSendTransactionResponse>>;
Sign: (mesasage: SendTransactionRequestParam) => Promise<Response>;
AddNetwork: (
chain: AddNetworkRequestParam,
) => Promise<Response<AddNetworkResponse>>;
SwitchNetwork: (chainId: string) => Promise<Response<SwitchNetworkResponse>>;
chain: AdenaAddNetworkRequestParam,
) => Promise<Response<AdenaAddNetworkResponse>>;
SwitchNetwork: (
chainId: string,
) => Promise<Response<AdenaSwitchNetworkResponse>>;
On: (eventName: string, callback: (message: string) => void) => void;
}

Expand Down Expand Up @@ -63,37 +65,52 @@ interface TransactionMessageOfContract {
send: string;
pkg_path: string;
func: string;
args: (string | number | boolean)[];
args: (string | number | boolean)[] | null;
}

/**
* Send Transaction Response
*/
type SendTransactionResponse =
| SendTransactionSuccessResponse
| SendTransactionErrorResponse;
export type AdenaSendTransactionResponse =
| AdenaSendTransactionSuccessResponse
| AdenaSendTransactionErrorResponse;

interface SendTransactionSuccessResponse {
export interface AdenaSendTransactionSuccessResponse {
hash: string;
height: string;
check_tx: AdenaSendTransactionSuccessResponseTransaction;
deliver_tx: AdenaSendTransactionSuccessResponseTransaction;
}

interface SendTransactionErrorResponse {
export interface AdenaSendTransactionSuccessResponseTransaction {
ResponseBase: {
Error: string | null;
Data: string | null;
Events: unknown[];
Log: string | null;
Info: string | null;
};
GasWanted: string;
GasUsed: string;
}

export interface AdenaSendTransactionErrorResponse {
type: string;
message: string;
}

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

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

interface SwitchNetworkResponse {
export interface AdenaSwitchNetworkResponse {
chainId: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { WalletResponse } from "./wallet-response";
export interface WalletTransactionMethod {
sendTransaction: (
transaction: SendTransactionRequestParam,
) => Promise<WalletResponse<SendTransactionResponse>>;
) => Promise<WalletResponse<SendTransactionResponse<string | null>>>;
}

export interface SendTransactionRequestParam {
Expand Down Expand Up @@ -31,18 +31,20 @@ export interface TransactionMessageOfContract {
send: string;
pkg_path: string;
func: string;
args: (string | number | boolean)[];
args: (string | number | boolean)[] | null;
}

/**
* Send Transaction Response
*/
export type SendTransactionResponse =
| SendTransactionSuccessResponse
export type SendTransactionResponse<T = unknown> =
| SendTransactionSuccessResponse<T>
| SendTransactionErrorResponse;

export interface SendTransactionSuccessResponse {
export interface SendTransactionSuccessResponse<T = unknown> {
hash: string;
height: string;
data: T;
}

export interface SendTransactionErrorResponse {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
export const PACKAGE_ROUTER_PATH =
process.env.NEXT_PUBLIC_PACKAGE_ROUTER_PATH || "";
export const PACKAGE_POOL_PATH =
process.env.NEXT_PUBLIC_PACKAGE_POOL_PATH || "";
export const PACKAGE_POSITION_PATH =
process.env.NEXT_PUBLIC_PACKAGE_POSITION_PATH || "";
export const PACKAGE_STAKER_PATH =
process.env.NEXT_PUBLIC_PACKAGE_STAKER_PATH || "";
export const PACKAGE_NFT_PATH = process.env.NEXT_PUBLIC_PACKAGE_NFT_PATH || "";
export const GOVERNANCE_PATH = process.env.NEXT_PUBLIC_GOVERNANCE_PATH || "";
export const WRAPPED_GNOT_PATH =
process.env.NEXT_PUBLIC_WRAPPED_GNOT_PATH || "";
export const PACKAGE_ROUTER_ADDRESS =
process.env.NEXT_PUBLIC_PACKAGE_ROUTER_ADDRESS || "";
export const PACKAGE_POOL_ADDRESS =
process.env.NEXT_PUBLIC_PACKAGE_POOL_ADDRESS || "";
export const PACKAGE_POSITION_ADDRESS =
process.env.NEXT_PUBLIC_PACKAGE_POSITION_ADDRESS || "";
export const PACKAGE_STAKER_ADDRESS =
process.env.NEXT_PUBLIC_PACKAGE_STAKER_ADDRESS || "";
export const PACKAGE_GOVERNANCE_ADDRESS =
process.env.NEXT_PUBLIC_PACKAGE_GOVERNANCE_ADDRESS || "";

export interface TransactionMessage {
caller: string;
send: string;
pkg_path: string;
func: string;
args: string[] | null;
}

export function makeTransactionMessage({
caller,
send,
packagePath,
func,
args,
}: {
caller: string;
send: string;
packagePath: string;
func: string;
args: string[] | null;
}): TransactionMessage {
return {
caller: caller,
send: send,
pkg_path: packagePath,
func: func,
args: args,
};
}

export function makeApproveMessage(
packagePath: string,
args: string[],
caller: string,
) {
return makeTransactionMessage({
caller,
send: "",
packagePath,
func: "Approve",
args,
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./common";
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { makeApproveMessage, PACKAGE_POOL_ADDRESS } from "./common";

export function makePoolTokenApproveMessage(
packagePath: string,
amount: string,
caller: string,
) {
return makeApproveMessage(
packagePath,
[PACKAGE_POOL_ADDRESS, amount],
caller,
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { makeTransactionMessage } from "./common";

export function makeDepositMessage(
packagePath: string,
amount: string,
denom: string,
caller: string,
) {
const send = `${amount.toString()}${denom}`;
return makeTransactionMessage({
packagePath,
send,
func: "Deposit",
args: null,
caller,
});
}

export function makeWithdrawMessage(
packagePath: string,
amount: string,
caller: string,
) {
return makeTransactionMessage({
packagePath,
send: "",
func: "Withdraw",
args: [amount.toString()],
caller,
});
}
8 changes: 8 additions & 0 deletions packages/web/src/common/errors/token/token-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ const ERROR_VALUE = {
status: 5005,
type: "Not found Recently tokens",
},
FAILED_WRAP_TOKEN: {
status: 5006,
type: "Failed to wrap toekn",
},
FAILED_UNWRAP_TOKEN: {
status: 5007,
type: "Failed to unwrap toekn",
},
};

type ErrorType = keyof typeof ERROR_VALUE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ const token: TokenModel = {
"isGasToken": false,
"description": "",
"websiteURL": "",
"originName": "",
"originSymbol": "",
"originDenom": "",
type: "grc20",
chainId: "dev.gnoswap",
createdAt: "2023-12-08T03:57:43Z",
Expand Down
Loading

0 comments on commit 3721bdb

Please sign in to comment.