Last active
August 3, 2020 21:39
-
-
Save 0xGabi/6b23859756867421a29f028c2fa12105 to your computer and use it in GitHub Desktop.
Example of how to use Aragon Connect to interact with a 3rd party smart contract using the Agent app of the organization. The example is doing an approve call to the cDAI mainnet smart contract.
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
import { ethers } from 'ethers' | |
import { connect } from '@aragon/connect' | |
const ACCOUNT = '0xf76604Ce7e7F0134a5310bCfc9C34cAEddf15873' | |
const targetAddress = '0x5d3a536e4d6dbd6114cc1ead35777bab948e3643' // cDAI | |
const targetSignature = | |
'function approve(address spender, uint256 amount) external returns (bool)' | |
const targetParams = [ | |
'0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643', // cDAI | |
'10000000000000000000', // 10 | |
] | |
/** | |
* Encode ACT function call | |
* @param signature Function signature | |
* @param params | |
*/ | |
export function encodeActCall(signature: string, params: any[] = []): string { | |
const functionFragment = ethers.utils.FunctionFragment.from(signature) | |
const ethersInterface = new ethers.utils.Interface([functionFragment]) | |
return ethersInterface.encodeFunctionData(functionFragment, params) | |
} | |
async function main() { | |
const org = await connect('beehive.aragonid.eth', 'thegraph') | |
const agent = await org.app('agent') | |
// Transaction intent | |
const intent = org.appIntent(agent.address, 'execute', [ | |
targetAddress, | |
ethers.utils.parseEther('0'), // ETH value send with the action | |
encodeActCall(targetSignature, targetParams), | |
]) | |
// Transaction path | |
const txPath = await intent.paths(ACCOUNT) | |
// Transaction request | |
txPath.transactions.map((tx: any) => console.log(tx)) | |
} | |
main() | |
.then(() => process.exit(0)) | |
.catch(err => { | |
console.log(`Error: `, err) | |
console.log( | |
'\nPlease report any problem to https://github.com/aragon/connect/issues' | |
) | |
process.exit(1) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment