Skip to content

Commit

Permalink
feat: Executor
Browse files Browse the repository at this point in the history
  • Loading branch information
rrr523 committed Apr 18, 2024
1 parent 171d5f7 commit a43b521
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 10 deletions.
5 changes: 5 additions & 0 deletions packages/bsc-cross-sdk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# BSC corss greenfield SDK

## Executor

## MultiMessage
5 changes: 5 additions & 0 deletions packages/bsc-cross-sdk/abi/MultiMessage.abi.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// function sendMessages(
// address[] calldata _targets,
// bytes[] calldata _data,
// uint256[] calldata _values
// ) external payable returns (bool);
export const MultiMessageAbi = [
{
inputs: [
Expand Down
2 changes: 1 addition & 1 deletion packages/bsc-cross-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
]
},
"dependencies": {
"@bnb-chain/greenfield-cosmos-types": "0.4.0-alpha.31",
"@bnb-chain/greenfield-cosmos-types": "0.4.0-alpha.32",
"long": "^5.2.1",
"reflect-metadata": "^0.1.13",
"tsyringe": "^4.8.0",
Expand Down
1 change: 1 addition & 0 deletions packages/bsc-cross-sdk/src/MultiMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default class MultiMessage {}
43 changes: 39 additions & 4 deletions packages/bsc-cross-sdk/src/executor/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
import { createPublicClient, http, parseAbi } from 'viem';
import { createPublicClient, createWalletClient, http, parseAbi, PrivateKeyAccount } from 'viem';
import { bscTestnet } from 'viem/chains';
import { CrossChainABI } from '../../abi/CrossChain.abi';
import { privateKeyToAccount } from 'viem/accounts';
import { ExecutorABI } from '../../abi/Executor.abi';
import { ExecuteParams } from '../types';
import { splitParams } from './messages';

export default class ExecutorClient {
account: PrivateKeyAccount;

constructor(
public publicClient = createPublicClient({
privateKey: `0x${string}`,
public executorAddress: `0x${string}`,
public crossChainAddress: `0x${string}`,
private publicClient = createPublicClient({
chain: bscTestnet,
transport: http(),
}),
private walletClient = createWalletClient({
chain: bscTestnet,
transport: http(),
}),
) {}
) {
this.account = privateKeyToAccount(privateKey);
this.executorAddress = executorAddress;
this.crossChainAddress = crossChainAddress;
}

async getRelayFee(address: `0x${string}`) {
const data = await this.publicClient.readContract({
Expand All @@ -20,5 +37,23 @@ export default class ExecutorClient {
return data[0];
}

// async execute() {}
async execute(params: ExecuteParams[]) {
if (params.length === 0) throw new Error('execute params is empty');

const { types, bytes } = splitParams(params);

const relayFee = await this.getRelayFee(this.crossChainAddress);

const { request } = await this.publicClient.simulateContract({
account: this.account,
address: this.executorAddress,
abi: parseAbi(ExecutorABI),
functionName: 'execute',
args: [types, bytes],
value: relayFee,
});

const txHash = await this.walletClient.writeContract(request);
return txHash;
}
}
103 changes: 103 additions & 0 deletions packages/bsc-cross-sdk/src/executor/messages/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import {
MsgCreatePaymentAccount,
MsgDeposit,
MsgDisableRefund,
MsgWithdraw,
} from '@bnb-chain/greenfield-cosmos-types/greenfield/payment/tx';
import {
MsgCancelMigrateBucket,
MsgCopyObject,
MsgMigrateBucket,
MsgSetBucketFlowRateLimit,
MsgSetTag,
MsgToggleSPAsDelegatedAgent,
MsgUpdateBucketInfo,
MsgUpdateGroupExtra,
MsgUpdateObjectInfo,
} from '@bnb-chain/greenfield-cosmos-types/greenfield/storage/tx';
import { toHex } from 'viem';
import { ExecuteParams } from '../../types';

// https://github.com/bnb-chain/greenfield-contracts/blob/develop/contracts/middle-layer/GreenfieldExecutor.sol

export default class ExecutorMsg {
static getCreatePaymentAccountParams = (msg: MsgCreatePaymentAccount): ExecuteParams => [
1,
toHex(MsgCreatePaymentAccount.encode(msg).finish()),
];

static getDepositParams = (msg: MsgDeposit): ExecuteParams => [
2,
toHex(MsgDeposit.encode(msg).finish()),
];

static getDisableRefundParams = (msg: MsgDisableRefund): ExecuteParams => [
3,
toHex(MsgDisableRefund.encode(msg).finish()),
];

static getWithdrawParams = (msg: MsgWithdraw): ExecuteParams => [
4,
toHex(MsgWithdraw.encode(msg).finish()),
];

static getMigrateBucketParams = (msg: MsgMigrateBucket): ExecuteParams => [
5,
toHex(MsgMigrateBucket.encode(msg).finish()),
];

static getCancelMigrateBucketParams = (msg: MsgCancelMigrateBucket): ExecuteParams => [
6,
toHex(MsgCancelMigrateBucket.encode(msg).finish()),
];

static getUpdateBucketInfoParams = (msg: MsgUpdateBucketInfo): ExecuteParams => [
7,
toHex(MsgUpdateBucketInfo.encode(msg).finish()),
];

static getToggleSPAsDelegatedAgentParams = (msg: MsgToggleSPAsDelegatedAgent): ExecuteParams => [
8,
toHex(MsgToggleSPAsDelegatedAgent.encode(msg).finish()),
];

static getSetBucketFlowRateLimitParams = (msg: MsgSetBucketFlowRateLimit): ExecuteParams => [
9,
toHex(MsgSetBucketFlowRateLimit.encode(msg).finish()),
];

static getCopyObjectParams = (msg: MsgCopyObject): ExecuteParams => [
10,
toHex(MsgCopyObject.encode(msg).finish()),
];

static getUpdateObjectInfoParams = (msg: MsgUpdateObjectInfo): ExecuteParams => [
11,
toHex(MsgUpdateObjectInfo.encode(msg).finish()),
];

static getUpdateGroupExtraParams = (msg: MsgUpdateGroupExtra): ExecuteParams => [
12,
toHex(MsgUpdateGroupExtra.encode(msg).finish()),
];

static getSetTagParams = (msg: MsgSetTag): ExecuteParams => [
13,
toHex(MsgSetTag.encode(msg).finish()),
];
}

export const splitParams = (params: ExecuteParams[]) => {
const types: number[] = [];
const bytes: `0x${string}`[] = [];

params.forEach((p) => {
types.push(p[0]);
bytes.push(p[1]);
});

return {
types,
bytes,
};
};
1 change: 1 addition & 0 deletions packages/bsc-cross-sdk/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type ExecuteParams = [number, `0x${string}`];
4 changes: 4 additions & 0 deletions packages/bsc-cross-sdk/tests/.env.simple
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ACCOUNT_PRIVATEKEY=
EXECUTOR_ADDRESS=
CROSSCHAIN_ADDRESS=
MULTIMESSAGE_ADDRESS=
9 changes: 9 additions & 0 deletions packages/bsc-cross-sdk/tests/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import dotenv from 'dotenv';
dotenv.config({
path: process.cwd() + '/tests/.env',
});

export const ACCOUNT_PRIVATEKEY = (process.env.ACCOUNT_PRIVATEKEY as `0x${string}`) || '0x';
export const ExecutorAddress = (process.env.EXECUTOR_ADDRESS as `0x${string}`) || '0x';
export const CrossChainAddress = (process.env.CROSSCHAIN_ADDRESS as `0x${string}`) || '0x';
export const MultiMessageAddress = (process.env.MULTIMESSAGE_ADDRESS as `0x${string}`) || '0x';
51 changes: 51 additions & 0 deletions packages/bsc-cross-sdk/tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { describe, expect, test } from '@jest/globals';
import ExecutorClient from '../src/executor';
import ExecutorMsg from '../src/executor/messages';
import { ACCOUNT_PRIVATEKEY, CrossChainAddress, ExecutorAddress } from './env';
import { privateKeyToAccount } from 'viem/accounts';

const executorClient = new ExecutorClient(ACCOUNT_PRIVATEKEY, ExecutorAddress, CrossChainAddress);

const account = privateKeyToAccount(ACCOUNT_PRIVATEKEY);

describe('deposit', () => {
test('it works', async () => {
const params = ExecutorMsg.getDepositParams({
creator: account.address,
to: '0x00000000000000000000',
amount: '0.001',
});

const txHash = await executorClient.execute([params]);
expect(txHash).toBeDefined();
});
});

describe('createPayment', () => {
test('it works', async () => {
const params = ExecutorMsg.getCreatePaymentAccountParams({
creator: account.address,
});

const txHash = await executorClient.execute([params]);
expect(txHash).toBeDefined();
});
});

describe('multiTx', () => {
test('it works', async () => {
const params1 = ExecutorMsg.getDepositParams({
creator: account.address,
to: '0x00000000000000000000',
amount: '0.001',
});

const params2 = ExecutorMsg.getCreatePaymentAccountParams({
creator: account.address,
});

const txHash = await executorClient.execute([params1, params2]);
// console.log('txHash', txHash);
expect(txHash).toBeDefined();
});
});
16 changes: 11 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a43b521

Please sign in to comment.