-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add entrypoints #538
Add entrypoints #538
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
export interface EntrypointConfig { | ||
networkProviderUrl: string; | ||
networkProviderKind: string; | ||
chainId: string; | ||
} | ||
|
||
export class TestnetEntrypointConfig implements EntrypointConfig { | ||
networkProviderUrl = "https://testnet-api.multiversx.com"; | ||
networkProviderKind = "api"; | ||
chainId = "T"; | ||
} | ||
|
||
export class DevnetEntrypointConfig implements EntrypointConfig { | ||
networkProviderUrl = "https://devnet-api.multiversx.com"; | ||
networkProviderKind = "api"; | ||
chainId = "D"; | ||
} | ||
|
||
export class MainnetEntrypointConfig implements EntrypointConfig { | ||
networkProviderUrl = "https://api.multiversx.com"; | ||
networkProviderKind = "api"; | ||
chainId = "1"; | ||
} | ||
|
||
export class LocalnetEntrypointConfig implements EntrypointConfig { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚀 |
||
networkProviderUrl = "http://localhost:7950"; | ||
networkProviderKind = "proxy"; | ||
chainId = "localnet"; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { assert } from "chai"; | ||
import { readFileSync } from "fs"; | ||
import { Account } from "../accounts/account"; | ||
import { Address } from "../address"; | ||
import { loadAbiRegistry, loadTestWallet, TestWallet } from "../testutils"; | ||
import { TransactionComputer } from "../transactionComputer"; | ||
import { DevnetEntrypoint } from "./entrypoints"; | ||
|
||
describe("TestEntrypoint", () => { | ||
const entrypoint = new DevnetEntrypoint(); | ||
let alicePem: TestWallet; | ||
let bobPem: TestWallet; | ||
let txComputer: TransactionComputer; | ||
|
||
before(async function () { | ||
alicePem = await loadTestWallet("alice"); | ||
bobPem = await loadTestWallet("bob"); | ||
txComputer = new TransactionComputer(); | ||
}); | ||
|
||
it("native transfer", async () => { | ||
const controller = entrypoint.createTransfersController(); | ||
const sender = Account.newFromPem(alicePem.pemFileText); | ||
sender.nonce = 77777; | ||
|
||
const transaction = await controller.createTransactionForTransfer( | ||
sender, | ||
BigInt(sender.getNonceThenIncrement().valueOf()), | ||
{ | ||
receiver: sender.address, | ||
nativeAmount: BigInt(0), | ||
data: Buffer.from("hello"), | ||
}, | ||
); | ||
assert.equal( | ||
Buffer.from(transaction.signature).toString("hex"), | ||
"69bc7d1777edd0a901e6cf94830475716205c5efdf2fd44d4be31badead59fc8418b34f0aa3b2c80ba14aed5edd30031757d826af58a1abb690a0bee89ba9309", | ||
); | ||
}); | ||
|
||
it("contract flow", async function () { | ||
this.timeout(30000); | ||
const abi = await loadAbiRegistry("src/testdata/adder.abi.json"); | ||
const sender = Account.newFromPem(alicePem.pemFileText); | ||
const accountAddress = new Address(sender.address.bech32()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there's no need to convert the address to bech32 then back to |
||
sender.nonce = await entrypoint.recallAccountNonce(accountAddress); | ||
|
||
const controller = entrypoint.createSmartContractController(abi); | ||
const bytecode = readFileSync("src/testdata/adder.wasm"); | ||
|
||
const transaction = await controller.createTransactionForDeploy( | ||
sender, | ||
BigInt(sender.getNonceThenIncrement().valueOf()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should switch to |
||
{ | ||
bytecode, | ||
gasLimit: BigInt(10_000_000), | ||
arguments: [0], | ||
}, | ||
); | ||
|
||
const txHash = await entrypoint.sendTransaction(transaction); | ||
const outcome = await controller.awaitCompletedDeploy(txHash); | ||
|
||
assert.equal(outcome.contracts.length, 1); | ||
|
||
const contractAddress = Address.fromBech32(outcome.contracts[0].address); | ||
|
||
const executeTransaction = await controller.createTransactionForExecute( | ||
sender, | ||
BigInt(sender.getNonceThenIncrement().valueOf()), | ||
{ | ||
contract: contractAddress, | ||
gasLimit: BigInt(10_000_000), | ||
function: "add", | ||
arguments: [7], | ||
}, | ||
); | ||
|
||
const txHashExecute = await entrypoint.sendTransaction(executeTransaction); | ||
await entrypoint.awaitCompletedTransaction(txHashExecute); | ||
|
||
const queryResult = await controller.queryContract(contractAddress, "getSum", []); | ||
assert.equal(queryResult.length, 1); | ||
assert.equal(queryResult[0], 7); | ||
}); | ||
|
||
it("create relayed transaction", async function () { | ||
const transferController = entrypoint.createTransfersController(); | ||
const sender = Account.newFromPem(alicePem.pemFileText); | ||
sender.nonce = 77777; | ||
|
||
const relayer = Account.newFromPem(bobPem.pemFileText); | ||
relayer.nonce = 7; | ||
|
||
const transaction = await transferController.createTransactionForTransfer( | ||
sender, | ||
BigInt(sender.getNonceThenIncrement().valueOf()), | ||
{ | ||
receiver: sender.address, | ||
data: Buffer.from("hello"), | ||
}, | ||
); | ||
const innerTransactionGasLimit = transaction.gasLimit; | ||
transaction.gasLimit = BigInt(0); | ||
transaction.signature = await sender.sign(txComputer.computeBytesForSigning(transaction)); | ||
|
||
const relayedController = entrypoint.createRelayedController(); | ||
const relayedTransaction = relayedController.createRelayedV2Transaction( | ||
relayer, | ||
BigInt(relayer.getNonceThenIncrement().valueOf()), | ||
{ | ||
innerTransaction: transaction, | ||
innerTransactionGasLimit, | ||
}, | ||
); | ||
|
||
assert.equal((await relayedTransaction).chainID, "D"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe also assert on the |
||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import { AbiRegistry } from "../abi"; | ||
import { AccountController } from "../accountManagement"; | ||
import { IAccount } from "../accounts/interfaces"; | ||
import { Address } from "../address"; | ||
import { DelegationController } from "../delegation"; | ||
import { ErrInvalidNetworkProviderKind } from "../errors"; | ||
import { Message, MessageComputer } from "../message"; | ||
import { ApiNetworkProvider, ProxyNetworkProvider, TransactionOnNetwork } from "../networkProviders"; | ||
import { RelayedController } from "../relayed/relayedController"; | ||
import { SmartContractController } from "../smartContracts/smartContractController"; | ||
import { TokenManagementController } from "../tokenManagement"; | ||
import { Transaction } from "../transaction"; | ||
import { TransactionComputer } from "../transactionComputer"; | ||
import { TransactionWatcher } from "../transactionWatcher"; | ||
import { TransfersController } from "../transfers/transfersControllers"; | ||
import { UserVerifier } from "../wallet"; | ||
import { DevnetEntrypointConfig, MainnetEntrypointConfig, TestnetEntrypointConfig } from "./config"; | ||
|
||
class NetworkEntrypoint { | ||
private networkProvider: ApiNetworkProvider | ProxyNetworkProvider; | ||
private chainId: string; | ||
|
||
constructor(networkProviderUrl: string, networkProviderKind: string, chainId: string) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe using the "options" pattern? 💭 |
||
if (networkProviderKind === "proxy") { | ||
this.networkProvider = new ProxyNetworkProvider(networkProviderUrl); | ||
} else if (networkProviderKind === "api") { | ||
this.networkProvider = new ApiNetworkProvider(networkProviderUrl); | ||
} else { | ||
throw new ErrInvalidNetworkProviderKind(); | ||
} | ||
|
||
this.chainId = chainId; | ||
} | ||
|
||
async signTransaction(transaction: Transaction, account: IAccount): Promise<void> { | ||
const txComputer = new TransactionComputer(); | ||
transaction.signature = await account.sign(txComputer.computeBytesForSigning(transaction)); | ||
} | ||
|
||
verifyTransactionSignature(transaction: Transaction): boolean { | ||
const verifier = UserVerifier.fromAddress(Address.fromBech32(transaction.sender)); | ||
const txComputer = new TransactionComputer(); | ||
return verifier.verify(txComputer.computeBytesForVerifying(transaction), transaction.signature); | ||
} | ||
|
||
async signMessage(message: Message, account: IAccount): Promise<void> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think they will be soon moved to account: multiversx/mx-sdk-specs#94 |
||
const messageComputer = new MessageComputer(); | ||
message.signature = await account.sign(messageComputer.computeBytesForSigning(message)); | ||
} | ||
|
||
verifyMessageSignature(message: Message): boolean { | ||
if (!message.address) { | ||
throw new Error("`address` property of Message is not set"); | ||
} | ||
|
||
if (!message.signature) { | ||
throw new Error("`signature` property of Message is not set"); | ||
} | ||
|
||
const verifier = UserVerifier.fromAddress(message.address); | ||
const messageComputer = new MessageComputer(); | ||
return verifier.verify(messageComputer.computeBytesForVerifying(message), message.signature); | ||
} | ||
|
||
async recallAccountNonce(address: Address): Promise<number> { | ||
return (await this.networkProvider.getAccount(address)).nonce; | ||
} | ||
|
||
sendTransactions(transactions: Transaction[]): Promise<string[]> { | ||
return this.networkProvider.sendTransactions(transactions); | ||
} | ||
|
||
sendTransaction(transaction: Transaction): Promise<string> { | ||
return this.networkProvider.sendTransaction(transaction); | ||
} | ||
|
||
async awaitCompletedTransaction(txHash: string): Promise<TransactionOnNetwork> { | ||
const transactionAwaiter = new TransactionWatcher(this.networkProvider); | ||
return transactionAwaiter.awaitCompleted(txHash); | ||
} | ||
|
||
createNetworkProvider(): ApiNetworkProvider | ProxyNetworkProvider { | ||
return this.networkProvider; | ||
} | ||
|
||
createDelegationController(): DelegationController { | ||
return new DelegationController({ chainID: this.chainId, networkProvider: this.networkProvider }); | ||
} | ||
|
||
createAccountController(): AccountController { | ||
return new AccountController({ chainID: this.chainId }); | ||
} | ||
|
||
createRelayedController(): RelayedController { | ||
return new RelayedController({ chainID: this.chainId }); | ||
} | ||
|
||
createSmartContractController(abi?: AbiRegistry): SmartContractController { | ||
return new SmartContractController({ chainID: this.chainId, networkProvider: this.networkProvider, abi }); | ||
} | ||
|
||
createTokenManagementController(): TokenManagementController { | ||
return new TokenManagementController({ chainID: this.chainId, networkProvider: this.networkProvider }); | ||
} | ||
|
||
createTransfersController(): TransfersController { | ||
return new TransfersController({ chainID: this.chainId }); | ||
} | ||
} | ||
|
||
export class TestnetEntrypoint extends NetworkEntrypoint { | ||
constructor(url?: string, kind?: string) { | ||
const entrypointConfig = new TestnetEntrypointConfig(); | ||
super( | ||
url || entrypointConfig.networkProviderUrl, | ||
kind || entrypointConfig.networkProviderKind, | ||
entrypointConfig.chainId, | ||
); | ||
} | ||
} | ||
|
||
export class DevnetEntrypoint extends NetworkEntrypoint { | ||
constructor(url?: string, kind?: string) { | ||
const entrypointConfig = new DevnetEntrypointConfig(); | ||
super( | ||
url || entrypointConfig.networkProviderUrl, | ||
kind || entrypointConfig.networkProviderKind, | ||
entrypointConfig.chainId, | ||
); | ||
} | ||
} | ||
|
||
export class MainnetEntrypoint extends NetworkEntrypoint { | ||
constructor(url?: string, kind?: string) { | ||
const entrypointConfig = new MainnetEntrypointConfig(); | ||
super( | ||
url || entrypointConfig.networkProviderUrl, | ||
kind || entrypointConfig.networkProviderKind, | ||
entrypointConfig.chainId, | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./config"; | ||
export * from "./entrypoints"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be easier to have constructors on these classes in case somebody wants to change one of the fields (e.g.
networkProviderUrl
) without having to access the fields after the initialization.