Skip to content

Commit

Permalink
Add evm module from minievm
Browse files Browse the repository at this point in the history
  • Loading branch information
joon9823 committed Mar 27, 2024
1 parent 564a04d commit c0f7df6
Show file tree
Hide file tree
Showing 11 changed files with 503 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/client/lcd/LCDClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
BankAPI,
DistributionAPI,
EvidenceAPI,
EvmAPI,
FeeGrantAPI,
GovAPI,
GroupAPI,
Expand Down Expand Up @@ -87,6 +88,7 @@ export class LCDClient {
public bank: BankAPI;
public distribution: DistributionAPI;
public evidence: EvidenceAPI;
public evm: EvmAPI;
public feeGrant: FeeGrantAPI;
public gov: GovAPI;
public group: GroupAPI;
Expand Down Expand Up @@ -137,6 +139,7 @@ export class LCDClient {
this.bank = new BankAPI(this.apiRequester);
this.distribution = new DistributionAPI(this.apiRequester);
this.evidence = new EvidenceAPI(this.apiRequester);
this.evm = new EvmAPI(this.apiRequester);
this.feeGrant = new FeeGrantAPI(this.apiRequester);
this.gov = new GovAPI(this.apiRequester);
this.group = new GroupAPI(this.apiRequester);
Expand Down
79 changes: 79 additions & 0 deletions src/client/lcd/api/EvmAPI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { AccAddress, EvmParams } from '../../../core';
import { APIParams } from '../APIRequester';
import { BaseAPI } from './BaseAPI';

export interface CallResponse {
response: string;
used_gas: string;
logs: {
address: AccAddress;
topics: string[];
data: string;
}[];
trace_output: string;
}

export class EvmAPI extends BaseAPI {
public async code(
contractAddr: AccAddress,
params: APIParams = {}
): Promise<string> {
return this.c
.get<{ code: string }>(`/minievm/evm/v1/codes/${contractAddr}`, params)
.then(d => d.code);
}

public async state(
contractAddr: AccAddress,
key: string,
params: APIParams = {}
): Promise<string> {
return this.c
.get<{ value: string }>(
`/minievm/evm/v1/states/${contractAddr}/${key}`,
params
)
.then(d => d.value);
}

public async contractAddrByDenom(
denom: string,
params: APIParams = {}
): Promise<AccAddress> {
return this.c
.get<{ address: AccAddress }>(`/minievm/evm/v1/contracts/by_denom`, {
...params,
denom,
})
.then(d => d.address);
}

public async denom(
contractAddr: AccAddress,
params: APIParams = {}
): Promise<string> {
return this.c
.get<{ denom: string }>(`/minievm/evm/v1/denoms/${contractAddr}`, params)
.then(d => d.denom);
}

public async call(
sender: AccAddress,
contractAddr: AccAddress,
input: string,
withTrace: boolean
): Promise<CallResponse> {
return this.c.post<CallResponse>(`/minievm/evm/v1/call`, {
sender,
contract_addr: contractAddr,
input,
with_trace: withTrace,
});
}

public async parameters(params: APIParams = {}): Promise<EvmParams> {
return this.c
.get<{ params: EvmParams.Data }>(`/minievm/evm/v1/params`, params)
.then(({ params: d }) => EvmParams.fromData(d));
}
}
1 change: 1 addition & 0 deletions src/client/lcd/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from './AuthzAPI';
export * from './BankAPI';
export * from './DistributionAPI';
export * from './EvidenceAPI';
export * from './EvmAPI';
export * from './FeeGrantAPI';
export * from './GovAPI';
export * from './GroupAPI';
Expand Down
29 changes: 29 additions & 0 deletions src/core/Msg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
MsgDepositValidatorRewardsPool,
} from './distribution';
import { EvidenceMsg, MsgSubmitEvidence } from './evidence';
import { EvmMsg, MsgCreate, MsgCall, MsgUpdateEvmParams } from './evm';
import { FeeGrantMsg, MsgGrantAllowance, MsgRevokeAllowance } from './feegrant';
import {
GovMsg,
Expand Down Expand Up @@ -206,6 +207,7 @@ export type Msg =
| CrisisMsg
| DistributionMsg
| EvidenceMsg
| EvmMsg
| FeeGrantMsg
| GovMsg
| GroupMsg
Expand Down Expand Up @@ -239,6 +241,7 @@ export namespace Msg {
| CrisisMsg.Amino
| DistributionMsg.Amino
| EvidenceMsg.Amino
| EvmMsg.Amino
| FeeGrantMsg.Amino
| GovMsg.Amino
| GroupMsg.Amino
Expand Down Expand Up @@ -267,6 +270,7 @@ export namespace Msg {
| CrisisMsg.Data
| DistributionMsg.Data
| EvidenceMsg.Data
| EvmMsg.Data
| FeeGrantMsg.Data
| GovMsg.Data
| GroupMsg.Data
Expand Down Expand Up @@ -299,6 +303,7 @@ export namespace Msg {
| CrisisMsg.Proto
| DistributionMsg.Proto
| EvidenceMsg.Proto
| EvmMsg.Proto
| FeeGrantMsg.Proto
| GovMsg.Proto
| GroupMsg.Proto
Expand Down Expand Up @@ -381,6 +386,14 @@ export namespace Msg {
case 'cosmos-sdk/MsgSubmitEvidence':
return MsgSubmitEvidence.fromAmino(data);

// evm
case 'evm/MsgCreate':
return MsgCreate.fromAmino(data);
case 'evm/MsgCall':
return MsgCall.fromAmino(data);
case 'evm/MsgUpdateParams':
return MsgUpdateEvmParams.fromAmino(data);

// feegrant
case 'cosmos-sdk/MsgGrantAllowance':
return MsgGrantAllowance.fromAmino(data);
Expand Down Expand Up @@ -677,6 +690,14 @@ export namespace Msg {
case '/cosmos.evidence.v1beta1.MsgSubmitEvidence':
return MsgSubmitEvidence.fromData(data);

// evm
case '/minievm.evm.v1.MsgCreate':
return MsgCreate.fromData(data);
case '/minievm.evm.v1.MsgCall':
return MsgCall.fromData(data);
case '/minievm.evm.v1.MsgUpdateParams':
return MsgUpdateEvmParams.fromData(data);

// feegrant
case '/cosmos.feegrant.v1beta1.MsgGrantAllowance':
return MsgGrantAllowance.fromData(data);
Expand Down Expand Up @@ -1040,6 +1061,14 @@ export namespace Msg {
case '/cosmos.evidence.v1beta1.MsgSubmitEvidence':
return MsgSubmitEvidence.unpackAny(proto);

// evm
case '/minievm.evm.v1.MsgCreate':
return MsgCreate.unpackAny(proto);
case '/minievm.evm.v1.MsgCall':
return MsgCall.unpackAny(proto);
case '/minievm.evm.v1.MsgUpdateParams':
return MsgUpdateEvmParams.unpackAny(proto);

// feegrant
case '/cosmos.feegrant.v1beta1.MsgGrantAllowance':
return MsgGrantAllowance.unpackAny(proto);
Expand Down
74 changes: 74 additions & 0 deletions src/core/evm/EvmParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { JSONSerializable } from '../../util/json';
import { Params as Params_pb } from '@initia/initia.proto/minievm/evm/v1/types';

export class EvmParams extends JSONSerializable<
EvmParams.Amino,
EvmParams.Data,
EvmParams.Proto
> {
/**
* @param extra_eips the additional EIPs for the vm.Config
* @param allowed_publishers list of addresses with permission to distribute contracts
*/
constructor(
public extra_eips: number[],
public allowed_publishers: string[]
) {
super();
}

public static fromAmino(data: EvmParams.Amino): EvmParams {
const { extra_eips, allowed_publishers } = data;
return new EvmParams(extra_eips.map(Number.parseInt), allowed_publishers);
}

public toAmino(): EvmParams.Amino {
const { extra_eips, allowed_publishers } = this;
return {
extra_eips: extra_eips.map(eip => eip.toString()),
allowed_publishers,
};
}

public static fromData(data: EvmParams.Data): EvmParams {
const { extra_eips, allowed_publishers } = data;
return new EvmParams(extra_eips.map(Number.parseInt), allowed_publishers);
}

public toData(): EvmParams.Data {
const { extra_eips, allowed_publishers } = this;
return {
extra_eips: extra_eips.map(eip => eip.toString()),
allowed_publishers,
};
}

public static fromProto(proto: EvmParams.Proto): EvmParams {
return new EvmParams(
proto.extraEips.map(eip => eip.toNumber()),
proto.allowedPublishers
);
}

public toProto(): EvmParams.Proto {
const { extra_eips, allowed_publishers } = this;
return Params_pb.fromPartial({
extraEips: extra_eips,
allowedPublishers: allowed_publishers,
});
}
}

export namespace EvmParams {
export interface Amino {
extra_eips: string[];
allowed_publishers: string[];
}

export interface Data {
extra_eips: string[];
allowed_publishers: string[];
}

export type Proto = Params_pb;
}
2 changes: 2 additions & 0 deletions src/core/evm/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './msgs';
export * from './EvmParams';
102 changes: 102 additions & 0 deletions src/core/evm/msgs/MsgCall.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { JSONSerializable } from '../../../util/json';
import { AccAddress } from '../../bech32';
import { Any } from '@initia/initia.proto/google/protobuf/any';
import { MsgCall as MsgCall_pb } from '@initia/initia.proto/minievm/evm/v1/tx';

export class MsgCall extends JSONSerializable<
MsgCall.Amino,
MsgCall.Data,
MsgCall.Proto
> {
/**
* @param sender the actor that signed the messages
* @param contract_addr the contract address to be executed, can be cosmos address or hex encoded address
* @param input hex encoded execution input bytes
*/
constructor(
public sender: AccAddress,
public contract_addr: AccAddress,
public input: string
) {
super();
}

public static fromAmino(data: MsgCall.Amino): MsgCall {
const {
value: { sender, contract_addr, input },
} = data;

return new MsgCall(sender, contract_addr, input);
}

public toAmino(): MsgCall.Amino {
const { sender, contract_addr, input } = this;
return {
type: 'evm/MsgCall',
value: {
sender,
contract_addr,
input,
},
};
}

public static fromData(data: MsgCall.Data): MsgCall {
const { sender, contract_addr, input } = data;
return new MsgCall(sender, contract_addr, input);
}

public toData(): MsgCall.Data {
const { sender, contract_addr, input } = this;
return {
'@type': '/minievm.evm.v1.MsgCall',
sender,
contract_addr,
input,
};
}

public static fromProto(data: MsgCall.Proto): MsgCall {
return new MsgCall(data.sender, data.contractAddr, data.input);
}

public toProto(): MsgCall.Proto {
const { sender, contract_addr, input } = this;
return MsgCall_pb.fromPartial({
sender,
contractAddr: contract_addr,
input,
});
}

public packAny(): Any {
return Any.fromPartial({
typeUrl: '/minievm.evm.v1.MsgCall',
value: MsgCall_pb.encode(this.toProto()).finish(),
});
}

public static unpackAny(msgAny: Any): MsgCall {
return MsgCall.fromProto(MsgCall_pb.decode(msgAny.value));
}
}

export namespace MsgCall {
export interface Amino {
type: 'evm/MsgCall';
value: {
sender: AccAddress;
contract_addr: AccAddress;
input: string;
};
}

export interface Data {
'@type': '/minievm.evm.v1.MsgCall';
sender: AccAddress;
contract_addr: AccAddress;
input: string;
}

export type Proto = MsgCall_pb;
}
Loading

0 comments on commit c0f7df6

Please sign in to comment.