@rhinestone/deposit-modal 0.2.4 → 0.2.5-alpha.0
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/dist/{DepositModalReown-CY5MSQGK.mjs → DepositModalReown-GIODYNOK.mjs} +4 -2
- package/dist/{DepositModalReown-2UMPITRN.cjs → DepositModalReown-SVVA3OZ6.cjs} +5 -3
- package/dist/{WithdrawModalReown-YSRO5ZTA.mjs → WithdrawModalReown-HTEB4XGU.mjs} +3 -2
- package/dist/{WithdrawModalReown-Z5BUZQ4Z.cjs → WithdrawModalReown-VNTKGALT.cjs} +4 -3
- package/dist/{chunk-QUOP5C6V.cjs → chunk-4S262VLP.cjs} +54 -338
- package/dist/{chunk-ARGMXV6E.cjs → chunk-AHQY2O3U.cjs} +507 -235
- package/dist/{chunk-WNFGZS56.mjs → chunk-DUGDAMAF.mjs} +134 -132
- package/dist/chunk-J2SWZSXL.mjs +295 -0
- package/dist/{chunk-NFXJEOE6.cjs → chunk-KE6CJVOV.cjs} +88 -86
- package/dist/chunk-LHOHM67Z.mjs +234 -0
- package/dist/{chunk-HFQV7EHS.mjs → chunk-RQ2VCKLS.mjs} +451 -179
- package/dist/{chunk-KJEHVIPZ.mjs → chunk-WA4RA4HB.mjs} +14 -298
- package/dist/chunk-YKGL66EF.cjs +295 -0
- package/dist/chunk-ZHLQMSQM.cjs +234 -0
- package/dist/deposit.cjs +5 -3
- package/dist/deposit.d.cts +3 -3
- package/dist/deposit.d.ts +3 -3
- package/dist/deposit.mjs +4 -2
- package/dist/index.cjs +6 -4
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.mjs +5 -3
- package/dist/reown.cjs +6 -4
- package/dist/reown.d.cts +2 -2
- package/dist/reown.d.ts +2 -2
- package/dist/reown.mjs +5 -3
- package/dist/safe-CB7TvRCc.d.cts +62 -0
- package/dist/safe-CB7TvRCc.d.ts +62 -0
- package/dist/safe.cjs +22 -1
- package/dist/safe.d.cts +11 -59
- package/dist/safe.d.ts +11 -59
- package/dist/safe.mjs +22 -0
- package/dist/{types-DGQzvl6v.d.ts → types-CeFbJ-MW.d.ts} +8 -1
- package/dist/{types-DJ1fzNC7.d.cts → types-D0NawmZ8.d.cts} +8 -1
- package/dist/withdraw.cjs +4 -3
- package/dist/withdraw.d.cts +3 -3
- package/dist/withdraw.d.ts +3 -3
- package/dist/withdraw.mjs +3 -2
- package/package.json +1 -1
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SAFE_ABI
|
|
3
|
+
} from "./chunk-J2SWZSXL.mjs";
|
|
4
|
+
|
|
5
|
+
// src/core/polymarket-safe.ts
|
|
6
|
+
import {
|
|
7
|
+
concat,
|
|
8
|
+
encodeAbiParameters,
|
|
9
|
+
encodeFunctionData,
|
|
10
|
+
encodePacked,
|
|
11
|
+
erc20Abi,
|
|
12
|
+
formatUnits,
|
|
13
|
+
getAddress,
|
|
14
|
+
getCreate2Address,
|
|
15
|
+
keccak256,
|
|
16
|
+
parseAbi,
|
|
17
|
+
parseEventLogs,
|
|
18
|
+
pad,
|
|
19
|
+
toHex,
|
|
20
|
+
zeroAddress
|
|
21
|
+
} from "viem";
|
|
22
|
+
var POLYMARKET_POLYGON_CHAIN_ID = 137;
|
|
23
|
+
var POLYMARKET_SAFE_FACTORY_ADDRESS = "0xaacFeEa03eb1561C4e67d661e40682Bd20E3541b";
|
|
24
|
+
var POLYMARKET_SAFE_INIT_CODE_HASH = "0x2bce2127ff07fb632d16c8347c4ebf501f4841168bed00d9e6ef715ddb6fcecf";
|
|
25
|
+
var POLYMARKET_USDCE_ADDRESS = "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174";
|
|
26
|
+
var POLYMARKET_PUSD_ADDRESS = "0xC011a7E12a19f7B1f670d46F03B03f3342E82DFB";
|
|
27
|
+
var POLYMARKET_COLLATERAL_OFFRAMP_ADDRESS = "0x2957922Eb93258b93368531d39fAcCA3B4dC5854";
|
|
28
|
+
var SAFE_MULTI_SEND_CALL_ONLY_ADDRESS = "0x40A2aCCbd92BCA938b02010E17A5b8929b49130D";
|
|
29
|
+
var COLLATERAL_OFFRAMP_ABI = parseAbi([
|
|
30
|
+
"function unwrap(address _asset, address _to, uint256 _amount)"
|
|
31
|
+
]);
|
|
32
|
+
var MULTI_SEND_ABI = parseAbi(["function multiSend(bytes transactions)"]);
|
|
33
|
+
function derivePolymarketSafeAddress(owner) {
|
|
34
|
+
const checksummedOwner = getAddress(owner);
|
|
35
|
+
const salt = keccak256(
|
|
36
|
+
encodeAbiParameters([{ type: "address" }], [checksummedOwner])
|
|
37
|
+
);
|
|
38
|
+
return getCreate2Address({
|
|
39
|
+
from: POLYMARKET_SAFE_FACTORY_ADDRESS,
|
|
40
|
+
salt,
|
|
41
|
+
bytecodeHash: POLYMARKET_SAFE_INIT_CODE_HASH
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
function isPolymarketAsset(asset) {
|
|
45
|
+
return asset.source === "polymarket";
|
|
46
|
+
}
|
|
47
|
+
async function fetchPolymarketBalances(params) {
|
|
48
|
+
const safeAddress = derivePolymarketSafeAddress(params.owner);
|
|
49
|
+
const balances = await Promise.all([
|
|
50
|
+
readTokenBalance(params.publicClient, POLYMARKET_USDCE_ADDRESS, safeAddress),
|
|
51
|
+
readTokenBalance(params.publicClient, POLYMARKET_PUSD_ADDRESS, safeAddress)
|
|
52
|
+
]);
|
|
53
|
+
const [usdceBalance, pusdBalance] = balances;
|
|
54
|
+
const assets = [];
|
|
55
|
+
if (usdceBalance > 0n) {
|
|
56
|
+
assets.push(
|
|
57
|
+
buildPolymarketAsset({
|
|
58
|
+
kind: "usdce",
|
|
59
|
+
safeAddress,
|
|
60
|
+
balance: usdceBalance
|
|
61
|
+
})
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
if (pusdBalance > 0n) {
|
|
65
|
+
assets.push(
|
|
66
|
+
buildPolymarketAsset({
|
|
67
|
+
kind: "pusd",
|
|
68
|
+
safeAddress,
|
|
69
|
+
balance: pusdBalance
|
|
70
|
+
})
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
safeAddress,
|
|
75
|
+
assets,
|
|
76
|
+
totalUsd: assets.reduce((sum, asset) => sum + (asset.balanceUsd ?? 0), 0)
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
async function executePolymarketSafeTransfer(params) {
|
|
80
|
+
const account = params.walletClient.account;
|
|
81
|
+
const chain = params.walletClient.chain;
|
|
82
|
+
if (!account || !chain) {
|
|
83
|
+
throw new Error("Wallet not connected");
|
|
84
|
+
}
|
|
85
|
+
if (chain.id !== POLYMARKET_POLYGON_CHAIN_ID) {
|
|
86
|
+
throw new Error("Switch to Polygon to sign");
|
|
87
|
+
}
|
|
88
|
+
const isOwner = await params.publicClient.readContract({
|
|
89
|
+
address: params.safeAddress,
|
|
90
|
+
abi: SAFE_ABI,
|
|
91
|
+
functionName: "isOwner",
|
|
92
|
+
args: [account.address]
|
|
93
|
+
});
|
|
94
|
+
if (!isOwner) {
|
|
95
|
+
throw new Error("Connected wallet is not a Polymarket Safe owner");
|
|
96
|
+
}
|
|
97
|
+
const safeTx = params.tokenKind === "pusd" ? buildPusdUnwrapSafeTx(params.recipient, params.amount) : buildUsdceTransferSafeTx(params.recipient, params.amount);
|
|
98
|
+
const signature = concat([
|
|
99
|
+
pad(account.address, { size: 32 }),
|
|
100
|
+
pad(toHex(0), { size: 32 }),
|
|
101
|
+
toHex(1, { size: 1 })
|
|
102
|
+
]);
|
|
103
|
+
const txHash = await params.walletClient.writeContract({
|
|
104
|
+
account,
|
|
105
|
+
chain,
|
|
106
|
+
address: params.safeAddress,
|
|
107
|
+
abi: SAFE_ABI,
|
|
108
|
+
functionName: "execTransaction",
|
|
109
|
+
args: [
|
|
110
|
+
safeTx.to,
|
|
111
|
+
0n,
|
|
112
|
+
safeTx.data,
|
|
113
|
+
safeTx.operation,
|
|
114
|
+
0n,
|
|
115
|
+
0n,
|
|
116
|
+
0n,
|
|
117
|
+
zeroAddress,
|
|
118
|
+
zeroAddress,
|
|
119
|
+
signature
|
|
120
|
+
]
|
|
121
|
+
});
|
|
122
|
+
const receipt = await params.publicClient.waitForTransactionReceipt({
|
|
123
|
+
hash: txHash
|
|
124
|
+
});
|
|
125
|
+
const parsed = parseEventLogs({
|
|
126
|
+
abi: SAFE_ABI,
|
|
127
|
+
logs: receipt.logs.filter(
|
|
128
|
+
(log) => log.address.toLowerCase() === params.safeAddress.toLowerCase()
|
|
129
|
+
),
|
|
130
|
+
strict: false
|
|
131
|
+
});
|
|
132
|
+
if (parsed.some((log) => log.eventName === "ExecutionFailure")) {
|
|
133
|
+
throw new Error("Polymarket Safe transaction failed");
|
|
134
|
+
}
|
|
135
|
+
if (!parsed.some((log) => log.eventName === "ExecutionSuccess")) {
|
|
136
|
+
throw new Error("Polymarket Safe transaction status unavailable");
|
|
137
|
+
}
|
|
138
|
+
return {
|
|
139
|
+
txHash,
|
|
140
|
+
sourceToken: POLYMARKET_USDCE_ADDRESS,
|
|
141
|
+
sourceSymbol: params.tokenKind === "pusd" ? "pUSD" : "USDC.e"
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
function buildPolymarketAsset(params) {
|
|
145
|
+
const isPusd = params.kind === "pusd";
|
|
146
|
+
const symbol = isPusd ? "pUSD" : "USDC.e";
|
|
147
|
+
const balanceUsd = Number(formatUnits(params.balance, 6));
|
|
148
|
+
return {
|
|
149
|
+
id: `polymarket:${params.kind}`,
|
|
150
|
+
chainId: POLYMARKET_POLYGON_CHAIN_ID,
|
|
151
|
+
token: isPusd ? POLYMARKET_PUSD_ADDRESS : POLYMARKET_USDCE_ADDRESS,
|
|
152
|
+
symbol,
|
|
153
|
+
name: isPusd ? "Polymarket USD" : "Polygon USDC.e",
|
|
154
|
+
decimals: 6,
|
|
155
|
+
balance: params.balance.toString(),
|
|
156
|
+
balanceUsd: Number.isFinite(balanceUsd) ? balanceUsd : 0,
|
|
157
|
+
source: "polymarket",
|
|
158
|
+
sourceLabel: "Polymarket",
|
|
159
|
+
balanceAddress: params.safeAddress,
|
|
160
|
+
depositToken: POLYMARKET_USDCE_ADDRESS,
|
|
161
|
+
polymarketTokenKind: params.kind,
|
|
162
|
+
polymarketSafeAddress: params.safeAddress,
|
|
163
|
+
reviewLabel: isPusd ? "You send pUSD from Polymarket" : "You send USDC.e from Polymarket"
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
async function readTokenBalance(publicClient, token, holder) {
|
|
167
|
+
try {
|
|
168
|
+
return await publicClient.readContract({
|
|
169
|
+
address: token,
|
|
170
|
+
abi: erc20Abi,
|
|
171
|
+
functionName: "balanceOf",
|
|
172
|
+
args: [holder]
|
|
173
|
+
});
|
|
174
|
+
} catch {
|
|
175
|
+
return 0n;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
function buildUsdceTransferSafeTx(recipient, amount) {
|
|
179
|
+
return {
|
|
180
|
+
to: POLYMARKET_USDCE_ADDRESS,
|
|
181
|
+
data: encodeFunctionData({
|
|
182
|
+
abi: erc20Abi,
|
|
183
|
+
functionName: "transfer",
|
|
184
|
+
args: [recipient, amount]
|
|
185
|
+
}),
|
|
186
|
+
operation: 0
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
function buildPusdUnwrapSafeTx(recipient, amount) {
|
|
190
|
+
const approveData = encodeFunctionData({
|
|
191
|
+
abi: erc20Abi,
|
|
192
|
+
functionName: "approve",
|
|
193
|
+
args: [POLYMARKET_COLLATERAL_OFFRAMP_ADDRESS, amount]
|
|
194
|
+
});
|
|
195
|
+
const unwrapData = encodeFunctionData({
|
|
196
|
+
abi: COLLATERAL_OFFRAMP_ABI,
|
|
197
|
+
functionName: "unwrap",
|
|
198
|
+
args: [POLYMARKET_USDCE_ADDRESS, recipient, amount]
|
|
199
|
+
});
|
|
200
|
+
return {
|
|
201
|
+
to: SAFE_MULTI_SEND_CALL_ONLY_ADDRESS,
|
|
202
|
+
data: encodeFunctionData({
|
|
203
|
+
abi: MULTI_SEND_ABI,
|
|
204
|
+
functionName: "multiSend",
|
|
205
|
+
args: [
|
|
206
|
+
concat([
|
|
207
|
+
encodeMultiSendCall(POLYMARKET_PUSD_ADDRESS, approveData),
|
|
208
|
+
encodeMultiSendCall(POLYMARKET_COLLATERAL_OFFRAMP_ADDRESS, unwrapData)
|
|
209
|
+
])
|
|
210
|
+
]
|
|
211
|
+
}),
|
|
212
|
+
operation: 1
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
function encodeMultiSendCall(to, data) {
|
|
216
|
+
return encodePacked(
|
|
217
|
+
["uint8", "address", "uint256", "uint256", "bytes"],
|
|
218
|
+
[0, to, 0n, BigInt((data.length - 2) / 2), data]
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export {
|
|
223
|
+
POLYMARKET_POLYGON_CHAIN_ID,
|
|
224
|
+
POLYMARKET_SAFE_FACTORY_ADDRESS,
|
|
225
|
+
POLYMARKET_SAFE_INIT_CODE_HASH,
|
|
226
|
+
POLYMARKET_USDCE_ADDRESS,
|
|
227
|
+
POLYMARKET_PUSD_ADDRESS,
|
|
228
|
+
POLYMARKET_COLLATERAL_OFFRAMP_ADDRESS,
|
|
229
|
+
SAFE_MULTI_SEND_CALL_ONLY_ADDRESS,
|
|
230
|
+
derivePolymarketSafeAddress,
|
|
231
|
+
isPolymarketAsset,
|
|
232
|
+
fetchPolymarketBalances,
|
|
233
|
+
executePolymarketSafeTransfer
|
|
234
|
+
};
|