Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Akira-Taniguchi committed Feb 4, 2021
1 parent 16ab9bf commit 67e0936
Show file tree
Hide file tree
Showing 18 changed files with 721 additions and 502 deletions.
30 changes: 2 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,6 @@
# Khaos Starter Kit
# Khaos Update Cap

How to start developing Khaos Functions

## Environment

First, fork this repository.

Then clone the forked repository.

```bash
git clone [email protected]:your/khaos-starter-kit.git
cd khaos-starter-kit
```

## Development

Edit the file in the `src` directory. You have to edit the following files:

- src/abi.ts
- src/addresses.ts
- src/authorize.ts
- src/event.ts
- src/oraclize.ts
- src/pack.ts

And update the tests.

You can add or remove dependencies if you want.
update cap of DEV Protocol

## Deployment

Expand Down
23 changes: 15 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "khaos-starter-kit",
"private": true,
"description": "Khaos Starter Kit",
"description": "Khaos update cap",
"scripts": {
"test": "ava",
"build": "tsc",
Expand All @@ -16,26 +16,33 @@
"deploy": "curl -X POST -F file=@bundled/index.js 'https://ipfs.infura.io:5001/api/v0/add?wrap-with-directory=true'"
},
"dependencies": {
"@devprotocol/khaos-core": "1.2.2",
"ethers": "5.0.29",
"@devprotocol/khaos-core": "1.2.1",
"@types/bent": "^7.3.2",
"@types/mathjs": "^6.0.11",
"@types/sinon": "^9.0.10",
"bent": "^7.3.12",
"bignumber.js": "^9.0.1",
"ethers": "5.0.26",
"mathjs": "9.1.0",
"ramda": "0.27.1"
},
"devDependencies": {
"@ava/typescript": "1.1.1",
"@types/node": "14.14.24",
"@types/node": "14.14.22",
"@types/ramda": "0.27.36",
"@typescript-eslint/eslint-plugin": "4.14.2",
"@typescript-eslint/parser": "4.14.2",
"@typescript-eslint/eslint-plugin": "4.14.1",
"@typescript-eslint/parser": "4.14.1",
"ava": "3.15.0",
"eslint": "7.19.0",
"eslint-config-prettier": "6.15.0",
"eslint-config-prettier": "7.2.0",
"eslint-plugin-functional": "3.2.1",
"esm": "3.2.25",
"husky": "4.3.8",
"parcel-bundler": "1.12.4",
"prettier": "2.2.1",
"rimraf": "3.0.2",
"typescript": "4.1.3"
"typescript": "4.1.3",
"sinon": "9.2.4"
},
"ava": {
"require": [
Expand Down
5 changes: 4 additions & 1 deletion src/abi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ import test from 'ava'
import { abi } from './abi'

test('Returns abi informations.', async (t) => {
t.is(abi.toString(), 'event Query()')
t.is(
abi.toString(),
'event Lockedup(address, address, uint256),function updateCap(uint256) external'
)
})
3 changes: 2 additions & 1 deletion src/abi.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Abi } from '@devprotocol/khaos-core'
import { lockupAbi } from './contract'

export const abi: Abi = ['event Query()']
export const abi: Abi = lockupAbi
36 changes: 32 additions & 4 deletions src/addresses.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
/* eslint-disable functional/no-let */
/* eslint-disable functional/prefer-readonly-type */
import test from 'ava'
import sinon from 'sinon'
import { ethers } from 'ethers'
import { addresses } from './addresses'
import { getLockupAddress } from './test-utils'
import * as contractModules from './contract'

test('Returns mainnet address', async (t) => {
let stub: sinon.SinonStub<[network: string], ethers.providers.BaseProvider>
test.before(() => {
const mainnetProvider = ethers.getDefaultProvider('homestead')
const ropstenProvider = ethers.getDefaultProvider('ropsten')
stub = sinon.stub(contractModules, 'getProvider')
stub.withArgs('mainnet').returns(mainnetProvider)
stub.withArgs('ropsten').returns(ropstenProvider)
})

test('Returns mainnet lockup address', async (t) => {
const provider = ethers.getDefaultProvider('homestead')
const res = await addresses({ network: 'mainnet' })
t.is(res, '0x1510EA12a30E5c40b406660871b335feA32f29A')
const lockupAddress = await getLockupAddress(
'0x1D415aa39D647834786EB9B5a333A50e9935b796',
provider
)
t.is(res, lockupAddress)
})

test('Returns ropsten address', async (t) => {
test('Returns ropsten lockup address', async (t) => {
const provider = ethers.getDefaultProvider('ropsten')
const res = await addresses({ network: 'ropsten' })
t.is(res, '0x609Fe85Dbb9487d55B5eF50451e20ba2Edc8F4B7')
const lockupAddress = await getLockupAddress(
'0xD6D07f1c048bDF2B3d5d9B6c25eD1FC5348D0A70',
provider
)
t.is(res, lockupAddress)
})
test.after(() => {
stub.restore()
})
10 changes: 6 additions & 4 deletions src/addresses.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { FunctionAddresses } from '@devprotocol/khaos-core'
import { getLockupContract, getProvider } from './contract'

export const addresses: FunctionAddresses = async ({ network }) =>
network === 'mainnet'
? '0x1510EA12a30E5c40b406660871b335feA32f29A'
: '0x609Fe85Dbb9487d55B5eF50451e20ba2Edc8F4B7'
export const addresses: FunctionAddresses = async ({ network }) => {
const provider = getProvider(network)
const lockup = await getLockupContract(provider)
return lockup.address
}
62 changes: 62 additions & 0 deletions src/contract.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* eslint-disable functional/immutable-data */
import test from 'ava'
import { ethers } from 'ethers'
import {
lockupAbi,
getProvider,
getLockupContract,
getTransactionBlockNumber,
} from './contract'
import { getLockupAddress } from './test-utils'

// lockupAbi
test('lthis function can get the ABI of the Lockup contract.', (t) => {
t.is(lockupAbi[0], 'event Lockedup(address, address, uint256)')
t.is(lockupAbi[1], 'function updateCap(uint256) external')
})

// getProvider
test('get the provider of the mainnet.', async (t) => {
process.env[`KHAOS_MAINNET_JSON_RPC`] = 'https://testdomain:1234'
const provider = getProvider('mainnet')
const converted = <ethers.providers.JsonRpcProvider>provider
t.is(converted.connection.url, 'https://testdomain:1234')
})

test('get the provider of the ropsten.', async (t) => {
process.env[`KHAOS_ROPSTEN_JSON_RPC`] = 'https://testdomainropsten:1234'
const provider = getProvider('ropsten')
const converted = <ethers.providers.JsonRpcProvider>provider
t.is(converted.connection.url, 'https://testdomainropsten:1234')
})

// getLockupContract
test('get the Lockup contract object for ropsten.', async (t) => {
const provider = ethers.getDefaultProvider('ropsten')
const lockup = await getLockupContract(provider)
const lockupAddress = await getLockupAddress(
'0xD6D07f1c048bDF2B3d5d9B6c25eD1FC5348D0A70',
provider
)
t.is(lockupAddress, lockup.address)
})

test('get the Lockup contract object for mainnet.', async (t) => {
const provider = ethers.getDefaultProvider('homestead')
const lockup = await getLockupContract(provider)
const lockupAddress = await getLockupAddress(
'0x1D415aa39D647834786EB9B5a333A50e9935b796',
provider
)
t.is(lockupAddress, lockup.address)
})

// getTransactionBlockNumber
test('The block number of the transaction can be obtained.', async (t) => {
const provider = ethers.getDefaultProvider('ropsten')
const number = await getTransactionBlockNumber(
provider,
'0xfa052419311d16810602fadc56cf9bf20d4a575e0a6c08d25e17ca1ed245632a'
)
t.is(number, 9270014)
})
43 changes: 43 additions & 0 deletions src/contract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { ethers, providers } from 'ethers'

export const getLockupContract = async (
provider: providers.BaseProvider
): Promise<ethers.Contract> => {
const addressConfigContraCt = getAddressConfigContract(provider)
const lockupAddress = await addressConfigContraCt.lockup()
const lockupContract = new ethers.Contract(lockupAddress, lockupAbi, provider)
return lockupContract
}

export const getProvider = (network: string): providers.BaseProvider => {
const endpoint = process.env[`KHAOS_${network.toUpperCase()}_JSON_RPC`]
return new ethers.providers.JsonRpcProvider(endpoint)
}

export const getTransactionBlockNumber = async (
provider: providers.BaseProvider,
transactionHash: string
): Promise<number> => {
const transaction = await provider.getTransaction(transactionHash)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return transaction.blockNumber!
}

export const lockupAbi = [
'event Lockedup(address, address, uint256)',
'function updateCap(uint256) external',
]

const getAddressConfigContract = (
provider: providers.BaseProvider
): ethers.Contract => {
const addressConfigAddress = isMainNet(provider)
? '0x1D415aa39D647834786EB9B5a333A50e9935b796'
: '0xD6D07f1c048bDF2B3d5d9B6c25eD1FC5348D0A70'
const abi = ['function lockup() external view returns (address)']
return new ethers.Contract(addressConfigAddress, abi, provider)
}

const isMainNet = (provider: providers.BaseProvider): boolean => {
return provider.network.chainId === 1
}
8 changes: 4 additions & 4 deletions src/event.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import test from 'ava'
import { event } from './event'

test('Returns `Query` when passed network is mainnet', async (t) => {
t.is(await event({ network: 'mainnet' }), 'Query')
test('Returns `Lockedup` when passed network is mainnet', async (t) => {
t.is(await event({ network: 'mainnet' }), 'Lockedup')
})

test('Returns `Query` when passed network is ropsten', async (t) => {
t.is(await event({ network: 'ropsten' }), 'Query')
test('Returns `Lockedup` when passed network is ropsten', async (t) => {
t.is(await event({ network: 'ropsten' }), 'Lockedup')
})
2 changes: 1 addition & 1 deletion src/event.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FunctionEvent } from '@devprotocol/khaos-core'
import { always } from 'ramda'

export const event: FunctionEvent = always(Promise.resolve('Query'))
export const event: FunctionEvent = always(Promise.resolve('Lockedup'))
63 changes: 63 additions & 0 deletions src/graphql.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* eslint-disable functional/immutable-data */
import test from 'ava'
import { ethers } from 'ethers'
import BigNumber from 'bignumber.js'
import {
createGraphQLPropertyLockupSumValuesFetcher,
createGraphQLPropertyAuthenticationFetcher,
graphql,
GraphQLPropertyLockupSumValuesResponse,
GraphQLPropertyPropertyAuthenticationResponse,
} from './graphql'

test('get the data for lockup sum values.', async (t) => {
process.env[`KHAOS_MAINNET_GRAPHQL`] = 'https://api.devprtcl.com/v1/graphql'
const fetchGraphQL = createGraphQLPropertyLockupSumValuesFetcher(
graphql('mainnet')
)
const lockupSumValues = await (async () =>
new Promise<
GraphQLPropertyLockupSumValuesResponse['data']['property_lockup_sum_values']
>((resolve) => {
const f = async (
prev: GraphQLPropertyLockupSumValuesResponse['data']['property_lockup_sum_values'] = []
): Promise<void> => {
const { data } = await fetchGraphQL()
const { property_lockup_sum_values: items } = data
const next = [...prev, ...items]
resolve(next)
}
f().catch(console.error)
}))()
t.is(lockupSumValues.length > 0, true)
lockupSumValues.map((data) => {
t.is(ethers.utils.isAddress(data.property_address), true)
const tmp = new BigNumber(data.sum_values)
t.is(tmp.gt(new BigNumber(0)), true)
})
})

test('Property_authintication data can be retrieved.', async (t) => {
process.env[`KHAOS_MAINNET_GRAPHQL`] = 'https://api.devprtcl.com/v1/graphql'
const fetchGraphQL = createGraphQLPropertyAuthenticationFetcher(
graphql('mainnet')
)
const lockupSumValues = await (async () =>
new Promise<
GraphQLPropertyPropertyAuthenticationResponse['data']['property_authentication']
>((resolve) => {
const f = async (
prev: GraphQLPropertyPropertyAuthenticationResponse['data']['property_authentication'] = []
): Promise<void> => {
const { data } = await fetchGraphQL()
const { property_authentication: items } = data
const next = [...prev, ...items]
resolve(next)
}
f().catch(console.error)
}))()
t.is(lockupSumValues.length > 0, true)
lockupSumValues.map((data) => {
t.is(ethers.utils.isAddress(data.property), true)
})
})
Loading

0 comments on commit 67e0936

Please sign in to comment.