-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for ink! contracts (#1015)
* feat: contracts endpoint boilerplate * add @polkadot/api-contracts * set this as a post request * modify post request handler type * cleanup service * add contracts controller to the default controller * boilerplate tests * set mock data for contracts * expand request types to include generic P type * cleanup controlelr with expanded types and clean naming. Add query params * add query params * comment out some tests * expand query params, and fix logic * fix small grumble * add validateAddress middleware * pass the ContractPromise into the service to make testing possible * add test for contracts ink * fix versions * cleanup contract query calls * fix inline comments, and cleanup code * remove parseBNorThrow (only needed for contrcuting contract txs) * docs * update 400 errors Co-authored-by: marshacb <cameron.marshall12@gmail.com>
- Loading branch information
Showing
15 changed files
with
505 additions
and
1 deletion.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,89 @@ | ||
// Copyright 2017-2022 Parity Technologies (UK) Ltd. | ||
// This file is part of Substrate API Sidecar. | ||
// | ||
// Substrate API Sidecar is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import { ApiPromise } from '@polkadot/api'; | ||
import { ContractPromise } from '@polkadot/api-contract'; | ||
import { RequestHandler } from 'express'; | ||
import { BadRequest } from 'http-errors'; | ||
|
||
import { validateAddress } from '../../middleware'; | ||
import { ContractsInkService } from '../../services'; | ||
import { | ||
IBodyContractMetadata, | ||
IContractQueryParam, | ||
IPostRequestHandler, | ||
} from '../../types/requests'; | ||
import AbstractController from '../AbstractController'; | ||
|
||
export default class ContractsInkController extends AbstractController<ContractsInkService> { | ||
constructor(api: ApiPromise) { | ||
super(api, '/contracts/ink/:address', new ContractsInkService(api)); | ||
this.initRoutes(); | ||
} | ||
|
||
protected initRoutes(): void { | ||
this.router.use(this.path, validateAddress); | ||
this.safeMountAsyncPostHandlers([ | ||
['/query', this.callContractQuery as RequestHandler], | ||
]); | ||
} | ||
|
||
/** | ||
* Send a message call to a contract. It defaults to get if nothing is inputted. | ||
* | ||
* @param _req | ||
* @param res | ||
*/ | ||
private callContractQuery: IPostRequestHandler< | ||
IBodyContractMetadata, | ||
IContractQueryParam | ||
> = async ( | ||
{ | ||
params: { address }, | ||
body, | ||
query: { method = 'get', gasLimit, storageDepositLimit, args }, | ||
}, | ||
res | ||
): Promise<void> => { | ||
const { api } = this; | ||
const argsArray = Array.isArray(args) ? args : []; | ||
const contract = new ContractPromise(api, body, address); | ||
if (!contract.query[method]) { | ||
throw new BadRequest( | ||
`Invalid Method: Contract does not have the given ${method} message.` | ||
); | ||
} | ||
|
||
const callMeta = contract.query[method].meta; | ||
if (callMeta.isPayable || callMeta.isMutating) { | ||
throw new BadRequest( | ||
`Invalid Method: This endpoint does not handle mutating or payable calls.` | ||
); | ||
} | ||
|
||
ContractsInkController.sanitizedSend( | ||
res, | ||
await this.service.fetchContractCall( | ||
contract, | ||
address, | ||
method, | ||
argsArray, | ||
gasLimit, | ||
storageDepositLimit | ||
) | ||
); | ||
}; | ||
} |
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 @@ | ||
export { default as ContractsInk } from './ContractsInkController'; |
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,77 @@ | ||
// Copyright 2017-2022 Parity Technologies (UK) Ltd. | ||
// This file is part of Substrate API Sidecar. | ||
// | ||
// Substrate API Sidecar is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import { ContractPromise } from '@polkadot/api-contract'; | ||
|
||
import { sanitizeNumbers } from '../../sanitize/sanitizeNumbers'; | ||
import { polkadotRegistryV9190 } from '../../test-helpers/registries'; | ||
import { defaultMockApi } from '../test-helpers/mock'; | ||
import { ContractsInkService } from '.'; | ||
|
||
const contractInkService = new ContractsInkService(defaultMockApi); | ||
|
||
const getFlipper = () => | ||
Promise.resolve().then(() => { | ||
return { | ||
debugMessage: polkadotRegistryV9190.createType('Text', ''), | ||
gasConsumed: polkadotRegistryV9190.createType('u64', '7437907045'), | ||
gasRequired: polkadotRegistryV9190.createType('u64', '74999922688'), | ||
output: true, | ||
result: polkadotRegistryV9190.createType('ContractExecResultResult', { | ||
ok: { | ||
flags: [], | ||
data: '0x01', | ||
}, | ||
}), | ||
storageDeposit: polkadotRegistryV9190.createType('StorageDeposit', { | ||
charge: '0', | ||
}), | ||
}; | ||
}); | ||
|
||
const mockContractPromise = { | ||
query: { | ||
get: getFlipper, | ||
}, | ||
} as unknown as ContractPromise; | ||
|
||
describe('ContractsInkService', () => { | ||
it('fetchContractCall', async () => { | ||
const address = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY'; | ||
const result = await contractInkService.fetchContractCall( | ||
mockContractPromise, | ||
address, | ||
'get' | ||
); | ||
const expectedResponse = { | ||
debugMessage: '', | ||
gasConsumed: '7437907045', | ||
gasRequired: '74999922688', | ||
output: true, | ||
result: { | ||
ok: { | ||
flags: [], | ||
data: '0x01', | ||
}, | ||
}, | ||
storageDeposit: { | ||
charge: '0', | ||
}, | ||
}; | ||
|
||
expect(sanitizeNumbers(result)).toStrictEqual(expectedResponse); | ||
}); | ||
}); |
Oops, something went wrong.