Skip to content

Commit 48d3db5

Browse files
authored
Merge pull request #686 from dev-protocol/add-positionsCreate
positionsCreate
2 parents 472d01f + 721318c commit 48d3db5

18 files changed

+5816
-134
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { l2AvailableNetworks } from '../const'
2+
import type { UndefinedOr } from '@devprotocol/util-ts'
3+
import { createLockupContract, LockupContract } from '../../../ethereum/lockup'
4+
import {
5+
createLockupContract as createLockupContractL2,
6+
LockupContract as LockupContractL2,
7+
} from '../../../l2/lockup'
8+
import { Provider } from '@ethersproject/abstract-provider'
9+
import { registryClientL1 } from './registryClientL1'
10+
11+
const cache: WeakMap<
12+
Provider,
13+
readonly [UndefinedOr<LockupContract>, UndefinedOr<LockupContractL2>]
14+
> = new WeakMap()
15+
16+
export const lockupClients = async (
17+
provider: Provider
18+
): Promise<
19+
readonly [UndefinedOr<LockupContract>, UndefinedOr<LockupContractL2>]
20+
> => {
21+
const res =
22+
cache.get(provider) ??
23+
(await (async () => {
24+
const net = await provider.getNetwork()
25+
const registry = await registryClientL1(provider)
26+
const l1 = registry
27+
? createLockupContract(provider)(await registry.lockup())
28+
: undefined
29+
const l2 = ((data) =>
30+
data ? createLockupContractL2(provider)(data.map.lockup) : undefined)(
31+
l2AvailableNetworks.find(({ chainId }) => chainId === net.chainId)
32+
)
33+
const results: readonly [
34+
UndefinedOr<LockupContract>,
35+
UndefinedOr<LockupContractL2>
36+
] = [l1, l2]
37+
// eslint-disable-next-line functional/no-expression-statement
38+
cache.set(provider, results)
39+
return results
40+
})())
41+
return res
42+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { l1AvailableNetworks } from '../const'
2+
import type { UndefinedOr } from '@devprotocol/util-ts'
3+
import {
4+
createRegistryContract,
5+
RegistryContract,
6+
} from '../../../ethereum/registry'
7+
import { Provider } from '@ethersproject/abstract-provider'
8+
9+
const cache: WeakMap<Provider, UndefinedOr<RegistryContract>> = new WeakMap()
10+
11+
export const registryClientL1 = async (
12+
provider: Provider
13+
): Promise<UndefinedOr<RegistryContract>> => {
14+
const res =
15+
cache.get(provider) ??
16+
(await (async () => {
17+
const net = await provider.getNetwork()
18+
const l1Info = l1AvailableNetworks.find(
19+
({ chainId }) => chainId === net.chainId
20+
)
21+
const l1 = l1Info
22+
? createRegistryContract(provider)(l1Info.registry)
23+
: undefined
24+
const results = l1
25+
// eslint-disable-next-line functional/no-expression-statement
26+
cache.set(provider, l1)
27+
return results
28+
})())
29+
return res
30+
}

lib/agent/common/const.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { addresses } from '../../addresses'
2+
3+
export type L1AvailableNetwork = {
4+
readonly chainId: number
5+
readonly registry: string
6+
}
7+
8+
export type L2AvailableNetwork = {
9+
readonly chainId: number
10+
readonly map: {
11+
readonly token: string
12+
readonly lockup: string
13+
readonly marketFactory: string
14+
readonly metricsFactory: string
15+
readonly policyFactory: string
16+
readonly propertyFactory: string
17+
readonly registry: string
18+
readonly sTokens: string
19+
readonly withdraw: string
20+
}
21+
}
22+
23+
export const l1AvailableNetworks: readonly L1AvailableNetwork[] = [
24+
{ chainId: 1, registry: addresses.eth.main.registry },
25+
{ chainId: 3, registry: addresses.eth.ropsten.registry },
26+
]
27+
28+
export const l2AvailableNetworks: readonly L2AvailableNetwork[] = [
29+
{
30+
chainId: 42161,
31+
map: addresses.arbitrum.one,
32+
},
33+
{ chainId: 421611, map: addresses.arbitrum.rinkeby },
34+
{ chainId: 137, map: addresses.polygon.mainnet },
35+
{ chainId: 80001, map: addresses.polygon.mumbai },
36+
]

lib/agent/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { positionsCreate } from './positionsCreate'

lib/agent/positionsClaim.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { TransactionResponse } from '@ethersproject/abstract-provider'
2+
import { FallbackableOverrides } from '../common/utils/execute'
3+
import { Provider } from '@ethersproject/abstract-provider'
4+
import { UndefinedOr } from '@devprotocol/util-ts'
5+
6+
type PositionsClaim = (options: {
7+
readonly provider: Provider
8+
readonly positionId: number
9+
readonly withdrawalAmount: string
10+
readonly overrides?: FallbackableOverrides
11+
}) => Promise<UndefinedOr<TransactionResponse>>

lib/agent/positionsCreate.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { positionsCreate } from './positionsCreate'
2+
3+
describe('positionsCreate.ts', () => {
4+
it.todo('TODO: Testing it')
5+
})

lib/agent/positionsCreate.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { TransactionResponse } from '@ethersproject/abstract-provider'
2+
import { FallbackableOverrides } from '../common/utils/execute'
3+
import { Provider } from '@ethersproject/abstract-provider'
4+
import { UndefinedOr } from '@devprotocol/util-ts'
5+
import { lockupClients } from './common/clients/lockupClients'
6+
7+
type PositionsCreate = (options: {
8+
readonly provider: Provider
9+
readonly destination: string
10+
readonly amount: string
11+
readonly overrides?: FallbackableOverrides
12+
}) => Promise<UndefinedOr<TransactionResponse>>
13+
14+
export const positionsCreate: PositionsCreate = async (options) => {
15+
const [l1, l2] = await lockupClients(options.provider)
16+
17+
return l1
18+
? l1.depositToProperty(options.destination, options.amount)
19+
: l2
20+
? l2.depositToProperty(options.destination, options.amount)
21+
: undefined
22+
}

lib/agent/positionsGet.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Provider } from '@ethersproject/abstract-provider'
2+
import { UndefinedOr } from '@devprotocol/util-ts'
3+
import { Positions } from '../ethereum/s-tokens/positions'
4+
5+
type PositionsGet = (options: {
6+
readonly provider: Provider
7+
readonly positionId: number
8+
}) => Promise<UndefinedOr<Positions>>

lib/agent/positionsList.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Provider } from '@ethersproject/abstract-provider'
2+
import { UndefinedOr } from '@devprotocol/util-ts'
3+
import { Positions } from '../ethereum/s-tokens/positions'
4+
5+
type PositionsList = (options: {
6+
readonly provider: Provider
7+
readonly destination?: string
8+
readonly user?: string
9+
}) => Promise<UndefinedOr<readonly Positions[]>>

lib/agent/positionsUpdate.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { TransactionResponse } from '@ethersproject/abstract-provider'
2+
import { FallbackableOverrides } from '../common/utils/execute'
3+
import { Provider } from '@ethersproject/abstract-provider'
4+
import { UndefinedOr } from '@devprotocol/util-ts'
5+
6+
type PositionsUpdate = (options: {
7+
readonly provider: Provider
8+
readonly positionId: number
9+
readonly additionalAmount: string
10+
readonly overrides?: FallbackableOverrides
11+
}) => Promise<UndefinedOr<TransactionResponse>>

0 commit comments

Comments
 (0)