-
Notifications
You must be signed in to change notification settings - Fork 799
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
88 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,44 @@ | ||
|
||
import { Contract, ContractFactory, JsonRpcSigner, parseEther, encodeRlp, AddressLike, getBytes, JsonRpcProvider } from 'ethers'; | ||
//! Run with bun run demo.ts | ||
import { Contract, ContractFactory, encodeRlp, getBytes, JsonRpcProvider } from 'ethers'; | ||
|
||
const provider = new JsonRpcProvider('http://localhost:9090'); | ||
const signer = await provider.getSigner(); | ||
console.log(`Signer address: ${await signer.getAddress()}, Nonce: ${await signer.getNonce()}`); | ||
|
||
|
||
const code = getBytes('0x50564d0001010424009000022363616c6cdeadbeef63616c6c5f6e657665726465706c6f797365616c5f72657475726e041001000000007365616c5f72657475726e051b03000a63616c6c5f6e6576657204066465706c6f79060463616c6c062c06011f000406081b1c06100408130013000211fc03100408040001040904040706100a05004e13005129a4b800'); | ||
const args = new Uint8Array() | ||
const bytecode = encodeRlp([ code, args ]); | ||
|
||
const contractFactory = new ContractFactory([], bytecode, signer); | ||
const contract = await contractFactory.deploy(); | ||
await contract.waitForDeployment(); | ||
const address = await contract.getAddress(); | ||
console.log(`Contract deployed: ${address}`); | ||
|
||
//main().catch(console.error); | ||
|
||
|
||
|
||
// const nonce = await signer.getNonce(); | ||
// console.log(`Nonce: ${nonce}`); | ||
// | ||
// document.getElementById('transferButton')?.addEventListener('click', async () => { | ||
// const address = (document.getElementById('transferInput') as HTMLInputElement).value; | ||
// await transfer(address); | ||
// }); | ||
// | ||
// document.getElementById('deployButton')?.addEventListener('click', async () => { | ||
// await deploy(); | ||
// }); | ||
// document.getElementById('deployAndCallButton')?.addEventListener('click', async () => { | ||
// const nonce = await signer.getNonce(); | ||
// console.log(`deploy with nonce: ${nonce}`); | ||
// | ||
// const address = await deploy(); | ||
// if (address) { | ||
// const nonce = await signer.getNonce(); | ||
// console.log(`call with nonce: ${nonce}`); | ||
// await call(address); | ||
// } | ||
// | ||
// }); | ||
// document.getElementById('callButton')?.addEventListener('click', async () => { | ||
// const address = (document.getElementById('callInput') as HTMLInputElement).value; | ||
// await call(address); | ||
// }); | ||
// | ||
// async function deploy() { | ||
// console.log('Deploying contract...'); | ||
// | ||
// const code = getBytes('0x50564d0001010424009000022363616c6cdeadbeef63616c6c5f6e657665726465706c6f797365616c5f72657475726e041001000000007365616c5f72657475726e051b03000a63616c6c5f6e6576657204066465706c6f79060463616c6c062c06011f000406081b1c06100408130013000211fc03100408040001040904040706100a05004e13005129a4b800'); | ||
// const args = new Uint8Array() | ||
// const bytecode = encodeRlp([ code, args ]); | ||
// | ||
// const contractFactory = new ContractFactory([], bytecode, signer); | ||
// | ||
// try { | ||
// const contract = await contractFactory.deploy(); | ||
// await contract.waitForDeployment(); | ||
// const address = await contract.getAddress(); | ||
// console.log(`Contract deployed: ${address}`); | ||
// return address; | ||
// } catch (e) { | ||
// console.error('Failed to deploy contract', e); | ||
// return; | ||
// } | ||
// } | ||
// | ||
// async function call(address: string) { | ||
// const abi = ['function getValue() view returns (uint256)', 'function setValue(uint256 _value)']; | ||
// | ||
// const contract = new Contract(address, abi, signer); | ||
// const tx = await contract.setValue(42); | ||
// | ||
// console.log('Transaction hash:', tx.hash); | ||
// } | ||
// | ||
// async function transfer(to: AddressLike) { | ||
// console.log(`transferring 1 DOT to ${to}...`); | ||
// try { | ||
// const tx = await signer.sendTransaction({ | ||
// to, | ||
// value: parseEther('1.0'), | ||
// }); | ||
// | ||
// const receipt = await tx.wait(); | ||
// console.log(`Transaction hash: ${receipt?.hash}`); | ||
// } catch (e) { | ||
// console.error('Failed to send transaction', e); | ||
// return; | ||
// } | ||
// } | ||
//}); | ||
function str_to_bytes(str: string): Uint8Array { | ||
return new TextEncoder().encode(str); | ||
} | ||
debugger | ||
// deploy | ||
async function deploy() { | ||
console.log(`Deploying Contract...`); | ||
const code = getBytes('0x50564d000101041c009000021c6465706c6f7963616c6c696e7075746465706f7369745f6576656e74041b02000000000d0000006465706f7369745f6576656e74696e707574050f0203066465706c6f79560463616c6c0681661b02810a0000030020002f003a00470055005600720081008c009a00a800a900af00ba00bd00c500d100e300eb00f100f300f800fc00040106014e1300021174ff03108800031584005217040980000405800004080610068e0003158000521702188000061008dc00011a800004078100297a1e04070000015219040806100cbd011088000115840002118c00130000021174ff03108800031584005217040980000405800004080610123b031580005217021880000610148a00011a800004078100297a1f0407000001521904080610186bff011088000115840002118c00130000040a102fa947287a12aa0308a70b070a0e527c1110c802cc012fbcfb14a909129cfc08cb0a2e0c1d128cff0004020000010102220101222c0c1103bc02bb042fabfb1299030f090a0513527a07090f089a091110a802aa012f9afb13004e0113008b88220a518488848a8868441451441122122a44449392b4244922882c492a5952fd00'); | ||
|
||
const args = str_to_bytes('ssh'); | ||
const bytecode = encodeRlp([ code, new Uint8Array() ]); | ||
console.log(`Bytecode length: ${bytecode.length}`, ); | ||
const contractFactory = new ContractFactory([ | ||
"constructor(bytes memory _data)", | ||
], bytecode, signer); | ||
|
||
console.log('Deploying contract with args:', args); | ||
const contract = await contractFactory.deploy(args); | ||
await contract.waitForDeployment(); | ||
const address = await contract.getAddress(); | ||
console.log(`Contract deployed: ${address}`); | ||
return address; | ||
} | ||
|
||
async function call(address: string) { | ||
console.log(`Calling Contract at ${address}...`); | ||
|
||
const abi = ['function call(bytes data)']; | ||
const contract = new Contract(address, abi, signer); | ||
const tx = await contract.call(str_to_bytes('world')); | ||
console.log('Call transaction hash:', tx.hash); | ||
} | ||
|
||
const address = await deploy() | ||
if (Deno.env.get('CALL') ) { | ||
await call(address) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"compilerOptions": { | ||
"compilerOptions": { | ||
"target": "esnext", | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters