Skip to content

Commit

Permalink
chore: sessions dx (#486)
Browse files Browse the repository at this point in the history
* feat: SMA-792 sessions devex
  • Loading branch information
joepegler authored May 14, 2024
1 parent 498b79b commit b9b2077
Show file tree
Hide file tree
Showing 38 changed files with 1,899 additions and 591 deletions.
7 changes: 7 additions & 0 deletions .changeset/yellow-snails-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@biconomy/account": minor
---

Sessions DevEx

- Improved DevEx related to the creating and using of sessions [chore: sessions dx](https://github.com/bcnmy/biconomy-client-sdk/pull/486)
6 changes: 3 additions & 3 deletions .size-limit.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
{
"name": "core (esm)",
"path": "./dist/_esm/index.js",
"limit": "60 kB",
"limit": "65 kB",
"import": "*"
},
{
"name": "core (cjs)",
"path": "./dist/_cjs/index.js",
"limit": "60 kB"
"limit": "65 kB"
},
{
"name": "account (tree-shaking)",
"path": "./dist/_esm/index.js",
"limit": "60 kB",
"limit": "65 kB",
"import": "{ createSmartAccountClient }"
},
{
Expand Down
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# @biconomy/account

## 4.4.0

### Minor Changes

- Sessions DevEx

- Improved DevEx related to the creating and using of sessions

## 4.3.0

### Minor Changes

- Added transferOwnership and gasOffsets

- added transferOwnerhsip() method on the Smart Account
- added gasOffsets parameter to increase gas values

## 4.2.0

### Minor Changes
Expand Down
94 changes: 4 additions & 90 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ The Biconomy SDK is your all-in-one toolkit for building decentralized applicati

- [📄 Documentation and Resources](#-documentation-and-resources)
- [💼 Examples](#-examples)

- [🛠️ Initialise a smartAccount](#-initialise-a-smartAccount)
- [📨 send some eth with sponsorship](#-send-some-eth-with-sponsorship)
- [🔢 send a multi tx and pay gas with a token](#️-send-a-multi-tx-and-pay-gas-with-a-token)

- [License](#license)
- [Connect with Biconomy 🍊](#connect-with-biconomy-🍊)

Expand Down Expand Up @@ -73,91 +68,10 @@ For a comprehensive understanding of our project and to contribute effectively,

## 💼 Examples

### [Initialise a smartAccount](https://bcnmy.github.io/biconomy-client-sdk/functions/createSmartAccountClient.html)

| Key | Description |
| -------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
| [signer](https://bcnmy.github.io/biconomy-client-sdk/packages/account/docs/interfaces/SmartAccountSigner.html) | This signer will be used for signing userOps for any transactions you build. Will accept ethers.JsonRpcSigner as well as a viemWallet |
| [paymasterUrl](https://dashboard.biconomy.io) | You can pass in a paymasterUrl necessary for sponsoring transactions (retrieved from the biconomy dashboard) |
| [bundlerUrl](https://dashboard.biconomy.io) | You can pass in a bundlerUrl (retrieved from the biconomy dashboard) for sending transactions |

```typescript
import { createSmartAccountClient } from "@biconomy/account";
import { createWalletClient, http, createPublicClient } from "viem";
import { privateKeyToAccount, generatePrivateKey } from "viem/accounts";
import { mainnet as chain } from "viem/chains";

const account = privateKeyToAccount(generatePrivateKey());
const signer = createWalletClient({ account, chain, transport: http() });

const smartAccount = await createSmartAccountClient({
signer,
bundlerUrl,
paymasterUrl,
});
```

### [Send some eth with sponsorship](https://bcnmy.github.io/biconomy-client-sdk/classes/BiconomySmartAccountV2.html#sendTransaction)

| Key | Description |
| --------------------------------------------------------------------------------- | -------------------------------------------------------------- |
| [oneOrManyTx](https://bcnmy.github.io/biconomy-client-sdk/types/Transaction.html) | Submit multiple or one transactions |
| [userOpReceipt](https://bcnmy.github.io/biconomy-client-sdk/types/UserOpReceipt) | Returned information about your tx, receipts, userOpHashes etc |

```typescript
const oneOrManyTx = { to: "0x...", value: 1 };

const { wait } = await smartAccount.sendTransaction(oneOrManyTx, {
paymasterServiceData: {
mode: PaymasterMode.SPONSORED,
},
});

const {
receipt: { transactionHash },
userOpHash,
success,
} = await wait();
```

### [Send a multi tx and pay gas with a token](https://bcnmy.github.io/biconomy-client-sdk/classes/BiconomySmartAccountV2.html#getTokenFees)

| Key | Description |
| -------------------------------------------------------------------------------------------------------- | ------------------------------------------ |
| [buildUseropDto](https://bcnmy.github.io/biconomy-client-sdk/types/BuildUserOpOptions.html) | Options for building a userOp |
| [paymasterServiceData](https://bcnmy.github.io/biconomy-client-sdk/types/PaymasterUserOperationDto.html) | PaymasterOptions set in the buildUseropDto |

```typescript
import { encodeFunctionData, parseAbi } from "viem";
const preferredToken = "0x747A4168DB14F57871fa8cda8B5455D8C2a8e90a"; // USDC

const tx = {
to: nftAddress,
data: encodeFunctionData({
abi: parseAbi(["function safeMint(address to) public"]),
functionName: "safeMint",
args: ["0x..."],
}),
};

const buildUseropDto = {
paymasterServiceData: {
mode: PaymasterMode.ERC20,
preferredToken,
},
};

const { wait } = await smartAccount.sendTransaction(
[tx, tx] /* Mint twice */,
buildUseropDto
);

const {
receipt: { transactionHash },
userOpHash,
success,
} = await wait();
```
- [Initialise a smartAccount](examples/INITIALISE_A_SMART_CONTRACT.md)
- [send some eth with sponsorship](examples/SEND_SOME_ETH_WITH_SPONSORSHIP.md)
- [send a multi tx and pay gas with an erc20 token](examples/SEND_A_MULTI_TX_AND_PAY_GAS_WITH_TOKEN.md)
- [create and use a session](examples/CREATE_AND_USE_A_SESSION.md)

## License

Expand Down
Binary file modified bun.lockb
Binary file not shown.
138 changes: 138 additions & 0 deletions examples/CREATE_AND_USE_A_BATCH_SESSION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
### Create and Use a Batch Session

| Key | Description |
| ------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------- |
| [sessionConfigs](https://bcnmy.github.io/biconomy-client-sdk/interfaces/CreateSessionDataParams.html) | svm criteria |
| [createERC20SessionDatum](https://bcnmy.github.io/biconomy-client-sdk/functions/createERC20SessionDatum.html) | helper that returns erc20 svm data (not recommended) |
| [createABISessionDatum](https://bcnmy.github.io/biconomy-client-sdk/types/createABISessionDatum.html) | helper that returns abi svm data (recommended) |

```typescript
import {
createSmartAccountClient,
createSession,
createSessionSmartAccountClient,
Rule,
PaymasterMode,
Policy,
} from "@biconomy/account";
import { createWalletClient, http, createPublicClient } from "viem";
import { privateKeyToAccount, generatePrivateKey } from "viem/accounts";
import { mainnet as chain } from "viem/chains";

const withSponsorship = {
paymasterServiceData: { mode: PaymasterMode.SPONSORED },
};

const account = privateKeyToAccount(generatePrivateKey());
const signer = createWalletClient({ account, chain, transport: http() });
const smartAccount = await createSmartAccountClient({
signer,
bundlerUrl,
paymasterUrl,
});

// Retrieve bundler and pymaster urls from dashboard
const smartAccountAddress = await smartAccount.getAccountAddress();

// creates a store for the session, and saves the keys to it to be later retrieved
const { sessionKeyAddress, sessionStorageClient } = await createSessionKeyEOA(
smartAccount,
chain
);

const leaves: CreateSessionParams[] = [
createERC20SessionDatum({
interval: {
validUntil: 0,
validAfter: 0,
},
sessionKeyAddress,
sessionKeyData: encodeAbiParameters(
[
{ type: "address" },
{ type: "address" },
{ type: "address" },
{ type: "uint256" },
],
[sessionKeyAddress, token, recipient, amount]
),
}),
createABISessionDatum({
interval: {
validUntil: 0,
validAfter: 0,
},
sessionKeyAddress,
contractAddress: nftAddress,
functionSelector: "safeMint(address)",
rules: [
{
offset: 0,
condition: 0,
referenceValue: smartAccountAddress,
},
],
valueLimit: 0n,
}),
];

const { wait, session } = await createBatchSession(
smartAccount,
sessionKeyAddress,
sessionStorageClient,
leaves,
withSponsorship
);

const {
receipt: { transactionHash },
success,
} = await wait();

const smartAccountWithSession = await createSessionSmartAccountClient(
{
accountAddress: smartAccountAddress, // Set the account address on behalf of the user
bundlerUrl,
paymasterUrl,
chainId,
},
session,
true // if batching
);

const transferTx: Transaction = {
to: token,
data: encodeFunctionData({
abi: parseAbi(["function transfer(address _to, uint256 _value)"]),
functionName: "transfer",
args: [recipient, amount],
}),
};
const nftMintTx: Transaction = {
to: nftAddress,
data: encodeFunctionData({
abi: parseAbi(["function safeMint(address _to)"]),
functionName: "safeMint",
args: [smartAccountAddress],
}),
};

const txs = [transferTx, nftMintTx];

const batchSessionParams = await getBatchSessionTxParams(
["ERC20", "ABI"],
txs,
session,
chain
);

const { wait: sessionWait } = await smartAccountWithSession.sendTransaction(
txs,
{
...batchSessionParams,
...withSponsorship,
}
);

const { success } = await sessionWait();
```
Loading

0 comments on commit b9b2077

Please sign in to comment.