Skip to content

Instantly share code, notes, and snippets.

@yihuang
Last active August 6, 2021 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yihuang/f072e6d91d4845105b7698f52171053a to your computer and use it in GitHub Desktop.
Save yihuang/f072e6d91d4845105b7698f52171053a to your computer and use it in GitHub Desktop.
some pseudocode to demonstrate interaction between evm contract and host system
contract ERC20 {
map[address][uint256] _balances;
// deposit through native module
// native module will first move user's native tokens to contract address,
// then mint on evm directly
function deposit(receipient, amount) {
require(msg.sender == native_module_address)
_balances[receipient] += amount;
}
// withdraw to native token
function withdraw(amount) {
_balances[msg.sender] -= amount
emit CosmosNative("bank.send", msg.sender, amount)
}
// or send to ethereum through gravity bridge
function withdraw_to_eth(amount, eth_dest) {
_balances[msg.sender] -= amount
emit CosmosNative("gravity.send", msg.sender, amount, eth_dest)
}
}
contract ERC20Manager {
map[string][ERC20] _contracts
function deploy(denom) {
require(msg.sender == native_module_address)
if denom not in _contracts {
_contracts[denom] = new ERC20();
}
}
function query(denom) address {
return _contracts[denom]
}
}
// native module logic
var erc20_manager_contract
func on_gravity_receive(receipent, amount, denom) {
erc20_manager_contract.deploy(denom)
contract = erc20_manager_contract.query(denom)
bank.SendFromAccountToAccount(receipent, contract, amount, denom);
contract.deposit(receipient, amount)
}
@leejw51crypto
Copy link

for SendFromModuleToAccount, is it minting native token?
from where, funds are deducted?

@yihuang
Copy link
Author

yihuang commented Aug 6, 2021

for SendFromModuleToAccount, is it minting native token?
from where, funds are deducted?

Hmm, I think at this stage, the gravity already moved the native tokens to the receipent, so we actually need to move it from receipient to contract.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment