Skip to content

Commit

Permalink
Merge branch 'main' into sc-16603-feature-sdk-add-syruprouter-abi-to-…
Browse files Browse the repository at this point in the history
…maple-js
  • Loading branch information
callum-hyland authored May 20, 2024
2 parents e269427 + c6d3d5e commit e979ee3
Show file tree
Hide file tree
Showing 8 changed files with 2,177 additions and 2,017 deletions.
14 changes: 9 additions & 5 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name: Release Version

on:
push:
branches:
- 'main'
workflow_dispatch:
# push:
# branches:
# - 'main'

jobs:
release-api:
Expand All @@ -20,12 +21,15 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: 18.18.2
corepack: true

- name: Extract package version
run: echo "PACKAGE_VERSION=v$(cat package.json | jq -r .version)" >> $GITHUB_ENV

- name: Install dependencies
run: yarn
- name: Enable corepack and install dependencies
run: |
corepack enable
yarn install
- name: Build
run: |
Expand Down
17 changes: 10 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Local
.DS_Store
.vscode
yarn-error.log

# Build
node_modules
Expand All @@ -12,11 +11,15 @@ dist
coverage
.env

npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Yarn 3 / Berry
.pnp.*
.yarn/*
.yarn/cache
.yarn/patches
.yarn/plugins
.yarn/releases
.yarn/sdks
.yarn/versions
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
70 changes: 61 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ A JavaScript SDK for interacting with Maple Protocol's smart contracts.
- [Additional Resources](#additional-resources)
- [Utils](#utils)
- [Generating Unsigned Transaction Data](#generating-unsigned-transaction-data)
- [Generating Signed Transaction Data](#generating-signed-transaction-data)
- [Broadcasting Signed Transactions](#broadcasting-signed-transactions)

## Getting Started

Expand Down Expand Up @@ -88,36 +90,46 @@ const method = await (await poolContract.deposit(depositAmount)).wait()

## Utils

**Generating Unsigned Transaction Data**
### Generating Unsigned Transaction Data

This feature allows you to generate unsigned transaction data, facilitating the creation of transactions that can later be signed and sent to the network. This is particularly useful for offline transaction preparation or when keys are managed externally.
This utility allows you to generate unsigned transaction data, facilitating the creation of transactions that can later be signed and sent to the network. This is particularly useful for offline transaction preparation or when keys are managed externally.
Usage

```
import { utils } from '@maplelabs/maple-js'
const { txBytes, txInstance } = utils.generateTransactionData(parameters)
const { txBytes, txInstance } = utils.generateUnsignedTransactionData(parameters)
```

The `generateTransactionData` function supports creating unsigned transactions for specific actions, currently including `poolDeposit` and `poolQueueWithdrawal`. Depending on the action, parameters must be provided accordingly:
The `generateUnsignedTransactionData` function supports creating unsigned transactions for specific actions, currently including `poolDeposit` and `poolQueueWithdrawal`. Depending on the action, parameters must be provided accordingly:

- For `poolApprove`, specify the spender address and approve amount in assets.
- For `poolDeposit`, specify the deposit amount in assets.
- For `poolQueueWithdrawal`, specify the withdrawal amount in shares.

**_Parameters_**
**Parameters**

All calls to `generateTransactionData` require the following parameters:
All calls to `generateUnsignedTransactionData` require the following parameters:

```
interface CommonInputs {
provider: Provider
walletAddress: string
contractAddress: string
type: `poolDeposit` | `poolQueueWithdrawal`
type: poolApprove | poolDeposit | poolQueueWithdrawal
params: {}
}
```

`poolApprove` requires the following `params`:

```
params: {
spender: string // address
amount: BigNumberish // denominated in assets
}
```

`poolDeposit` requires the following `params`:

```
Expand All @@ -134,9 +146,49 @@ interface CommonInputs {
}
```

**_Example_**
### Generating Signed Transaction Data

This utility provides the functionality to combine unsigned transaction with a signature to create a signed transaction string. This is crucial for scenarios where transactions are prepared offline or in secure environments.

```
import { utils } from '@maplelabs/maple-js'
const signedTxData = utils.generateSignedTransactionData({
txBytes: 'unsignedTransactionBytes',
signature: 'signature'
})
```

**Parameters**

- `txBytes`: The serialized unsigned transaction data.
- `signature`: The signature obtained from signing the transaction hash.

This function returns the serialized signed transaction data, ready for broadcasting to the Ethereum network.

### Broadcasting Signed Transactions

This utility allows you to broadcast a signed transaction to the Ethereum network. This step is the final stage in submitting transactions, where the transaction is sent to a node in the network for processing and inclusion in the blockchain.

```
import { utils } from '@maplelabs/maple-js'
const txReceipt = await utils.broadcastSignedTransaction(
'signedTransactionData',
'rpcUrl'
)
```

**Parameters**

- `signedTxData`: The serialized signed transaction data.
- `rpcUrl`: The URL of the Ethereum JSON-RPC endpoint to which you are broadcasting the transaction.

This function sends the signed transaction to the network and returns the transaction receipt once the transaction has been processed.

**Utils Example**

An example usage of this feature, including parameter setup and function calls, can be found in the repository at `src/helpers/serialiseExampleUse`.
An example usage of these utilities, including parameter setup and function calls, can be found in the repository at `src/helpers/serialiseExampleUse`.

## Additional Resources

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@maplelabs/maple-js",
"version": "1.5.5",
"version": "1.5.8",
"description": "Maple SDK for JavaScript",
"author": "Maple Labs",
"license": "AGPL-3.0-only",
Expand Down Expand Up @@ -55,5 +55,6 @@
"@typechain/ethers-v5": "^11.1.2",
"ethers": "^5.5.3",
"typechain": "^8.3.2"
}
},
"packageManager": "yarn@4.1.1"
}
69 changes: 52 additions & 17 deletions src/helpers/serialiseExampleUse.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
import { utils } from 'ethers'
import { BigNumber } from '@ethersproject/bignumber'
import { joinSignature } from '@ethersproject/bytes'
import { JsonRpcProvider } from '@ethersproject/providers'
import { Wallet } from '@ethersproject/wallet'
import { parseTransaction } from 'ethers/lib/utils'
import * as dotenv from 'dotenv'

import { UnsignedTransactionBundle, generateTransactionData } from './serialiseTransaction'
import {
UnsignedTransactionBundle,
generateUnsignedTransactionData,
generateSignedTransactionData,
broadcastSignedTransaction
} from './serialiseTransaction'

import addresses from '../addresses/sepolia-prod'

dotenv.config()

const poolAddress = '0x4ff5637548e48abaf07baf4a5769b8abbe03a322'
const amount = BigInt('1000000')
const walletAddress = '0xaA5aA072369A3F34fcA3926DDf31977fAD95022D'
// Amounts
const AMOUNT = BigNumber.from(1e6) // 1 USDC / micro eth amount

// Addresses
const poolAddress = '0x722da756e3f615dc1fc8d84061e25bf0f181bdfb' // example pool in sepolia-prod
const walletAddress = '0xfoo' // your wallet address
const usdcAddress = addresses.USDC

async function main() {
// 🚨 1) Setup 🚨
Expand All @@ -24,47 +37,69 @@ async function main() {
const walletWithProvider = wallet.connect(provider)

// 🚨 2) Serialize the transaction (unsigned) 🚨
const { txBytes, txInstance }: UnsignedTransactionBundle = await generateTransactionData({
const { txBytes }: UnsignedTransactionBundle = await generateUnsignedTransactionData({
provider,
walletAddress,
contractAddress: poolAddress,
type: 'poolDeposit',
contractAddress: usdcAddress, // Asset to approve spending by pool (eg: usdc or weth)
type: 'poolApprove',
params: {
depositAmount: amount
amount: AMOUNT,
spender: poolAddress
}
})

// const { txBytes }: UnsignedTransactionBundle = await generateUnsignedTransactionData({
// provider,
// walletAddress,
// contractAddress: poolAddress, // address of the pool contract for depositing funds
// type: 'poolDeposit',
// params: {
// depositAmount: AMOUNT
// }
// })

// const { txBytes, txInstance }: UnsignedTransactionBundle = await generateTransactionData({
// provider,
// walletAddress,
// contractAddress: poolAddress,
// type: 'poolQueueWithdrawal',
// params: {
// withdrawalAmount: amount
// withdrawalAmount: AMOUNT
// }
// })

// 🚨 3) Sign the transaction 🚨
const deserializeTx = utils.parseTransaction(txBytes)
const { nonce, gasPrice, gasLimit, to, value, data, chainId } = deserializeTx
const deserializeTx = parseTransaction(txBytes)
const { nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, to, value, data, chainId, accessList, type } =
deserializeTx

if (!type) return

const transactionRequest = {
nonce,
gasPrice,
maxPriorityFeePerGas,
maxFeePerGas,
gasLimit,
to,
value: value.toHexString(),
data,
chainId
chainId,
accessList,
type
}

const signedTx = await walletWithProvider.signTransaction(transactionRequest)
const transactionParsed = parseTransaction(signedTx)
const { r, s, v } = transactionParsed
if (!r) return

console.log('✍🏼 :::', { signedTx })
const joinedSignature = joinSignature({ r, s, v })
const signedTxData = await generateSignedTransactionData({ txBytes, signature: joinedSignature })

// 🚨 4) Broadcast the transaction 🚨
const txResponse = await provider.sendTransaction(signedTx)
console.log('#️⃣ :::', { transactionHash: txResponse.hash })
const rpcUrl = process.env.RPC_URL as string
const txReceipt = await broadcastSignedTransaction(signedTxData, rpcUrl)
console.log({ txReceipt })
}

main()
Loading

0 comments on commit e979ee3

Please sign in to comment.