Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion packages/ccip-js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,19 @@ const walletClient = createWalletClient({
transport: custom(window.ethereum!),
})

// Using ethers.js signer & provider
import { ethers } from 'ethers'
import { ethersSignerToWalletClient, ethersProviderToPublicClient } from '@chainlink/ccip-js'

const ethersProvider = new ethers.JsonRpcProvider('https://rpc.example.com')
const ethersSigner = new ethers.Wallet(PRIVATE_KEY, ethersProvider)

const ethersWalletClient = await ethersSignerToWalletClient(ethersSigner, mainnet)
const ethersPublicClient = ethersProviderToPublicClient(ethersProvider, mainnet)

// The SDK methods accept Viem clients. The adapters above allow
// passing ethers providers and signers by converting them to Viem clients.

// Approve Router to transfer tokens on user's behalf
const { txHash, txReceipt } = await ccipClient.approveRouter({
client: walletClient,
Expand Down Expand Up @@ -605,7 +618,15 @@ folder. From there, the relevant ABI arrays must be manually moved to `./src/abi

#### Running tests

1. Integration tests against testnets are favored in the ccip-js package.
1. Integration tests against testnets are favored in the ccip-js package. They require the you set the following environment variables:

```
PRIVATE_KEY=
AVALANCHE_FUJI_RPC_URL
SEPOLIA_RPC_URL
HEDERA_TESTNET_RPC_URL

```

2. Start by cd into `packages/ccip-js` and then run `pnpm install` OR from the project root you can run `pnpm i -w`

Expand Down
4 changes: 3 additions & 1 deletion packages/ccip-js/package.json
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: we will need to bump version on this before we publish
Current published version: https://www.npmjs.com/package/@chainlink/ccip-js

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"build": "tsc && hardhat compile",
"lint": "eslint 'src/**/*.{ts,js}'",
"format": "prettier --write 'src/**/*.{ts,js,json,md}'",
"t:int": "jest --coverage -u --testMatch=\"**/integration-testnet.test.ts\" --detectOpenHandles",
"t:int": "jest --coverage -u --testMatch=\"**/integration-testnet**.test.ts\" --detectOpenHandles",
"t:int:viem": "jest --coverage -u --testMatch=\"**/integration-testnet.test.ts\" --detectOpenHandles",
"t:int:ethers": "jest --coverage -u --testMatch=\"**/integration-testnet-ethers.test.ts\" --detectOpenHandles",
"t:unit": "jest --coverage -u --testMatch=\"**/unit.test.ts\" ",
"test:hh": "hardhat test"
},
Expand Down
6 changes: 6 additions & 0 deletions packages/ccip-js/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ import { TRANSFER_STATUS_FROM_BLOCK_SHIFT, ExecutionStateChangedABI } from './co
import { parseAbi } from 'viem'

export { IERC20ABI }
export {
ethersProviderToTransport,
ethersSignerToAccount,
ethersProviderToPublicClient,
ethersSignerToWalletClient,
} from './ethers-adapters'

/** An object containing methods for cross-chain transfer management.
* @typedef {Object} Client */
Expand Down
65 changes: 65 additions & 0 deletions packages/ccip-js/src/ethers-adapters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import type { Provider, Signer, TypedDataField } from 'ethers'
import type {Address, Hash, Transport, WalletClient, PublicClient } from 'viem'

import { custom, createPublicClient, createWalletClient } from 'viem'
import { toAccount } from 'viem/accounts'

/** Convert an ethers provider to a viem transport. */
export function ethersProviderToTransport(provider: Provider): Transport {
return custom({
async request({ method, params }) {
return (provider as any).send(method, params as any)
},
})
}

/** Convert an ethers signer to a viem LocalAccount. */
export async function ethersSignerToAccount(signer: Signer) {
return toAccount({
address: (await signer.getAddress()) as unknown as Address,
async signMessage({ message }) {
const data = typeof message === 'string' ? message : new TextDecoder().decode(message as any)
return signer.signMessage(data) as unknown as Hash
},
async signTransaction(txn) {
return signer.signTransaction({
chainId: txn.chainId,
data: txn.data,
gasLimit: txn.gas,
gasPrice: txn.gasPrice,
nonce: txn.nonce,
to: txn.to,
value: txn.value,
type: txn.type === 'legacy' ? 0 : txn.type === 'eip2930' ? 1 : txn.type === 'eip1559' ? 2 : undefined,
...(txn.type && txn.accessList ? { accessList: txn.accessList } : {}),
...(txn.maxPriorityFeePerGas ? { maxPriorityFeePerGas: txn.maxPriorityFeePerGas } : {}),
...(txn.maxFeePerGas ? { maxFeePerGas: txn.maxFeePerGas } : {}),
} as any) as unknown as Hash
},
async signTypedData({ domain, types, message }) {
const { EIP712Domain: _removed, ...rest } = types as any
const signTypedData = (signer as any)._signTypedData ?? (signer as any).signTypedData
return signTypedData(domain ?? {}, rest as Record<string, TypedDataField[]>, message) as unknown as Hash
},
})
}

/** Create a viem PublicClient from an ethers provider. */
export function ethersProviderToPublicClient(provider: Provider, chain: any): PublicClient {
return createPublicClient({ chain: chain as any, transport: ethersProviderToTransport(provider) }) as unknown as PublicClient
}

/** Create a viem WalletClient from an ethers signer. */
export async function ethersSignerToWalletClient(
signer: Signer & { provider: Provider | null },
chain: any,
): Promise<WalletClient> {
if (!signer.provider) {
throw new Error('ethers signer must be connected to a provider')
}
return createWalletClient({
chain: chain as any,
transport: ethersProviderToTransport(signer.provider),
account: await ethersSignerToAccount(signer),
}) as unknown as WalletClient
}
Loading
Loading