Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@maticnetwork/maticjs-staking",
"version": "1.1.0",
"version": "1.2.0-beta.5",
"description": "Library for interacting with polygon staking.",
"main": "dist/npm.export.js",
"types": "dist/ts/index.d.ts",
Expand Down
171 changes: 120 additions & 51 deletions src/contracts/matic_token.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,123 @@
import { BaseToken, IPOSClientConfig, ITransactionOption, MAX_AMOUNT, Web3SideChainClient } from "@maticnetwork/maticjs";
import { BaseToken, IPOSClientConfig, ITransactionOption, MAX_AMOUNT, TYPE_AMOUNT, Web3SideChainClient, Converter } from "@maticnetwork/maticjs";

export class MaticToken extends BaseToken<IPOSClientConfig> {

stakeManagerAddress: string;

constructor(client: Web3SideChainClient<IPOSClientConfig>, address: string, stakeManagerAddress: string) {
super(
{
isParent: true,
address: address,
name: "ERC20",
bridgeType: 'plasma'
},
client
);
this.stakeManagerAddress = stakeManagerAddress;
}

protected getMethod(name: string, ...args) {
return this.getContract().then(contract => {
return contract.method(name, ...args);
});
}

getAllowanceForStakingManager(userAddress: string) {
return this.getMethod(
"allowance",
userAddress,
this.stakeManagerAddress
).then(method => {
return this.processRead(method);
});
}

approveMaxForStakingManager(option?: ITransactionOption) {
return this.getMethod(
"approve",
this.stakeManagerAddress,
MAX_AMOUNT
).then(method => {
return this.processWrite(method, option);
});
}

getBalance(userAddress: string) {
return this.getMethod(
"balanceOf",
userAddress,
).then(method => {
return this.processRead(method);
});
}
}
stakeManagerAddress: string;

constructor(client: Web3SideChainClient<IPOSClientConfig>, address: string, stakeManagerAddress: string) {
super(
{
isParent: true,
address: address,
name: "PolToken",
bridgeType: 'plasma'
},
client
);
this.stakeManagerAddress = stakeManagerAddress;
}

protected getMethod(name: string, ...args) {
return this.getContract().then(contract => {
return contract.method(name, ...args);
});
}

getAllowanceForStakingManager(userAddress: string) {
return this.getMethod(
"allowance",
userAddress,
this.stakeManagerAddress
).then(method => {
return this.processRead(method);
});
}

approveMaxForStakingManager(option?: ITransactionOption) {
return this.getMethod(
"approve",
this.stakeManagerAddress,
MAX_AMOUNT
).then(method => {
return this.processWrite(method, option);
});
}

getBalance(userAddress: string) {
return this.getMethod(
"balanceOf",
userAddress,
).then(method => {
return this.processRead(method);
});
}

async getPermitData(
amount: TYPE_AMOUNT,
spender: string
): Promise<{ v: number; r: string; s: string; deadline: number }> {
const client = this.client.parent;

const [accounts, contract, chainId] = await Promise.all([
client.name === 'WEB3' ? client.getAccountsUsingRPC_() : client.getAccounts(),
this.getContract(),
client.getChainId(),
])

const account = accounts[0]
// const name = await this.processRead<string>(contract.method('name'));
// to increase speed, we can hardcode the name
const name = 'Polygon Ecosystem Token';
const nonce = await this.processRead<string>(contract.method('nonces', account))

const deadline = Math.floor(Date.now() / 1000) + 1800; // 30 mins from now
const value = Converter.toHex(amount);

const domain = {
name,
version: '1',
chainId: `0x${chainId.toString(16)}`,
verifyingContract: this.contractParam.address,
};

const types = {
Permit: [
{ name: 'owner', type: 'address' },
{ name: 'spender', type: 'address' },
{ name: 'value', type: 'uint256' },
{ name: 'nonce', type: 'uint256' },
{ name: 'deadline', type: 'uint256' },
],
};

const message = {
owner: account,
spender,
value,
nonce,
deadline,
};

const signature = await client.signTypedData(account, {
types: {
...types, EIP712Domain: [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' }
]
},
primaryType: 'Permit',
domain,
message,
});

const sig = signature.startsWith('0x') ? signature.slice(2) : signature;
const r = '0x' + sig.substring(0, 64);
const s = '0x' + sig.substring(64, 128);
let v = parseInt(sig.substring(128, 130), 16);
if (v < 27) v += 27;

return { v, r, s, deadline };
}
}
39 changes: 38 additions & 1 deletion src/contracts/validator_share.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,43 @@ export class ValidatorShare extends BaseToken<IPOSClientConfig> {
});
}

/**
* delegate amount to validator using permit (POL token only)
*
* internally it calls method - **buyVoucherWithPermit**
*
* @param {TYPE_AMOUNT} amount Amount to delegate
* @param {TYPE_AMOUNT} minAmountToStake Minimum shares expected to mint
* @param {number} deadline Permit signature deadline
* @param {number} v Part of the signature
* @param {string} r Part of the signature
* @param {string} s Part of the signature
* @param {ITransactionOption} [option] Optional transaction parameters
* @return {*} Transaction result
* @memberof ValidatorShare
*/
delegateAmountPOLwithPermit(
amount: TYPE_AMOUNT,
minAmountToStake: TYPE_AMOUNT,
deadline: number,
v: number,
r: string,
s: string,
option?: ITransactionOption
) {
return this.getMethod(
"buyVoucherWithPermit",
Converter.toHex(amount),
Converter.toHex(minAmountToStake),
deadline,
v,
r,
s
).then(method => {
return this.processWrite(method, option);
});
}

/**
* unstake delegated amount
*
Expand Down Expand Up @@ -236,4 +273,4 @@ export class ValidatorShare extends BaseToken<IPOSClientConfig> {
}


}
}
28 changes: 21 additions & 7 deletions test/debug.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const { StakingClient } = require("@maticnetwork/maticjs-staking");
const { use } = require("@maticnetwork/maticjs");
const { Web3ClientPlugin } = require("@maticnetwork/maticjs-web3");
const { Web3ClientPlugin } = require("@maticnetwork/maticjs-ethers");
use(Web3ClientPlugin);

const HDWalletProvider = require("@truffle/hdwallet-provider");
const { ethers } = require("ethers");

const { user1, rpc, pos, user2, validatorAddress } = require("./config");
const from = user1.address;
Expand All @@ -13,23 +12,38 @@ const execute = async () => {
const client = new StakingClient();
await client.init({
log: true,
network: 'testnet',
version: 'amoy',
network: 'mainnet',
version: 'v1',
parent: {
provider: new HDWalletProvider(privateKey, rpc.parent),
provider: new ethers.Wallet(privateKey, new ethers.providers.JsonRpcProvider(rpc.parent)),
defaultConfig: {
from
}
},
child: {
provider: new HDWalletProvider(privateKey, rpc.child),
provider: new ethers.Wallet(privateKey, new ethers.providers.JsonRpcProvider(rpc.child)),
defaultConfig: {
from
}
}
});
console.log("init called");

let amount = '1'
const permitData = await client.polToken.getPermitData(amount, "0x5e3Ef299fDDf15eAa0432E6e66473ace8c13D908")
console.log(permitData)

const DelegateWithPermit = await client.validatorShare('0xa180Dd33e0fe5f8c2a4022d613145c383BE5F6a6')
.delegateAmountPOLwithPermit(
amount, amount,
permitData.deadline,
permitData.v,
permitData.r,
permitData.s,
{ returnTransaction: true }
)
return (console.log(DelegateWithPermit))

const value = await client.stakeManager.getTotalStake()
return console.log(value)

Expand Down
Loading