Install the polywrap package with yarn or npm:
yarn add polywrap
npm install polywrapInstall the near-plugin package with yarn or npm:
yarn add @cidt/near-plugin-js
npm install @cidt/near-plugin-jsThe following steps show how to set up the Polywrap Client, add Near Plugin, create a Near transaction, sign and send the transaction.
First of all, we need to create a Polywrap Client, which contains all the required utilities to interact with the Near. It acts as a wrapper for Near JavaScript API.
Use an import or require statement, depending on which your environment supports.
import { PolywrapClient } from "@polywrap/client-js";
import { nearPlugin } from "@cidt/near-plugin-js"
import { keyStores, KeyPair } from "near-api-js";//Create keyStore
const network = 'testnet'
const keyStore = new keyStores.InMemoryKeyStore(),
// Create keyPair using your private key
const keyPair = KeyPair.fromString(<PRIVATE_KEY>);
keyStore.setKey(network, <ACCOUNT_ID>, keyPair);
// Make a config
const nearConfig = {
headers: {},
networkId: network,
keyStore: keyStore,
nodeUrl: "https://rpc.testnet.near.org",
walletUrl: "https://wallet.testnet.near.org",
helperUrl: "https://helper.testnet.near.org"
}Then, you will be able to use the Polywrap Client with Near plugin like so:
// Instantiate the PolywrapClient.
const client = new PolywrapClient({
plugins: [{
uri:"wrap://ens/nearPlugin.polywrap.eth",
plugin: nearPlugin(nearConfig)
}
]
});Examples of near-wrapper usage
const transactionResult = await client.invoke({
uri: "wrap://ens/near.polywrap.eth",
method: "sendMoney",
args: {
receiverId: <RECEIVER_ID>,
amount: <AMOUNT>,
}
})const transaction = await client.invoke({
uri: "wrap://ens/near.polywrap.eth",
method: "createTransaction",
args: {
publicKey: <PUBLIC_KEY>, // your public key
receiverId: <RECEIVER_ID>,
actions: [{ deposit: <AMOUNT> }] // Actions defines what transaction will be executed. Check near documentation for additional details
},
}) const signedTransaction = await client.invoke({
uri: "wrap://ens/near.polywrap.eth",
method: "signTransaction",
args: {
transaction,
},
}); const transactionResult = await client.invoke({
uri: "wrap://ens/near.polywrap.eth",
method: "sendTransaction",
args: {
signedTx: signedTransaction,
},
}); const transactionResult = await client.invoke({
uri: "wrap://ens/near.polywrap.eth",
method: "getAccountBalance",
args: {
accountId: <ACCOUNT_ID>,
},
});All available method you can check at wrapper schema
Near wrapper uses wrapper borsh-serializer to serialize/deserialize transaction
In case you want mannualy serialize/deserialize transactions, borsh-wrapper provides methods serializeTransaction, deserializeTransaction, serializeSignedTransaction, deserializeSignedTransaction
Serialize
const transactionBytes = await client.invoke({
uri: "wrap://ens/near.borsh.polywrap.eth",
method: "serializeTransaction",
args: {
transaction: transaction,
},
});Deserialize:
const transaction = await client.invoke({
uri: "wrap://ens/near.borsh.polywrap.eth",
method: "deserializeTransaction",
args: {
transactionBytes: transactionBytes,
},
});Serialize signed transaction:
const signedTransactionBytes = await client.invoke({
uri: "wrap://ens/near.borsh.polywrap.eth",
method: "serializeTransaction",
args: {
signedTransaction: signedTransaction,
},
});Deserialize signed transaction:
const signedTransaction = await client.invoke({
uri: "wrap://ens/near.borsh.polywrap.eth",
method: "deserializeTransaction",
args: {
signedTxBytes: signedTransactionBytes,
},
});