Skip to content
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

fix: export token registry #9

Merged
merged 7 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
185 changes: 184 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
} from '@tradetrust-tt/tradetrust-core'

const document = {
// raw v2 document with dns-did as identitify proof
// raw tradetrust v2 document with dns-did as identitify proof
} as any

async function start() {
Expand All @@ -46,6 +46,177 @@ async function start() {
start()
```

#### Deploying token-registry

This example provides how to deploy tradetrust standard token-registry for [transferrable records](https://docs.tradetrust.io/docs/tutorial/transferable-records/overview/). It requires less gas compared to [standalone deployment](#deploying-standalone-token-registry), as it uses deployer and implementation addresses for deployment. Replace the values for `<your_private_key>` and `<your_provider_url>` with your wallet private key and the JSON RPC url for desired network accordingly. Currently, it supports the following networks.

- ethereum
- sepolia
- polygon
- stabilitytestnet
- stability

```ts
import {
TDocDeployer__factory,
TOKEN_REG_CONSTS,
DeploymentEvent,
encodeInitParams,
getEventFromReceipt,
} from '@tradetrust-tt/tradetrust-core'
import { Wallet, ethers } from 'ethers'

async function start() {
const unconnectedWallet = new Wallet('<your_private_key>')
const provider = new ethers.providers.JsonRpcProvider('<your_provider_url>')
const wallet = unconnectedWallet.connect(provider)
const walletAddress = await wallet.getAddress()
const chainId = await wallet.getChainId()

const { TokenImplementation, Deployer } = TOKEN_REG_CONSTS.contractAddress

const deployerContract = TDocDeployer__factory.connect(
Deployer[chainId],
wallet
)

const initParam = encodeInitParams({
name: 'DemoTokenRegistry',
symbol: 'DTR',
deployer: walletAddress,
})

const tx = await deployerContract.deploy(
TokenImplementation[chainId],
initParam
)
const receipt = await tx.wait()
const registryAddress = getEventFromReceipt<DeploymentEvent>(
receipt,
deployerContract.interface.getEventTopic('Deployment')
).args.deployed

// new token registry contract address
console.log(registryAddress)
}
start()
```

#### Deploying standalone token-registry

This example provides how to deploy tradetrust standalone token-registry for [transferrable records](https://docs.tradetrust.io/docs/tutorial/transferable-records/overview/). Replace the values for `<your_private_key>` and `<your_provider_url>` with your wallet private key and the JSON RPC url for desired network accordingly. It works on all the [supported networks](https://docs.tradetrust.io/docs/topics/introduction/supported-network/#tradetrust-supported-networks).

```ts
import {
TradeTrustToken__factory,
TOKEN_REG_CONSTS,
} from '@tradetrust-tt/tradetrust-core'
import { Wallet, ethers } from 'ethers'

async function start() {
const unconnectedWallet = new Wallet('<your_private_key>')
const provider = new ethers.providers.JsonRpcProvider('<your_provider_url>')
const wallet = unconnectedWallet.connect(provider)
const tokenFactory = new TradeTrustToken__factory(wallet)
const CHAIN_ID = await wallet.getChainId()
// get the title escrow factory address for each network
const TitleEscrowFactory =
TOKEN_REG_CONSTS.contractAddress.TitleEscrowFactory[CHAIN_ID]
const tokenRegistry = await tokenFactory.deploy(
'DemoTokenRegistry',
'DTR',
TitleEscrowFactory
)
const registryAddress = tokenRegistry.address
// new standalone token registry contract address
console.log(registryAddress)
}
start()
```

#### Wrapping & Minting of Transferrable Record

This example provides how to wrap the raw document and mint the tradetrust token for [transferrable record](https://docs.tradetrust.io/docs/tutorial/transferable-records/overview/) using the existing token registry address. Replace the place holders `<your_private_key>`, `<your_provider_url>`, `<token_registry_address>`, `<beneficiary_address>` and '<holder_address>' accordingly. After successfully minted, transaction hash will be displayed and the wrappedDocument should be successfully [verified](#verifying).

```ts
import { TradeTrustToken__factory } from '@tradetrust-tt/tradetrust-core'
import { Wallet, ethers } from 'ethers'

async function start() {
const document = {
// raw tradetrust v2 document with dns-txt as identitify proof
}
const wrappedDocuments = wrapDocumentsV2([document as any])
const wrappedDocument = wrappedDocuments[0]
const tokenId = wrappedDocument.signature.targetHash
const unconnectedWallet = new Wallet('<your_private_key>')
const provider = new ethers.providers.JsonRpcProvider('<your_provider_url>')
const wallet = unconnectedWallet.connect(provider)

const connectedRegistry = TradeTrustToken__factory.connect(
'<token_registry_address>',
wallet
)
const transaction = await connectedRegistry.mint(
'<beneficiary_address>',
'<holder_address>',
tokenId'
)
console.log(`Waiting for transaction ${transaction.hash} to be completed`)
const receipt = await transaction.wait()
// transaction hash
console.log(receipt.transactionHash)
}

start()
```

#### Manage ownership of tradetrust token

This example demonstrates how to manage and represent the ownership of a TradeTrust token between a beneficiary and holder for [Title Transfer](https://docs.tradetrust.io/docs/topics/introduction/transferable-records/title-transfer), and eventually surrender the document. During [minting](#minting-of-transferrable-record), the [Token Registry](https://docs.tradetrust.io/docs/topics/appendix/glossary/#token-registry) will create and assign a [Title Escrow](https://docs.tradetrust.io/docs/topics/introduction/transferable-records/title-transfer/#title-escrow) as the owner of that token. The actual owners will use the Title Escrow contract to perform their ownership operations.

```ts
import { connectToTitleEscrow } from '@tradetrust-tt/tradetrust-core'

const titleEscrow = await connectToTitleEscrow({ tokenId, address, wallet })
```

After getting the titleEscrow instance, we can call the following methods to change the ownership of the tradetrust token.

```ts
/*
allow the holder of the transferable record to endorse the transfer to an approved owner and approved holder of the transferable record.
*/
const transaction = await titleEscrow.transferBeneficiary(
beneficiaryNomineeAddress
)
await transaction.wait()

// allow the owner of a transferable record to change its holder.
const transaction = await titleEscrow.transferHolder(newHolderAddress)
await transaction.wait()

/* change the owner and holder. It will fail if the provided holder and owner's addresses are the same as the current owner and current holder's addresses.
*/

const transaction = await titleEscrow.transferOwners(
beneficiaryNomineeAddress,
newHolderAddress
)
await transaction.wait()

/* allow the owner of the transferable record to nominate a new owner of the transferable record. It will fail if you are not the owner of the transferable record.
*/
const transaction = await titleEscrow.nominate(beneficiaryNomineeAddress)
await transaction.wait()

/*
allow the entity (who is both an owner and holder) to surrender it's transferable record to the token registry.
*/
const transaction = await titleEscrow.surrender()
await transaction.wait()
```

#### Verifying

This example provides how to verify tradetrust document using your own provider configurations.
Expand Down Expand Up @@ -152,6 +323,10 @@ It checks that the signature of the document corresponds to the actual content i

Note that this method does not check against the blockchain or any registry if this document has been published. The merkle root of this document need to be checked against a publicly accessible document store (can be a smart contract on the blockchain).

#### `connectToTitleEscrow`

It accepts the tokenId and address of the token resgitry and returns the address of the [TitleEscrow](https://docs.tradetrust.io/docs/topics/introduction/transferable-records/title-transfer/#title-escrow) which is connected to that token registry.

#### `isWrappedV2Document`

type guard for wrapped v2 document
Expand All @@ -168,6 +343,14 @@ type guard for wrapped v3 document

type guard for signed v3 document

#### `getEventFromReceipt`

extracts a specific event from a transaction receipt.

#### `encodeInitParams`

prepare the initialization parameters for deploying the [token-registry](#deploying-token-registry)

## Contributing

We welcome contributions to the TradeTrust core library. Please feel free to submit a pull request or open an issue.
Loading
Loading