Skip to content

Commit

Permalink
feat(core): draft custom contract call
Browse files Browse the repository at this point in the history
  • Loading branch information
extg3 committed Jul 4, 2024
1 parent 15ed251 commit 1ed947f
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 8 deletions.
33 changes: 26 additions & 7 deletions examples/sandbox/src/app/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import {
MultiSenderAction,
Erc20TokenABI as ERC20_ABI,
CallDataBuilder,
CustomContractCall,
} from '@ditto-network/core';
import { EthersSigner, EthersContractFactory } from '@ditto-network/ethers';
import disperseAbi from '../lib/disperse-abi';

import { Button, Textarea } from '../components/ui';
import useLocalStorage from '../hooks/use-local-storage';
Expand Down Expand Up @@ -294,15 +296,32 @@ export function App() {
name: 'MultiSender Action Example',
triggers: [new InstantTrigger()],
actions: [
new MultiSenderAction(
// new MultiSenderAction(
// {
// items: recepients.map(([to, amount]) => ({
// to,
// amount: parseUnits(amount, tokens.usdt.decimals),
// asset: tokens.usdt,
// })),
// },
// commonConfig
// ),
new CustomContractCall(
{
items: recepients.map(([to, amount]) => ({
to,
amount: parseUnits(amount, tokens.usdt.decimals),
asset: tokens.usdt,
})),
items: [
{
contract: '0xD152f549545093347A162Dce210e7293f1452150',
abi: disperseAbi,
method: 'disperse',
args: [
recepients.map(([to]) => to),
recepients.map(([, amount]) => parseUnits(amount, tokens.usdt.decimals)),
],
},
],
value: recepients.reduce((acc, [, amount]) => acc + parseUnits(amount, tokens.usdt.decimals), BigInt(0)),
},
commonConfig
commonConfig,
),
],
chainId,
Expand Down
64 changes: 64 additions & 0 deletions examples/sandbox/src/lib/disperse-abi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
export default [
{
constant: false,
inputs: [
{
name: "token",
type: "address",
},
{
name: "recipients",
type: "address[]",
},
{
name: "values",
type: "uint256[]",
},
],
name: "disperseTokenSimple",
outputs: [],
payable: false,
stateMutability: "nonpayable",
type: "function",
},
{
constant: false,
inputs: [
{
name: "token",
type: "address",
},
{
name: "recipients",
type: "address[]",
},
{
name: "values",
type: "uint256[]",
},
],
name: "disperseToken",
outputs: [],
payable: false,
stateMutability: "nonpayable",
type: "function",
},
{
constant: false,
inputs: [
{
name: "recipients",
type: "address[]",
},
{
name: "values",
type: "uint256[]",
},
],
name: "disperseEther",
outputs: [],
payable: true,
stateMutability: "payable",
type: "function",
},
] as const;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,6 @@
],
"nx": {
"includedScripts": []
}
},
"packageManager": "[email protected]+sha512.0e9d42e92bd2318408ed81eaff2da5f78baf23ee7d12a6eed44a6e2901d0f29d7ab715d1b918ade601f72e769a824d9a5c322383f22bbbda5dd396e79de2a077"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import VaultABI from '../../../blockchain/abi/VaultABI.json';
import {
CallData,
CallDataBuilder,
CallDataBuilderReturnData,
CommonBuilderOptions,
} from '../../builders/types';
import { Address } from '../../../types';

export type CallItem = {
contract: Address;
method: string;
abi: any;
args: any[];
};

type ActionConfig = {
items: CallItem[];
value?: bigint;
};

export class CustomContractCall implements CallDataBuilder {
constructor(
protected readonly config: ActionConfig,
protected readonly commonCallDataBuilderConfig: CommonBuilderOptions
) {}

public async build(): Promise<CallDataBuilderReturnData> {
const callData = new Set<CallData>();

for (const item of this.config.items) {
if (!item.contract || !item.method || !item.abi) continue;

const contractInterface = this.commonCallDataBuilderConfig.provider
.getContractFactory()
.getContractInterface(JSON.stringify(item.abi));

callData.add({
to: item.contract,
callData: contractInterface.encodeFunctionData(item.method, item.args),
});
}

return { callData, value: this.config.value || BigInt(0) };
}
}




// public encodeExecuteFunctionCallRaw(
// pointer: string,
// to: string,
// callData: string,
// nativeAmount?: string | number
// ): CallData {
// return {
// pointer: pointer,
// callData: this.vaultInterface.encodeFunctionData("execute", [
// to,
// nativeAmount ?? "0",
// callData
// ]),
// value: nativeAmount?.toString()
// }
// }

// public encodeExecuteFunctionCall(
// pointer: string,
// callParams: {
// functionName: string,
// data: any[],
// to: string,
// nativeAmount?: string | number,
// viewData?: string
// }
// ): CallData {
// const functionCall = this.encodeFunctionData(pointer, callParams.functionName, callParams.data, callParams.viewData)

// return {
// pointer,
// callData: this.vaultInterface.encodeFunctionData("execute", [
// callParams.to,
// callParams.nativeAmount ?? "0",
// functionCall.callData
// ])
// }
// }
1 change: 1 addition & 0 deletions packages/core/src/lib/workflow/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { CustomContractCall } from './custom-contract-call/custom-contract-call';
export { UniswapSwapActionCallDataBuilder } from './uniswap-swap/uniswap-swap-action';
export { MultiSenderAction } from './multisender/multisender';

0 comments on commit 1ed947f

Please sign in to comment.