Pillar's etherspot (OK)

node v15.14.0
version: 1.0.0
endpointsharetweet
https://github.com/etherspot/etherspot-sdk
require("rxjs") const { ethers } = require("ethers") require("ws") require("reflect-metadata") const { take } = require('rxjs/operators')
const { Sdk, randomPrivateKey, NetworkNames, EnvNames, GetGatewaySupportedTokenDto } = require("etherspot@1.2.6")
EnvNames
NetworkNames
randomPrivateKey()
fixed on private key = "0xef60ad624bf7850ffc262995860bfc84485ccf7fbe252faf770960e427f3e5cc"
account: 0xf3e06eeC1A90A7aEB10F768B924351A0F0158A1A
const privateKeyR = "0x91c2d9fe7295c2a2cfb7c74b3ad3f859ce867db4a595e1c8e963ce8badb1660b"
const privateKey = "0x398dd483a53fef9b5b37c142bdbabcef69a9b5e133885ffb62981f6484ee7aa1" //"0xef60ad624bf7850ffc262995860bfc84485ccf7fbe252faf770960e427f3e5cc"
01-create-smart-wallet.ts
const sdk = new Sdk(privateKey , { env: EnvNames.TestNets, // Use EnvNames.Mainnet, If you are accessing Mainnets networkName: NetworkNames.Ropsten, //projectKey: 'test-project', //optional can be used to uniquely identify your project });
sdk.state.network
sdk.state.account
const balanceR = await sdk.getAccountBalances() const balR = balanceR.items[0].balance ethers.utils.formatEther(balR)
await sdk.syncAccount()
//sdk.destroy(); const output = await sdk.createSession(); console.log('session object', output); console.log('session graphql headers', { ['x-auth-token']: output.token, });
const { state, notifications$ } = sdk;
const notification = notifications$.pipe(take(1)).toPromise();
console.log( 'create session with ttl', await sdk.createSession({ ttl: 100, }), );
state.account
state.accout is a "Smart Wallet Account" represent your sdk("privatekey") account A (to do batch). which is a contract.
key account: 0x4cD135D404fa2be4c104BE834a3515D0bcb26306
Before sdk.computeContractAccount, state.accout is a Key account (from the private key)
contract account :0xeD21e13a4c54692c4c021bcd4F95e1D7A74F447c
await sdk.computeContractAccount({sync: true});
await sdk.computeContractAccount({sync: false});
state.account
above is Kavon account
similar to await sdk.computeContractAccount({sync: true});
await sdk.syncAccount()
sdk.supportedNetworks
after account sync
console.log('Account balances ', await sdk.getAccountBalances());
const balance = await sdk.getAccountBalances()
const bal = balance.items[0].balance
ethers.utils.formatEther(bal)
02-list demo https://docs.etherspot.dev/classes/getgatewaysupportedtokendto.html
don't use a random Private Key, because the Smart Wallet account generate with no ETH, it will revert the transation.
const sdk1 = await new Sdk(randomPrivateKey(), { env: EnvNames.TestNets, networkName: NetworkNames.Kovan, });
sdk1.supportedNetworks
03-submit-eth-transaction.ts
sdk is fixed private key on kavon account
You should fund this Smart wallet Account some ETH for transfer.
need another ETH in send account, otherwise, will be reverted.
const receiver = '0xf3e06eeC1A90A7aEB10F768B924351A0F0158A1A'
const receiver2 = '0x8587eA108898749538372Cd3Df459870C4a1A56F'; // Replace with address of your choice const amtInWei = '5'; //Send 0.0005 ETH //this method will add the transaction to a batch, which has to be executed later. const transaction = await sdk.batchExecuteAccountTransaction({ to: receiver,//wallet address value: amtInWei,//in wei }); console.log('Estimating transaction'); await sdk.estimateGatewayBatch().then(async (result) => { console.log('Estimation ', result.estimation); const hash = await sdk.submitGatewayBatch(); console.log('Transaction submitted ', hash); }) .catch((error) => { console.log('Transaction estimation failed with error ',error.message); });
const erc20Abi = require( 'human-standard-token-abi')
const abiCoder = require('web3-eth-abi');
const methodName = erc20Abi.find(a => a.name === 'transfer');
const tokenAddress = '0x9de9cde889a40b7402d824386064d17792298e1b'; //PLR contract on Kovan const tokens = '1000000000000000000000'; // 1000 PLR
const encodedData = abiCoder.encodeFunctionCall( methodName, [ receiver, tokens ]);
const transaction = await sdk.batchExecuteAccountTransaction({ to: receiver,//wallet address data: encodedData, });
await notification;
const { ContractNames, getContractAddress } = require( '@etherspot/contracts')
04-ens.ts
const { providers, utils, Wallet, getDefaultProvider } = require('ethers') const p2 = getDefaultProvider('kovan') const apiKey = "JYAweIkfvFWwmmDQAixHI6ERftUrx3UA" const p3 = new providers.AlchemyProvider("kovan", apiKey);
const provider = new providers.InfuraProvider("kovan", "6835ff4f8cb1454093e9ddf037830185");
const ensTopLevelDomains = await sdk.getENSTopLevelDomains();
const ensName = `random${Date.now().toString(17)}.${ensTopLevelDomains[0]}`;
// const ensNode = await sdk.reserveENSName({ name: ensName, });
//await notification;
const LOCAL_A_FAUCET_PRIVATE_KEY = '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80'; const LOCAL_A_PROVIDER_ENDPOINT = 'http://localhost:8545'; const LOCAL_A_PROVIDER_CHAIN_ID = 9999; const LOCAL_B_FAUCET_PRIVATE_KEY = '0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d'; const LOCAL_B_PROVIDER_ENDPOINT = 'http://localhost:9545'; const LOCAL_B_PROVIDER_CHAIN_ID = 6666;
const localAProvider = new providers.JsonRpcProvider(LOCAL_A_PROVIDER_ENDPOINT);
// Create a wallet instance from a mnemonic... mnemonic = "announce room limb pattern dry unit scale effort smooth jazz weasel alcohol" walletMnemonic = Wallet.fromMnemonic(mnemonic) walletPrivateKey = new Wallet(walletMnemonic.privateKey, provider) //await walletMnemonic.signMessage("Hello World") walletPrivateKey.address const signer = walletPrivateKey
[signer.address, signer.publicKey, signer.privateKey]
privateKey
// ...or from a private key const kovanWallet = new Wallet(walletMnemonic.privateKey, p3) //const kovanWallet = walletMnemonic.connect(provider) wallet = walletMnemonic.connect(p3) // Querying the network await wallet.getBalance(); // { BigNumber: "42" } //await wallet.getTransactionCount(); // 錯誤是正常的 // const kovanWallet = new Wallet(privateKey, provider)
const localAWallet = new Wallet(LOCAL_A_FAUCET_PRIVATE_KEY, localAProvider);
function getWallet(networkName = NetworkNames.LocalA) { let result = null; console.log(networkName) switch (networkName) { case NetworkNames.Kovan: result = kovanWallet; console.log("yes kovan") break; case NetworkNames.LocalA: result = localAWallet; break; case NetworkNames.LocalB: result = localBWallet; break; } return result; }
async function topUpAccount( account, value, networkName = NetworkNames.Kovan, ) { const wallet = getWallet(networkName); console.log(wallet) const nonce = await wallet.getTransactionCount(); console.log(nonce) const response = await wallet.sendTransaction({ to: account, value: utils.parseEther(value), nonce, }); await response.wait(); }
state.account.address
await topUpAccount(state.account.address, 0.01)
Loading…

no comments

    sign in to comment