Skip to content

Commit

Permalink
Merge pull request #60 from aldrin-labs/fix/create-coin
Browse files Browse the repository at this point in the history
Fix/create coin
  • Loading branch information
avernikoz authored Apr 22, 2024
2 parents b033992 + 59f1245 commit f52cba0
Show file tree
Hide file tree
Showing 32 changed files with 1,059 additions and 82 deletions.
4 changes: 3 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
dist/
dts.config.js
rollup.config.js
rollup.config.js
src/managers/coin/create-coin/utils/move-bytecode-template.js
src/managers/coin/create-coin/utils/template.ts
2 changes: 1 addition & 1 deletion examples/coin/coin-address-validation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isValidTokenAddress } from "../../src/providers/utils/isValidTokenAddress";
import { CoinManagerSingleton } from "../../src/managers/CoinManager";
import { CoinManagerSingleton } from "../../src/managers/coin/CoinManager";
import { Providers } from "../../src/managers/types";
import { AftermathSingleton } from "../../src/providers/aftermath/aftermath";
import { CetusSingleton } from "../../src/providers/cetus/cetus";
Expand Down
2 changes: 1 addition & 1 deletion examples/coin/coin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CoinManagerSingleton } from "../../src/managers/CoinManager";
import { CoinManagerSingleton } from "../../src/managers/coin/CoinManager";
import { Providers } from "../../src/managers/types";
import { AftermathSingleton } from "../../src/providers/aftermath/aftermath";
import { CetusSingleton } from "../../src/providers/cetus/cetus";
Expand Down
2 changes: 1 addition & 1 deletion examples/coin/find-duplicates-by-coin-address.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CoinManagerSingleton } from "../../src/managers/CoinManager";
import { CoinManagerSingleton } from "../../src/managers/coin/CoinManager";
import { Providers } from "../../src/managers/types";
import { AftermathSingleton } from "../../src/providers/aftermath/aftermath";
import { CetusSingleton } from "../../src/providers/cetus/cetus";
Expand Down
2 changes: 1 addition & 1 deletion examples/coin/find-min-max-decimals-across-all-coins.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CoinManagerSingleton } from "../../src/managers/CoinManager";
import { CoinManagerSingleton } from "../../src/managers/coin/CoinManager";
import { Providers } from "../../src/managers/types";
import { AftermathSingleton } from "../../src/providers/aftermath/aftermath";
import { CetusSingleton } from "../../src/providers/cetus/cetus";
Expand Down
2 changes: 1 addition & 1 deletion examples/create-coin/create-coin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { keypair, provider, user } from "../common";
import { CoinManagerSingleton } from "../../src/managers/CoinManager";
import { CoinManagerSingleton } from "../../src/managers/coin/CoinManager";
import { CreateCoinTransactionParams } from "../../src";

// yarn ts-node examples/create-coin/create-coin.ts
Expand Down
2 changes: 1 addition & 1 deletion examples/create-pool/create-pool.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable require-jsdoc */
import BigNumber from "bignumber.js";
import { createClient } from "redis";
import { CoinManagerSingleton } from "../../src/managers/CoinManager";
import { CoinManagerSingleton } from "../../src/managers/coin/CoinManager";
import { Providers } from "../../src/managers/types";
import { AftermathSingleton } from "../../src/providers/aftermath/aftermath";
import { getLpCoinDecimals } from "../../src/providers/aftermath/create-pool-utils";
Expand Down
2 changes: 1 addition & 1 deletion examples/router/multiple-use.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CoinManagerSingleton } from "../../src/managers/CoinManager";
import { CoinManagerSingleton } from "../../src/managers/coin/CoinManager";
import { RouteManager } from "../../src/managers/RouteManager";
import { SHORT_SUI_COIN_TYPE } from "../../src/providers/common";
import { USDC_COIN_TYPE } from "../coin-types";
Expand Down
2 changes: 1 addition & 1 deletion examples/router/router-dca.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DCAManagerSingleton, TurbosSingleton } from "../../src";
import { CoinManagerSingleton } from "../../src/managers/CoinManager";
import { CoinManagerSingleton } from "../../src/managers/coin/CoinManager";
import { RouteManager } from "../../src/managers/RouteManager";
import { LONG_SUI_COIN_TYPE } from "../../src/providers/common";
import { USDC_COIN_TYPE } from "../coin-types";
Expand Down
2 changes: 1 addition & 1 deletion examples/router/router.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CoinManagerSingleton } from "../../src/managers/CoinManager";
import { CoinManagerSingleton } from "../../src/managers/coin/CoinManager";
import { RouteManager } from "../../src/managers/RouteManager";
import { SHORT_SUI_COIN_TYPE } from "../../src/providers/common";
import { USDC_COIN_TYPE } from "../coin-types";
Expand Down
2 changes: 1 addition & 1 deletion examples/wallet/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CoinManagerSingleton } from "../../src/managers/CoinManager";
import { CoinManagerSingleton } from "../../src/managers/coin/CoinManager";
import { AftermathSingleton } from "../../src/providers/aftermath/aftermath";
import { CetusSingleton } from "../../src/providers/cetus/cetus";
import { clmmMainnet } from "../../src/providers/cetus/config";
Expand Down
2 changes: 1 addition & 1 deletion examples/wallet/withdrawal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CoinManagerSingleton } from "../../src/managers/CoinManager";
import { CoinManagerSingleton } from "../../src/managers/coin/CoinManager";
import { AftermathSingleton } from "../../src/providers/aftermath/aftermath";
import { CetusSingleton } from "../../src/providers/cetus/cetus";
import { clmmMainnet } from "../../src/providers/cetus/config";
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"dependencies": {
"@cetusprotocol/cetus-sui-clmm-sdk": "^3.17.8",
"@flowx-pkg/ts-sdk": "^0.0.11",
"@mysten/sui.js": "^0.42.0",
"@mysten/sui.js": "yarn:@mysten/sui.js@^0.42.0",
"@mysten/sui.js-0.51.2": "yarn:@mysten/sui.js@^0.51.2",
"@types/redis": "^4.0.11",
"aftermath-ts-sdk": "^1.1.32",
"bech32": "^2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Managers
export * from "./managers/CoinManager";
export * from "./managers/coin/CoinManager";
export * from "./managers/RouteManager";
export * from "./managers/WalletManager";
export * from "./managers/types";
Expand Down
2 changes: 1 addition & 1 deletion src/managers/RouteManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { NoRoutesError } from "../errors/NoRoutesError";
import { CetusSingleton } from "../providers/cetus/cetus";
import { SUI_DENOMINATOR, SWAP_GAS_BUDGET } from "../providers/common";
import { isSuiCoinType } from "../providers/utils/isSuiCoinType";
import { CoinManagerSingleton } from "./CoinManager";
import { CoinManagerSingleton } from "./coin/CoinManager";
import { BestRouteData, IRouteManager, Provider, Providers, ProvidersToRouteDataMap } from "./types";
import { getFiltredProviders, getRouterMaps, tokenFromIsTokenTo } from "./utils";
import { TryCatchWrapperResult } from "../providers/types";
Expand Down
2 changes: 1 addition & 1 deletion src/managers/WalletManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { TransactionBlock } from "@mysten/sui.js/transactions";
import { SUI_DECIMALS } from "@mysten/sui.js/utils";
import BigNumber from "bignumber.js";
import { SUI_DENOMINATOR, SWAP_GAS_BUDGET } from "../providers/common";
import { CoinManagerSingleton } from "./CoinManager";
import { CoinManagerSingleton } from "./coin/CoinManager";
import { CoinAssetData, IWalletManager } from "./types";
import { getCoinsAssetsFromCoinObjects, normalizeMnemonic } from "./utils";
import { bech32 } from "bech32";
Expand Down
77 changes: 26 additions & 51 deletions src/managers/CoinManager.ts → src/managers/coin/CoinManager.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { CoinMetadata, SuiClient } from "@mysten/sui.js/client";
import { LONG_SUI_COIN_TYPE, SHORT_SUI_COIN_TYPE } from "../providers/common";
import {
CommonCoinData,
CreateCoinExternalApiResType,
CreateCoinTransactionParams,
ICoinManager,
Providers,
UpdatedCoinsCache,
} from "./types";
import { TransactionBlock } from "@mysten/sui.js/transactions";
import { isValidResForCreateCoin } from "./utils";
import BigNumber from "bignumber.js";
import { normalizeSuiAddress } from "@mysten/sui.js/utils";
import { LONG_SUI_COIN_TYPE, SHORT_SUI_COIN_TYPE } from "../../providers/common";
import { CommonCoinData, CreateCoinTransactionParams, ICoinManager, Providers, UpdatedCoinsCache } from "../types";
import initMoveByteCodeTemplate from "./create-coin/utils/move-bytecode-template";
import { getBytecode } from "./create-coin/utils/template";

/**
* @class CoinManagerSingleton
Expand All @@ -22,13 +16,7 @@ export class CoinManagerSingleton implements ICoinManager {
private allCoinsCache: Map<string, CommonCoinData> = new Map();
private coinsByProviderNameCache: Map<string, Map<string, CommonCoinData>> = new Map();
private provider: SuiClient;
private static COIN_CREATION_API_URL = "https://www.api.interestprotocol.com/api/v1/token/makeToken";
private static COIN_CREATION_HEADERS = {
"Content-Type": "application/json",
Host: "www.api.interestprotocol.com",
Origin: "https://www.suicoins.com",
Referer: "https://www.suicoins.com/",
};
private static COIN_CREATION_BYTECODE_TEMPLATE_URL = "https://www.suicoins.com/move_bytecode_template_bg.wasm";

/**
* Constructs a new instance of the SuiProvider class with the provided SUI provider URL.
Expand Down Expand Up @@ -245,47 +233,34 @@ export class CoinManagerSingleton implements ICoinManager {
transaction,
}: CreateCoinTransactionParams): Promise<TransactionBlock> {
const tx = transaction ?? new TransactionBlock();
let txData: CreateCoinExternalApiResType;

// convert mint amount to string respecting provided decimals
const mintAmountRespectingDecimals = new BigNumber(mintAmount)
.multipliedBy(new BigNumber(10).pow(decimals))
.toString();

try {
const result = await fetch(this.COIN_CREATION_API_URL, {
method: "POST",
headers: this.COIN_CREATION_HEADERS,
body: JSON.stringify({
name,
symbol,
decimals,
fixedSupply,
mintAmount: mintAmountRespectingDecimals,
url,
description,
}),
await initMoveByteCodeTemplate(CoinManagerSingleton.COIN_CREATION_BYTECODE_TEMPLATE_URL);

const [upgradeCap] = tx.publish({
modules: [
[
...getBytecode({
name,
symbol,
totalSupply: mintAmount,
description,
fixedSupply,
decimals: +decimals,
imageUrl: url,
recipient: signerAddress,
}),
],
],
dependencies: [normalizeSuiAddress("0x1"), normalizeSuiAddress("0x2")],
});

if (!result.ok) {
throw new Error(`[CoinManager.getCreateCoinTransaction] API request failed with status: ${result.status}`);
}

const data: CreateCoinExternalApiResType = await result.json();
tx.transferObjects([upgradeCap], tx.pure(signerAddress));

if (isValidResForCreateCoin(data)) {
txData = data;
} else {
throw new Error("[CoinManager.getCreateCoinTransaction] Invalid API response structure");
}
return tx;
} catch (error) {
console.error("[CoinManager.getCreateCoinTransaction] error: ", error);
throw error;
}

const [upgradeCap] = tx.publish({ modules: txData.modules, dependencies: txData.dependencies });
tx.transferObjects([upgradeCap], tx.pure(signerAddress));

return tx;
}
}
11 changes: 11 additions & 0 deletions src/managers/coin/create-coin/utils/create-token.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// This file is partially copied from https://github.com/interest-protocol/sui-coins.

export interface ICreateTokenForm {
name: string;
symbol: string;
totalSupply: string;
decimals?: number | undefined;
imageUrl?: string | undefined;
description: string;
fixedSupply: NonNullable<boolean | undefined>;
}
Loading

0 comments on commit f52cba0

Please sign in to comment.