Skip to content

Commit

Permalink
add emulate interface
Browse files Browse the repository at this point in the history
  • Loading branch information
aggre committed Mar 4, 2021
1 parent c0ef227 commit 6dad73b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"license": "MPL-2.0",
"dependencies": {
"@devprotocol/khaos-functions": "0.3.2",
"@ethersproject/contracts": "5.0.11",
"bent": "^7.3.12",
"ramda": "^0.27.1",
"type-fest": "^0.21.2"
Expand Down
34 changes: 27 additions & 7 deletions src/client/emulate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ethereum } from '../util/endpoint'
import * as x from '../_lib/_defaultExport'
import { emulate } from './emulate'

const bentFakeFetcher = fake.returns(Promise.resolve())
const bentFakeFetcher = fake.returns(Promise.resolve({ data: 'just a test' }))
const bentFakeCaller = fake.returns(bentFakeFetcher)
const bentStub = stub(x, 'bent').callsFake(bentFakeCaller)

Expand All @@ -13,13 +13,18 @@ test.after(() => {
})

test('Send emulate request', async (t) => {
await emulate(
const res = await emulate(
'test',
'ropsten'
)({
myParam: 1,
event: {
args: {
myParam: 1,
},
},
})

t.deepEqual(res, { data: 'just a test' } as any)
t.deepEqual(bentFakeCaller.getCall(0).args, [
`${ethereum.ropsten}/emulate/test`,
'POST',
Expand All @@ -28,16 +33,26 @@ test('Send emulate request', async (t) => {
t.deepEqual(bentFakeFetcher.getCall(0).args, [
'/',
{
myParam: 1,
network: 'ropsten',
event: {
args: {
myParam: 1,
},
},
},
])
})

test('Send sign request as mainnet by default', async (t) => {
await emulate('test')({
myParam: 1,
const res = await emulate('test')({
event: {
args: {
myParam: 1,
},
},
})

t.deepEqual(res, { data: 'just a test' } as any)
t.deepEqual(bentFakeCaller.getCall(1).args, [
`${ethereum.mainnet}/emulate/test`,
'POST',
Expand All @@ -46,7 +61,12 @@ test('Send sign request as mainnet by default', async (t) => {
t.deepEqual(bentFakeFetcher.getCall(1).args, [
'/',
{
myParam: 1,
network: 'mainnet',
event: {
args: {
myParam: 1,
},
},
},
])
})
25 changes: 17 additions & 8 deletions src/client/emulate.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
import { ethereum } from '../util/endpoint'
import { bent } from '../_lib/_defaultExport'
import { call } from '@devprotocol/khaos-functions'
import { PromiseValue } from 'type-fest'
import { PromiseValue, SetOptional, Merge } from 'type-fest'
import { Event } from '@ethersproject/contracts'

const _pack = call()({
method: 'pack',
options: { results: { status: 0, statusMessage: '', message: '' } },
})

export type KhaosEmulateOptions = Readonly<
Record<string, string | number | boolean>
>
type ArgsWrap = {
readonly args: Record<string, string | number | undefined | null>
}

type MergedEvent = Merge<Event, ArgsWrap>

export type KhaosEmulateOptions = {
readonly event: SetOptional<MergedEvent, keyof MergedEvent>
}

export type KhaosEmulateResponse = PromiseValue<typeof _pack>

export const emulate = (
id: string,
network: keyof typeof ethereum = 'mainnet'
): ((options: KhaosEmulateOptions) => Promise<PromiseValue<typeof _pack>>) => {
): ((options: KhaosEmulateOptions) => Promise<KhaosEmulateResponse>) => {
const fetcher = bent(`${ethereum[network]}/emulate/${id}`, 'POST', 'json')
return (options: KhaosEmulateOptions): typeof _pack =>
fetcher('/', options).then(
(r) => (r as unknown) as PromiseValue<typeof _pack>
return (options: KhaosEmulateOptions): Promise<KhaosEmulateResponse> =>
fetcher('/', { ...options, ...{ network } }).then(
(r) => (r as unknown) as KhaosEmulateResponse
)
}

0 comments on commit 6dad73b

Please sign in to comment.