Skip to content

Commit

Permalink
feat: add mainnet polygon as supported chain
Browse files Browse the repository at this point in the history
  • Loading branch information
rthomare committed Jun 7, 2023
1 parent 3158673 commit 468f28a
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ The primary interfaces are the `SmartAccountProvider` and `BaseSmartContractAcco
The `SmartAccountProvider` is an ERC-1193 compliant Provider that wraps JSON RPC methods and some Wallet Methods (signing, sendTransaction, etc). It also provides two utility methods for sending UserOperations:

1. `sendUserOperation` -- this takes in `target`, `callData`, and an optional `value` which then constructs a UserOperation (UO), sends it, and returns the `hash` of the UO. It handles estimating gas, fetching fee data, (optionally) requesting paymasterAndData, and lastly signing. This is done via a middleware stack that runs in a specific order. The middleware order is `getDummyPaymasterData` => `estimateGas` => `getFeeData` => `getPaymasterAndData`. The paymaster fields are set to `0x` by default. They can be changed using `provider.withPaymasterMiddleware`.
2. `sendTransaction` -- this takes in a traditional Transaction Request object which then gets converted into a UO. Currently, the only data being used from the Transaction Request object is `to`, `callData` and `value`. Support for other fields is coming soon.
2. `sendTransaction` -- this takes in a traditional Transaction Request object which then gets converted into a UO. Currently, the only data being used from the Transaction Request object is `from`, `to`, `data` and `value`. Support for other fields is coming soon.

If you want to add support for your own `SmartAccounts` then you will need to provide an implementation of `BaseSmartContractAccount`. You can see an example of this in [SimpleSmartContractAccount](packages/core/src/account/simple.ts). You will need to implement 4 methods:

Expand Down
9 changes: 8 additions & 1 deletion examples/alchemy-daapp/src/configs/clientConfigs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Chain, polygonMumbai, sepolia } from "viem/chains";
import { Chain, polygon, polygonMumbai, sepolia } from "viem/chains";

export interface DAAppConfiguration {
nftContractAddress: `0x${string}`;
Expand All @@ -10,6 +10,13 @@ export interface DAAppConfiguration {

// TODO: Replace with your own contract addresses and policy ids, feel free to add or remove chains.
export const daappConfigurations: Record<number, DAAppConfiguration> = {
[polygon.id]: {
nftContractAddress: "0xb7b9424ef3d1b9086b7e53276c4aad68a1dd971c",
simpleAccountFactoryAddress: "0x15Ba39375ee2Ab563E8873C8390be6f2E2F50232",
gasManagerPolicyId: "f0f6fb99-28b0-4e9e-a40d-201ceb1f2b3b",
rpcUrl: `/api/rpc/proxy?chainId=${polygon.id}`,
chain: polygon,
},
[polygonMumbai.id]: {
nftContractAddress: "0x5679b0de84bba361d31b2e7152ab20f0f8565245",
simpleAccountFactoryAddress: "0x9406Cc6185a346906296840746125a0E44976454",
Expand Down
3 changes: 2 additions & 1 deletion examples/alchemy-daapp/src/configs/serverConfigs.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { polygonMumbai, sepolia } from "viem/chains";
import { polygon, polygonMumbai, sepolia } from "viem/chains";
import { env } from "~/env.mjs";

// TODO: Replace with your own api urls per chain.
const API_URLs: Record<number, string> = {
[polygonMumbai.id]: env.MUMBAI_ALCHEMY_API_URL,
[sepolia.id]: env.SEPOLIA_ALCHEMY_API_URL,
[polygon.id]: env.POLYGON_ALCHEMY_API_URL,
};

export function getApiUrl(chainId: number | string) {
Expand Down
2 changes: 2 additions & 0 deletions examples/alchemy-daapp/src/env.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const env = createEnv({
NODE_ENV: z.enum(["development", "test", "production"]),
MUMBAI_ALCHEMY_API_URL: z.string().url(),
SEPOLIA_ALCHEMY_API_URL: z.string().url(),
POLYGON_ALCHEMY_API_URL: z.string().url(),
},

/**
Expand All @@ -25,6 +26,7 @@ export const env = createEnv({
*/
runtimeEnv: {
NODE_ENV: process.env.NODE_ENV,
POLYGON_ALCHEMY_API_URL: process.env.POLYGON_ALCHEMY_API_URL,
MUMBAI_ALCHEMY_API_URL: process.env.MUMBAI_ALCHEMY_API_URL,
SEPOLIA_ALCHEMY_API_URL: process.env.SEPOLIA_ALCHEMY_API_URL,
},
Expand Down
3 changes: 2 additions & 1 deletion examples/alchemy-daapp/src/pages/api/rpc/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
return res.send(
await callEndpoint("POST", getApiUrl(chainId as string), req.body)
);
} catch {
} catch (e) {
console.error(e);
return res.status(500).send("Internal Server Error");
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -168,19 +168,20 @@ const onboardingStepHandlers: Record<
if (!context.smartAccountSigner?.account || !targetAddress) {
throw new Error("No SCW account was found");
}
const { hash: mintDeployOpHash } =
await context.smartAccountSigner.sendUserOperation(
appConfig.nftContractAddress,
encodeFunctionData({
abi: NFTContractABI.abi,
functionName: "mintTo",
args: [targetAddress],
})
);
const mintDeployTxnHash = await context.smartAccountSigner.sendTransaction({
from: await context.smartAccountSigner.account.getAddress(),
to: appConfig.nftContractAddress,
data: encodeFunctionData({
abi: NFTContractABI.abi,
functionName: "mintTo",
args: [targetAddress],
}),
});

return {
nextStep: OnboardingStepIdentifier.CHECK_OP_COMPLETE,
addedContext: {
mintDeployOpHash: mintDeployOpHash as `0x${string}`,
mintDeployTxnHash: mintDeployTxnHash as `0x${string}`,
},
};
},
Expand All @@ -190,11 +191,11 @@ const onboardingStepHandlers: Record<
*/
[OnboardingStepIdentifier.CHECK_OP_COMPLETE]: async (context) => {
await pollForLambdaForComplete(async () => {
if (!context.mintDeployOpHash) {
if (!context.mintDeployTxnHash) {
throw new Error("No mint deploy operation Hash was found");
}
return context
.client!.getUserOperationReceipt(context.mintDeployOpHash)
.client!.getTransactionReceipt({ hash: context.mintDeployTxnHash })
.then((receipt) => {
return receipt !== null;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface OnboardingContext {
smartAccountSigner: SmartAccountProvider;
chain: Chain;
entrypointAddress: `0x${string}`;
mintDeployOpHash: `0x${string}`;
mintDeployTxnHash: `0x${string}`;
}

export interface OnboardingStep {
Expand Down Expand Up @@ -115,10 +115,10 @@ export function metaForStepIdentifier(
<Text>
Waiting for operation{" "}
<Link
href={`${chain.blockExplorers?.default}/${context.mintDeployOpHash}`}
href={`${chain.blockExplorers?.default}/${context.mintDeployTxnHash}`}
target="_blank"
>
${context.mintDeployOpHash}
${context.mintDeployTxnHash}
</Link>{" "}
to complete.
</Text>
Expand Down
10 changes: 10 additions & 0 deletions examples/contracts/DAAppNFT/migrations.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Polygon Mumbai

## Version 1

Deployer: 0x15a411507e901f26965f0ebcd3155834b058a6b2
Expand All @@ -9,3 +11,11 @@ Transaction hash: 0x46f95ea67e2b103f607884a87fd25d53d619eaa7d67437f1d7cb51f7c99b
Deployer: 0x15a411507e901f26965f0ebcd3155834b058a6b2
Deployed to: 0x5679b0de84bba361d31b2e7152ab20f0f8565245
Transaction hash: 0x59b65a2bd7a730efbea86376c986efcc2cd65c076f39a47cea84a6d4246c8d03

# Polygon Mainnet

## Version 1

Deployer: 0x15a411507e901f26965f0ebcd3155834b058a6b2
Deployed to: 0xb7b9424ef3d1b9086b7e53276c4aad68a1dd971c
Transaction hash: 0xdc73899cf183cf852605c7a7473c0b110554155d5f4cc03452c11a4ce0d4ff33

0 comments on commit 468f28a

Please sign in to comment.