|
| 1 | +import * as assert from "assert"; |
| 2 | + |
| 3 | +import { getAliceSigner, getDevnetApi, getRandomSubstrateKeypair } from "../src/substrate" |
| 4 | +import { generateRandomEthersWallet } from "../src/utils"; |
| 5 | +import { devnet, MultiAddress } from "@polkadot-api/descriptors" |
| 6 | +import { PolkadotSigner, TypedApi } from "polkadot-api"; |
| 7 | +import { convertH160ToPublicKey, convertH160ToSS58, convertPublicKeyToSs58 } from "../src/address-utils" |
| 8 | +import { IProxyABI, IPROXY_ADDRESS } from "../src/contracts/proxy" |
| 9 | +import { ethers } from 'ethers'; |
| 10 | +import { forceSetBalanceToEthAddress, forceSetBalanceToSs58Address } from "../src/subtensor"; |
| 11 | +import { KeyPair } from "@polkadot-labs/hdkd-helpers"; |
| 12 | + |
| 13 | +import { decodeAddress } from "@polkadot/util-crypto"; |
| 14 | + |
| 15 | +async function getTransferCallCode(api: TypedApi<typeof devnet>, receiver: KeyPair, transferAmount: number) { |
| 16 | + |
| 17 | + const unsignedTx = api.tx.Balances.transfer_keep_alive({ |
| 18 | + dest: MultiAddress.Id(convertPublicKeyToSs58(receiver.publicKey)), |
| 19 | + value: BigInt(1000000000), |
| 20 | + }); |
| 21 | + const encodedCallDataBytes = await unsignedTx.getEncodedData(); |
| 22 | + |
| 23 | + // encoded call should be 0x050300d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d02286bee |
| 24 | + // const transferCall = encodedCallDataBytes |
| 25 | + |
| 26 | + const data = encodedCallDataBytes.asBytes() |
| 27 | + |
| 28 | + return [...data] |
| 29 | +} |
| 30 | + |
| 31 | +async function getProxies(api: TypedApi<typeof devnet>, address: string) { |
| 32 | + const entries = await api.query.Proxy.Proxies.getEntries() |
| 33 | + const result = [] |
| 34 | + for (const entry of entries) { |
| 35 | + const proxyAddress = entry.keyArgs[0] |
| 36 | + const values = entry.value |
| 37 | + const proxies = values[0] |
| 38 | + for (const proxy of proxies) { |
| 39 | + if (proxy.delegate === address) { |
| 40 | + result.push(proxyAddress) |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + return result |
| 45 | +} |
| 46 | + |
| 47 | +describe("Test pure proxy precompile", () => { |
| 48 | + const evmWallet = generateRandomEthersWallet(); |
| 49 | + const evmWallet2 = generateRandomEthersWallet(); |
| 50 | + const evmWallet3 = generateRandomEthersWallet(); |
| 51 | + const receiver = getRandomSubstrateKeypair(); |
| 52 | + |
| 53 | + let api: TypedApi<typeof devnet> |
| 54 | + |
| 55 | + let alice: PolkadotSigner; |
| 56 | + |
| 57 | + before(async () => { |
| 58 | + api = await getDevnetApi() |
| 59 | + alice = await getAliceSigner(); |
| 60 | + |
| 61 | + await forceSetBalanceToEthAddress(api, evmWallet.address) |
| 62 | + await forceSetBalanceToEthAddress(api, evmWallet2.address) |
| 63 | + await forceSetBalanceToEthAddress(api, evmWallet3.address) |
| 64 | + }) |
| 65 | + |
| 66 | + it("Call createPureProxy, then use proxy to call transfer", async () => { |
| 67 | + const proxies = await getProxies(api, convertH160ToSS58(evmWallet.address)) |
| 68 | + const contract = new ethers.Contract(IPROXY_ADDRESS, IProxyABI, evmWallet) |
| 69 | + console.log("evmWallet", evmWallet.address) |
| 70 | + |
| 71 | + const type = 0; |
| 72 | + const delay = 0; |
| 73 | + const index = 0; |
| 74 | + const tx = await contract.createPureProxy(type, delay, index) |
| 75 | + const response = await tx.wait() |
| 76 | + console.log("response", response.blockNumber) |
| 77 | + |
| 78 | + const proxiesAfterAdd = await getProxies(api, convertH160ToSS58(evmWallet.address)) |
| 79 | + |
| 80 | + const length = proxiesAfterAdd.length |
| 81 | + assert.equal(length, proxies.length + 1, "proxy should be set") |
| 82 | + const proxy = proxiesAfterAdd[proxiesAfterAdd.length - 1] |
| 83 | + |
| 84 | + await forceSetBalanceToSs58Address(api, proxy) |
| 85 | + const balance = (await api.query.System.Account.getValue(convertPublicKeyToSs58(receiver.publicKey))).data.free |
| 86 | + |
| 87 | + const amount = 1000000000; |
| 88 | + |
| 89 | + const callCode = await getTransferCallCode(api, receiver, amount) |
| 90 | + const tx2 = await contract.proxyCall(decodeAddress(proxy), [type], callCode) |
| 91 | + await tx2.wait() |
| 92 | + |
| 93 | + const balanceAfter = (await api.query.System.Account.getValue(convertPublicKeyToSs58(receiver.publicKey))).data.free |
| 94 | + assert.equal(balanceAfter, balance + BigInt(amount), "balance should be increased") |
| 95 | + }) |
| 96 | + |
| 97 | + it("Call createPureProxy, add multiple proxies", async () => { |
| 98 | + const contract = new ethers.Contract(IPROXY_ADDRESS, IProxyABI, evmWallet) |
| 99 | + const type = 0; |
| 100 | + const delay = 0; |
| 101 | + const index = 0; |
| 102 | + const proxies = await getProxies(api, convertH160ToSS58(evmWallet.address)) |
| 103 | + const length = proxies.length |
| 104 | + for (let i = 0; i < 5; i++) { |
| 105 | + const tx = await contract.createPureProxy(type, delay, index) |
| 106 | + await tx.wait() |
| 107 | + |
| 108 | + await new Promise(resolve => setTimeout(resolve, 500)); |
| 109 | + const currentProxies = await getProxies(api, convertH160ToSS58(evmWallet.address)) |
| 110 | + assert.equal(currentProxies.length, length + i + 1, "proxy should be set") |
| 111 | + } |
| 112 | + }) |
| 113 | + |
| 114 | + it("Call createPureProxy, edge cases, call via wrong proxy", async () => { |
| 115 | + const contract = new ethers.Contract(IPROXY_ADDRESS, IProxyABI, evmWallet2) |
| 116 | + const amount = 1000000000; |
| 117 | + const callCode = await getTransferCallCode(api, receiver, amount) |
| 118 | + const type = 0; |
| 119 | + |
| 120 | + // call with wrong proxy |
| 121 | + try { |
| 122 | + const tx = await contract.proxyCall(receiver, [type], callCode) |
| 123 | + await tx.wait() |
| 124 | + } catch (error) { |
| 125 | + assert.notEqual(error, undefined, "should fail if proxy not set") |
| 126 | + } |
| 127 | + }) |
| 128 | + |
| 129 | + it("Call createProxy, then use proxy to call transfer", async () => { |
| 130 | + const proxies = await api.query.Proxy.Proxies.getValue(convertH160ToSS58(evmWallet2.address)) |
| 131 | + const contract = new ethers.Contract(IPROXY_ADDRESS, IProxyABI, evmWallet2) |
| 132 | + |
| 133 | + const type = 0; |
| 134 | + const delay = 0; |
| 135 | + |
| 136 | + const tx = await contract.addProxy(convertH160ToPublicKey(evmWallet3.address), type, delay) |
| 137 | + await tx.wait() |
| 138 | + |
| 139 | + |
| 140 | + const proxiesAfterAdd = await await api.query.Proxy.Proxies.getValue(convertH160ToSS58(evmWallet2.address)) |
| 141 | + |
| 142 | + const length = proxiesAfterAdd[0].length |
| 143 | + assert.equal(length, proxies[0].length + 1, "proxy should be set") |
| 144 | + const proxy = proxiesAfterAdd[0][proxiesAfterAdd[0].length - 1] |
| 145 | + |
| 146 | + assert.equal(proxy.delegate, convertH160ToSS58(evmWallet3.address), "proxy should be set") |
| 147 | + |
| 148 | + |
| 149 | + const balance = (await api.query.System.Account.getValue(convertPublicKeyToSs58(receiver.publicKey))).data.free |
| 150 | + |
| 151 | + const amount = 1000000000; |
| 152 | + |
| 153 | + const contract2 = new ethers.Contract(IPROXY_ADDRESS, IProxyABI, evmWallet3) |
| 154 | + |
| 155 | + |
| 156 | + const callCode = await getTransferCallCode(api, receiver, amount) |
| 157 | + const tx2 = await contract2.proxyCall(convertH160ToPublicKey(evmWallet2.address), [type], callCode) |
| 158 | + await tx2.wait() |
| 159 | + |
| 160 | + const balanceAfter = (await api.query.System.Account.getValue(convertPublicKeyToSs58(receiver.publicKey))).data.free |
| 161 | + assert.equal(balanceAfter, balance + BigInt(amount), "balance should be increased") |
| 162 | + }) |
| 163 | +}); |
0 commit comments