This repository has been archived by the owner on Oct 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for EVM view and function calls (#517)
* Adding support for EVM view and function calls. * Move things around a bit in eth-call * Fix linter * evm-call renaming: evmAccounts » nearEvmAccountId, contractName » evmContractName * specify gas as "NEAR gas" in evm-call * correct search/replace mistake key evmAccountId for NearProvider added back * standardize arg names, log args better, unique names for scheduleEVMFunctionCall * package.json: latest version for near-api-js and near-web3-provider * update yarn.lock file * readme: add evm-view/evm-call Co-authored-by: Mike Purvis <mikedotexe@gmail.com>
- Loading branch information
1 parent
890c8bb
commit d55a359
Showing
7 changed files
with
2,584 additions
and
115 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
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,56 @@ | ||
const exitOnError = require('../utils/exit-on-error'); | ||
const web3 = require('web3'); | ||
const { NearProvider, utils } = require('near-web3-provider'); | ||
const assert = require('assert'); | ||
|
||
module.exports = { | ||
command: 'evm-call <evmAccount> <contractName> <methodName> [args]', | ||
desc: 'Schedule call inside EVM machine', | ||
builder: (yargs) => yargs | ||
.option('gas', { | ||
desc: 'Max amount of NEAR gas this call can use', | ||
type: 'string', | ||
default: '100000000000000' | ||
}) | ||
.option('amount', { | ||
desc: 'Number of tokens to attach', | ||
type: 'string', | ||
default: '0' | ||
}) | ||
.option('args', { | ||
desc: 'Arguments to the contract call, in JSON format (e.g. \'[1, "str"]\') based on contract ABI', | ||
type: 'string', | ||
default: null | ||
}) | ||
.option('accountId', { | ||
required: true, | ||
desc: 'Unique identifier for the account that will be used to sign this call', | ||
type: 'string', | ||
}) | ||
.option('abi', { | ||
required: true, | ||
desc: 'Path to ABI for given contract', | ||
type: 'string', | ||
}), | ||
handler: exitOnError(scheduleEVMFunctionCall) | ||
}; | ||
|
||
async function scheduleEVMFunctionCall(options) { | ||
const args = JSON.parse(options.args || '[]'); | ||
console.log(`Scheduling a call inside ${options.evmAccount} EVM:`); | ||
console.log(`${options.contractName}.${options.methodName}()` + | ||
(options.amount && options.amount !== '0' ? ` with attached ${options.amount} NEAR` : '')); | ||
console.log(' with args', args); | ||
const web = new web3(); | ||
web.setProvider(new NearProvider({ | ||
nodeUrl: options.nodeUrl, | ||
// TODO: make sure near-api-js has the same version between near-web3-provider. | ||
// keyStore: options.keyStore, | ||
masterAccountId: options.accountId, | ||
networkId: options.networkId, | ||
evmAccountId: options.evmAccount, | ||
})); | ||
const contract = new web.eth.Contract(options.abi, options.contractName); | ||
assert(options.methodName in contract.methods, `${options.methodName} is not present in ABI`); | ||
await contract.methods[options.methodName](...args).send({ from: utils.nearAccountToEvmAddress(options.accountId) }); | ||
} |
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,42 @@ | ||
const exitOnError = require('../utils/exit-on-error'); | ||
const web3 = require('web3'); | ||
const { NearProvider, utils } = require('near-web3-provider'); | ||
const assert = require('assert'); | ||
|
||
module.exports = { | ||
command: 'evm-view <evmAccount> <contractName> <methodName> [args]', | ||
desc: 'View call inside EVM machine', | ||
builder: (yargs) => yargs | ||
.option('args', { | ||
desc: 'Arguments to the contract call, in JSON format (e.g. \'[1, "str"]\') based on contract ABI', | ||
type: 'string', | ||
default: null | ||
}) | ||
.option('accountId', { | ||
required: true, | ||
desc: 'Unique identifier for the account that will be used to sign this call', | ||
type: 'string', | ||
}) | ||
.option('abi', { | ||
desc: 'Path to ABI for given contract', | ||
type: 'string', | ||
}), | ||
handler: exitOnError(scheduleEVMFunctionView) | ||
}; | ||
|
||
async function scheduleEVMFunctionView(options) { | ||
const web = new web3(); | ||
web.setProvider(new NearProvider({ | ||
nodeUrl: options.nodeUrl, | ||
// TODO: make sure near-api-js has the same version between near-web3-provider. | ||
// keyStore: options.keyStore, | ||
masterAccountId: options.accountId, | ||
networkId: options.networkId, | ||
evmAccountId: options.evmAccount, | ||
})); | ||
const contract = new web.eth.Contract(options.abi, options.contractName); | ||
const args = JSON.parse(options.args || '[]'); | ||
assert(options.methodName in contract.methods, `${options.methodName} is not present in ABI`); | ||
const result = await contract.methods[options.methodName](...args).call({ from: utils.nearAccountToEvmAddress(options.accountId) }); | ||
console.log(result); | ||
} |
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,11 @@ | ||
const fs = require('fs'); | ||
|
||
async function loadAbi(abiPath) { | ||
return JSON.parse(fs.readFileSync(abiPath)).abi; | ||
} | ||
|
||
module.exports = async function parseAbi(options) { | ||
if (options.abi) { | ||
options.abi = await loadAbi(options.abi); | ||
} | ||
}; |
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.