-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1032 from lens-protocol/T-23349/ethers-adapter
feat: ethers.js adapter
- Loading branch information
Showing
8 changed files
with
255 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { testnet } from '@lens-protocol/env'; | ||
import { afterAll, beforeAll, describe, it } from 'vitest'; | ||
|
||
import { assertOk, evmAddress, uri } from '@lens-protocol/types'; | ||
import { handleWith } from '.'; | ||
import { post } from '../actions/post'; | ||
import { PublicClient } from '../clients'; | ||
|
||
import { Network, Wallet, getDefaultProvider } from '@lens-network/sdk/ethers'; | ||
import { TestLock } from '../../testing-utils'; | ||
|
||
// biome-ignore lint/suspicious/noExplicitAny: needs a fix in @lens-network/sdk | ||
const wallet = new Wallet(import.meta.env.PRIVATE_KEY, getDefaultProvider(Network.Testnet) as any); | ||
|
||
const owner = evmAddress(wallet.address); | ||
const app = evmAddress(import.meta.env.TEST_APP); | ||
const account = evmAddress(import.meta.env.TEST_ACCOUNT); | ||
|
||
const publicClient = PublicClient.create({ | ||
environment: testnet, | ||
origin: 'http://example.com', | ||
}); | ||
|
||
describe('Given an integration with ethers.js', () => { | ||
beforeAll(async () => { | ||
await TestLock.acquire('post'); | ||
}); | ||
|
||
afterAll(() => { | ||
TestLock.release('post'); | ||
}); | ||
|
||
describe('When handling transaction actions', () => { | ||
it.sequential('Then it should be possible to chain them with other helpers', async () => { | ||
const authenticated = await publicClient.login({ | ||
accountOwner: { account, app, owner }, | ||
signMessage: (message: string) => wallet.signMessage(message), | ||
}); | ||
const sessionClient = authenticated._unsafeUnwrap(); | ||
|
||
const result = await post(sessionClient, { | ||
contentUri: uri('https://devnet.irys.xyz/3n3Ujg3jPBHX58MPPqYXBSQtPhTgrcTk4RedJgV1Ejhb'), | ||
}) | ||
.andThen(handleWith(wallet)) | ||
.andThen(sessionClient.waitForTransaction); | ||
|
||
assertOk(result); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './signer'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { ResultAsync, errAsync, okAsync, txHash } from '@lens-protocol/types'; | ||
import type { TxHash } from '@lens-protocol/types'; | ||
import type { Signer } from 'ethers'; | ||
import { types } from 'zksync-ethers'; | ||
|
||
import type { | ||
SelfFundedTransactionRequest, | ||
SponsoredTransactionRequest, | ||
} from '@lens-protocol/graphql'; | ||
import { SigningError, ValidationError } from '../errors'; | ||
import { type OperationHandler, type OperationResult, isTransactionRequest } from '../types'; | ||
|
||
function nullableToOptional<T extends object>( | ||
input: T, | ||
): Partial<{ [K in keyof T]: Exclude<T[K], null> }> { | ||
// biome-ignore lint/suspicious/noExplicitAny: simplicity | ||
const result: any = {}; | ||
|
||
for (const key in input) { | ||
if (Object.prototype.hasOwnProperty.call(input, key)) { | ||
const value = input[key]; | ||
if (value !== null) { | ||
result[key] = value; | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
function signWith( | ||
signer: Signer, | ||
request: SponsoredTransactionRequest | SelfFundedTransactionRequest, | ||
): ResultAsync<TxHash, SigningError> { | ||
if (request.__typename === 'SponsoredTransactionRequest') { | ||
const { __typename, from, customData, ...transactionLike } = request.raw; | ||
const tx: types.TransactionLike = types.Transaction.from({ | ||
...transactionLike, | ||
...nullableToOptional(customData), | ||
}); | ||
|
||
return ResultAsync.fromPromise( | ||
signer.sendTransaction(tx).then((tx) => txHash(tx.hash)), | ||
(err) => SigningError.from(err), | ||
); | ||
} | ||
const { __typename, from, ...transactionLike } = request.raw; | ||
return ResultAsync.fromPromise( | ||
signer.sendTransaction(transactionLike).then((tx) => txHash(tx.hash)), | ||
(err) => SigningError.from(err), | ||
); | ||
} | ||
|
||
export function handleWith(signer: Signer): OperationHandler { | ||
return <T extends string, E extends string>( | ||
result: OperationResult<T, E>, | ||
): ResultAsync<TxHash, SigningError | ValidationError<E>> => { | ||
if ('hash' in result) { | ||
return okAsync(result.hash); | ||
} | ||
|
||
if (isTransactionRequest(result)) { | ||
return signWith(signer, result); | ||
} | ||
|
||
return errAsync(ValidationError.fromErrorResponse(result)); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.