Skip to content

Commit

Permalink
[pallet-revive] use evm decimals in call host fn (#6466)
Browse files Browse the repository at this point in the history
This PR update the pallet to use the EVM 18 decimal balance in contracts
call and host functions instead of the native balance.

It also updates the js example to add the piggy-bank solidity contract
that expose the problem

---------

Co-authored-by: GitHub Action <[email protected]>
  • Loading branch information
pgherveou and actions-user authored Nov 15, 2024
1 parent 5bc571b commit 39eba14
Show file tree
Hide file tree
Showing 42 changed files with 1,537 additions and 770 deletions.
17 changes: 6 additions & 11 deletions cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ use frame_support::{
ord_parameter_types, parameter_types,
traits::{
fungible, fungibles,
tokens::{
imbalance::ResolveAssetTo, nonfungibles_v2::Inspect, Fortitude::Polite,
Preservation::Expendable,
},
tokens::{imbalance::ResolveAssetTo, nonfungibles_v2::Inspect},
AsEnsureOriginWithArg, ConstBool, ConstU128, ConstU32, ConstU64, ConstU8, InstanceFilter,
Nothing, TransformOrigin,
},
Expand All @@ -68,7 +65,7 @@ use parachains_common::{
NORMAL_DISPATCH_RATIO,
};
use sp_api::impl_runtime_apis;
use sp_core::{crypto::KeyTypeId, OpaqueMetadata, H160};
use sp_core::{crypto::KeyTypeId, OpaqueMetadata, H160, U256};
use sp_runtime::{
generic, impl_opaque_keys,
traits::{AccountIdConversion, BlakeTwo256, Block as BlockT, Saturating, Verify},
Expand Down Expand Up @@ -127,7 +124,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: alloc::borrow::Cow::Borrowed("westmint"),
impl_name: alloc::borrow::Cow::Borrowed("westmint"),
authoring_version: 1,
spec_version: 1_016_005,
spec_version: 1_016_006,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 16,
Expand Down Expand Up @@ -2080,10 +2077,8 @@ impl_runtime_apis! {

impl pallet_revive::ReviveApi<Block, AccountId, Balance, Nonce, BlockNumber, EventRecord> for Runtime
{
fn balance(address: H160) -> Balance {
use frame_support::traits::fungible::Inspect;
let account = <Runtime as pallet_revive::Config>::AddressMapper::to_account_id(&address);
Balances::reducible_balance(&account, Expendable, Polite)
fn balance(address: H160) -> U256 {
Revive::evm_balance(&address)
}

fn nonce(address: H160) -> Nonce {
Expand All @@ -2093,7 +2088,7 @@ impl_runtime_apis! {
fn eth_transact(
from: H160,
dest: Option<H160>,
value: Balance,
value: U256,
input: Vec<u8>,
gas_limit: Option<Weight>,
storage_deposit_limit: Option<Balance>,
Expand Down
12 changes: 12 additions & 0 deletions prdoc/pr_6466.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
title: '[pallet-revive] add piggy-bank sol example'
doc:
- audience: Runtime Dev
description: |-
This PR update the pallet to use the EVM 18 decimal balance in contracts call and host functions instead of the native balance.

It also updates the js example to add the piggy-bank solidity contract that expose the problem
crates:
- name: pallet-revive-eth-rpc
bump: minor
- name: pallet-revive
bump: minor
11 changes: 5 additions & 6 deletions substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use frame_support::{
},
tokens::{
imbalance::ResolveAssetTo, nonfungibles_v2::Inspect, pay::PayAssetFromAccount,
Fortitude::Polite, GetSalary, PayFromAccount, Preservation::Preserve,
GetSalary, PayFromAccount,
},
AsEnsureOriginWithArg, ConstBool, ConstU128, ConstU16, ConstU32, ConstU64, Contains,
Currency, EitherOfDiverse, EnsureOriginWithArg, EqualPrivilegeOnly, Imbalance, InsideBoth,
Expand Down Expand Up @@ -86,6 +86,7 @@ use pallet_nis::WithMaximumOf;
use pallet_nomination_pools::PoolId;
use pallet_revive::{evm::runtime::EthExtra, AddressMapper};
use pallet_session::historical as pallet_session_historical;
use sp_core::U256;
// Can't use `FungibleAdapter` here until Treasury pallet migrates to fungibles
// <https://github.com/paritytech/polkadot-sdk/issues/226>
use pallet_broker::TaskId;
Expand Down Expand Up @@ -3205,10 +3206,8 @@ impl_runtime_apis! {

impl pallet_revive::ReviveApi<Block, AccountId, Balance, Nonce, BlockNumber, EventRecord> for Runtime
{
fn balance(address: H160) -> Balance {
use frame_support::traits::fungible::Inspect;
let account = <Runtime as pallet_revive::Config>::AddressMapper::to_account_id(&address);
Balances::reducible_balance(&account, Preserve, Polite)
fn balance(address: H160) -> U256 {
Revive::evm_balance(&address)
}

fn nonce(address: H160) -> Nonce {
Expand All @@ -3219,7 +3218,7 @@ impl_runtime_apis! {
fn eth_transact(
from: H160,
dest: Option<H160>,
value: Balance,
value: U256,
input: Vec<u8>,
gas_limit: Option<Weight>,
storage_deposit_limit: Option<Balance>,
Expand Down
34 changes: 34 additions & 0 deletions substrate/frame/revive/rpc/examples/js/abi/event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "value",
"type": "uint256"
},
{
"indexed": false,
"internalType": "string",
"name": "message",
"type": "string"
}
],
"name": "ExampleEvent",
"type": "event"
},
{
"inputs": [],
"name": "triggerEvent",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
65 changes: 65 additions & 0 deletions substrate/frame/revive/rpc/examples/js/abi/piggyBank.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
[
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [],
"name": "deposit",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [],
"name": "getDeposit",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "owner",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "withdrawAmount",
"type": "uint256"
}
],
"name": "withdraw",
"outputs": [
{
"internalType": "uint256",
"name": "remainingBal",
"type": "uint256"
}
],
"stateMutability": "nonpayable",
"type": "function"
}
]
14 changes: 14 additions & 0 deletions substrate/frame/revive/rpc/examples/js/abi/revert.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [],
"name": "doRevert",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
Binary file modified substrate/frame/revive/rpc/examples/js/bun.lockb
Binary file not shown.
32 changes: 32 additions & 0 deletions substrate/frame/revive/rpc/examples/js/contracts/PiggyBank.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract PiggyBank {

uint private balance;
address public owner;

constructor() {
owner = msg.sender;
balance = 0;
}

function deposit() public payable returns (uint) {
balance += msg.value;
return balance;
}

function getDeposit() public view returns (uint) {
return balance;
}

function withdraw(uint withdrawAmount) public returns (uint remainingBal) {
require(msg.sender == owner);
balance -= withdrawAmount;
(bool success, ) = payable(msg.sender).call{value: withdrawAmount}("");
require(success, "Transfer failed");

return balance;
}
}

56 changes: 0 additions & 56 deletions substrate/frame/revive/rpc/examples/js/evm-contracts.json

This file was deleted.

53 changes: 31 additions & 22 deletions substrate/frame/revive/rpc/examples/js/index.html
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="https://polkadot.com/favicon.ico">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>MetaMask Playground</title>
<style>
input { width: 300px; margin-right: 10px; }
<head>
<meta charset="UTF-8" />
<link rel="icon" href="https://polkadot.com/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>MetaMask Playground</title>
<style>
input {
width: 300px;
margin-right: 10px;
}

button {
display: block;
margin-bottom: 10px;
}
</style>
</head>
<body>
<input id="transferInput" type="text" style="float: left" placeholder="Destination address" value="0x3cd0a705a2dc65e5b1e1205896baa2be8a07c6e0" />
<button id="transferButton">Transfer coins</button>
button {
display: block;
margin-bottom: 10px;
}
</style>
</head>
<body>
<input
id="transferInput"
type="text"
style="float: left"
placeholder="Destination address"
value="0x3cd0a705a2dc65e5b1e1205896baa2be8a07c6e0"
/>
<button id="transferButton">Transfer coins</button>

<button id="deployButton">Deploy Contract</button>
<button id="deployButton">Deploy Contract</button>

<input id="callInput" type="text" style="float: left" placeholder="Contract address" />
<button id="callButton">Call Contract</button>
<input id="callInput" type="text" style="float: left" placeholder="Contract address" />
<button id="callButton">Call Contract</button>

<button id="deployAndCallButton">Deploy and Call Contract</button>
<script type="module" src="src/web.ts"></script>
</body>
<button id="deployAndCallButton">Deploy and Call Contract</button>
<script type="module" src="src/web.ts"></script>
</body>
</html>
Loading

0 comments on commit 39eba14

Please sign in to comment.