-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added addresses to the default context. (#70)
- Loading branch information
Showing
23 changed files
with
413 additions
and
61 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
"@mangrovedao/mgv": minor | ||
--- | ||
|
||
Added the smart router client | ||
|
||
The `getUserRouter` functions were moved from the order subfolders to `smart-router.ts` files | ||
|
||
Please enter a summary for your changes. |
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,5 @@ | ||
--- | ||
"@mangrovedao/mgv": minor | ||
--- | ||
|
||
Added addresses to the default context |
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
Empty file.
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
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,88 @@ | ||
import { isAddress } from 'viem' | ||
import { describe, expect, inject, it } from 'vitest' | ||
import { getClient } from '~test/src/client.js' | ||
import { adminParams } from '../builder/smart-router.js' | ||
import { | ||
getUserRouter, | ||
isBound, | ||
simulateBind, | ||
simulateDeployRouter, | ||
} from './smart-router.js' | ||
|
||
const client = getClient() | ||
const params = inject('mangrove') | ||
|
||
describe('smart router', () => { | ||
it('gets router address', async () => { | ||
const router = await getUserRouter(client, params, { | ||
user: client.account.address, | ||
}) | ||
expect(isAddress(router)).toBeTruthy() | ||
}) | ||
|
||
it('deploys router', async () => { | ||
const { router, created, request } = await simulateDeployRouter( | ||
client, | ||
params, | ||
{ | ||
user: client.account.address, | ||
}, | ||
) | ||
|
||
const routerExpected = await getUserRouter(client, params, { | ||
user: client.account.address, | ||
}) | ||
|
||
expect(isAddress(router)).toBeTruthy() | ||
expect(created).toBeTruthy() | ||
expect(router).toAddressEqual(routerExpected) | ||
|
||
const tx = await client.writeContract(request) | ||
await client.waitForTransactionReceipt({ | ||
hash: tx, | ||
}) | ||
|
||
const admin = await client.readContract({ | ||
address: router, | ||
...adminParams, | ||
}) | ||
|
||
expect(admin).toAddressEqual(client.account.address) | ||
}) | ||
|
||
it('binds router', async () => { | ||
const { request, router } = await simulateDeployRouter(client, params, { | ||
user: client.account.address, | ||
}) | ||
|
||
let tx = await client.writeContract(request) | ||
await client.waitForTransactionReceipt({ | ||
hash: tx, | ||
}) | ||
|
||
const isBoundToMangroveOrder = await isBound(client, router, { | ||
maker: params.mgvOrder, | ||
}) | ||
|
||
expect(isBoundToMangroveOrder).toBeTruthy() | ||
|
||
const isBoundToMangrove = await isBound(client, router, { | ||
maker: params.mgv, | ||
}) | ||
|
||
expect(isBoundToMangrove).toBeFalsy() | ||
|
||
const { request: request2 } = await simulateBind(client, router, { | ||
target: params.mgv, | ||
}) | ||
tx = await client.writeContract(request2) | ||
await client.waitForTransactionReceipt({ | ||
hash: tx, | ||
}) | ||
|
||
const isBoundToMangroveAfter = await isBound(client, router, { | ||
maker: params.mgv, | ||
}) | ||
expect(isBoundToMangroveAfter).toBeTruthy() | ||
}) | ||
}) |
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,129 @@ | ||
import type { Address, Client, SimulateContractReturnType } from 'viem' | ||
import { | ||
type ReadContractParameters, | ||
readContract, | ||
simulateContract, | ||
} from 'viem/actions' | ||
import { | ||
type BindParams, | ||
type DeployRouterParams, | ||
type GetUserRouterParams, | ||
type IsBoundParams, | ||
bindParams, | ||
deployRouterParams, | ||
getUserRouterParams, | ||
isBoundParams, | ||
type routerProxyFactoryABI, | ||
type smartRouterABI, | ||
} from '../builder/smart-router.js' | ||
import type { | ||
BuiltArgs, | ||
MangroveActionsDefaultParams, | ||
} from '../types/actions/index.js' | ||
import type { SimulationParams } from '../types/actions/simulation.js' | ||
import { getAction } from '../utils/getAction.js' | ||
|
||
export type GetUserRouterArgs = GetUserRouterParams & | ||
Omit< | ||
ReadContractParameters<typeof routerProxyFactoryABI, 'computeProxyAddress'>, | ||
BuiltArgs | ||
> | ||
|
||
export async function getUserRouter( | ||
client: Client, | ||
actionParams: MangroveActionsDefaultParams, | ||
args: GetUserRouterArgs, | ||
) { | ||
return getAction( | ||
client, | ||
readContract, | ||
'readContract', | ||
)({ | ||
...getUserRouterParams(actionParams, args), | ||
address: actionParams.routerProxyFactory, | ||
...args, | ||
}) | ||
} | ||
|
||
export type IsBoundArgs = IsBoundParams & | ||
Omit<ReadContractParameters<typeof smartRouterABI, 'isBound'>, BuiltArgs> | ||
|
||
export async function isBound( | ||
client: Client, | ||
router: Address, | ||
args: IsBoundArgs, | ||
) { | ||
return getAction( | ||
client, | ||
readContract, | ||
'readContract', | ||
)({ | ||
...args, | ||
...isBoundParams(args), | ||
address: router, | ||
}) | ||
} | ||
|
||
type DeployRouterSimulationParams = SimulationParams< | ||
typeof routerProxyFactoryABI, | ||
'instantiate' | ||
> | ||
|
||
export type DeployRouterArgs = DeployRouterParams & | ||
Omit<DeployRouterSimulationParams, BuiltArgs> | ||
|
||
export type DeployRouterResult = { | ||
router: Address | ||
created: boolean | ||
request: SimulateContractReturnType< | ||
typeof routerProxyFactoryABI, | ||
'instantiate' | ||
>['request'] | ||
} | ||
|
||
export async function simulateDeployRouter( | ||
client: Client, | ||
actionParams: MangroveActionsDefaultParams, | ||
args: DeployRouterArgs, | ||
) { | ||
const { | ||
request, | ||
result: [router, created], | ||
} = await getAction( | ||
client, | ||
simulateContract, | ||
'simulateContract', | ||
)({ | ||
...(args as unknown as DeployRouterSimulationParams), | ||
address: actionParams.routerProxyFactory, | ||
...deployRouterParams(actionParams, args), | ||
}) | ||
return { | ||
router, | ||
created, | ||
request, | ||
} | ||
} | ||
|
||
type BindSimulationParams = SimulationParams<typeof smartRouterABI, 'bind'> | ||
export type BindArgs = BindParams & Omit<BindSimulationParams, BuiltArgs> | ||
export type BindResult = SimulateContractReturnType< | ||
typeof smartRouterABI, | ||
'bind' | ||
> | ||
|
||
export async function simulateBind( | ||
client: Client, | ||
router: Address, | ||
args: BindArgs, | ||
): Promise<BindResult> { | ||
return getAction( | ||
client, | ||
simulateContract, | ||
'simulateContract', | ||
)({ | ||
...(args as unknown as BindSimulationParams), | ||
address: router, | ||
...bindParams(args), | ||
}) | ||
} |
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.