Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 614b5f6

Browse files
committedDec 6, 2024·
changes
1 parent f5ceaf9 commit 614b5f6

File tree

1 file changed

+97
-130
lines changed

1 file changed

+97
-130
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,58 @@
1-
<<<<<<< HEAD
21
import type {
32
SolanaReadRequest,
43
SolanaTransaction,
54
SolanaWalletClient,
65
} from "@goat-sdk/core";
76
import {
87
type Connection,
8+
PublicKey,
99
TransactionMessage,
1010
VersionedTransaction,
11-
PublicKey,
1211
} from "@solana/web3.js";
13-
=======
14-
import type { SolanaReadRequest, SolanaTransaction, SolanaWalletClient } from "@goat-sdk/core";
15-
import { type Connection, PublicKey, TransactionMessage, VersionedTransaction } from "@solana/web3.js";
16-
>>>>>>> origin/main
1712
import bs58 from "bs58";
1813
import { createCrossmintAPI } from "./api";
1914

2015
type CommonParameters = {
21-
chain: "solana";
22-
connection: Connection;
23-
env?: "staging" | "production";
16+
chain: "solana";
17+
connection: Connection;
18+
env?: "staging" | "production";
2419
};
2520

2621
type EmailLocatorParameters = CommonParameters & {
27-
email: string;
22+
email: string;
2823
};
2924

3025
type PhoneLocatorParameters = CommonParameters & {
31-
phone: string;
26+
phone: string;
3227
};
3328

3429
type UserIdLocatorParameters = CommonParameters & {
35-
userId: number;
30+
userId: number;
3631
};
3732

3833
type AddressLocatorParameters = CommonParameters & {
39-
address: string;
34+
address: string;
4035
};
4136

4237
type CustodialOptions =
43-
| EmailLocatorParameters
44-
| PhoneLocatorParameters
45-
| UserIdLocatorParameters
46-
| AddressLocatorParameters;
38+
| EmailLocatorParameters
39+
| PhoneLocatorParameters
40+
| UserIdLocatorParameters
41+
| AddressLocatorParameters;
4742

4843
function getLocator(params: CustodialOptions): string | number {
49-
if ("address" in params) return params.address;
50-
if ("email" in params) return `email:${params.email}`;
51-
if ("phone" in params) return `phone:${params.phone}`;
52-
return `userId:${params.userId}`;
44+
if ("address" in params) return params.address;
45+
if ("email" in params) return `email:${params.email}`;
46+
if ("phone" in params) return `phone:${params.phone}`;
47+
return `userId:${params.userId}`;
5348
}
5449

5550
export function custodialFactory(apiKey: string) {
56-
return async function custodial(params: CustodialOptions): Promise<SolanaWalletClient> {
57-
const { connection, env = "staging" } = params;
51+
return async function custodial(
52+
params: CustodialOptions,
53+
): Promise<SolanaWalletClient> {
54+
const { connection, env = "staging" } = params;
5855

59-
<<<<<<< HEAD
6056
const locator = `${getLocator(params)}`;
6157
const client = createCrossmintAPI(apiKey, env);
6258
const { address } = await client.getWallet(locator);
@@ -77,43 +73,27 @@ export function custodialFactory(apiKey: string) {
7773
locator,
7874
message,
7975
);
80-
=======
81-
const locator = `${getLocator(params)}:solana-custodial-wallet`;
82-
const client = createCrossmintAPI(apiKey, env);
83-
const { address } = await client.getWallet(locator);
84-
85-
return {
86-
getAddress() {
87-
return address;
88-
},
89-
getChain() {
90-
return {
91-
type: "solana",
92-
};
93-
},
94-
async signMessage(message: string) {
95-
try {
96-
const { id } = await client.signMessageForCustodialWallet(locator, message);
97-
>>>>>>> origin/main
98-
99-
while (true) {
100-
const latestSignature = await client.checkSignatureStatus(id, address);
101-
102-
if (latestSignature.status === "success") {
103-
if (!latestSignature.outputSignature) {
104-
throw new Error("Signature is undefined");
105-
}
106-
107-
return {
108-
signature: latestSignature.outputSignature,
109-
};
110-
}
111-
112-
if (latestSignature.status === "failed") {
113-
throw new Error("Signature failed");
114-
}
115-
116-
<<<<<<< HEAD
76+
77+
while (true) {
78+
const latestSignature = await client.checkSignatureStatus(
79+
id,
80+
address,
81+
);
82+
83+
if (latestSignature.status === "success") {
84+
if (!latestSignature.outputSignature) {
85+
throw new Error("Signature is undefined");
86+
}
87+
88+
return {
89+
signature: latestSignature.outputSignature,
90+
};
91+
}
92+
93+
if (latestSignature.status === "failed") {
94+
throw new Error("Signature failed");
95+
}
96+
11797
await new Promise((resolve) => setTimeout(resolve, 3000)); // Wait 3 seconds
11898
}
11999
} catch (error) {
@@ -135,73 +115,60 @@ export function custodialFactory(apiKey: string) {
135115
const encodedVersionedTransaction = bs58.encode(
136116
serializedVersionedTransaction,
137117
);
138-
=======
139-
await new Promise((resolve) => setTimeout(resolve, 3000)); // Wait 3 seconds
140-
}
141-
} catch (error) {
142-
throw new Error(`Failed to sign message: ${error}`);
143-
}
144-
},
145-
async sendTransaction({ instructions }: SolanaTransaction) {
146-
const latestBlockhash = await connection.getLatestBlockhash("confirmed");
147-
const message = new TransactionMessage({
148-
// Placeholder payer key since Crossmint will override it
149-
payerKey: new PublicKey("placeholder"),
150-
recentBlockhash: latestBlockhash.blockhash,
151-
instructions,
152-
}).compileToV0Message();
153-
const transaction = new VersionedTransaction(message);
154-
const serializedVersionedTransaction = transaction.serialize();
155-
const encodedVersionedTransaction = bs58.encode(serializedVersionedTransaction);
156-
>>>>>>> origin/main
157-
158-
const { id: transactionId } = await client.createTransactionForCustodialWallet(
159-
locator,
160-
encodedVersionedTransaction,
161-
);
162-
163-
while (true) {
164-
const latestTransaction = await client.checkTransactionStatus(locator, transactionId);
165-
166-
if (latestTransaction.status === "success") {
167-
console.log(`Transaction ${latestTransaction.status}`);
168-
return {
169-
hash: latestTransaction.onChain?.txId ?? "",
170-
};
171-
}
172-
173-
if (latestTransaction.status === "failed") {
174-
throw new Error(`Transaction failed: ${latestTransaction.onChain?.txId}`);
175-
}
176-
177-
await new Promise((resolve) => setTimeout(resolve, 3000)); // Wait 3 seconds
178-
}
179-
},
180-
async read(request: SolanaReadRequest) {
181-
const { accountAddress } = request;
182-
183-
const pubkey = new PublicKey(accountAddress);
184-
const accountInfo = await connection.getAccountInfo(pubkey);
185-
186-
if (!accountInfo) {
187-
throw new Error(`Account ${accountAddress} not found`);
188-
}
189-
190-
return {
191-
value: accountInfo,
192-
};
193-
},
194-
async balanceOf(address: string) {
195-
const pubkey = new PublicKey(address);
196-
const balance = await connection.getBalance(pubkey);
197-
198-
return {
199-
value: BigInt(balance),
200-
decimals: 9,
201-
symbol: "SOL",
202-
name: "Solana",
203-
};
204-
},
205-
};
206-
};
118+
119+
const { id: transactionId } =
120+
await client.createTransactionForCustodialWallet(
121+
locator,
122+
encodedVersionedTransaction,
123+
);
124+
125+
while (true) {
126+
const latestTransaction = await client.checkTransactionStatus(
127+
locator,
128+
transactionId,
129+
);
130+
131+
if (latestTransaction.status === "success") {
132+
console.log(`Transaction ${latestTransaction.status}`);
133+
return {
134+
hash: latestTransaction.onChain?.txId ?? "",
135+
};
136+
}
137+
138+
if (latestTransaction.status === "failed") {
139+
throw new Error(
140+
`Transaction failed: ${latestTransaction.onChain?.txId}`,
141+
);
142+
}
143+
144+
await new Promise((resolve) => setTimeout(resolve, 3000)); // Wait 3 seconds
145+
}
146+
},
147+
async read(request: SolanaReadRequest) {
148+
const { accountAddress } = request;
149+
150+
const pubkey = new PublicKey(accountAddress);
151+
const accountInfo = await connection.getAccountInfo(pubkey);
152+
153+
if (!accountInfo) {
154+
throw new Error(`Account ${accountAddress} not found`);
155+
}
156+
157+
return {
158+
value: accountInfo,
159+
};
160+
},
161+
async balanceOf(address: string) {
162+
const pubkey = new PublicKey(address);
163+
const balance = await connection.getBalance(pubkey);
164+
165+
return {
166+
value: BigInt(balance),
167+
decimals: 9,
168+
symbol: "SOL",
169+
name: "Solana",
170+
};
171+
},
172+
};
173+
};
207174
}

0 commit comments

Comments
 (0)
Please sign in to comment.