@zoralabs/coins-sdk 0.2.3 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/dist/actions/createCoin.d.ts +11 -4
- package/dist/actions/createCoin.d.ts.map +1 -1
- package/dist/actions/updateCoinURI.d.ts +1 -1
- package/dist/actions/updateCoinURI.d.ts.map +1 -1
- package/dist/actions/updatePayoutRecipient.d.ts +1 -1
- package/dist/actions/updatePayoutRecipient.d.ts.map +1 -1
- package/dist/api/index.d.ts.map +1 -1
- package/dist/index.cjs +63 -106
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +79 -122
- package/dist/index.js.map +1 -1
- package/dist/utils/getPrepurchaseHook.d.ts +16 -0
- package/dist/utils/getPrepurchaseHook.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/actions/createCoin.ts +32 -7
- package/src/actions/updateCoinURI.ts +1 -1
- package/src/actions/updatePayoutRecipient.ts +1 -1
- package/src/api/index.ts +1 -0
- package/src/index.ts +6 -9
- package/src/utils/getPrepurchaseHook.ts +59 -0
- package/dist/actions/tradeCoin.d.ts +0 -75
- package/dist/actions/tradeCoin.d.ts.map +0 -1
- package/src/actions/tradeCoin.ts +0 -182
package/package.json
CHANGED
|
@@ -12,16 +12,17 @@ import {
|
|
|
12
12
|
Hex,
|
|
13
13
|
Account,
|
|
14
14
|
} from "viem";
|
|
15
|
+
import { base, baseSepolia } from "viem/chains";
|
|
15
16
|
import { COIN_FACTORY_ADDRESS } from "../constants";
|
|
16
17
|
import { validateClientNetwork } from "../utils/validateClientNetwork";
|
|
17
|
-
import { GenericPublicClient } from "
|
|
18
|
-
import { validateMetadataURIContent, ValidMetadataURI } from "
|
|
18
|
+
import { GenericPublicClient } from "../utils/genericPublicClient";
|
|
19
|
+
import { validateMetadataURIContent, ValidMetadataURI } from "../metadata";
|
|
19
20
|
import { getAttribution } from "../utils/attribution";
|
|
20
|
-
import { base, baseSepolia } from "viem/chains";
|
|
21
21
|
import {
|
|
22
22
|
COIN_ETH_PAIR_POOL_CONFIG,
|
|
23
23
|
COIN_ZORA_PAIR_POOL_CONFIG,
|
|
24
|
-
} from "
|
|
24
|
+
} from "../utils/poolConfigUtils";
|
|
25
|
+
import { getPrepurchaseHook } from "../utils/getPrepurchaseHook";
|
|
25
26
|
|
|
26
27
|
export type CoinDeploymentLogArgs = ContractEventArgsFromTopics<
|
|
27
28
|
typeof zoraFactoryImplABI,
|
|
@@ -33,15 +34,24 @@ export enum DeployCurrency {
|
|
|
33
34
|
ETH = 2,
|
|
34
35
|
}
|
|
35
36
|
|
|
37
|
+
export enum InitialPurchaseCurrency {
|
|
38
|
+
ETH = 1,
|
|
39
|
+
// TODO: Add USDC and ZORA support with signature approvals
|
|
40
|
+
}
|
|
41
|
+
|
|
36
42
|
export type CreateCoinArgs = {
|
|
37
43
|
name: string;
|
|
38
44
|
symbol: string;
|
|
39
45
|
uri: ValidMetadataURI;
|
|
40
|
-
chainId
|
|
46
|
+
chainId?: number;
|
|
41
47
|
owners?: Address[];
|
|
42
48
|
payoutRecipient: Address;
|
|
43
49
|
platformReferrer?: Address;
|
|
44
50
|
currency?: DeployCurrency;
|
|
51
|
+
initialPurchase?: {
|
|
52
|
+
currency: InitialPurchaseCurrency;
|
|
53
|
+
amount: bigint;
|
|
54
|
+
};
|
|
45
55
|
};
|
|
46
56
|
|
|
47
57
|
function getPoolConfig(currency: DeployCurrency, chainId: number) {
|
|
@@ -72,6 +82,7 @@ export async function createCoinCall({
|
|
|
72
82
|
currency,
|
|
73
83
|
chainId = base.id,
|
|
74
84
|
platformReferrer = "0x0000000000000000000000000000000000000000",
|
|
85
|
+
initialPurchase,
|
|
75
86
|
}: CreateCoinArgs): Promise<
|
|
76
87
|
SimulateContractParameters<typeof zoraFactoryImplABI, "deploy">
|
|
77
88
|
> {
|
|
@@ -88,6 +99,19 @@ export async function createCoinCall({
|
|
|
88
99
|
// This will throw an error if the metadata is not valid
|
|
89
100
|
await validateMetadataURIContent(uri);
|
|
90
101
|
|
|
102
|
+
let deployHook = {
|
|
103
|
+
hook: zeroAddress as Address,
|
|
104
|
+
hookData: "0x" as Hex,
|
|
105
|
+
value: 0n,
|
|
106
|
+
};
|
|
107
|
+
if (initialPurchase) {
|
|
108
|
+
deployHook = await getPrepurchaseHook({
|
|
109
|
+
initialPurchase,
|
|
110
|
+
payoutRecipient,
|
|
111
|
+
chainId,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
91
115
|
return {
|
|
92
116
|
abi: zoraFactoryImplABI,
|
|
93
117
|
functionName: "deploy",
|
|
@@ -100,10 +124,11 @@ export async function createCoinCall({
|
|
|
100
124
|
symbol,
|
|
101
125
|
poolConfig,
|
|
102
126
|
platformReferrer,
|
|
103
|
-
|
|
104
|
-
|
|
127
|
+
deployHook.hook,
|
|
128
|
+
deployHook.hookData,
|
|
105
129
|
keccak256(toBytes(Math.random().toString())), // coinSalt
|
|
106
130
|
],
|
|
131
|
+
value: deployHook.value,
|
|
107
132
|
dataSuffix: getAttribution(),
|
|
108
133
|
} as const;
|
|
109
134
|
}
|
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
SimulateContractParameters,
|
|
7
7
|
WalletClient,
|
|
8
8
|
} from "viem";
|
|
9
|
-
import { GenericPublicClient } from "
|
|
9
|
+
import { GenericPublicClient } from "../utils/genericPublicClient";
|
|
10
10
|
import { getAttribution } from "../utils/attribution";
|
|
11
11
|
|
|
12
12
|
export type UpdateCoinURIArgs = {
|
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
SimulateContractParameters,
|
|
7
7
|
WalletClient,
|
|
8
8
|
} from "viem";
|
|
9
|
-
import { GenericPublicClient } from "
|
|
9
|
+
import { GenericPublicClient } from "../utils/genericPublicClient";
|
|
10
10
|
import { getAttribution } from "../utils/attribution";
|
|
11
11
|
|
|
12
12
|
export type UpdatePayoutRecipientArgs = {
|
package/src/api/index.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -3,16 +3,12 @@ export {
|
|
|
3
3
|
createCoinCall,
|
|
4
4
|
getCoinCreateFromLogs,
|
|
5
5
|
DeployCurrency,
|
|
6
|
+
InitialPurchaseCurrency,
|
|
7
|
+
} from "./actions/createCoin";
|
|
8
|
+
export type {
|
|
9
|
+
CreateCoinArgs,
|
|
10
|
+
CoinDeploymentLogArgs,
|
|
6
11
|
} from "./actions/createCoin";
|
|
7
|
-
export type { CreateCoinArgs } from "./actions/createCoin";
|
|
8
|
-
|
|
9
|
-
export {
|
|
10
|
-
simulateBuy,
|
|
11
|
-
tradeCoin,
|
|
12
|
-
tradeCoinCall,
|
|
13
|
-
getTradeFromLogs,
|
|
14
|
-
} from "./actions/tradeCoin";
|
|
15
|
-
export type { TradeParams } from "./actions/tradeCoin";
|
|
16
12
|
|
|
17
13
|
export {
|
|
18
14
|
getOnchainCoinDetails,
|
|
@@ -31,6 +27,7 @@ export type { UpdatePayoutRecipientArgs } from "./actions/updatePayoutRecipient"
|
|
|
31
27
|
export * from "./api/queries";
|
|
32
28
|
export type * from "./api/queries";
|
|
33
29
|
|
|
30
|
+
// API Explore Actions
|
|
34
31
|
export * from "./api/explore";
|
|
35
32
|
export type * from "./api/explore";
|
|
36
33
|
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import {
|
|
2
|
+
encodeBuySupplyWithMultiHopSwapRouterHookCall,
|
|
3
|
+
wethAddress,
|
|
4
|
+
} from "@zoralabs/protocol-deployments";
|
|
5
|
+
import { InitialPurchaseCurrency } from "../actions/createCoin";
|
|
6
|
+
import { Address, concat, Hex, pad, toHex } from "viem";
|
|
7
|
+
import { ZORA_ADDRESS } from "./poolConfigUtils";
|
|
8
|
+
import { base } from "viem/chains";
|
|
9
|
+
|
|
10
|
+
const BASE_UDSC_ADDRESS = "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913";
|
|
11
|
+
|
|
12
|
+
const USDC_ZORA_FEE = 3000;
|
|
13
|
+
const WETH_BASE_FEE = 3000;
|
|
14
|
+
|
|
15
|
+
const encodeFee = (fee: number) => pad(toHex(fee), { size: 3 });
|
|
16
|
+
|
|
17
|
+
export const getPrepurchaseHook = async ({
|
|
18
|
+
payoutRecipient,
|
|
19
|
+
initialPurchase,
|
|
20
|
+
chainId,
|
|
21
|
+
}: {
|
|
22
|
+
initialPurchase: {
|
|
23
|
+
currency: InitialPurchaseCurrency;
|
|
24
|
+
amount: bigint;
|
|
25
|
+
amountOutMinimum?: bigint;
|
|
26
|
+
};
|
|
27
|
+
payoutRecipient: Address;
|
|
28
|
+
chainId: number;
|
|
29
|
+
}) => {
|
|
30
|
+
if (
|
|
31
|
+
initialPurchase.currency !== InitialPurchaseCurrency.ETH &&
|
|
32
|
+
chainId !== base.id
|
|
33
|
+
) {
|
|
34
|
+
throw new Error("Initial purchase currency and/or chain not supported");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const path = concat([
|
|
38
|
+
wethAddress[base.id],
|
|
39
|
+
encodeFee(WETH_BASE_FEE),
|
|
40
|
+
BASE_UDSC_ADDRESS,
|
|
41
|
+
encodeFee(USDC_ZORA_FEE),
|
|
42
|
+
ZORA_ADDRESS,
|
|
43
|
+
]);
|
|
44
|
+
|
|
45
|
+
return encodeBuySupplyWithMultiHopSwapRouterHookCall({
|
|
46
|
+
ethValue: initialPurchase.amount,
|
|
47
|
+
buyRecipient: payoutRecipient,
|
|
48
|
+
exactInputParams: {
|
|
49
|
+
path,
|
|
50
|
+
amountIn: initialPurchase.amount,
|
|
51
|
+
amountOutMinimum: initialPurchase.amountOutMinimum || 0n,
|
|
52
|
+
},
|
|
53
|
+
chainId: base.id,
|
|
54
|
+
}) as {
|
|
55
|
+
hook: Address;
|
|
56
|
+
hookData: Hex;
|
|
57
|
+
value: bigint;
|
|
58
|
+
};
|
|
59
|
+
};
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { coinABI } from "@zoralabs/protocol-deployments";
|
|
2
|
-
import { Address, TransactionReceipt, WalletClient, SimulateContractParameters, ContractEventArgsFromTopics } from "viem";
|
|
3
|
-
import { GenericPublicClient } from "src/utils/genericPublicClient";
|
|
4
|
-
export type SellEventArgs = ContractEventArgsFromTopics<typeof coinABI, "CoinSell">;
|
|
5
|
-
export type BuyEventArgs = ContractEventArgsFromTopics<typeof coinABI, "CoinBuy">;
|
|
6
|
-
export type TradeEventArgs = SellEventArgs | BuyEventArgs;
|
|
7
|
-
/**
|
|
8
|
-
* Simulates a buy order to get the expected output amount
|
|
9
|
-
* @param {Object} params - The simulation parameters
|
|
10
|
-
* @param {Address} params.target - The target coin contract address
|
|
11
|
-
* @param {bigint} params.requestedOrderSize - The desired input amount for the buy
|
|
12
|
-
* @param {PublicClient} params.publicClient - The viem public client instance
|
|
13
|
-
* @returns {Promise<{orderSize: bigint, amountOut: bigint}>} The simulated order size and output amount
|
|
14
|
-
*/
|
|
15
|
-
export declare function simulateBuy({ target, requestedOrderSize, publicClient, }: {
|
|
16
|
-
target: Address;
|
|
17
|
-
requestedOrderSize: bigint;
|
|
18
|
-
publicClient: GenericPublicClient;
|
|
19
|
-
}): Promise<{
|
|
20
|
-
orderSize: bigint;
|
|
21
|
-
amountOut: bigint;
|
|
22
|
-
}>;
|
|
23
|
-
/**
|
|
24
|
-
* Parameters for creating a trade call
|
|
25
|
-
* @typedef {Object} TradeParams
|
|
26
|
-
* @property {'sell' | 'buy'} direction - The trade direction
|
|
27
|
-
* @property {Address} target - The target coin contract address
|
|
28
|
-
* @property {Object} args - The trade arguments
|
|
29
|
-
* @property {Address} args.recipient - The recipient of the trade output
|
|
30
|
-
* @property {bigint} args.orderSize - The size of the order
|
|
31
|
-
* @property {bigint} [args.minAmountOut] - The minimum amount to receive
|
|
32
|
-
* @property {bigint} [args.sqrtPriceLimitX96] - The price limit for the trade
|
|
33
|
-
* @property {Address} [args.tradeReferrer] - The referrer address for the trade
|
|
34
|
-
*/
|
|
35
|
-
export type TradeParams = {
|
|
36
|
-
direction: "sell" | "buy";
|
|
37
|
-
target: Address;
|
|
38
|
-
args: {
|
|
39
|
-
recipient: Address;
|
|
40
|
-
orderSize: bigint;
|
|
41
|
-
minAmountOut?: bigint;
|
|
42
|
-
sqrtPriceLimitX96?: bigint;
|
|
43
|
-
tradeReferrer?: Address;
|
|
44
|
-
};
|
|
45
|
-
};
|
|
46
|
-
/**
|
|
47
|
-
* Creates a trade call parameters object for buy or sell
|
|
48
|
-
* @param {TradeParams} params - The trade parameters
|
|
49
|
-
* @returns {SimulateContractParameters} The contract call parameters
|
|
50
|
-
*/
|
|
51
|
-
export declare function tradeCoinCall({ target, direction, args: { recipient, orderSize, minAmountOut, sqrtPriceLimitX96, tradeReferrer, }, }: TradeParams): SimulateContractParameters;
|
|
52
|
-
/**
|
|
53
|
-
* Gets the trade event from transaction receipt logs
|
|
54
|
-
* @param {TransactionReceipt} receipt - The transaction receipt containing the logs
|
|
55
|
-
* @param {'buy' | 'sell'} direction - The direction of the trade
|
|
56
|
-
* @returns {TradeEventArgs | undefined} The decoded trade event args if found
|
|
57
|
-
*/
|
|
58
|
-
export declare function getTradeFromLogs(receipt: TransactionReceipt, direction: "buy" | "sell"): TradeEventArgs | undefined;
|
|
59
|
-
/**
|
|
60
|
-
* Executes a trade transaction
|
|
61
|
-
* @param {TradeParams} params - The trade parameters
|
|
62
|
-
* @param {PublicClient} publicClient - The viem public client instance
|
|
63
|
-
* @param {WalletClient} walletClient - The viem wallet client instance
|
|
64
|
-
* @returns {Promise<{
|
|
65
|
-
* hash: `0x${string}`,
|
|
66
|
-
* receipt: TransactionReceipt,
|
|
67
|
-
* trade: TradeEventArgs | undefined
|
|
68
|
-
* }>} The transaction result with trade details
|
|
69
|
-
*/
|
|
70
|
-
export declare function tradeCoin(params: TradeParams, walletClient: WalletClient, publicClient: GenericPublicClient): Promise<{
|
|
71
|
-
hash: `0x${string}`;
|
|
72
|
-
receipt: any;
|
|
73
|
-
trade: TradeEventArgs | undefined;
|
|
74
|
-
}>;
|
|
75
|
-
//# sourceMappingURL=tradeCoin.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"tradeCoin.d.ts","sourceRoot":"","sources":["../../src/actions/tradeCoin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,gCAAgC,CAAC;AAEzD,OAAO,EACL,OAAO,EACP,kBAAkB,EAClB,YAAY,EACZ,0BAA0B,EAG1B,2BAA2B,EAE5B,MAAM,MAAM,CAAC;AAEd,OAAO,EAAE,mBAAmB,EAAE,MAAM,+BAA+B,CAAC;AAIpE,MAAM,MAAM,aAAa,GAAG,2BAA2B,CACrD,OAAO,OAAO,EACd,UAAU,CACX,CAAC;AACF,MAAM,MAAM,YAAY,GAAG,2BAA2B,CACpD,OAAO,OAAO,EACd,SAAS,CACV,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG,aAAa,GAAG,YAAY,CAAC;AAO1D;;;;;;;GAOG;AACH,wBAAsB,WAAW,CAAC,EAChC,MAAM,EACN,kBAAkB,EAClB,YAAY,GACb,EAAE;IACD,MAAM,EAAE,OAAO,CAAC;IAChB,kBAAkB,EAAE,MAAM,CAAC;IAC3B,YAAY,EAAE,mBAAmB,CAAC;CACnC,GAAG,OAAO,CAAC;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,CAAC,CAwBpD;AAED;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,WAAW,GAAG;IACxB,SAAS,EAAE,MAAM,GAAG,KAAK,CAAC;IAC1B,MAAM,EAAE,OAAO,CAAC;IAChB,IAAI,EAAE;QACJ,SAAS,EAAE,OAAO,CAAC;QACnB,SAAS,EAAE,MAAM,CAAC;QAClB,YAAY,CAAC,EAAE,MAAM,CAAC;QACtB,iBAAiB,CAAC,EAAE,MAAM,CAAC;QAC3B,aAAa,CAAC,EAAE,OAAO,CAAC;KACzB,CAAC;CACH,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,EAC5B,MAAM,EACN,SAAS,EACT,IAAI,EAAE,EACJ,SAAS,EACT,SAAS,EACT,YAAiB,EACjB,iBAAsB,EACtB,aAA2B,GAC5B,GACF,EAAE,WAAW,GAAG,0BAA0B,CAc1C;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAC9B,OAAO,EAAE,kBAAkB,EAC3B,SAAS,EAAE,KAAK,GAAG,MAAM,GACxB,cAAc,GAAG,SAAS,CAU5B;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,SAAS,CAC7B,MAAM,EAAE,WAAW,EACnB,YAAY,EAAE,YAAY,EAC1B,YAAY,EAAE,mBAAmB;;;;GAgBlC"}
|
package/src/actions/tradeCoin.ts
DELETED
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
import { coinABI } from "@zoralabs/protocol-deployments";
|
|
2
|
-
import { validateClientNetwork } from "../utils/validateClientNetwork";
|
|
3
|
-
import {
|
|
4
|
-
Address,
|
|
5
|
-
TransactionReceipt,
|
|
6
|
-
WalletClient,
|
|
7
|
-
SimulateContractParameters,
|
|
8
|
-
parseEther,
|
|
9
|
-
zeroAddress,
|
|
10
|
-
ContractEventArgsFromTopics,
|
|
11
|
-
parseEventLogs,
|
|
12
|
-
} from "viem";
|
|
13
|
-
import { baseSepolia } from "viem/chains";
|
|
14
|
-
import { GenericPublicClient } from "src/utils/genericPublicClient";
|
|
15
|
-
import { getAttribution } from "../utils/attribution";
|
|
16
|
-
// Define trade event args type
|
|
17
|
-
|
|
18
|
-
export type SellEventArgs = ContractEventArgsFromTopics<
|
|
19
|
-
typeof coinABI,
|
|
20
|
-
"CoinSell"
|
|
21
|
-
>;
|
|
22
|
-
export type BuyEventArgs = ContractEventArgsFromTopics<
|
|
23
|
-
typeof coinABI,
|
|
24
|
-
"CoinBuy"
|
|
25
|
-
>;
|
|
26
|
-
|
|
27
|
-
export type TradeEventArgs = SellEventArgs | BuyEventArgs;
|
|
28
|
-
|
|
29
|
-
// We'll use this address to ensure it will have funds to simulate an eth trade.
|
|
30
|
-
// @dev: This only works on OP chains and is a fix for a bug. Another approach should be taken long term.
|
|
31
|
-
const OP_BRIDGE_ADDRESS =
|
|
32
|
-
"0x4200000000000000000000000000000000000016" as Address;
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Simulates a buy order to get the expected output amount
|
|
36
|
-
* @param {Object} params - The simulation parameters
|
|
37
|
-
* @param {Address} params.target - The target coin contract address
|
|
38
|
-
* @param {bigint} params.requestedOrderSize - The desired input amount for the buy
|
|
39
|
-
* @param {PublicClient} params.publicClient - The viem public client instance
|
|
40
|
-
* @returns {Promise<{orderSize: bigint, amountOut: bigint}>} The simulated order size and output amount
|
|
41
|
-
*/
|
|
42
|
-
export async function simulateBuy({
|
|
43
|
-
target,
|
|
44
|
-
requestedOrderSize,
|
|
45
|
-
publicClient,
|
|
46
|
-
}: {
|
|
47
|
-
target: Address;
|
|
48
|
-
requestedOrderSize: bigint;
|
|
49
|
-
publicClient: GenericPublicClient;
|
|
50
|
-
}): Promise<{ orderSize: bigint; amountOut: bigint }> {
|
|
51
|
-
const numberResult = await publicClient.simulateContract({
|
|
52
|
-
address: target,
|
|
53
|
-
abi: coinABI,
|
|
54
|
-
functionName: "buy",
|
|
55
|
-
dataSuffix: getAttribution(),
|
|
56
|
-
args: [
|
|
57
|
-
OP_BRIDGE_ADDRESS,
|
|
58
|
-
requestedOrderSize,
|
|
59
|
-
0n, // minAmountOut
|
|
60
|
-
0n, // sqrtPriceLimitX96
|
|
61
|
-
zeroAddress, // tradeReferrer
|
|
62
|
-
],
|
|
63
|
-
// We want to ensure that the multicall3 contract has enough ETH to buy in the simulation
|
|
64
|
-
stateOverride: [
|
|
65
|
-
{
|
|
66
|
-
address: baseSepolia.contracts.multicall3.address,
|
|
67
|
-
balance: parseEther("10000000"),
|
|
68
|
-
},
|
|
69
|
-
],
|
|
70
|
-
});
|
|
71
|
-
const orderSize = numberResult.result[0];
|
|
72
|
-
const amountOut = numberResult.result[1];
|
|
73
|
-
return { orderSize, amountOut };
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Parameters for creating a trade call
|
|
78
|
-
* @typedef {Object} TradeParams
|
|
79
|
-
* @property {'sell' | 'buy'} direction - The trade direction
|
|
80
|
-
* @property {Address} target - The target coin contract address
|
|
81
|
-
* @property {Object} args - The trade arguments
|
|
82
|
-
* @property {Address} args.recipient - The recipient of the trade output
|
|
83
|
-
* @property {bigint} args.orderSize - The size of the order
|
|
84
|
-
* @property {bigint} [args.minAmountOut] - The minimum amount to receive
|
|
85
|
-
* @property {bigint} [args.sqrtPriceLimitX96] - The price limit for the trade
|
|
86
|
-
* @property {Address} [args.tradeReferrer] - The referrer address for the trade
|
|
87
|
-
*/
|
|
88
|
-
export type TradeParams = {
|
|
89
|
-
direction: "sell" | "buy";
|
|
90
|
-
target: Address;
|
|
91
|
-
args: {
|
|
92
|
-
recipient: Address;
|
|
93
|
-
orderSize: bigint;
|
|
94
|
-
minAmountOut?: bigint;
|
|
95
|
-
sqrtPriceLimitX96?: bigint;
|
|
96
|
-
tradeReferrer?: Address;
|
|
97
|
-
};
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Creates a trade call parameters object for buy or sell
|
|
102
|
-
* @param {TradeParams} params - The trade parameters
|
|
103
|
-
* @returns {SimulateContractParameters} The contract call parameters
|
|
104
|
-
*/
|
|
105
|
-
export function tradeCoinCall({
|
|
106
|
-
target,
|
|
107
|
-
direction,
|
|
108
|
-
args: {
|
|
109
|
-
recipient,
|
|
110
|
-
orderSize,
|
|
111
|
-
minAmountOut = 0n,
|
|
112
|
-
sqrtPriceLimitX96 = 0n,
|
|
113
|
-
tradeReferrer = zeroAddress,
|
|
114
|
-
},
|
|
115
|
-
}: TradeParams): SimulateContractParameters {
|
|
116
|
-
return {
|
|
117
|
-
abi: coinABI,
|
|
118
|
-
functionName: direction,
|
|
119
|
-
address: target,
|
|
120
|
-
args: [
|
|
121
|
-
recipient,
|
|
122
|
-
orderSize,
|
|
123
|
-
minAmountOut,
|
|
124
|
-
sqrtPriceLimitX96,
|
|
125
|
-
tradeReferrer,
|
|
126
|
-
],
|
|
127
|
-
value: direction === "buy" ? orderSize : 0n,
|
|
128
|
-
} as const;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Gets the trade event from transaction receipt logs
|
|
133
|
-
* @param {TransactionReceipt} receipt - The transaction receipt containing the logs
|
|
134
|
-
* @param {'buy' | 'sell'} direction - The direction of the trade
|
|
135
|
-
* @returns {TradeEventArgs | undefined} The decoded trade event args if found
|
|
136
|
-
*/
|
|
137
|
-
export function getTradeFromLogs(
|
|
138
|
-
receipt: TransactionReceipt,
|
|
139
|
-
direction: "buy" | "sell",
|
|
140
|
-
): TradeEventArgs | undefined {
|
|
141
|
-
const eventLogs = parseEventLogs({
|
|
142
|
-
abi: coinABI,
|
|
143
|
-
logs: receipt.logs,
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
if (direction === "buy") {
|
|
147
|
-
return eventLogs.find((log) => log.eventName === "CoinBuy")?.args;
|
|
148
|
-
}
|
|
149
|
-
return eventLogs.find((log) => log.eventName === "CoinSell")?.args;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Executes a trade transaction
|
|
154
|
-
* @param {TradeParams} params - The trade parameters
|
|
155
|
-
* @param {PublicClient} publicClient - The viem public client instance
|
|
156
|
-
* @param {WalletClient} walletClient - The viem wallet client instance
|
|
157
|
-
* @returns {Promise<{
|
|
158
|
-
* hash: `0x${string}`,
|
|
159
|
-
* receipt: TransactionReceipt,
|
|
160
|
-
* trade: TradeEventArgs | undefined
|
|
161
|
-
* }>} The transaction result with trade details
|
|
162
|
-
*/
|
|
163
|
-
export async function tradeCoin(
|
|
164
|
-
params: TradeParams,
|
|
165
|
-
walletClient: WalletClient,
|
|
166
|
-
publicClient: GenericPublicClient,
|
|
167
|
-
) {
|
|
168
|
-
validateClientNetwork(publicClient);
|
|
169
|
-
const { request } = await publicClient.simulateContract({
|
|
170
|
-
...tradeCoinCall(params),
|
|
171
|
-
account: walletClient.account,
|
|
172
|
-
});
|
|
173
|
-
const hash = await walletClient.writeContract(request);
|
|
174
|
-
const receipt = await publicClient.waitForTransactionReceipt({ hash });
|
|
175
|
-
const trade = getTradeFromLogs(receipt, params.direction);
|
|
176
|
-
|
|
177
|
-
return {
|
|
178
|
-
hash,
|
|
179
|
-
receipt,
|
|
180
|
-
trade,
|
|
181
|
-
};
|
|
182
|
-
}
|