Skip to content

Commit

Permalink
chore: Add EVM OApp SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista committed Nov 28, 2023
1 parent 940bd3a commit 4c54913
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/ua-utils-evm/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.turbo
dist
node_modules
3 changes: 3 additions & 0 deletions packages/ua-utils-evm/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../.eslintrc.json"
}
23 changes: 23 additions & 0 deletions packages/ua-utils-evm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<p align="center">
<a href="https://layerzero.network">
<img alt="LayerZero" style="max-width: 500px" src="https://d3a2dpnnrypp5h.cloudfront.net/bridge-app/lz.png"/>
</a>
</p>

<h1 align="center">@layerzerolabs/ua-utils-evm</h1>

<!-- The badges section -->
<p align="center">
<!-- Shields.io NPM published package version -->
<a href="https://www.npmjs.com/package/@layerzerolabs/ua-utils-evm"><img alt="NPM Version" src="https://img.shields.io/npm/v/@layerzerolabs/ua-utils-evm"/></a>
<!-- Shields.io NPM downloads -->
<a href="https://www.npmjs.com/package/@layerzerolabs/ua-utils-evm"><img alt="Downloads" src="https://img.shields.io/npm/dm/@layerzerolabs/ua-utils-evm"/></a>
<!-- Shields.io license badge -->
<a href="https://www.npmjs.com/package/@layerzerolabs/ua-utils-evm"><img alt="NPM License" src="https://img.shields.io/npm/l/@layerzerolabs/ua-utils-evm"/></a>
</p>

## Installation

```sh
$ npm install @layerzerolabs/ua-utils-evm
```
8 changes: 8 additions & 0 deletions packages/ua-utils-evm/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1',
},
};
55 changes: 55 additions & 0 deletions packages/ua-utils-evm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"name": "@layerzerolabs/ua-utils-evm",
"version": "0.1.0",
"private": true,
"description": "Utilities for working with LayerZero contracts",
"repository": {
"type": "git",
"url": "git+https://github.com/LayerZero-Labs/lz-utils.git",
"directory": "packages/ua-utils-evm"
},
"license": "MIT",
"exports": {
".": {
"types": "./dist/index.d.ts",
"require": "./dist/index.js",
"import": "./dist/index.mjs"
}
},
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"files": [
"dist/"
],
"scripts": {
"prebuild": "tsc -noEmit",
"build": "npx tsup",
"clean": "rm -rf dist",
"lint": "npx eslint '**/*.{js,ts,json}'",
"test": "jest"
},
"devDependencies": {
"@ethersproject/constants": "^5.7.0",
"@ethersproject/contracts": "^5.7.0",
"@layerzerolabs/lz-definitions": "~1.5.62",
"@layerzerolabs/test-utils": "~0.0.1",
"@layerzerolabs/ua-utils": "~0.1.0",
"@layerzerolabs/utils-evm": "~0.0.1",
"@types/jest": "^29.5.10",
"fast-check": "^3.14.0",
"jest": "^29.7.0",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"tslib": "~2.6.2",
"tsup": "^7.2.0",
"typescript": "^5.2.2",
"zod": "^3.22.4"
},
"peerDependencies": {
"@layerzerolabs/lz-definitions": "~1.5.62",
"@layerzerolabs/ua-utils": "~0.0.1",
"@layerzerolabs/utils-evm": "~0.0.1",
"zod": "^3.22.4"
}
}
25 changes: 25 additions & 0 deletions packages/ua-utils-evm/src/oapp/sdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Address, OmniTransaction } from '@layerzerolabs/ua-utils'
import { omniContractToPoint, OmniContract, ignoreZero, makeZero } from '@layerzerolabs/utils-evm'
import { EndpointId } from '@layerzerolabs/lz-definitions'
import { IOApp } from '@layerzerolabs/ua-utils'

export class OApp implements IOApp {
constructor(public readonly contract: OmniContract) {}

async peers(eid: EndpointId): Promise<string | undefined> {
return ignoreZero(await this.contract.contract.peers(eid))
}

async setPeer(eid: EndpointId, address: Address | null | undefined): Promise<OmniTransaction> {
const data = this.contract.contract.interface.encodeFunctionData('setPeer', [eid, makeZero(address)])

return this.createTransaction(data)
}

protected createTransaction(data: string): OmniTransaction {
return {
point: omniContractToPoint(this.contract),
data,
}
}
}
122 changes: 122 additions & 0 deletions packages/ua-utils-evm/test/oapp/sdk.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import fc from 'fast-check'
import { endpointArbitrary, evmAddressArbitrary } from '@layerzerolabs/test-utils'
import { Contract } from '@ethersproject/contracts'
import { OApp } from '@/oapp/sdk'
import { OmniContract, makeZero } from '@layerzerolabs/utils-evm'

describe('oapp/sdk', () => {
const jestFunctionArbitrary = fc.anything().map(() => jest.fn())

const oappOmniContractArbitrary = fc.record({
address: evmAddressArbitrary,
peers: jestFunctionArbitrary,
interface: fc.record({
encodeFunctionData: jestFunctionArbitrary,
}),
}) as fc.Arbitrary<unknown> as fc.Arbitrary<Contract>

const omniContractArbitrary: fc.Arbitrary<OmniContract> = fc.record({
eid: endpointArbitrary,
contract: oappOmniContractArbitrary,
})

describe('peers', () => {
it('should call peers on the contract', async () => {
await fc.assert(
fc.asyncProperty(omniContractArbitrary, endpointArbitrary, async (omniContract, peerEid) => {
const sdk = new OApp(omniContract)

await sdk.peers(peerEid)

expect(omniContract.contract.peers).toHaveBeenCalledTimes(1)
expect(omniContract.contract.peers).toHaveBeenCalledWith(peerEid)
})
)
})

it('should return undefined if peers() returns a zero address, null or undefined', async () => {
const peerArbitrary = fc.constantFrom(null, undefined, makeZero(null))

await fc.assert(
fc.asyncProperty(
omniContractArbitrary,
endpointArbitrary,
peerArbitrary,
async (omniContract, peerEid, peer) => {
omniContract.contract.peers.mockResolvedValue(peer)

const sdk = new OApp(omniContract)

expect(sdk.peers(peerEid)).resolves.toBeUndefined()
}
)
)
})

it('should return undefined if peers() returns null or undefined', async () => {
const peerArbitrary = evmAddressArbitrary

await fc.assert(
fc.asyncProperty(
omniContractArbitrary,
endpointArbitrary,
peerArbitrary,
async (omniContract, peerEid, peer) => {
omniContract.contract.peers.mockResolvedValue(peer)

const sdk = new OApp(omniContract)

expect(sdk.peers(peerEid)).resolves.toBe(peer)
}
)
)
})
})

describe('setPeer', () => {
it('should encode data for a setPeer call', async () => {
await fc.assert(
fc.asyncProperty(
omniContractArbitrary,
endpointArbitrary,
evmAddressArbitrary,
async (omniContract, peerEid, peerAddress) => {
const sdk = new OApp(omniContract)
const encodeFunctionData = omniContract.contract.interface.encodeFunctionData

await sdk.setPeer(peerEid, peerAddress)

expect(encodeFunctionData).toHaveBeenCalledTimes(1)
expect(encodeFunctionData).toHaveBeenCalledWith('setPeer', [peerEid, makeZero(peerAddress)])
}
)
)
})

it('should return an OmniTransaction', async () => {
await fc.assert(
fc.asyncProperty(
omniContractArbitrary,
endpointArbitrary,
evmAddressArbitrary,
fc.string(),
async (omniContract, peerEid, peerAddress, data) => {
const encodeFunctionData = omniContract.contract.interface.encodeFunctionData as jest.Mock
encodeFunctionData.mockReturnValue(data)

const sdk = new OApp(omniContract)
const transaction = await sdk.setPeer(peerEid, peerAddress)

expect(transaction).toEqual({
data,
point: {
eid: omniContract.eid,
address: omniContract.contract.address,
},
})
}
)
)
})
})
})
11 changes: 11 additions & 0 deletions packages/ua-utils-evm/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.json",
"exclude": ["dist", "node_modules"],
"include": ["src", "test"],
"compilerOptions": {
"types": ["node", "jest"],
"paths": {
"@/*": ["./src/*"]
}
}
}
12 changes: 12 additions & 0 deletions packages/ua-utils-evm/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { defineConfig } from 'tsup'

export default defineConfig({
entry: ['src/index.ts'],
outDir: './dist',
clean: true,
dts: true,
sourcemap: true,
splitting: false,
treeshake: true,
format: ['esm', 'cjs'],
})

0 comments on commit 4c54913

Please sign in to comment.