Skip to content

Commit

Permalink
Merge pull request #686 from dev-protocol/add-positionsCreate
Browse files Browse the repository at this point in the history
positionsCreate
  • Loading branch information
aggre authored Feb 2, 2022
2 parents 472d01f + 721318c commit 48d3db5
Show file tree
Hide file tree
Showing 18 changed files with 5,816 additions and 134 deletions.
42 changes: 42 additions & 0 deletions lib/agent/common/clients/lockupClients.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { l2AvailableNetworks } from '../const'
import type { UndefinedOr } from '@devprotocol/util-ts'
import { createLockupContract, LockupContract } from '../../../ethereum/lockup'
import {
createLockupContract as createLockupContractL2,
LockupContract as LockupContractL2,
} from '../../../l2/lockup'
import { Provider } from '@ethersproject/abstract-provider'
import { registryClientL1 } from './registryClientL1'

const cache: WeakMap<
Provider,
readonly [UndefinedOr<LockupContract>, UndefinedOr<LockupContractL2>]
> = new WeakMap()

export const lockupClients = async (
provider: Provider
): Promise<
readonly [UndefinedOr<LockupContract>, UndefinedOr<LockupContractL2>]
> => {
const res =
cache.get(provider) ??
(await (async () => {
const net = await provider.getNetwork()
const registry = await registryClientL1(provider)
const l1 = registry
? createLockupContract(provider)(await registry.lockup())
: undefined
const l2 = ((data) =>
data ? createLockupContractL2(provider)(data.map.lockup) : undefined)(
l2AvailableNetworks.find(({ chainId }) => chainId === net.chainId)
)
const results: readonly [
UndefinedOr<LockupContract>,
UndefinedOr<LockupContractL2>
] = [l1, l2]
// eslint-disable-next-line functional/no-expression-statement
cache.set(provider, results)
return results
})())
return res
}
30 changes: 30 additions & 0 deletions lib/agent/common/clients/registryClientL1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { l1AvailableNetworks } from '../const'
import type { UndefinedOr } from '@devprotocol/util-ts'
import {
createRegistryContract,
RegistryContract,
} from '../../../ethereum/registry'
import { Provider } from '@ethersproject/abstract-provider'

const cache: WeakMap<Provider, UndefinedOr<RegistryContract>> = new WeakMap()

export const registryClientL1 = async (
provider: Provider
): Promise<UndefinedOr<RegistryContract>> => {
const res =
cache.get(provider) ??
(await (async () => {
const net = await provider.getNetwork()
const l1Info = l1AvailableNetworks.find(
({ chainId }) => chainId === net.chainId
)
const l1 = l1Info
? createRegistryContract(provider)(l1Info.registry)
: undefined
const results = l1
// eslint-disable-next-line functional/no-expression-statement
cache.set(provider, l1)
return results
})())
return res
}
36 changes: 36 additions & 0 deletions lib/agent/common/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { addresses } from '../../addresses'

export type L1AvailableNetwork = {
readonly chainId: number
readonly registry: string
}

export type L2AvailableNetwork = {
readonly chainId: number
readonly map: {
readonly token: string
readonly lockup: string
readonly marketFactory: string
readonly metricsFactory: string
readonly policyFactory: string
readonly propertyFactory: string
readonly registry: string
readonly sTokens: string
readonly withdraw: string
}
}

export const l1AvailableNetworks: readonly L1AvailableNetwork[] = [
{ chainId: 1, registry: addresses.eth.main.registry },
{ chainId: 3, registry: addresses.eth.ropsten.registry },
]

export const l2AvailableNetworks: readonly L2AvailableNetwork[] = [
{
chainId: 42161,
map: addresses.arbitrum.one,
},
{ chainId: 421611, map: addresses.arbitrum.rinkeby },
{ chainId: 137, map: addresses.polygon.mainnet },
{ chainId: 80001, map: addresses.polygon.mumbai },
]
1 change: 1 addition & 0 deletions lib/agent/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { positionsCreate } from './positionsCreate'
11 changes: 11 additions & 0 deletions lib/agent/positionsClaim.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { TransactionResponse } from '@ethersproject/abstract-provider'
import { FallbackableOverrides } from '../common/utils/execute'
import { Provider } from '@ethersproject/abstract-provider'
import { UndefinedOr } from '@devprotocol/util-ts'

type PositionsClaim = (options: {
readonly provider: Provider
readonly positionId: number
readonly withdrawalAmount: string
readonly overrides?: FallbackableOverrides
}) => Promise<UndefinedOr<TransactionResponse>>
5 changes: 5 additions & 0 deletions lib/agent/positionsCreate.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { positionsCreate } from './positionsCreate'

describe('positionsCreate.ts', () => {
it.todo('TODO: Testing it')
})
22 changes: 22 additions & 0 deletions lib/agent/positionsCreate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { TransactionResponse } from '@ethersproject/abstract-provider'
import { FallbackableOverrides } from '../common/utils/execute'
import { Provider } from '@ethersproject/abstract-provider'
import { UndefinedOr } from '@devprotocol/util-ts'
import { lockupClients } from './common/clients/lockupClients'

type PositionsCreate = (options: {
readonly provider: Provider
readonly destination: string
readonly amount: string
readonly overrides?: FallbackableOverrides
}) => Promise<UndefinedOr<TransactionResponse>>

export const positionsCreate: PositionsCreate = async (options) => {
const [l1, l2] = await lockupClients(options.provider)

return l1
? l1.depositToProperty(options.destination, options.amount)
: l2
? l2.depositToProperty(options.destination, options.amount)
: undefined
}
8 changes: 8 additions & 0 deletions lib/agent/positionsGet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Provider } from '@ethersproject/abstract-provider'
import { UndefinedOr } from '@devprotocol/util-ts'
import { Positions } from '../ethereum/s-tokens/positions'

type PositionsGet = (options: {
readonly provider: Provider
readonly positionId: number
}) => Promise<UndefinedOr<Positions>>
9 changes: 9 additions & 0 deletions lib/agent/positionsList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Provider } from '@ethersproject/abstract-provider'
import { UndefinedOr } from '@devprotocol/util-ts'
import { Positions } from '../ethereum/s-tokens/positions'

type PositionsList = (options: {
readonly provider: Provider
readonly destination?: string
readonly user?: string
}) => Promise<UndefinedOr<readonly Positions[]>>
11 changes: 11 additions & 0 deletions lib/agent/positionsUpdate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { TransactionResponse } from '@ethersproject/abstract-provider'
import { FallbackableOverrides } from '../common/utils/execute'
import { Provider } from '@ethersproject/abstract-provider'
import { UndefinedOr } from '@devprotocol/util-ts'

type PositionsUpdate = (options: {
readonly provider: Provider
readonly positionId: number
readonly additionalAmount: string
readonly overrides?: FallbackableOverrides
}) => Promise<UndefinedOr<TransactionResponse>>
11 changes: 7 additions & 4 deletions lib/ethereum/lockup/depositToProperty.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { createDepositToPropertyCaller } from './depositToProperty'
import { stubbedSendTx } from '../../common/utils/for-test'
import { stubTransactionResposeFactory } from '../../common/utils/for-test'

describe('depositToProperty.spec.ts', () => {
describe('createDepositToPropertyCaller', () => {
it('call success', async () => {
const expected = true
const stubTx = stubTransactionResposeFactory({})

const lockupContract = {
depositToProperty: jest.fn().mockImplementation(stubbedSendTx),
// eslint-disable-next-line @typescript-eslint/no-unused-vars
depositToProperty: (propertyAddress: string, amount: number) =>
Promise.resolve(stubTx),
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -17,7 +20,7 @@ describe('depositToProperty.spec.ts', () => {
'100'
)

expect(result).toEqual(expected)
expect(result).toEqual(stubTx)
})

it('call failure', async () => {
Expand Down
6 changes: 3 additions & 3 deletions lib/ethereum/lockup/depositToProperty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import {
FallbackableOverrides,
MutationOption,
} from '../../common/utils/execute'
import { T } from 'ramda'
import { TransactionResponse } from '@ethersproject/abstract-provider'

export type CreateDepositToPropertyCaller = (
contract: ethers.Contract
) => (
propertyAddress: string,
amount: string,
overrides?: FallbackableOverrides
) => Promise<boolean>
) => Promise<TransactionResponse>

export const createDepositToPropertyCaller: CreateDepositToPropertyCaller =
(contract: ethers.Contract) =>
Expand All @@ -28,4 +28,4 @@ export const createDepositToPropertyCaller: CreateDepositToPropertyCaller =
mutation: true,
args: [propertyAddress, amount],
overrides,
}).then(T)
})
3 changes: 2 additions & 1 deletion lib/ethereum/lockup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { createDepositToPropertyCaller } from './depositToProperty'
import { createDepositToPositionCaller } from './depositToPosition'
import { createMigrateToSTokensCaller } from './migrateToSTokens'
import { FallbackableOverrides } from '../../common/utils/execute'
import { TransactionResponse } from '@ethersproject/abstract-provider'

export type LockupContract = {
readonly getValue: (
Expand Down Expand Up @@ -61,7 +62,7 @@ export type LockupContract = {
propertyAddress: string,
amount: string,
overrides?: FallbackableOverrides
) => Promise<boolean>
) => Promise<TransactionResponse>
readonly depositToPosition: (
positionTokenId: string,
amount: string,
Expand Down
3 changes: 2 additions & 1 deletion lib/l2/lockup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
createGetLockedupPropertiesCaller,
LockedupProperty,
} from './getLockedupProperties'
import { TransactionResponse } from '@ethersproject/abstract-provider'

export type LockupContract = {
readonly withdrawByPosition: (
Expand All @@ -38,7 +39,7 @@ export type LockupContract = {
readonly depositToProperty: (
propertyAddress: string,
amount: string
) => Promise<boolean>
) => Promise<TransactionResponse>
readonly depositToPosition: (
positionTokenId: string,
amount: string
Expand Down
17 changes: 12 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devprotocol/dev-kit",
"version": "5.12.0",
"version": "6.0.0",
"description": "Dev Kit for JavaScript",
"author": "abyssparanoia",
"license": "Apache-2.0",
Expand All @@ -13,6 +13,10 @@
"./l2": {
"import": "./l2/index.mjs",
"require": "./l2/index.js"
},
"./agent": {
"import": "./agent/index.mjs",
"require": "./agent/index.js"
}
},
"types": "./dist/dev-kit.d.ts",
Expand All @@ -22,6 +26,7 @@
"dist/*.ts",
"lib/**/*.ts",
"l2",
"agent",
"!**/*.spec.*"
],
"scripts": {
Expand All @@ -34,21 +39,21 @@
"test": "jest",
"test:coverage": "jest --coverage",
"prepack": "yarn test && yarn build",
"clean": "rimraf dist l2",
"clean": "rimraf dist l2 agent",
"prepare": "husky install"
},
"devDependencies": {
"@babel/preset-env": "7.16.11",
"@rollup/plugin-babel": "5.3.0",
"@rollup/plugin-multi-entry": "4.1.0",
"@rollup/plugin-node-resolve": "13.1.3",
"@types/jest": "27.4.0",
"@types/ramda": "0.27.64",
"@typescript-eslint/eslint-plugin": "5.10.2",
"@typescript-eslint/parser": "5.10.2",
"eslint": "8.8.0",
"eslint-config-prettier": "8.3.0",
"eslint-plugin-functional": "4.1.1",
"@types/jest": "27.4.0",
"@types/ramda": "0.27.64",
"eslint-plugin-jest": "26.0.0",
"ethers": "5.5.4",
"husky": "7.0.4",
Expand Down Expand Up @@ -84,9 +89,11 @@
],
"transformIgnorePatterns": [
"<rootDir>/node_modules/"
]
],
"testTimeout": 500000
},
"dependencies": {
"@devprotocol/util-ts": "^2.2.1",
"@types/bent": "^7.3.1",
"async-ray": "^3.2.0",
"bent": "^7.3.12",
Expand Down
5 changes: 5 additions & 0 deletions rollup.config.d.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ export default [
output: [{ file: './l2/index.d.ts', format: 'es' }],
plugins,
},
{
input: 'dist/lib/agent/index.d.ts',
output: [{ file: './agent/index.d.ts', format: 'es' }],
plugins,
},
]
Loading

0 comments on commit 48d3db5

Please sign in to comment.