Deploying Solidity Contract
cli initia.js
Copy export FILE_NAME = my-first-solidity
export FILE_PATH = ./$FILE_NAME.sol
export KEY_NAME = my-first-key
export CHAIN_ID = my-first-chain-1
export GAS_PRICES = 0.15unova
# compile solidity
mkdir build
solc $FILE_PATH --bin --abi -o build
# CREATE opcode
export TX_HASH = $( \
minitiad tx evm create \
./build/ $FILE_NAME .bin \
--from $KEY_NAME \
--chain-id $CHAIN_ID \
--gas-prices $GAS_PRICES \
| grep txhash \
| sed 's/.*: //' \
)
# CREATE2 opcode
export TX_HASH = $( \
minitiad tx evm create2 \
1:./build/ $FILE_NAME .bin \
--from $KEY_NAME \
--chain-id $CHAIN_ID \
--gas-prices $GAS_PRICES \
| grep txhash \
| sed 's/.*: //' \
)
# find contract address from the tx
minitiad q tx $TX_HASH | grep contract -A 1
Copy export FILE_NAME = my-first-solidity
export FILE_PATH = ./$FILE_NAME.sol
export LCD_URL = http://127.0.0.1:1317
export GAS_PRICES = 0.15unova
# compile solidity
mkdir build
solc $FILE_PATH --bin --abi -o build
scripts
Copy import { LCDClient , MnemonicKey , MsgCreate , Wallet , /* MsgCreate2 */ } from '@initia/initia.js' ;
import * as fs from 'fs' ;
const path = `build/ ${ FILE_NAME } .bin` ;
const lcdURL = process . env . LCD_URL ;
const gasPrices = process . env . GAS_PRICES ;
async function deploy () {
const lcd = new LCDClient (lcdURL , {
gasPrices : gasPrices ,
gasAdjustment : '1.5' ,
});
const key = new MnemonicKey ({
mnemonic :
'beauty sniff protect ...' ,
});
const wallet = new Wallet (lcd , key);
const codeBytes = fs .readFileSync (path , "utf-8" );
const msgs = [
new MsgCreate ( key .accAddress , codeBytes) ,
// new MsgCreate2(key.accAddress, codeBytes, slat)
];
// sign tx
const signedTx = await wallet .createAndSignTx ({ msgs });
// send(broadcast) tx
lcd . tx .broadcastSync (signedTx) .then (res => console .log (res));
// {
// height: 0,
// txhash: '162AA29DE237BD060EFEFFA862DBD07ECD1C562EBFDD965AD6C34DF856B53DC2',
// raw_log: '[]'
// }
}
deploy ();
Interacting with EVM Contract
cli initia.js
Copy export CONTRACT_ADDR = 0x1
export KEY_NAME = my-first-key
# querying code bytes
minitiad q evm code $CONTRACT_ADDR
# querying state
minitiad q evm state $CONTRACT_ADDR 0x00
# querying erc20 contract address of a denom
minitiad q evm contract-addr-by-denom unova
# querying denom of erc20 contract
minitiad q evm denom $CONTRACT_ADDR
# executing call with no state transition
minitiad q evm call $( minitiad keys show $KEY_NAME -a ) \
$CONTRACT_ADDR \
[0x-hex-input-string] \
--trace \
--with-memory \
--with-stack \
--with-return-data \
--with-storage
# executing call with state transition
minitiad tx evm call \
$CONTRACT_ADDR \
[0x-hex-input-string] \
--from $KEY_NAME \
--chain-id $CHAIN_ID \
--gas-prices $GAS_PRICES
Copy import { LCDClient , MnemonicKey , Wallet , CallResponse , MsgCall } from '@initia/initia.js' ;
const gasPrices = process . env . GAS_PRICES ;
const lcd = new LCDClient (lcdURL , {
gasPrices : gasPrices ,
gasAdjustment : '1.5' ,
});
function queryCode (contractAddr : string ) : Promise < string > {
return lcd . evm .code (contractAddr);
}
function queryState (contractAddr : string , key : string ) : Promise < string > {
return lcd . evm .state (contractAddr , key);
}
function queryDenom (erc20ContractAddr : string ) : Promise < string > {
return lcd . evm .denom (erc20ContractAddr);
}
function queryERC20Addr (denom : string ) : Promise < string > {
return lcd . evm .contractAddrByDenom (denom);
}
// input: 0x prefixed hex string
function queryCall (sender : string , contractAddr : string , input : string ) : Promise < CallResponse > {
return lcd . evm .call (sender , contractAddr , input)
}
// input: 0x prefixed hex string
function executeCall (contractAddr : string , input : string ) {
const key = new MnemonicKey ({
mnemonic :
'beauty sniff protect ...' ,
});
const wallet = new Wallet (lcd , key);
const msgs = [
new MsgCall ( key .accAddress , contractAddr , input) ,
];
// sign tx
const signedTx = await wallet .createAndSignTx ({ msgs });
// send(broadcast) tx
lcd . tx .broadcastSync (signedTx) .then (res => console .log (res));
// {
// height: 0,
// txhash: '162AA29DE237BD060EFEFFA862DBD07ECD1C562EBFDD965AD6C34DF856B53DC2',
// raw_log: '[]'
// }
}
Conclusion
Now that you have the necessary information on how to deploy your contracts and interact with them on the Initia platform, you are well-equipped to start implementing your projects. For further guidance and practical insights, please refer to upcoming sections that provide examples of EVM contracts. These examples will offer valuable context and demonstrate how to leverage the unique capabilities of the EVM module within the Cosmos ecosystem effectively. Whether you are transitioning existing Ethereum applications or developing new ones, these resources will support you in optimizing your deployment and operational strategies.
Last updated 6 months ago